PageRenderTime 44ms CodeModel.GetById 24ms app.highlight 16ms RepoModel.GetById 0ms app.codeStats 1ms

/CMSModules/Forums/Controls/Posts/PostListing.ascx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 373 lines | 272 code | 60 blank | 41 comment | 37 complexity | dd31ced897627f5e59ca35cd298467d1 MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Collections.Generic;
  6using System.Web;
  7using System.Web.Security;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.WebControls.WebParts;
 11using System.Web.UI.HtmlControls;
 12
 13using CMS.GlobalHelper;
 14using CMS.UIControls;
 15using CMS.SettingsProvider;
 16using CMS.WorkflowEngine;
 17using CMS.SiteProvider;
 18using CMS.TreeEngine;
 19using CMS.Forums;
 20using CMS.CMSHelper;
 21
 22using TimeZoneInfo = CMS.SiteProvider.TimeZoneInfo;
 23
 24public partial class CMSModules_Forums_Controls_Posts_PostListing : CMSAdminEditControl
 25{
 26    #region "Variables & enums"
 27
 28    private int mPostId = 0;
 29    private int mForumId = 0;
 30    private int mCommunityGroupID = 0;
 31    private ForumPostInfo mPostInfo = null;
 32
 33    private CurrentUserInfo currentUserInfo = null;
 34    private SiteInfo currentSiteInfo = null;
 35
 36    protected enum Action
 37    {
 38        Approve = 0,
 39        ApproveSubTree = 1,
 40        Reject = 2,
 41        RejectSubTree = 3,
 42        Delete = 4
 43    }
 44
 45    protected enum What
 46    {
 47        Selected = 0,
 48        All = 1
 49    }
 50
 51    #endregion
 52
 53
 54    #region "Properties"
 55
 56    /// <summary>
 57    /// Gets or sets ID of the parent post, its child-posts are displayed.
 58    /// </summary>
 59    public int PostId
 60    {
 61        get
 62        {
 63            return mPostId;
 64        }
 65        set
 66        {
 67            mPostId = value;
 68            mPostInfo = null;
 69        }
 70    }
 71
 72
 73    /// <summary>
 74    /// Gets or sets forums id. Is is used when postid is 0 = forum thread should be displayed.
 75    /// </summary>
 76    public int ForumId
 77    {
 78        get
 79        {
 80            return mForumId;
 81        }
 82        set
 83        {
 84            mForumId = value;
 85        }
 86    }
 87
 88
 89    /// <summary>
 90    /// Gets community group ID.
 91    /// </summary>
 92    private int CommunityGroupID
 93    {
 94        get
 95        {
 96            // Community group id is not set
 97            if ((mCommunityGroupID == 0) && (ForumId != 0))
 98            {
 99                ForumInfo fi = ForumInfoProvider.GetForumInfo(ForumId);
100                if (fi != null)
101                {
102                    ForumGroupInfo fgi = ForumGroupInfoProvider.GetForumGroupInfo(fi.ForumGroupID);
103                    if (fgi != null)
104                    {
105                        mCommunityGroupID = fgi.GroupGroupID;
106                    }
107                }
108            }
109
110            return mCommunityGroupID;
111        }
112    }
113
114
115    /// <summary>
116    /// Gets or sets post info.
117    /// </summary>
118    public ForumPostInfo PostInfo
119    {
120        get
121        {
122            if ((mPostInfo == null) && (this.PostId > 0))
123            {
124                mPostInfo = ForumPostInfoProvider.GetForumPostInfo(this.PostId);
125
126                // Update post id
127                if (mPostInfo != null)
128                {
129                    mPostId = mPostInfo.PostId;
130                }
131            }
132
133            return mPostInfo;
134        }
135        set
136        {
137            mPostInfo = value;
138
139            // Update post id
140            if (mPostInfo != null)
141            {
142                mPostId = mPostInfo.PostId;
143            }
144        }
145    }
146
147    #endregion
148
149
150    #region "Methods"
151
152    protected void Page_Load(object sender, EventArgs e)
153    {
154        bool reloadTree = QueryHelper.GetBoolean("reloadtree", true);
155        ScriptHelper.RegisterClientScriptBlock(this.Page, typeof(string), "PostListingScripts",
156            ScriptHelper.GetScript(
157            "function ViewPost(postId) { \n" +
158            "   location.href = 'ForumPost_View.aspx?postid=' + postId+'&listingpost=" + this.PostId + ";" + this.ForumId + "'; \n" +
159            "} \n" +
160            "function SelectPost(postId, forumId) { \n" +
161            "   location.href = 'ForumPost_Listing.aspx?postid=' + postId + ';' + forumId; \n" +
162            "} \n" +
163
164            "function SelectInTree(postId, force) { \n" +
165            "   var treeFrame = parent.frames['posts_tree']; \n" +
166            "   if (treeFrame != null) { \n" +
167            "      // Refresh tree if necessary \n" +
168            "      if ((treeFrame.selectedPostId != postId) || force) { \n" +
169            "          treeFrame.RefreshTree(postId); \n" +
170            "      } \n" +
171            "   } \n" +
172            "} \n" +
173            "SelectInTree(" + this.PostId + ", false); \n"
174        ));
175
176
177        if (!RequestHelper.IsPostBack())
178        {
179            DataHelper.FillListControlWithEnum(typeof(Action), drpAction, "Forums.ListingActions.", null);
180            drpAction.Items.Insert(0, new ListItem(GetString("general.SelectAction"), "-1"));
181            //DataHelper.FillListControlWithEnum(typeof(What), drpWhat, "Forums.ListingWhat.", null);            
182        }
183
184        string where = (this.PostId > 0) ? "PostParentID = " + this.PostId : "PostParentID IS NULL AND PostForumID=" + this.ForumId;
185        gridPosts.WhereCondition = where;
186
187        gridPosts.ZeroRowsText = GetString("forums.listing.nochildposts");
188        gridPosts.FilteredZeroRowsText = GetString("forums.listing.nochildpostssearch");
189
190        //gridPosts.OnDataReload += gridPosts_OnDataReload;
191        gridPosts.OnExternalDataBound += gridPosts_OnExternalDataBound;
192        gridPosts.OnAction += gridPosts_OnAction;
193        btnOk.Click += btnOk_Click;
194
195        currentUserInfo = CMSContext.CurrentUser;
196        currentSiteInfo = CMSContext.CurrentSite;
197    }
198
199
200    /// <summary>
201    /// OnPreRender.
202    /// </summary>
203    protected override void OnPreRender(EventArgs e)
204    {
205        // Hide action panel if no data
206        pnlFooter.Visible = !DataHelper.DataSourceIsEmpty(gridPosts.GridView.DataSource);
207    }
208
209    #endregion
210
211
212    #region "Grid events"
213
214    /// <summary>
215    /// External data binding handler.
216    /// </summary>
217    protected object gridPosts_OnExternalDataBound(object sender, string sourceName, object parameter)
218    {        
219        sourceName = sourceName.ToLower();
220        switch (sourceName)
221        {
222            case "subposts":
223                DataRowView data = ((GridViewRow)parameter).DataItem as DataRowView;
224                int subposts = ValidationHelper.GetInteger(data["PostThreadPostsAbsolute"], 0);
225                int postid = ValidationHelper.GetInteger(data["PostId"], 0);
226                int postForumId = ValidationHelper.GetInteger(data["PostForumId"], 0);
227                int postLevel = ValidationHelper.GetInteger(data["PostLevel"], 0);
228                ImageButton btnSubPosts = sender as ImageButton;
229
230                // Hide button if post has no child, thread counts itself = 1 means no subposts
231                if ((subposts == 0) || ((subposts == 1) && (postLevel == 0)))
232                {
233                    btnSubPosts.Visible = false;
234                }
235                else
236                {
237                    btnSubPosts.ImageUrl = GetImageUrl("/CMSModules/CMS_Forums/subposts.png");
238                    btnSubPosts.OnClientClick = "SelectPost(" + postid + "," + postForumId + ");return false;";
239                }
240                break;
241
242            case "postapproved":
243                return UniGridFunctions.ColoredSpanYesNo(parameter);
244        }
245
246        return parameter;
247    }
248
249
250    protected void gridPosts_OnAction(string actionName, object actionArgument)
251    {
252        int postId = ValidationHelper.GetInteger(actionArgument, 0);
253        if (actionName.ToLower() == "delete")
254        {
255            if (CommunityGroupID == 0)
256            {
257                // Check forums modify permissions
258                if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.forums", CMSAdminControl.PERMISSION_MODIFY))
259                {
260                    RedirectToAccessDenied("cms.forums", CMSAdminControl.PERMISSION_MODIFY);
261                }
262            }
263            else
264            {
265                // Check group permissions
266                CMSGroupPage.CheckGroupPermissions(CommunityGroupID, CMSAdminControl.PERMISSION_MANAGE); 
267            }
268
269            ForumPostInfoProvider.DeleteForumPostInfo(postId);
270        }
271    }
272
273    #endregion
274
275
276    #region "Multiple action handling"
277
278    protected void btnOk_Click(object sender, EventArgs e)
279    {
280        if (CommunityGroupID == 0)
281        {
282            // Check forums modify permissions
283            if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.forums", CMSAdminControl.PERMISSION_MODIFY))
284            {
285                RedirectToAccessDenied("cms.forums", CMSAdminControl.PERMISSION_MODIFY);
286            }
287        }
288        else
289        {
290            // Check group permissions
291            CMSGroupPage.CheckGroupPermissions(CommunityGroupID, CMSAdminControl.PERMISSION_MANAGE);
292        }
293
294        Action action = (Action)ValidationHelper.GetInteger(drpAction.SelectedValue, 0);
295        What what = What.Selected; //(What)ValidationHelper.GetInteger(drpWhat.SelectedValue, 0);
296
297        List<int> items = new List<int>();
298        switch (what)
299        {
300            // Only selected posts
301            case What.Selected:
302                foreach (object item in gridPosts.SelectedItems)
303                {
304                    items.Add(ValidationHelper.GetInteger(item, 0));
305                }
306                break;
307
308            // On posts in unigrid
309            case What.All:
310                DataSet ds = gridPosts.GridView.DataSource as DataSet;
311                if (!DataHelper.DataSourceIsEmpty(ds))
312                {
313                    foreach (DataRow dr in ds.Tables[0].Rows)
314                    {
315                        int postId = ValidationHelper.GetInteger(dr["PostId"], 0);
316                        items.Add(postId);
317                    }
318                }
319                break;
320        }
321
322        // For all specified forum posts
323        foreach (int postId in items)
324        {
325            ForumPostInfo fpi = ForumPostInfoProvider.GetForumPostInfo(postId);
326            if (fpi != null)
327            {
328                switch (action)
329                {
330                    // Approve post
331                    case Action.Approve:
332                        fpi.Approve();
333                        break;
334
335                    // Approve subtree
336                    case Action.ApproveSubTree:
337                        fpi.Approve(CMSContext.CurrentUser.UserID, true);
338                        break;
339
340                    // Reject post
341                    case Action.Reject:
342                        fpi.Reject();
343                        break;
344
345                    // Reject subtree
346                    case Action.RejectSubTree:
347                        fpi.Reject(true);
348                        break;
349
350                    // Delete post
351                    case Action.Delete:
352                        ForumPostInfoProvider.DeleteForumPostInfo(fpi);
353                        break;
354                }
355            }
356        }
357
358        // If something happened
359        if (items.Count > 0)
360        {
361            // Get rid of selection
362            gridPosts.ResetSelection();
363
364            // Reload unigrid to see changes
365            gridPosts.ReloadData();
366
367            // Force refresh post tree
368            ScriptHelper.RegisterClientScriptBlock(this.Page, typeof(string), "RefreshPostTree", ScriptHelper.GetScript("SelectInTree(" + this.PostId + ", true);"));
369        }
370    }
371
372    #endregion
373}