Example #1
0
        private void CloneWidgets(int originalContentId, ContentItem destinationContentItem)
        {
            if (originalContentId <= 0)
            {
                return;
            }

            // recupero i WidgetExPart del Content selezionato come Master (CloneFrom)
            var widgets = _widgetManager.GetWidgets(originalContentId);

            foreach (var widget in widgets)
            {
                // recupero il content widget nel ContentItem Master
                //var widgetPart = _widgetsService.GetWidget(widget.Id);

                // Clono il ContentMaster e recupero la parte WidgetExPart
                var clonedContentitem = _contentManager.Clone(widget.ContentItem);

                var widgetExPart = clonedContentitem.As <WidgetExPart>();

                // assegno il nuovo contenitore se non nullo ( nel caso di HtmlWidget per esempio la GetWidget ritorna nullo...)
                if (widgetExPart != null)
                {
                    widgetExPart.Host = destinationContentItem;
                    _contentManager.Publish(widgetExPart.ContentItem);
                }
            }
        }
Example #2
0
        public ActionResult Clone(int id)
        {
            var originalContentItem = _contentManager.GetLatest(id);

            if (!Services.Authorizer.Authorize(Permissions.ViewContent, originalContentItem, T("You do not have permission to view existing content.")))
            {
                return(new HttpUnauthorizedResult());
            }

            if (!Services.Authorizer.Authorize(Permissions.CreateContent, originalContentItem, T("Couldn't clone content")))
            {
                return(new HttpUnauthorizedResult());
            }

            // pass a dummy content to the authorization check to check for "own" variations
            var dummyContent = _contentManager.New(originalContentItem.ContentType);

            if (!Services.Authorizer.Authorize(Permissions.EditContent, dummyContent, T("You do not have permission to edit (or create) content.")))
            {
                return(new HttpUnauthorizedResult());
            }

            var cloneContentItem = _contentManager.Clone(originalContentItem);

            Services.Notifier.Success(string.IsNullOrWhiteSpace(originalContentItem.TypeDefinition.DisplayName)
                ? T("The content has been cloned as a draft.")
                : T("The {0} has been cloned as a draft.", originalContentItem.TypeDefinition.DisplayName));

            var adminRouteValues = _contentManager.GetItemMetadata(cloneContentItem).AdminRouteValues;

            return(RedirectToRoute(adminRouteValues));
        }
Example #3
0
        public ActionResult Clone(int id)
        {
            var originalContentItem = _contentManager.GetLatest(id);

            if (!Services.Authorizer.Authorize(Permissions.ViewContent, originalContentItem, T("Couldn't open original content")))
            {
                return(new HttpUnauthorizedResult());
            }

            // pass a dummy content to the authorization check to check for "own" variations
            var dummyContent = _contentManager.New(originalContentItem.ContentType);

            if (!Services.Authorizer.Authorize(Permissions.EditContent, dummyContent, T("Couldn't create clone content")))
            {
                return(new HttpUnauthorizedResult());
            }

            var cloneContentItem = _contentManager.Clone(originalContentItem);

            Services.Notifier.Success(T("Successfully cloned. The clone was saved as a draft."));

            var adminRouteValues = _contentManager.GetItemMetadata(cloneContentItem).AdminRouteValues;

            return(RedirectToRoute(adminRouteValues));
        }
