public IHttpActionResult CopyPageToVersion([FromBody] CopyPageToVersionsClientParameters parameters)
        {
            if (!Sitecore.Context.User.IsAuthenticated)
            {
                return(Unauthorized());
            }

            if (!Sitecore.Context.ContentDatabase.Name.Equals("master", StringComparison.InvariantCultureIgnoreCase))
            {
                return(Unauthorized());
            }

            var pipelineArgs = new CopyPageToVersionsPipelineArgs
            {
                IsDoNotCopyFieldValues           = parameters.Option1CheckBox,
                IsDoNotCreateRelatedItemVersions = parameters.Option2CheckBox,
                IsForceCopyFieldValues           = parameters.Option3CheckBox,
                PageId = parameters.PageId,
                SourceLanguageIdString  = parameters.SourceLanguageTreeView,
                TargetLanguageIdStrings = parameters.TargetLanguageTreeView
            };

            CorePipeline.Run("copyPageToVersions", pipelineArgs);

            return(Ok(new JObject(new JProperty("message", pipelineArgs.ResultMessage))));
        }
 private void AbortPipeline(CopyPageToVersionsPipelineArgs args, string message)
 {
     Log.Error(ErrorMessagePrefix + message, this);
     args.ErrorMessages.Add(message);
     SetErrorResultMessage(args);
     args.Stopwatch.Stop();
     args.AbortPipeline();
 }
 private static void SetSuccessMessage(CopyPageToVersionsPipelineArgs args, List <string> targetLanguagesStrings, string sourceLanguage)
 {
     if (args.ErrorMessages.Count > 0)
     {
         args.ResultMessage = string.Format("Successfully copied page language versoin {0} to versions {1} with errors: {2} \r\n", sourceLanguage, string.Join(", ", targetLanguagesStrings), string.Join("\r\n", args.ErrorMessages));
     }
     args.ResultMessage = string.Format("Successfully copied page language versoin {0} to versions {1}", sourceLanguage, string.Join(", ", targetLanguagesStrings));
 }
        /// <summary>
        /// Aggregates all related items for the copy process, depending on the configured template whitelist
        /// </summary>
        /// <param name="args">the pipeline args</param>
        public void Process(CopyPageToVersionsPipelineArgs args)
        {
            Item configuration = args.SourcePageItem.Database.GetItem(CopyPageToVersionsConfigurationPath);

            if (configuration == null)
            {
                AbortPipeline(args, ErrorMessageNoServiceConfiguration);
                args.Stopwatch.Stop();
                return;
            }

            List <ID> templateIdWhitelist          = GetIdListFromField(configuration.Fields["Template Whitelist"]);
            bool      isFilterWhitelistedTemplates = GetCheckboxStateFromField(configuration.Fields["Filter Whitelisted Templates"]);

            List <Item> relatedItems = GetRelatedItems(args.SourcePageItem, args.SourceLanguage, templateIdWhitelist,
                                                       isFilterWhitelistedTemplates);

            args.RelatedItems.AddRange(relatedItems);
        }
        /// <summary>
        /// Creates a page version for each target language and creates or updates the final renderings.
        /// </summary>
        /// <param name="args">the pipeline args</param>
        public void Process(CopyPageToVersionsPipelineArgs args)
        {
            List <string> targetLanguagesStrings = new List <string>();

            foreach (Language targetLanguage in args.TargetLanguages)
            {
                if (args.SourceLanguage.Equals(targetLanguage))
                {
                    SetWarnMessage(args, string.Format(WarnMessageSourceEqualsTarget, args.SourceLanguage.Name, targetLanguage.Name));
                    continue;
                }

                var targetPageItem = GetOrCreateItemLanguageVersion(args.SourcePageItem, targetLanguage, !args.IsDoNotCopyFieldValues, args.IsForceCopyFieldValues);
                if (targetPageItem == null)
                {
                    SetWarnMessage(args, string.Format(ErrorMessageNoTargetVersion, args.SourcePageItem.ID, targetLanguage));
                    continue;
                }

                CreateOrUpdateFinalRenderings(args.SourcePageItem, targetPageItem, args);

                if (!args.IsDoNotCreateRelatedItemVersions)
                {
                    foreach (Item relatedItem in args.RelatedItems)
                    {
                        GetOrCreateItemLanguageVersion(relatedItem, targetLanguage, !args.IsDoNotCopyFieldValues, args.IsForceCopyFieldValues);
                    }
                }

                targetLanguagesStrings.Add(targetLanguage.Name);
            }

            SetSuccessMessage(args, targetLanguagesStrings, args.SourceLanguage.Name);
            Log.Info(string.Format("{0}copied page language version {1} to language versions {2}", ErrorMessagePrefix, args.SourceLanguage.Name, string.Join(", ", targetLanguagesStrings)), this);
            args.Stopwatch.Stop();
            Log.Info(string.Format("{0}ended in {1}", ErrorMessagePrefix, args.Stopwatch.Elapsed), this);
        }
 private static void SetErrorResultMessage(CopyPageToVersionsPipelineArgs args)
 {
     args.ResultMessage = "An Error occured: \r\n" + string.Join("\r\n", args.ErrorMessages);
 }
 private void SetWarnMessage(CopyPageToVersionsPipelineArgs args, string message)
 {
     Log.Warn(ErrorMessagePrefix + message, this);
     args.ErrorMessages.Add(message);
 }
        private void CreateOrUpdateFinalRenderings(Item sourcePageItem, Item targetPageItem, CopyPageToVersionsPipelineArgs args)
        {
            if (sourcePageItem == null || targetPageItem == null)
            {
                return;
            }

            var sourceFinalRenderingsField = sourcePageItem.Fields[FieldIDs.FinalLayoutField];
            var targetFinalRenderingsField = targetPageItem.Fields[FieldIDs.FinalLayoutField];

            if (sourceFinalRenderingsField == null || targetFinalRenderingsField == null)
            {
                SetWarnMessage(args, ErrorMessageNoRenderingField);
                return;
            }
            if (string.IsNullOrWhiteSpace(targetFinalRenderingsField.Value))
            {
                CreateFinalRenderings(targetPageItem, targetFinalRenderingsField, sourceFinalRenderingsField);
            }
            else
            {
                UpdateFinalRenderings(sourcePageItem, targetPageItem, sourceFinalRenderingsField, targetFinalRenderingsField);
            }
        }
