RelatedPosts.cs :  » Bloggers » BlogEngine.NET » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » Controls » RelatedPosts.cs
#region Using

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.IO;
using BlogEngine.Core;

#endregion

namespace Controls{

  public class RelatedPosts : Control
  {

    static RelatedPosts()
    {
      Post.Saved += new EventHandler<SavedEventArgs>(Post_Saved);
    }

    static void Post_Saved(object sender, SavedEventArgs e)
    {
      if (e.Action == SaveAction.Update)
      {
        Post post = (Post)sender;
        if (_Cache.ContainsKey(post.Id))
          _Cache.Remove(post.Id);
      }
    }

    #region Properties

    private IPublishable _Item;

    public IPublishable Item
    {
      get { return _Item; }
      set { _Item = value; }
    }

    private int _MaxResults = 3;

    public int MaxResults
    {
      get { return _MaxResults; }
      set { _MaxResults = value; }
    }

    private bool _ShowDescription;

    public bool ShowDescription
    {
      get { return _ShowDescription; }
      set { _ShowDescription = value; }
    }

    private int _DescriptionMaxLength = 100;

    public int DescriptionMaxLength
    {
      get { return _DescriptionMaxLength; }
      set { _DescriptionMaxLength = value; }
    }

    private string _Headline = Resources.labels.relatedPosts;

    public string Headline
    {
      get { return _Headline; }
      set { _Headline = value; }
    }

    #endregion

    #region Private fiels

    private static Dictionary<Guid, string> _Cache = new Dictionary<Guid, string>();
    private static object _SyncRoot = new object();

    #endregion

    /// <summary>
    /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"></see> object and stores tracing information about the control if tracing is enabled.
    /// </summary>
    /// <param name="writer">The <see cref="T:System.Web.UI.HTmlTextWriter"></see> object that receives the control content.</param>
    public override void RenderControl(HtmlTextWriter writer)
    {
      if (!BlogSettings.Instance.EnableRelatedPosts || Item == null)
        return;

      if (!_Cache.ContainsKey(Item.Id))
      {
        lock (_SyncRoot)
        {
          if (!_Cache.ContainsKey(Item.Id))
          {
            List<IPublishable> relatedPosts = SearchForPosts();
            if (relatedPosts.Count <= 1)
              return;

            CreateList(relatedPosts);
          }
        }
      }

      writer.Write(_Cache[Item.Id].Replace("+++", this.Headline));
    }

    /// <summary>
    /// Creates the list of related posts in HTML.
    /// </summary>
    /// <param name="relatedPosts">The related posts.</param>
    private void CreateList(List<IPublishable> relatedPosts)
    {
      System.Text.StringBuilder sb = new System.Text.StringBuilder();

      string link = "<a href=\"{0}\">{1}</a>";
      string desc = "<span>{0}</span>";
      sb.Append("<div id=\"relatedPosts\">");
      sb.Append("<p>+++</p>");
      sb.Append("<div>");
      
      int count = 0;
      foreach (IPublishable post in relatedPosts)
      {
        if (post != this.Item)
        {
          sb.Append(string.Format(link, post.RelativeLink, HttpUtility.HtmlEncode(post.Title)));
          if (ShowDescription)
          {
            string description = post.Description;
            if (description != null && description.Length > DescriptionMaxLength)
              description = description.Substring(0, DescriptionMaxLength) + "...";

            if (String.IsNullOrEmpty(description))
            {
              string content = Utils.StripHtml(post.Content);
              description = content.Length > DescriptionMaxLength ? content.Substring(0, DescriptionMaxLength) + "..." : content;
            }

            sb.Append(string.Format(desc, description));
          }
          count++;
        }

        if (count == MaxResults)
          break;
      }

      sb.Append("</div>");
      sb.Append("</div>");
      _Cache.Add(Item.Id, sb.ToString());
    }

    private List<IPublishable> SearchForPosts()
    {
      return Search.FindRelatedItems(this.Item);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.