public EditTypeViewModel GetType(string name)
        {
            var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(name);

            if (contentTypeDefinition == null)
                return null;

            var viewModel = new EditTypeViewModel(contentTypeDefinition)
            {
                Templates = _contentDefinitionEditorEvents.Invoke(x => x.TypeEditor(contentTypeDefinition), Logger)
            };

            foreach (var part in viewModel.Parts)
            {
                part._Definition.ContentTypeDefinition = contentTypeDefinition;
                part.Templates = _contentDefinitionEditorEvents.Invoke(x => x.TypePartEditor(part._Definition), Logger);
                foreach (var field in part.PartDefinition.Fields)
                    field.Templates = _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditor(field._Definition), Logger);
            }

            if (viewModel.Fields.Any())
            {
                foreach (var field in viewModel.Fields)
                    field.Templates = _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditor(field._Definition), Logger);
            }

            return viewModel;
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> CreatePOST(CreateTypeViewModel viewModel)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
                return Unauthorized();

            viewModel.DisplayName = viewModel.DisplayName ?? String.Empty;
            viewModel.Name = viewModel.Name ?? String.Empty;

            if (String.IsNullOrWhiteSpace(viewModel.DisplayName))
            {
                ModelState.AddModelError("DisplayName", S["The Display Name name can't be empty."]);
            }

            if (String.IsNullOrWhiteSpace(viewModel.Name))
            {
                ModelState.AddModelError("Name", S["The Content Type Id can't be empty."]);
            }

            if (_contentDefinitionService.GetTypes().Any(t => String.Equals(t.Name.Trim(), viewModel.Name.Trim(), StringComparison.OrdinalIgnoreCase)))
            {
                ModelState.AddModelError("Name", S["A type with the same Id already exists."]);
            }

            if (!String.IsNullOrWhiteSpace(viewModel.Name) && !viewModel.Name[0].IsLetter())
            {
                ModelState.AddModelError("Name", S["The technical name must start with a letter."]);
            }

            if (_contentDefinitionService.GetTypes().Any(t => String.Equals(t.DisplayName.Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase)))
            {
                ModelState.AddModelError("DisplayName", S["A type with the same Display Name already exists."]);
            }

            if (!ModelState.IsValid)
            {
                _session.Cancel();
                return View(viewModel);
            }

            var contentTypeDefinition = _contentDefinitionService.AddType(viewModel.Name, viewModel.DisplayName);

            var typeViewModel = new EditTypeViewModel(contentTypeDefinition);


            _notifier.Success(T["The \"{0}\" content type has been created.", typeViewModel.DisplayName]);

            return RedirectToAction("AddPartsTo", new { id = typeViewModel.Name });
        }
Ejemplo n.º 3
0
        public async Task<ActionResult> EditPOST(string id, EditTypeViewModel viewModel)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
                return Unauthorized();

            var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(id);

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

            viewModel.Settings = contentTypeDefinition.Settings;
            viewModel.TypeDefinition = contentTypeDefinition;
            viewModel.DisplayName = contentTypeDefinition.DisplayName;
            viewModel.Editor = await _contentDefinitionDisplayManager.UpdateTypeEditorAsync(contentTypeDefinition, this);

            if (!ModelState.IsValid)
            {
                _session.Cancel();

                HackModelState(nameof(EditTypeViewModel.OrderedFieldNames));
                HackModelState(nameof(EditTypeViewModel.OrderedPartNames));

                return View(viewModel);
            }
            else
            {
                var ownedPartDefinition = _contentDefinitionManager.GetPartDefinition(contentTypeDefinition.Name);
                _contentDefinitionService.AlterPartFieldsOrder(ownedPartDefinition, viewModel.OrderedFieldNames);
                _contentDefinitionService.AlterTypePartsOrder(contentTypeDefinition, viewModel.OrderedPartNames);
                _notifier.Success(T["\"{0}\" settings have been saved.", contentTypeDefinition.Name]);
            }

            return RedirectToAction("Edit", new { id });
        }