Example #4
0
        public ActionResult Translate(int id, string to)
        {
            var masterContentItem = _contentManager.Get(id, VersionOptions.Latest);

            if (masterContentItem == null)
            {
                return(HttpNotFound());
            }

            if (!Services.Authorizer.Authorize(Permissions.ViewContent, masterContentItem, T("Couldn't open original content")))
            {
                return(new HttpUnauthorizedResult());
            }

            var masterLocalizationPart = masterContentItem.As <LocalizationPart>();

            if (masterLocalizationPart == null)
            {
                return(HttpNotFound());
            }

            // Check if current item still exists, and redirect.
            var existingTranslation = _localizationService.GetLocalizedContentItem(masterContentItem, to);

            if (existingTranslation != null)
            {
                var existingTranslationMetadata = _contentManager.GetItemMetadata(existingTranslation);
                return(RedirectToAction(
                           Convert.ToString(existingTranslationMetadata.EditorRouteValues["action"]),
                           existingTranslationMetadata.EditorRouteValues));
            }

            // pass a dummy content to the authorization check to check for "own" variations
            var dummyContent = _contentManager.New(masterContentItem.ContentType);

            var contentItemTranslation = _contentManager.Clone(masterContentItem);

            if (!Services.Authorizer.Authorize(Permissions.EditContent, contentItemTranslation, T("Couldn't create translated content")))
            {
                return(new HttpUnauthorizedResult());
            }

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

            if (localizationPart != null)
            {
                localizationPart.MasterContentItem = masterContentItem;
                localizationPart.Culture           = string.IsNullOrWhiteSpace(to) ? null : _cultureManager.GetCultureByName(to);
            }

            Services.Notifier.Success(T("Successfully cloned. The translated content was saved as a draft."));

            var adminRouteValues = _contentManager.GetItemMetadata(contentItemTranslation).AdminRouteValues;

            return(RedirectToRoute(adminRouteValues));
        }
        public ActionResult Translate(int id, string to)
        {
            var masterContentItem = _contentManager.Get(id, VersionOptions.Latest);

            if (masterContentItem == null)
            {
                return(HttpNotFound());
            }

            if (!Services.Authorizer.Authorize(Permissions.ViewContent, masterContentItem, T("Couldn't open original content")))
            {
                return(new HttpUnauthorizedResult());
            }

            var masterLocalizationPart = masterContentItem.As <LocalizationPart>();

            if (masterLocalizationPart == null)
            {
                return(HttpNotFound());
            }

            // Check if current item still exists, and redirect.
            var existingTranslation = _localizationService.GetLocalizedContentItem(masterContentItem, to);

            if (existingTranslation != null)
            {
                var existingTranslationMetadata = _contentManager.GetItemMetadata(existingTranslation);
                return(RedirectToAction(
                           Convert.ToString(existingTranslationMetadata.EditorRouteValues["action"]),
                           existingTranslationMetadata.EditorRouteValues));
            }

            // pass a dummy content to the authorization check to check for "own" variations
            var dummyContent = _contentManager.New(masterContentItem.ContentType);

            if (!Services.Authorizer.Authorize(Permissions.EditContent, dummyContent, T("Couldn't create translated content")))
            {
                return(new HttpUnauthorizedResult());
            }

            var contentItemTranslation = _contentManager.Clone(masterContentItem);

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

            if (localizationPart != null)
            {
                localizationPart.MasterContentItem = masterLocalizationPart.MasterContentItem == null ? masterContentItem : masterLocalizationPart.MasterContentItem;
                localizationPart.Culture           = string.IsNullOrWhiteSpace(to) ? null : _cultureManager.GetCultureByName(to);
            }

            Services.Notifier.Success(T("Successfully cloned. The translated content was saved as a draft."));

            var editorRouteValues = _contentManager.GetItemMetadata(contentItemTranslation).EditorRouteValues;

            // adds request variables of current controller to the new redirect route
            // for example the returnUrl parameter
            foreach (var key in Request.Form.AllKeys.Where(x => !x.StartsWith("__") && !editorRouteValues.Keys.Contains(x)))
            {
                editorRouteValues.Add(key, Request.Form[key]);
            }
            foreach (var key in Request.QueryString.AllKeys.Where(x => !x.StartsWith("__") && !editorRouteValues.Keys.Contains(x)))
            {
                editorRouteValues.Add(key, Request.QueryString[key]);
            }
            return(RedirectToRoute(editorRouteValues));
        }
