Beispiel #1
0
        protected override void BuildEditorShape(BuildEditorContext context)
        {
            // case new translation of contentitem
            var localizationPart = context.ContentItem.As <LocalizationPart>();

            if (localizationPart == null || localizationPart.Culture != null || context.ContentItem.As <LocalizationPart>().MasterContentItem == null || context.ContentItem.As <LocalizationPart>().MasterContentItem.Id == 0)
            {
                return;
            }
            var partFieldDefinitions = context.ContentItem.Parts.SelectMany(p => p.PartDefinition.Fields).Where(x => x.FieldDefinition.Name == "TaxonomyField");

            if (partFieldDefinitions == null)
            {
                return; // contentitem without taxonomy
            }
            base.BuildEditorShape(context);
            var missingCultures = RetrieveMissingCultures(localizationPart, localizationPart.Culture != null);

            foreach (var partFieldDefinition in partFieldDefinitions)
            {
                if (partFieldDefinition.Settings.GetModel <TaxonomyFieldLocalizationSettings>().TryToLocalize)
                {
                    var originalTermParts = _taxonomyService.GetTermsForContentItem(context.ContentItem.Id, partFieldDefinition.Name, VersionOptions.Latest).Distinct(new TermPartComparer()).ToList();
                    var newTermParts      = new List <TermPart>();
                    foreach (var originalTermPart in originalTermParts)
                    {
                        var masterTermPart = _taxonomyExtensionsService.GetMasterItem(originalTermPart.ContentItem);
                        if (masterTermPart != null)
                        {
                            foreach (var missingCulture in missingCultures)
                            {
                                var newTerm = _localizationService.GetLocalizedContentItem(masterTermPart, missingCulture);
                                if (newTerm != null)
                                {
                                    if (!newTermParts.Contains(newTerm.ContentItem.As <TermPart>()))  //Prevent duplicates
                                    {
                                        newTermParts.Add(newTerm.ContentItem.As <TermPart>());
                                    }
                                }
                                else
                                {
                                    _notifier.Add(NotifyType.Warning, T("Term {0} can't be localized on {1}, term has been removed on this language", originalTermPart.ContentItem.As <TitlePart>().Title, missingCulture));
                                }
                            }
                        }
                        else
                        {
                            _notifier.Add(NotifyType.Warning, T("Term {0} can't be localized, term has been removed", originalTermPart.ContentItem.As <TitlePart>().Title));
                        }
                    }
                    _taxonomyService.UpdateTerms(context.ContentItem, newTermParts, partFieldDefinition.Name);
                }
            }
        }
        public ActionResult GetTaxonomy(string contentTypeName, string taxonomyFieldName, int contentId, string culture)
        {
            var  viewModel         = new TaxonomyFieldViewModel();
            bool autocomplete      = false;
            var  contentDefinition = _contentDefinitionManager.GetTypeDefinition(contentTypeName);

            if (contentDefinition != null)
            {
                var taxonomyField             = contentDefinition.Parts.SelectMany(p => p.PartDefinition.Fields).Where(x => x.FieldDefinition.Name == "TaxonomyField" && x.Name == taxonomyFieldName).FirstOrDefault();
                var contentTypePartDefinition = contentDefinition.Parts.Where(x => x.PartDefinition.Fields.Any(a => a.FieldDefinition.Name == "TaxonomyField" && a.Name == taxonomyFieldName)).FirstOrDefault();
                ViewData.TemplateInfo.HtmlFieldPrefix = contentTypePartDefinition.PartDefinition.Name + "." + taxonomyField.Name;
                if (taxonomyField != null)
                {
                    var taxonomySettings = taxonomyField.Settings.GetModel <TaxonomyFieldSettings>();
                    // Getting the translated taxonomy and its terms

                    var      masterTaxonomy = _taxonomyExtensionsService.GetMasterItem(_taxonomyService.GetTaxonomyByName(taxonomySettings.Taxonomy));
                    IContent taxonomy;
                    var      trytranslate = _localizationService.GetLocalizedContentItem(masterTaxonomy, culture);
                    if (trytranslate == null) // case taxonomy not localized
                    {
                        taxonomy = masterTaxonomy;
                    }
                    else
                    {
                        taxonomy = _localizationService.GetLocalizedContentItem(masterTaxonomy, culture).ContentItem;
                    }
                    var terms = taxonomy != null // && !taxonomySettings.Autocomplete
                        ? _taxonomyService.GetTerms(taxonomy.Id).Where(t => !string.IsNullOrWhiteSpace(t.Name)).Select(t => t.CreateTermEntry()).Where(te => !te.HasDraft).ToList()
                        : new List <TermEntry>(0);

                    List <TermPart> appliedTerms = new List <TermPart>();
                    if (contentId > 0)
                    {
                        appliedTerms = _taxonomyService.GetTermsForContentItem(contentId, taxonomyFieldName, VersionOptions.Published).Distinct(new TermPartComparer()).ToList();
                        terms.ForEach(t => t.IsChecked = appliedTerms.Select(x => x.Id).Contains(t.Id));
                    }
                    viewModel = new TaxonomyFieldViewModel {
                        DisplayName   = taxonomyField.DisplayName,
                        Name          = taxonomyField.Name,
                        Terms         = terms,
                        SelectedTerms = appliedTerms,
                        Settings      = taxonomySettings,
                        SingleTermId  = appliedTerms.Select(t => t.Id).FirstOrDefault(),
                        TaxonomyId    = taxonomy != null ? taxonomy.Id : 0,
                        HasTerms      = taxonomy != null && _taxonomyService.GetTermsCount(taxonomy.Id) > 0
                    };
                    if (taxonomySettings.Autocomplete)
                    {
                        autocomplete = true;
                    }
                }
            }
            var templateName = autocomplete ? "../EditorTemplates/Fields/TaxonomyField.Autocomplete" : "../EditorTemplates/Fields/TaxonomyField";

            return(View(templateName, viewModel));
        }
        protected override DriverResult Editor(TermPart termPart, IUpdateModel updater, dynamic shapeHelper)
        {
            if (updater.TryUpdateModel(termPart, Prefix, null, null))
            {
                //If the term is localized and has a parent term I check if the term culture is the same of its parent culture
                if (termPart.Has <LocalizationPart>())
                {
                    if (IsParentLocalized(termPart, updater))
                    {
                        //Retrieving the parent taxonomy and the parent term
                        ContentItem parentTerm     = _taxonomyExtensionsService.GetParentTerm(termPart);
                        ContentItem parentTaxonomy = _taxonomyExtensionsService.GetParentTaxonomy(termPart);

                        if (termPart.As <LocalizationPart>().Culture != null)
                        {
                            CultureRecord termCulture     = termPart.As <LocalizationPart>().Culture;
                            CultureRecord taxonomyCulture = null;

                            //If a parent term exists I retrieve its localized version.
                            ContentItem localizedParentTerm = parentTerm;
                            if (parentTerm != null)
                            {
                                if (parentTerm.Has <LocalizationPart>())
                                {
                                    CultureRecord parentTermCulture = parentTerm.As <LocalizationPart>().Culture;

                                    if (parentTermCulture != null && termCulture.Id != parentTermCulture.Id)
                                    {
                                        IContent masterParentTerm = _taxonomyExtensionsService.GetMasterItem(parentTerm);
                                        var      localizedParent  = _localizationService.GetLocalizedContentItem(masterParentTerm, termCulture.Culture);
                                        if (localizedParent != null)
                                        {
                                            localizedParentTerm = localizedParent.As <ContentItem>();
                                        }
                                    }
                                }
                            }

                            //Retrieving the localized version of the Taxonomy
                            ContentItem localizedParentTaxonomy = parentTaxonomy;
                            if (parentTaxonomy.Has <LocalizationPart>())
                            {
                                if (parentTaxonomy.As <LocalizationPart>().Culture != null)
                                {
                                    taxonomyCulture = parentTaxonomy.As <LocalizationPart>().Culture;
                                    if (termCulture.Id != taxonomyCulture.Id)
                                    {
                                        IContent masterTaxonomy    = _taxonomyExtensionsService.GetMasterItem(parentTaxonomy);
                                        var      localizedTaxonomy = _localizationService.GetLocalizedContentItem(masterTaxonomy, termCulture.Culture);
                                        if (localizedTaxonomy != null)
                                        {
                                            localizedParentTaxonomy = localizedTaxonomy.As <ContentItem>();
                                        }
                                    }
                                }
                            }

                            //Assigning to the term the corresponding localized container
                            if ((localizedParentTerm == null && localizedParentTaxonomy != parentTaxonomy) || (localizedParentTerm != null && localizedParentTerm != parentTerm && localizedParentTerm.Id != termPart.Id))
                            {
                                termPart.Container = localizedParentTerm == null?localizedParentTaxonomy.As <TaxonomyPart>().ContentItem : localizedParentTerm.As <TermPart>().ContentItem;

                                termPart.Path = localizedParentTerm != null?localizedParentTerm.As <TermPart>().FullPath + "/" : "/";

                                if (localizedParentTerm == null)
                                {
                                    termPart.TaxonomyId = localizedParentTaxonomy.Id;
                                }
                                else
                                {
                                    termPart.TaxonomyId = localizedParentTerm.As <TermPart>().TaxonomyId;
                                }

                                if (localizedParentTaxonomy != parentTaxonomy)
                                {
                                    _notifier.Add(NotifyType.Information, T("The parent taxonomy has been changed to its localized version associated to the culture {0}.", localizedParentTaxonomy.As <LocalizationPart>().Culture.Culture));
                                }

                                if (localizedParentTerm != null && localizedParentTerm != parentTerm)
                                {
                                    _notifier.Add(NotifyType.Information, T("The parent term has been changed to its localized version associated to the culture {0}.", localizedParentTerm.As <LocalizationPart>().Culture.Culture));
                                }
                            }
                            else if (termCulture != taxonomyCulture && taxonomyCulture != null && _localizationService.GetLocalizations(parentTaxonomy).Count() > 0)
                            {
                                //I can associate to a taxonomy a term of a different culture only if the taxonomy is unlocalized or has no translations
                                updater.AddModelError("WrongTaxonomyLocalization", T("A localization of the taxonomy in the specified language does not exist. Please create it first."));
                            }
                        }
                    }
                }
            }

            return(null);
        }