Exemplo n.º 1
0
        public async Task <ActionResult> EditTypePartPOST(string id, EditTypePartViewModel viewModel)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
            {
                return(Unauthorized());
            }

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

            var typeDefinition = _contentDefinitionManager.GetTypeDefinition(id);

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

            var part = typeDefinition.Parts.FirstOrDefault(x => x.Name == viewModel.Name);

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

            viewModel.TypePartDefinition = part;

            if (part.PartDefinition.IsReusable())
            {
                if (part.DisplayName() != viewModel.DisplayName)
                {
                    // Prevent null reference exception in validation
                    viewModel.DisplayName = viewModel.DisplayName?.Trim() ?? String.Empty;

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

                    if (typeDefinition.Parts.Any(t => t.Name != viewModel.Name && String.Equals(t.DisplayName()?.Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase)))
                    {
                        ModelState.AddModelError("DisplayName", S["A part with the same display name already exists."]);
                    }

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

                _contentDefinitionService.AlterTypePart(viewModel);
            }

            viewModel.Editor = await _contentDefinitionDisplayManager.UpdateTypePartEditorAsync(part, this);

            if (!ModelState.IsValid)
            {
                _session.Cancel();
                return(View(viewModel));
            }
            else
            {
                _notifier.Success(T["The \"{0}\" part settings have been saved.", part.DisplayName()]);
            }

            return(RedirectToAction("Edit", new { id }));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> EditTypePartPOST(string id, EditTypePartViewModel viewModel)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
            {
                return(Forbid());
            }

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

            var typeDefinition = _contentDefinitionManager.LoadTypeDefinition(id);

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

            var part = typeDefinition.Parts.FirstOrDefault(x => x.Name == viewModel.Name);

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

            viewModel.TypePartDefinition = part;

            if (part.PartDefinition.IsReusable())
            {
                if (part.DisplayName() != viewModel.DisplayName)
                {
                    // Prevent null reference exception in validation
                    viewModel.DisplayName = viewModel.DisplayName?.Trim() ?? String.Empty;

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

                    if (typeDefinition.Parts.Any(t => t.Name != viewModel.Name && String.Equals(t.DisplayName()?.Trim(), viewModel.DisplayName.Trim(), StringComparison.OrdinalIgnoreCase)))
                    {
                        ModelState.AddModelError("DisplayName", S["A part with the same Display Name already exists."]);
                    }

                    if (!ModelState.IsValid)
                    {
                        viewModel.Shape = await _contentDefinitionDisplayManager.UpdateTypePartEditorAsync(part, _updateModelAccessor.ModelUpdater);

                        await _documentStore.CancelAsync();

                        return(View(viewModel));
                    }
                }
            }

            _contentDefinitionService.AlterTypePart(viewModel);

            // Refresh the local part variable in case it has been altered
            part = _contentDefinitionManager.LoadTypeDefinition(id).Parts.FirstOrDefault(x => x.Name == viewModel.Name);

            viewModel.Shape = await _contentDefinitionDisplayManager.UpdateTypePartEditorAsync(part, _updateModelAccessor.ModelUpdater);

            if (!ModelState.IsValid)
            {
                await _documentStore.CancelAsync();

                return(View(viewModel));
            }
            else
            {
                await _notifier.SuccessAsync(H["The \"{0}\" part settings have been saved.", part.DisplayName()]);
            }

            return(RedirectToAction(nameof(Edit), new { id }));
        }