using Composite.C1Console.Elements.Plugins.ElementActionProvider; using Composite.C1Console.Security; using Composite.C1Console.Elements; using Composite.Data; using Composite.Data.ProcessControlled.ProcessControllers.GenericPublishProcessController; using Composite.Data.Types; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using System.Collections.Generic; using Composite.C1Console.Actions; using Composite.Data.ProcessControlled; using System.Linq; using System; namespace Composite.Pages.PublishTree { [ConfigurationElementType(typeof(NonConfigurableElementActionProvider))] public sealed class PublishTreeElementActionProvider : IElementActionProvider { private static readonly ActionGroup _actionGroup = new ActionGroup("Workflow", ActionGroupPriority.PrimaryLow); public IEnumerable GetActions(EntityToken entityToken) { DataEntityToken dataEntityToken = entityToken as DataEntityToken; // check if it's really data entity token if (dataEntityToken != null) { // we need only IPage - doing our check if (dataEntityToken.Data.DataSourceId.InterfaceType == typeof(IPage)) { // add our action token ElementAction action = new ElementAction(new ActionHandle(new PublishTreeActionToken())) { VisualData = new ActionVisualizedData { // right click menu icon settings - label, tooltip, image and display location settings Label = "Publish pages tree", ToolTip = "Publish pages tree", Icon = GenericPublishProcessController.Publish, ActionLocation = new ActionLocation { ActionType = ActionType.Edit, IsInFolder = false, IsInToolbar = false, ActionGroup = _actionGroup } } }; yield return action; } } } public sealed class PublishTreeActionExecutor : Composite.C1Console.Actions.IActionExecutor { public Composite.C1Console.Actions.FlowToken Execute(Composite.C1Console.Security.EntityToken entityToken, Composite.C1Console.Security.ActionToken actionToken, Composite.C1Console.Actions.FlowControllerServicesContainer flowControllerServicesContainer) { // doing our magic with data here DataEntityToken token = (DataEntityToken)entityToken; UpdateTreeRefresher treeRefresher = new UpdateTreeRefresher(token.Data.GetDataEntityToken(), flowControllerServicesContainer); IPage page = (IPage)token.Data; // call our functions PublishPagesTree(page.Id); // call refresh tree treeRefresher.PostRefreshMesseges(page.GetDataEntityToken()); return null; } #region Our functions executed here private void PublishPage(Guid pageId) { using (DataConnection connection = new DataConnection()) { var page = connection.Get().Where(p => p.Id == pageId).FirstOrDefault(); IPublishControlled publishControlled = page as IPublishControlled; if (publishControlled != null) { if (publishControlled.PublicationStatus != GenericPublishProcessController.Published) { // publish page publishControlled.PublicationStatus = GenericPublishProcessController.Published; connection.Update(publishControlled); } } } } private void PublishPagesTree(Guid pageId) { // publish page PublishPage(pageId); // get childrens for current page using (DataConnection connection = new DataConnection()) { IEnumerable childPageIds = (from ps in connection.Get() where ps.ParentId == pageId orderby ps.LocalOrdering select ps.Id).ToList(); // publish childrens foreach (var childPageId in childPageIds) { PublishPagesTree(childPageId); } } } #endregion } #region We add here action token PublishTreeActionToken [ActionExecutor(typeof(PublishTreeActionExecutor))] public sealed class PublishTreeActionToken : ActionToken { // publish permissions required to execute action static private IEnumerable _permissionTypes = new PermissionType[] { PermissionType.Publish }; public override IEnumerable PermissionTypes { get { return _permissionTypes; } } public override string Serialize() { return "PublishTree"; } public static Composite.C1Console.Security.ActionToken Deserialize(string serializedData) { return new PublishTreeActionToken(); } } #endregion } }