Пример #1
0
        protected override DriverResult Editor(ContentPart part, TaxonomyField field, dynamic shapeHelper)
        {
            return(ContentShape("Fields_TaxonomyField_Edit", GetDifferentiator(field, part), () => {
                var settings = field.PartFieldDefinition.Settings.GetModel <TaxonomyFieldSettings>();
                var appliedTerms = _taxonomyService.GetTermsForContentItem(part.ContentItem.Id, field.Name).Distinct(new TermPartComparer()).ToDictionary(t => t.Id, t => t);
                var taxonomy = _taxonomyService.GetTaxonomyByName(settings.Taxonomy);
                var terms = taxonomy != null
                    ? _taxonomyService.GetTerms(taxonomy.Id).Where(t => !string.IsNullOrWhiteSpace(t.Name)).Select(t => t.CreateTermEntry()).ToList()
                    : new List <TermEntry>(0);

                terms.ForEach(t => t.IsChecked = appliedTerms.ContainsKey(t.Id));

                var viewModel = new TaxonomyFieldViewModel {
                    DisplayName = field.DisplayName,
                    Name = field.Name,
                    Terms = terms,
                    Settings = settings,
                    SingleTermId = terms.Where(t => t.IsChecked).Select(t => t.Id).FirstOrDefault(),
                    TaxonomyId = taxonomy != null ? taxonomy.Id : 0
                };

                var templateName = settings.Autocomplete ? "Fields/TaxonomyField.Autocomplete" : "Fields/TaxonomyField";
                return shapeHelper.EditorTemplate(TemplateName: templateName, Model: viewModel, Prefix: GetPrefix(field, part));
            }));
        }
Пример #2
0
        protected override DriverResult Editor(ContentPart part, TaxonomyField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new TaxonomyFieldViewModel {
                Terms = new List <TermEntry>()
            };

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                var checkedTerms = viewModel.Terms
                                   .Where(t => (t.IsChecked || t.Id == viewModel.SingleTermId))
                                   .Select(t => GetOrCreateTerm(t, viewModel.TaxonomyId, field))
                                   .Where(t => t != null).ToList();

                var settings = field.PartFieldDefinition.Settings.GetModel <TaxonomyFieldSettings>();
                if (settings.Required && !checkedTerms.Any())
                {
                    updater.AddModelError(GetPrefix(field, part), T("The field {0} is mandatory.", T(field.DisplayName)));
                }
                else
                {
                    _taxonomyService.UpdateTerms(part.ContentItem, checkedTerms, field.Name);
                }
            }

            return(Editor(part, field, shapeHelper));
        }
        protected override DriverResult Editor(ContentPart part, TaxonomyField field, IUpdateModel updater, dynamic shapeHelper)
        {
            // Initializing viewmodel using the terms that are already selected to prevent loosing them when updating an editor group this field isn't displayed in.
            var appliedTerms = GetAppliedTerms(part, field, VersionOptions.Latest).ToList();
            var viewModel    = new TaxonomyFieldViewModel {
                Terms = appliedTerms.Select(t => t.CreateTermEntry()).ToList()
            };

            foreach (var item in viewModel.Terms)
            {
                item.IsChecked = true;
            }

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                var checkedTerms = viewModel.Terms
                                   .Where(t => (t.IsChecked || t.Id == viewModel.SingleTermId))
                                   .Select(t => GetOrCreateTerm(t, viewModel.TaxonomyId, field))
                                   .Where(t => t != null).ToList();

                var settings = field.PartFieldDefinition.Settings.GetModel <TaxonomyFieldSettings>();
                if (settings.Required && !checkedTerms.Any())
                {
                    updater.AddModelError(GetPrefix(field, part), T("The field {0} is mandatory.", T(field.DisplayName)));
                }
                else
                {
                    _taxonomyService.UpdateTerms(part.ContentItem, checkedTerms, field.Name);
                }
            }

            return(BuildEditorShape(part, field, shapeHelper, viewModel));
        }
        private ContentShapeResult BuildEditorShape(ContentPart part, TaxonomyField field, dynamic shapeHelper, TaxonomyFieldViewModel appliedViewModel = null)
        {
            return(ContentShape("Fields_TaxonomyField_Edit", GetDifferentiator(field, part), () => {
                var settings = field.PartFieldDefinition.Settings.GetModel <TaxonomyFieldSettings>();
                var appliedTerms = GetAppliedTerms(part, field, VersionOptions.Latest).ToDictionary(t => t.Id, t => t);
                var taxonomy = _taxonomyService.GetTaxonomyByName(settings.Taxonomy);
                var terms = taxonomy != null && !settings.Autocomplete
                    ? _taxonomyService.GetTerms(taxonomy.Id).Where(t => !string.IsNullOrWhiteSpace(t.Name)).Select(t => t.CreateTermEntry()).ToList()
                    : new List <TermEntry>(0);

                // Ensure the modified taxonomy items are not lost if a model validation error occurs
                if (appliedViewModel != null)
                {
                    terms.ForEach(t => t.IsChecked = appliedViewModel.Terms.Any(at => at.Id == t.Id && at.IsChecked) || t.Id == appliedViewModel.SingleTermId);
                }
                else
                {
                    terms.ForEach(t => t.IsChecked = appliedTerms.ContainsKey(t.Id));
                }

                var viewModel = new TaxonomyFieldViewModel {
                    DisplayName = field.DisplayName,
                    Name = field.Name,
                    Terms = terms,
                    SelectedTerms = appliedTerms.Select(t => t.Value),
                    Settings = settings,
                    SingleTermId = appliedTerms.Select(t => t.Key).FirstOrDefault(),
                    TaxonomyId = taxonomy != null ? taxonomy.Id : 0,
                    HasTerms = taxonomy != null && _taxonomyService.GetTermsCount(taxonomy.Id) > 0
                };

                var templateName = settings.Autocomplete ? "Fields/TaxonomyField.Autocomplete" : "Fields/TaxonomyField";
                return shapeHelper.EditorTemplate(TemplateName: templateName, Model: viewModel, Prefix: GetPrefix(field, part));
            }));
        }
        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));
        }