Example #6
0
        protected override void UpdateEditorShape(UpdateEditorContext context)
        {
            //Here we implement the logic based on the settings introduced in MediaLibraryPickerFieldLocalizationSettings
            //These settings should only be active if the ContentItem that is being updated has a LocalizationPart
            if (context.ContentItem.Parts.Any(part => part is LocalizationPart))
            {
                var lPart          = (LocalizationPart)context.ContentItem.Parts.Single(part => part is LocalizationPart);
                var fields         = context.ContentItem.Parts.SelectMany(x => x.Fields.Where(f => f.FieldDefinition.Name == typeof(MediaLibraryPickerField).Name)).Cast <MediaLibraryPickerField>();
                var contentCulture = context.ContentItem.As <LocalizationPart>().Culture != null?context.ContentItem.As <LocalizationPart>().Culture.Culture : null;

                foreach (var field in fields)
                {
                    var fieldSettings = field.PartFieldDefinition.Settings.GetModel <MediaLibraryPickerFieldSettings>();
                    var settings      = field.PartFieldDefinition.Settings.GetModel <MediaLibraryPickerFieldLocalizationSettings>();

                    if (settings.TryToLocalizeMedia)
                    {
                        //try to replace items in the field with their translation
                        var itemsInField = _contentManager.GetMany <ContentItem>(field.Ids, VersionOptions.Latest, QueryHints.Empty);
                        var mediaIds     = new List <int>();
                        foreach (var item in itemsInField)
                        {
                            // negatives id whoud be localized
                            var mediaItem          = _contentManager.Get(item.Id, VersionOptions.Latest);
                            var mediaIsLocalizable = mediaItem.As <LocalizationPart>() != null;
                            var mediaCulture       = mediaIsLocalizable && mediaItem.As <LocalizationPart>().Culture != null?mediaItem.As <LocalizationPart>().Culture.Culture : null;

                            if (mediaItem != null && mediaIsLocalizable)
                            {
                                // The media is localizable
                                if (contentCulture == mediaCulture)
                                {
                                    // The content culture and the media culture match
                                    mediaIds.Add(mediaItem.Id);
                                }
                                else
                                {
                                    if (mediaCulture == null)
                                    {
                                        // The media has not a culture, so it takes the content culture
                                        _localizationServices.SetContentCulture(mediaItem, contentCulture);
                                        mediaIds.Add(mediaItem.Id);
                                        _orchardServices.Notifier.Warning(T(
                                                                              "{0}: the media item {1} was culture neutral and it has been localized",
                                                                              field.DisplayName,
                                                                              mediaItem.As <MediaPart>().FileName));
                                    }
                                    else
                                    {
                                        // The media has a culture
                                        var localizedMedia = _localizationServices.GetLocalizedContentItem(mediaItem, contentCulture);
                                        if (localizedMedia != null)
                                        {
                                            // The media has a translation, so the field will replace current media with the right localized one.
                                            mediaIds.Add(localizedMedia.Id);
                                            _orchardServices.Notifier.Warning(T(
                                                                                  "{0}: the media item {1} has been replaced by its localized version",
                                                                                  field.DisplayName,
                                                                                  mediaItem.As <MediaPart>().FileName));
                                        }
                                        else
                                        {
                                            if (!settings.RemoveItemsWithoutLocalization)
                                            {
                                                // The media supports translations but have not a localized version, so it will be cloned in the right language
                                                var clonedMedia           = _contentManager.Clone(mediaItem);
                                                var mediaLocalizationPart = mediaItem.As <LocalizationPart>();
                                                if (mediaLocalizationPart != null)
                                                {
                                                    _localizationServices.SetContentCulture(clonedMedia, contentCulture);
                                                    clonedMedia.As <LocalizationPart>().MasterContentItem = mediaLocalizationPart.MasterContentItem == null ? mediaItem : mediaLocalizationPart.MasterContentItem;
                                                }
                                                _contentManager.Publish(clonedMedia);
                                                mediaIds.Add(clonedMedia.Id);
                                                _orchardServices.Notifier.Warning(T(
                                                                                      "{0}: a localized version of media item {1} has been created",
                                                                                      field.DisplayName,
                                                                                      mediaItem.As <MediaPart>().FileName));
                                            }
                                            else
                                            {
                                                _orchardServices.Notifier.Warning(T(
                                                                                      "{0}: the media item {1} has been removed from the field because its culture differs from content's culture",
                                                                                      field.DisplayName,
                                                                                      mediaItem.As <MediaPart>().FileName));
                                            }
                                        }
                                    }
                                }
                            }
                            else if (mediaItem != null && !mediaIsLocalizable)
                            {
                                if (!settings.RemoveItemsWithNoLocalizationPart)
                                {
                                    mediaIds.Add(mediaItem.Id);
                                }
                                else
                                {
                                    _orchardServices.Notifier.Warning(T(
                                                                          "{0}: the media item {1} has been removed from the field because culture neutral",
                                                                          field.DisplayName,
                                                                          mediaItem.As <MediaPart>().FileName));
                                }
                            }
                        }

                        field.Ids = mediaIds.Distinct().ToArray();

                        if (field.Ids.Length == 0 && fieldSettings.Required)
                        {
                            context.Updater.AddModelError("Id", T("The {0} field is required.", field.DisplayName));
                        }
                    }
                }
            }
        }