Beispiel #1
0
        public async Task <IActionResult> Clone(string contentItemId, string returnUrl)
        {
            var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Latest);

            if (contentItem == null)
            {
                return(NotFound());
            }

            if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.CloneContent, contentItem))
            {
                return(Forbid());
            }

            try
            {
                await _contentManager.CloneAsync(contentItem);
            }
            catch (InvalidOperationException)
            {
                _notifier.Warning(H["Could not clone the content item."]);
                return(Url.IsLocalUrl(returnUrl) ? (IActionResult)LocalRedirect(returnUrl) : RedirectToAction("List"));
            }

            _notifier.Information(H["Successfully cloned. The clone was saved as a draft."]);

            return(Url.IsLocalUrl(returnUrl) ? (IActionResult)LocalRedirect(returnUrl) : RedirectToAction("List"));
        }
        public async Task <ContentItem> LocalizeAsync(ContentItem content, string targetCulture)
        {
            var localizationPart = content.As <LocalizationPart>();
            var siteSettings     = await _siteService.GetSiteSettingsAsync();

            // not sure if this is redundant or not. The check is also done in the Admin controller
            if (!siteSettings.GetConfiguredCultures().Any(c => String.Equals(c, targetCulture, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidOperationException("Cannot localize an unsupported culture");
            }

            if (String.IsNullOrEmpty(localizationPart.LocalizationSet))
            {
                // If the source content item is not yet localized, define its defaults

                localizationPart.LocalizationSet = _iidGenerator.GenerateUniqueId();
                localizationPart.Culture         = await GetDefaultCultureNameAsync();

                _session.Save(content);
            }
            else
            {
                var existingContent = await GetContentItem(localizationPart.LocalizationSet, targetCulture);

                if (existingContent != null)
                {
                    // already localized
                    return(existingContent);
                }
            }

            // Cloning the content item
            var cloned = await _contentManager.CloneAsync(content);

            var clonedPart = cloned.As <LocalizationPart>();

            clonedPart.Culture         = targetCulture;
            clonedPart.LocalizationSet = localizationPart.LocalizationSet;
            clonedPart.Apply();

            var context = new LocalizationContentContext(content, localizationPart.LocalizationSet, targetCulture);

            await Handlers.InvokeAsync(async handler => await handler.LocalizingAsync(context), _logger);

            await ReversedHandlers.InvokeAsync(async handler => await handler.LocalizedAsync(context), _logger);

            _session.Save(cloned);
            return(cloned);
        }
Beispiel #3
0
        public async Task <ContentItem> LocalizeAsync(ContentItem content, string targetCulture)
        {
            var supportedCultures = await _localizationService.GetSupportedCulturesAsync();

            if (!supportedCultures.Any(c => String.Equals(c, targetCulture, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidOperationException("Cannot localize an unsupported culture");
            }

            var localizationPart = content.As <LocalizationPart>();

            if (String.IsNullOrEmpty(localizationPart.LocalizationSet))
            {
                // If the source content item is not yet localized, define its defaults
                localizationPart.LocalizationSet = _iidGenerator.GenerateUniqueId();
                localizationPart.Culture         = await _localizationService.GetDefaultCultureAsync();

                _session.Save(content);
            }
            else
            {
                var existingContent = await GetContentItemAsync(localizationPart.LocalizationSet, targetCulture);

                if (existingContent != null)
                {
                    // already localized
                    return(existingContent);
                }
            }

            // Cloning the content item
            var cloned = await _contentManager.CloneAsync(content);

            var clonedPart = cloned.As <LocalizationPart>();

            clonedPart.Culture         = targetCulture;
            clonedPart.LocalizationSet = localizationPart.LocalizationSet;
            clonedPart.Apply();

            var context = new LocalizationContentContext(cloned, content, localizationPart.LocalizationSet, targetCulture);

            await Handlers.InvokeAsync((handler, context) => handler.LocalizingAsync(context), context, _logger);

            await ReversedHandlers.InvokeAsync((handler, context) => handler.LocalizedAsync(context), context, _logger);

            _session.Save(cloned);
            return(cloned);
        }
        public async Task <IActionResult> AssignListItem(string contentitemid, string containerid, string returnUrl)
        {
            if (String.IsNullOrWhiteSpace(contentitemid))
            {
                return(NotFound());
            }

            var contentItemtoassign = await _contentManager.GetAsync(contentitemid, VersionOptions.Latest);

            if (contentItemtoassign == null)
            {
                return(NotFound());
            }
//            if (!await _authorizationService.AuthorizeAsync(User, Permissions.CloneContent, contentItem))
//            {
//                return Unauthorized();
//            }


            try
            {
                var newContentItem = await _contentManager.CloneAsync(contentItemtoassign);

                if (newContentItem != null)
                {
                    //set root content item
                    string parentDocumentContentItemId = !String.IsNullOrEmpty(containerid) ? containerid : string.Empty;
                    if (string.IsNullOrEmpty(parentDocumentContentItemId))
                    {
                        //assign to files document as root object
                        await AssignNewItemToToFilesRootObject(newContentItem);
                    }
                    else
                    {
                        //this is coming from lists.part . Document is contained within this document
                        var parentDocument = await _contentManager.GetAsync(parentDocumentContentItemId);

                        if (parentDocument == null)
                        {
                            return(NotFound());
                        }
                        await _listContainerService.AddChildContentItemReference(parentDocument, newContentItem);

                        //persist
                        //create was created in previous step
                        // await _contentManager.UpdateAsync(newContentItem);
                        await _contentManager.PublishAsync(newContentItem);

/*                        // Add UrlHelper, if we have an MVC Action context.
 *                      var actionContext = _serviceProvider.GetService<IActionContextAccessor>()?.ActionContext;
 *                      if (actionContext != null)
 *                      {
 *                          var urlHelperFactory = _serviceProvider.GetRequiredService<IUrlHelperFactory>();
 *                          _urlHelper = urlHelperFactory.GetUrlHelper(actionContext);
 *                      }
 *
 *                      var metadataitem = await _contentManager.PopulateAspectAsync<ContentItemMetadata>(newContentItem);
 *                      String newItemUrl = _urlHelper.Action(metadataitem.DisplayRouteValues["action"].ToString(),metadataitem.DisplayRouteValues);
 *                      _notifier.Success(T[
 *                          $"Successfully cloned.New folder was saved as a draft.<a href=\"{newItemUrl}\"> Click here </a> to display item"]);*/
                    }
                }
            }
            catch (InvalidOperationException)
            {
                _notifier.Warning(T["Could not assign the content item."]);
                return(Url.IsLocalUrl(returnUrl) ? (IActionResult)LocalRedirect(returnUrl) : RedirectToAction("/"));
            }

            return(Url.IsLocalUrl(returnUrl) ? (IActionResult)LocalRedirect(returnUrl) : RedirectToAction("/"));
        }