예제 #9
0
        /// <summary>
        /// Processes the posted inputs form the dialog and sets the SourceLanguage, TargetLanguages and the SourcePageItem.
        /// </summary>
        /// <param name="args">the pipeline args</param>
        public void Process(CopyPageToVersionsPipelineArgs args)
        {
            Log.Info("CopyPageToVersionsProcessor: started ...", this);
            args.Stopwatch.Start();

            Database database = Database.GetDatabase("master");

            if (database == null)
            {
                AbortPipeline(args, ErrorMessageDatabase);
                return;
            }

            // get source language
            if (!GetLanguageFromLangItem(args.SourceLanguageIdString, database, out var sourceLanguage))
            {
                AbortPipeline(args, string.Format(ErrorMessageSourceLanguage, args.SourceLanguageIdString));
                return;
            }
            args.SourceLanguage = sourceLanguage;

            // get target languages
            if (string.IsNullOrEmpty(args.TargetLanguageIdStrings))
            {
                AbortPipeline(args, ErrorMessageNoTargetLanguages);
                return;
            }

            // check languages root id
            if (string.IsNullOrEmpty(LanguagesRootId) || !ID.IsID(LanguagesRootId))
            {
                AbortPipeline(args, ErrorMessageNoLanguagesRoot);
                return;
            }

            List <Language> targetLanguages = new List <Language>();

            if (args.TargetLanguageIdStrings.Equals(LanguagesRootId))
            {
                targetLanguages.AddRange(LanguageManager.GetLanguages(database));
            }
            else
            {
                // get all target languages from splitted languages item list
                List <string> splittedTargetLanguages = args.TargetLanguageIdStrings.Split('|').ToList();
                foreach (string splittedTargetLanguage in splittedTargetLanguages)
                {
                    if (!ID.IsID(splittedTargetLanguage))
                    {
                        SetWarnMessage(args, string.Format(ErrorMessageInvalidTargetLanguage, splittedTargetLanguage));
                        continue;
                    }

                    if (!GetLanguageFromLangItem(splittedTargetLanguage, database, out var targetLanguage))
                    {
                        SetWarnMessage(args, string.Format(ErrorMessageInvalidTargetLanguage, splittedTargetLanguage));
                        continue;
                    }

                    targetLanguages.Add(targetLanguage);
                }
            }
            args.TargetLanguages.AddRange(targetLanguages);

            // get page item
            if (string.IsNullOrEmpty(args.PageId) || !ID.TryParse(args.PageId, out var pageId))
            {
                AbortPipeline(args, ErrorMessageNoPageId);
                return;
            }

            Item pageItem = database.GetItem(pageId);

            if (pageItem == null)
            {
                AbortPipeline(args, ErrorMessageNoPageItem);
                return;
            }

            args.SourcePageItem = database.GetItem(pageId, sourceLanguage);

            if (args.SourcePageItem.Versions.Count > 0)
            {
                return;
            }

            AbortPipeline(args, string.Format(ErrorMessageNoSourceVersion, args.SourcePageItem.ID, sourceLanguage.Name));
        }