PageRenderTime 60ms CodeModel.GetById 30ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/User controls/PostList.ascx.cs

#
C# | 222 lines | 141 code | 38 blank | 43 comment | 19 complexity | 295226c46e45f9904712f3ceba49514f MD5 | raw file
  1namespace UserControls
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.Web.UI;
  6
  7    using BlogEngine.Core;
  8    using BlogEngine.Core.Web.Controls;
  9
 10    /// <summary>
 11    /// The post list user control.
 12    /// </summary>
 13    public partial class PostList : UserControl
 14    {
 15        #region Constants and Fields
 16
 17        /// <summary>
 18        ///     The posts.
 19        /// </summary>
 20        private List<IPublishable> publishables;
 21
 22        /// <summary>
 23        ///     Initializes a new instance of the <see cref = "PostList" /> class.
 24        /// </summary>
 25        public PostList()
 26        {
 27            this.ContentBy = ServingContentBy.AllContent;
 28        }
 29
 30        #endregion
 31
 32        #region Properties
 33
 34        /// <summary>
 35        ///     Gets or sets the criteria by which the content is being served (by tag, category, author, etc).
 36        /// </summary>
 37        public ServingContentBy ContentBy { get; set; }
 38
 39        /// <summary>
 40        ///     Gets or sets the list of posts to display.
 41        /// </summary>
 42        public List<IPublishable> Posts
 43        {
 44            get
 45            {
 46                return this.publishables;
 47            }
 48
 49            set
 50            {
 51                this.publishables = value;
 52                this.pager1.Posts = value;
 53            }
 54        }
 55
 56        #endregion
 57
 58        #region Methods
 59
 60        protected override void OnInit(EventArgs e)
 61        {
 62            base.OnInit(e);
 63
 64            if (!Security.IsAuthorizedTo(Rights.ViewPublicPosts))
 65            {
 66                this.Visible = false;
 67            }
 68
 69        }
 70
 71        /// <summary>
 72        /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
 73        /// </summary>
 74        /// <param name="e">
 75        /// The <see cref="T:System.EventArgs"/> object that contains the event data.
 76        /// </param>
 77        protected override void OnLoad(EventArgs e)
 78        {
 79            base.OnLoad(e);
 80
 81            if (this.Page.IsCallback)
 82            {
 83                return;
 84            }
 85
 86            this.BindPosts();
 87            this.InitPaging();
 88        }
 89
 90        /// <summary>
 91        /// Binds the list of posts to individual postview.ascx controls
 92        ///     from the current theme.
 93        /// </summary>
 94        private void BindPosts()
 95        {
 96			if (this.Posts == null) {
 97				// no posts provided, load all posts by default
 98				Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));
 99			}
100			
101            if (this.Posts.Count == 0)
102            {
103                this.hlPrev.Visible = false;
104                return;
105            }
106
107            var visiblePosts = this.Posts.FindAll(p => p.IsVisible);
108
109            var count = Math.Min(BlogSettings.Instance.PostsPerPage, visiblePosts.Count);
110            var page = this.GetPageIndex();
111            var index = page * count;
112            var stop = count;
113            if (index + count > visiblePosts.Count)
114            {
115                stop = visiblePosts.Count - index;
116            }
117
118            if (stop < 0 || stop + index > visiblePosts.Count)
119            {
120                this.hlPrev.Visible = false;
121                this.hlNext.Visible = false;
122                return;
123            }
124
125            var path = string.Format("{0}themes/{1}/PostView.ascx", Utils.ApplicationRelativeWebRoot, BlogSettings.Instance.GetThemeWithAdjustments(this.Request.QueryString["theme"]));
126            var counter = 0;
127
128            foreach (Post post in visiblePosts.GetRange(index, stop))
129            {
130                if (counter == stop)
131                {
132                    break;
133                }
134
135                var postView = (PostViewBase)this.LoadControl(path);
136                postView.ShowExcerpt = ShowExcerpt();
137                postView.Post = post;
138                postView.ID = post.Id.ToString().Replace("-", string.Empty);
139                postView.Location = ServingLocation.PostList;
140                postView.Index = counter;
141                this.posts.Controls.Add(postView);
142                counter++;
143            }
144
145            if (index + stop == this.Posts.Count)
146            {
147                this.hlPrev.Visible = false;
148            }
149        }
150
151        /// <summary>
152        /// Retrieves the current page index based on the QueryString.
153        /// </summary>
154        /// <returns>
155        /// The get page index.
156        /// </returns>
157        private int GetPageIndex()
158        {
159            int index;
160            if (int.TryParse(this.Request.QueryString["page"], out index))
161            {
162                index--;
163            }
164
165            return index;
166        }
167
168        /// <summary>
169        /// Initializes the Next and Previous links
170        /// </summary>
171        private void InitPaging()
172        {
173            var path = this.Request.RawUrl.Replace("Default.aspx", string.Empty);
174
175            path = path.Contains("?")
176                       ? (path.Contains("page=")
177                              ? path.Substring(0, path.IndexOf("page="))
178                              : string.Format("{0}&", path))
179                       : string.Format("{0}?", path);
180
181            var page = this.GetPageIndex();
182            var url = string.Format("{0}page={{0}}", path);
183
184            // if (page != 1)
185            this.hlNext.HRef = string.Format(url, page);
186
187            // else
188            // hlNext.HRef = path.Replace("?", string.Empty);
189            this.hlPrev.HRef = string.Format(url, page + 2);
190
191            if (page == 0)
192            {
193                this.hlNext.Visible = false;
194            }
195            else
196            {
197                ((BlogBasePage)this.Page).AddGenericLink("next", "Next page", this.hlNext.HRef);
198            }
199
200            if (this.hlPrev.Visible)
201            {
202                ((BlogBasePage)this.Page).AddGenericLink("prev", "Previous page", string.Format(url, page + 2));
203            }
204        }
205
206        /// <summary>
207        /// Whether or not to show the entire post or just the excerpt/description
208        /// in the post list 
209        /// </summary>
210        /// <returns></returns>
211        private bool ShowExcerpt()
212        {
213            string url = this.Request.RawUrl.ToUpperInvariant();
214            bool tagOrCategory = url.Contains("/CATEGORY/") || url.Contains("?TAG=/");
215
216            return BlogSettings.Instance.ShowDescriptionInPostList ||
217                (BlogSettings.Instance.ShowDescriptionInPostListForPostsByTagOrCategory && tagOrCategory);
218        }
219
220        #endregion
221    }
222}