Action Window Plugin to Publish and Move pages to recycling bin

Vote:
 

Hello there,

 

I just wanted to share this solution as there were many elements that were very tricky.


This requirements were for a plugin in Action Winwod which would enable users with Administration rights to Publish or move pages to the Recycling bin from a PageList with Pagination.

 

ASCX

<%@ Control Language="c#" CodeBehind="ManageComments.ascx.cs" AutoEventWireup="true"
    Inherits="MyProject.Templates.Admin.ManageComments" %>
<%@ Import Namespace="EPiServer" %>
<%@ Import Namespace="EPiServer.Core" %>
<%@ Register TagPrefix="MyProject" Namespace="MyProject.Extension" Assembly="MyProject" %>
<h1>
    Comments</h1>
<asp:Literal ID="MessageLiteral" Visible="false" runat="server">This plugin is only available to users that belong to the Administrators group.</asp:Literal>
<asp:Panel ID="ControlPanel" Visible="false" runat="server">
    <MyProject:LanguageDropDownList ID="ddSelectLanguage" runat="server" EnableViewState="true"
        OnSelectedIndexChanged="ddSelectLanguage_OnSelectedIndexChanged" />
    <br />
    <div style="padding-bottom: 3px">
        &nbsp;</div>
    <EPiServer:PageList ID="CommentsList" runat="server" PublishedStatus="Ignore" PagesPerPagingItem="10" Paging="true">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <div style="float: left; border: 1px; border-style: solid; padding: 10px; display: block;
                width: 100%">
                <div style="float: left; width: 85%;">
                    <dl style="float: left;">
                        <dt style="float: left; clear: left; width: 15%; margin-right: 10px;">User name</dt>
                        <dd style="float: left; width: 80%; margin-left: 0;">
                            <%# GetLastVersion(((PageData)Container.DataItem).PageLink).Property["UserName"] %>
                        </dd>
                        <dt style="float: left; clear: left; width: 15%; margin-right: 10px;">Date:</dt>
                        <dd style="float: left; width: 80%; margin-left: 0;">
                            <%# GetLastVersion(((PageData)Container.DataItem).PageLink).Property["PageStartPublish"]%>
                        </dd>
                        <dt style="float: left; clear: left; width: 15%; margin-right: 10px;">URL:</dt>
                        <dd style="float: left; width: 80%; margin-left: 0;">
                            <a href="<%# ((PageData)Container.DataItem).LinkURL %>" target="PreviewFrame">
                                <%# ((PageData)Container.DataItem).LinkURL %></a></dd>
                        <dt style="float: left; clear: left; width: 15%; margin-right: 10px;">Comment:</dt>
                        <dd style="float: left; width: 80%; margin-left: 0;">
                            <%# GetLastVersion(((PageData)Container.DataItem).PageLink).Property["Comment"]%>
                        </dd>
                    </dl>
                </div>
                <div style="float: right; width: 15%;">
                    <%# GetStatus(((PageData)Container.DataItem))%>
                    <asp:Button ID="ApproveButton" runat="server" Text="Approve" OnClick="ApproveButton_OnClick"
                        CommandArgument="<%# ((PageData)Container.DataItem).PageLink.ID %>" Visible='<%# ((PageData)Container.DataItem).Property["PagePendingPublish"].ToString() == string.Empty? false: true %>' />
                    <br />
                    <asp:Button ID="RejectButton" runat="server" Text="Reject" OnClick="RejectButton_OnClick"
                        CommandArgument="<%# ((PageData)Container.DataItem).PageLink.ID %>" Visible='<%# ((PageData)Container.DataItem).Property["PagePendingPublish"].ToString() == string.Empty? false: true %>' />
                </div>
            </div>
            <div style="padding-bottom: 1px">
                &nbsp;</div>
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
        <PagingHeaderTemplate>
            <div style="float: left; border: 1px; border-style: solid; padding: 10px; display: block;
                width: 100%; background-color: LightGray">
                <div style="float: left; width: 30%;">
        </PagingHeaderTemplate>
        <PagingFooterTemplate>
            </div>
            <div style="float: left; width: 70%;">
                <%# GetPagingText(CommentsList.PagingControl.CurrentPagingItemIndex, ((PageDataCollection)CommentsList.DataSource).Count)%>
            </div>
            </div>
        </PagingFooterTemplate>
    </EPiServer:PageList>
</asp:Panel>

 

ASCX.CS

using System;
using System.Globalization;
using System.Security.Principal;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAccess;
using EPiServer.Filters;
using EPiServer.PlugIn;
using EPiServer.UI;


namespace MyProject.Templates.Admin
{
    [GuiPlugIn(DisplayName = "Manage Comments", Description = "Centralised area within EPiServer which collates all of the user generated comments that have been created for the MyPageType site page branch.", Area = PlugInArea.ActionWindow, Url = "~/Templates/Admin/ManageComments.ascx")]
    public partial class ManageComments : UserControlBase
    {
        private CultureInfo _cultureInfo;
        private bool _selectionHasChanged;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

                SetLanguageBranch();
                GetComments();