Пример #6
0
        protected override DriverResult Editor(ContentPart part, TaxonomyField field, IUpdateModel updater, dynamic shapeHelper)
        {
            var viewModel = new TaxonomyFieldViewModel {
                Terms = new List <TermEntry>()
            };

            if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null))
            {
                var checkedTerms = viewModel.Terms.Where(t => t.IsChecked || t.Id == viewModel.SingleTermId).Select(t => _taxonomyService.GetTerm(t.Id)).ToList();
                _taxonomyService.UpdateTerms(part.ContentItem, checkedTerms, field.Name);
            }

            return(Editor(part, field, shapeHelper));
        }
Пример #7
0
        protected override DriverResult Editor(ContentPart part, TaxonomyField field, dynamic shapeHelper)
        {
            var settings = field.PartFieldDefinition.Settings.GetModel <TaxonomyFieldSettings>();

            var appliedTerms = _taxonomyService.GetTermsForContentItem(part.ContentItem.Id, field.Name).ToDictionary(t => t.Id, t => t);
            var taxonomy     = _taxonomyService.GetTaxonomyByName(settings.Taxonomy);
            var terms        = _taxonomyService.GetTerms(taxonomy.Id).Select(t => t.CreateTermEntry()).ToList();

            terms.ForEach(t => t.IsChecked = appliedTerms.ContainsKey(t.Id));

            var viewModel = new TaxonomyFieldViewModel {
                Name         = field.Name,
                Terms        = terms,
                Settings     = settings,
                SingleTermId = terms.Where(t => t.IsChecked).Select(t => t.Id).FirstOrDefault()
            };

            return(ContentShape("Fields_Contrib_TaxonomyField_Edit", GetDifferentiator(field, part),
                                () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: viewModel, Prefix: GetPrefix(field, part))));
        }