Example #1
0
        public ActionResult CreatePOST(CreateTypeViewModel viewModel)
        {
            if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to create a content type.")))
            {
                return(new HttpUnauthorizedResult());
            }

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

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

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

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

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

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

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

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

            // adds CommonPart by default
            _contentDefinitionService.AddPartToType("CommonPart", viewModel.Name);

            var typeViewModel = new EditTypeViewModel(contentTypeDefinition);


            Services.Notifier.Information(T("The \"{0}\" content type has been created.", typeViewModel.DisplayName));

            return(RedirectToAction("AddPartsTo", new { id = typeViewModel.Name }));
        }
Example #2
0
        public async Task <ActionResult> AddPartsToPOST(string id)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.EditContentTypes))
            {
                return(Unauthorized());
            }

            var typeViewModel = _contentDefinitionService.GetType(id);

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

            var viewModel = new AddPartsViewModel();

            if (!await TryUpdateModelAsync(viewModel))
            {
                return(await AddPartsTo(id));
            }

            var partsToAdd = viewModel.PartSelections.Where(ps => ps.IsSelected).Select(ps => ps.PartName);

            foreach (var partToAdd in partsToAdd)
            {
                _contentDefinitionService.AddPartToType(partToAdd, typeViewModel.Name);
                _notifier.Success(T["The \"{0}\" part has been added.", partToAdd]);
            }

            if (!ModelState.IsValid)
            {
                _session.Cancel();
                return(await AddPartsTo(id));
            }

            return(RedirectToAction("Edit", new { id }));
        }
Example #3
0
        public ActionResult AddPartsToPOST(string id)
        {
            if (!Services.Authorizer.Authorize(Permissions.EditContentTypes, T("Not allowed to edit a content type.")))
            {
                return(new HttpUnauthorizedResult());
            }

            var typeViewModel = _contentDefinitionService.GetType(id);

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

            var viewModel = new AddPartsViewModel();

            if (!TryUpdateModel(viewModel))
            {
                return(AddPartsTo(id));
            }

            var partsToAdd = viewModel.PartSelections.Where(ps => ps.IsSelected).Select(ps => ps.PartName);

            foreach (var partToAdd in partsToAdd)
            {
                _contentDefinitionService.AddPartToType(partToAdd, typeViewModel.Name);
                Services.Notifier.Information(T("The \"{0}\" part has been added.", partToAdd));
            }

            if (!ModelState.IsValid)
            {
                Services.TransactionManager.Cancel();
                return(AddPartsTo(id));
            }

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