            IPrincipal user = System.Web.HttpContext.Current.User;
            if (!IsPostBack)
            {
                // The language branch dropdown should only be visible if the current user is in the
                //‘Administrator’ role.
                if (user.IsInRole("Administrators"))
                {
                    ControlPanel.Visible = true;
                }
                else
                {
                    MessageLiteral.Visible = true;
                }
            }
        }



        protected void ddSelectLanguage_OnSelectedIndexChanged(object sender, CommandEventArgs e)
        {
            _selectionHasChanged = true;
            CommentsList.PagingControl.CurrentPagingItemIndex = 0;
            SetLanguageBranch();
            GetComments();
        }

        /// 
        /// SetLanguageBranch Method
        /// This method sets the language branch.
        /// The language branch dropdown will display all available current site languages. It will
        /// default to the current user’s language. Upon selection of a new language the page
        /// will reload – displaying the comments from the selected language branch.
        /// 
        private void SetLanguageBranch()
        {
            // Get Current UI Language
            if (ddSelectLanguage.SelectedLanguage == string.Empty)
            {
                _cultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
            }
            else
            {
                _cultureInfo = new CultureInfo(ddSelectLanguage.SelectedLanguage.ToLower());
            }
        }

        /// 
        /// GetComments Method
        /// The list will show all pages that are of type ‘Cooking Chef - Recipe Comment’ that exist
        /// for the selected language branch under the MyPageType page tree node.
        /// The list of comments are sorted by page published status (unpublished first) then
        /// the page creation date.
        /// The list is paginated and shows ten comments per page. Each comment will display the name of the user who created the comment, the date
        /// the comment was created, the url to the parent page (where the comment will be
        /// displayed if approved) and also the comment text body.
        /// 
        private void GetComments()
        {
            // Get Published
            var criteriasPublished = new PropertyCriteriaCollection();
            var pageTypeCriteria = new PropertyCriteria
                                       {
                                           Condition = CompareCondition.Equal,
                                           Name = "PageTypeName",
                                           Type = PropertyDataType.String,
                                           Value = "[Private] MyPageType - Recipe Comment",
                                           Required = true
                                       };

            criteriasPublished.Add(pageTypeCriteria);

            PageDataCollection pages = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, criteriasPublished, _cultureInfo.ToString());

            new FilterPropertySort("PagePendingPublish", FilterSortDirection.Ascending).Filter(pages);

            CommentsList.DataSource = pages;
            CommentsList.DataBind();
        }

        /// 
        /// GetStatus Method
        /// This method will give clear visual notification of the current comment page status (approved / unapproved).
        /// 
        /// 
        /// 
        protected string GetStatus(PageData pd)
        {
            return pd.PendingPublish ? "UNAPPROVED" : "APPROVED";
        }

        /// 
        /// ApproveButton_OnClick Event Handler
        /// The approve button will ‘Publish’ the comment page.
        /// The page will reload after any action (approve / reject ) has been performed. It will keep
        /// the same language branch as selected prior to the action, but will update the list of
        /// comments to reflect the action performed.
        /// Both of these action buttons should be hidden if the comment has already been approved
        /// 
        /// 
        /// 
        public void ApproveButton_OnClick(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            var pageID = btn.CommandArgument;
            var pd = DataFactory.Instance.GetPage(new PageReference(pageID));
            PageData clone = pd.CreateWritableClone();
            DataFactory.Instance.Save(clone, SaveAction.Publish);
            SetLanguageBranch();
            GetComments();
        }

        /// 
        /// RejectButton_OnClick Event Handler
        /// The reject linkbutton will move the comment page to the EPiServer recycle bin.
        /// 
        /// 
        /// 
        public void RejectButton_OnClick(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            var pageID = btn.CommandArgument;
            DataFactory.Instance.MoveToWastebasket(new PageReference(pageID));
            SetLanguageBranch();
            GetComments();
        }

        public PageData GetLastVersion(PageReference pageRef)
        {
            PageVersionCollection pageVersions = PageVersion.List(pageRef);
            PageReference lastVersion = pageVersions[0].ID;
            foreach (PageVersion pageVersion in pageVersions)
            {
                if (pageVersion.IsMasterLanguageBranch)
                {
                    lastVersion = pageVersion.ID;
                }
            }
            return DataFactory.Instance.GetPage(lastVersion,
                     LanguageSelector.AutoDetect(true));
        }

        public string GetPagingText(int page, int count)
        {
            int startNumber = 1;
            int itemsPerPage = CommentsList.PagesPerPagingItem;
            int endNumber = itemsPerPage;
            
            string s = count > 1 ? "s" : string.Empty;
            if (page > 0)
            {
                startNumber = (page * itemsPerPage) + 1;
            }

            if (page > 0 && (page + 1) * itemsPerPage < count)
            {
                endNumber = (page + 1) * itemsPerPage;
            }
            else if ((page + 1) * itemsPerPage >= count)
            {
                endNumber = count;
            }

            string pagingText = string.Format("Showing {0} - {1} comment{2} of {3} comment{4} for {5}", startNumber, endNumber, s, count, s, _cultureInfo.EnglishName);
            return pagingText;
        }
    }
}
#41498
Jul 08, 2010 15:58
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.