Ejemplo n.º 4
0
        public async Task<ActionResult> EditPOST(string id)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
                return new HttpUnauthorizedResult();

            var typeViewModel = _contentDefinitionService.GetType(id);

            if (typeViewModel == null)
                return HttpNotFound();

            var edited = new EditTypeViewModel();
            await TryUpdateModelAsync(edited);
            typeViewModel.DisplayName = edited.DisplayName ?? string.Empty;

            if (String.IsNullOrWhiteSpace(typeViewModel.DisplayName))
            {
                ModelState.AddModelError("DisplayName", T["The Content Type name can't be empty."]);
            }

            if (_contentDefinitionService.GetTypes().Any(t => String.Equals(t.DisplayName.Trim(), typeViewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase) && !String.Equals(t.Name, id)))
            {
                ModelState.AddModelError("DisplayName", T["A type with the same name already exists."]);
            }

            if (!ModelState.IsValid)
                return View(typeViewModel);

            _contentDefinitionService.AlterType(typeViewModel, this);

            if (!ModelState.IsValid)
            {
                _session.Cancel();
                return View(typeViewModel);
            }

            //Services.Notifier.Information(T("\"{0}\" settings have been saved.", typeViewModel.DisplayName));

            return RedirectToAction("List");
        }
        public void AlterType(EditTypeViewModel typeViewModel, IUpdateModel updateModel)
        {
            var updater = new PrefixedModelUpdater(updateModel);
            _contentDefinitionManager.AlterTypeDefinition(typeViewModel.Name, typeBuilder =>
            {
                typeBuilder.DisplayedAs(typeViewModel.DisplayName);

                // allow extensions to alter type configuration
                _contentDefinitionEditorEvents.Invoke(x => x.TypeEditorUpdating(typeBuilder), Logger);
                typeViewModel.Templates = _contentDefinitionEditorEvents.Invoke(x => x.TypeEditorUpdate(typeBuilder, updater), Logger);
                _contentDefinitionEditorEvents.Invoke(x => x.TypeEditorUpdated(typeBuilder), Logger);

                foreach (var part in typeViewModel.Parts)
                {
                    var partViewModel = part;

                    // enable updater to be aware of changing part prefix
                    updater.Prefix = secondHalf => String.Format("{0}.{1}", partViewModel.Prefix, secondHalf);

                    // allow extensions to alter typePart configuration
                    typeBuilder.WithPart(partViewModel.PartDefinition.Name, typePartBuilder =>
                    {
                        _contentDefinitionEditorEvents.Invoke(x => x.TypePartEditorUpdating(typePartBuilder), Logger);
                        partViewModel.Templates = _contentDefinitionEditorEvents.Invoke(x => x.TypePartEditorUpdate(typePartBuilder, updater), Logger);
                        _contentDefinitionEditorEvents.Invoke(x => x.TypePartEditorUpdated(typePartBuilder), Logger);
                    });

                    if (!partViewModel.PartDefinition.Fields.Any())
                        continue;

                    _contentDefinitionManager.AlterPartDefinition(partViewModel.PartDefinition.Name, partBuilder =>
                    {
                        var fieldFirstHalf = String.Format("{0}.{1}", partViewModel.Prefix, partViewModel.PartDefinition.Prefix);
                        foreach (var field in partViewModel.PartDefinition.Fields)
                        {
                            var fieldViewModel = field;

                            // enable updater to be aware of changing field prefix
                            updater.Prefix = secondHalf =>
                                String.Format("{0}.{1}.{2}", fieldFirstHalf, fieldViewModel.Prefix, secondHalf);
                            // allow extensions to alter partField configuration
                            partBuilder.WithField(fieldViewModel.Name, partFieldBuilder =>
                            {
                                _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditorUpdating(partFieldBuilder), Logger);
                                fieldViewModel.Templates = _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditorUpdate(partFieldBuilder, updater), Logger);
                                _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditorUpdated(partFieldBuilder), Logger);
                            });
                        }
                    });
                }

                if (typeViewModel.Fields.Any())
                {
                    _contentDefinitionManager.AlterPartDefinition(typeViewModel.Name, partBuilder =>
                    {
                        foreach (var field in typeViewModel.Fields)
                        {
                            var fieldViewModel = field;

                            // enable updater to be aware of changing field prefix
                            updater.Prefix = secondHalf =>
                                string.Format("{0}.{1}", fieldViewModel.Prefix, secondHalf);

                            // allow extensions to alter partField configuration
                            partBuilder.WithField(fieldViewModel.Name, partFieldBuilder =>
                            {
                                _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditorUpdating(partFieldBuilder), Logger);
                                fieldViewModel.Templates = _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditorUpdate(partFieldBuilder, updater), Logger);
                                _contentDefinitionEditorEvents.Invoke(x => x.PartFieldEditorUpdated(partFieldBuilder), Logger);
                            });
                        }
                    });
                }
            });
        }