/// <summary> /// Retrieves form for master page zones update. /// </summary> /// <param name="context">Form context.</param> /// <returns>Form object.</returns> private Form GetMasterPageZonesForm(string context) { // Get tenant identifier long tenantId = _authenticationService.TenantId; // Get identifier of master page that is being updated string[] parts = context.Split('|'); long masterPageId = Convert.ToInt64(parts[0]); // Get existing master page details MasterPage masterPage = _masterPageService.Read(tenantId, masterPageId); // Construct view model Form form = new Form { Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context }; form.SubmitLabel = MasterPageResource.UpdateZonesButtonLabel; // Create sub forms form.SubForms = new Dictionary <string, Form>(); form.SubForms.Add("zone", GetMasterPageZoneForm(context)); // Set master page as form data form.Data = JsonConvert.SerializeObject(masterPage.MasterPageZones); // Return form return(form); }
/// <summary> /// Gets form for updating existing master page. /// </summary> /// <param name="context">Form context.</param> /// <returns>Form for creating a new master page.</returns> private Form GetUpdateMasterPageForm(string context) { // Get identifier of master page that is being updated string[] parts = context.Split('|'); long masterPageId = Convert.ToInt64(parts[1]); // Get existing master page details long tenantId = _authenticationService.TenantId; MasterPage masterPage = _masterPageService.Read(tenantId, masterPageId); // Get form Form form = GetMasterPageForm(context); form.SubmitLabel = MasterPageResource.UpdateMasterPageButtonLabel; form.Data = JsonConvert.SerializeObject(masterPage); return(form); }
/// <summary> /// Gets view model for master page read. /// </summary> /// <param name="tenantId">Identifies website that master pages belong to.</param> /// <param name="masterPageId">Identifies master page whose details are returned.</param> /// <returns>View model for master page read.</returns> public AdminPageViewModel <MasterPageViewModel> Read(long tenantId, long masterPageId) { // Get master page MasterPage masterPage = _masterPageService.Read(tenantId, masterPageId); // Get breadcrumbs List <NavigationItemViewModel> breadcrumbs = new List <NavigationItemViewModel>(); breadcrumbs.Add(new NavigationItemViewModel { Name = MasterPageResource.SearchMasterPagesBreadcrumbLabel, Url = _webHelperService.GetUrl(new UrlParameters { ControllerName = "masterpages", ActionName = "search" }) }); breadcrumbs.Add(new NavigationItemViewModel { Name = string.Format(MasterPageResource.ReadMasterPageBreadcrumbLabel, masterPage.Name), Url = _webHelperService.GetUrl(new UrlParameters { ControllerName = "masterpages", ActionName = "read", RouteValues = new { masterpageid = masterPage.MasterPageId } }) }); // Construct UI navigation NavigationViewModel navigation = new NavigationViewModel { ActionItems = GetActionItems(DataAction.Read, masterPage), BreadcrumbItems = breadcrumbs, HamburgerItems = _administrationPortalService.GetHamburgerItems(AdministrationArea.MasterPage) }; // Construct model to return AdminPageViewModel <MasterPageViewModel> viewModel = new AdminPageViewModel <MasterPageViewModel> { Model = _masterPageConverter.GetViewModel(masterPage), Navigation = navigation }; // Return result return(viewModel); }
public Form GetForm(string context) { // Check permissions _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements); // Get tenant, page and page zone identifiers string[] parts = context.Split('|'); long pageId = Convert.ToInt64(parts[0]); long pageZoneId = Convert.ToInt64(parts[1]); long tenantId = _authenticationService.TenantId; // Get page and master page zones Page page = _pageService.Read(tenantId, pageId); MasterPage masterPage = _masterPageService.Read(tenantId, page.MasterPageId); PageZone pageZone = page.PageZones.Where(pz => pz.PageZoneId == pageZoneId).First(); MasterPageZone masterPageZone = masterPage.MasterPageZones.Where(mpz => mpz.MasterPageZoneId == pageZone.MasterPageZoneId).First(); // Get all element types List <IElementSettings> elements = new List <IElementSettings>(); IEnumerable <ElementType> elementTypes = _elementService.ListTypes(); Dictionary <Guid, ElementType> elementTypesById = elementTypes.GroupBy(t => t.ElementTypeId).ToDictionary(t => t.Key, t => t.First()); // Construct form Form form = new Form { Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context }; List <ElementType> availableElementTypes = new List <ElementType>(); foreach (MasterPageZoneElementType masterPageZoneElementType in masterPageZone.MasterPageZoneElementTypes) { ElementType elementType = elementTypesById[masterPageZoneElementType.ElementTypeId]; availableElementTypes.Add(elementType); } form.FieldSets = GetFieldSets(elementTypesById, pageZone); form.NamedFieldSets = GetNamedFieldSets(availableElementTypes); form.SubmitLabel = PageResource.PageZoneAdminButtonLabel; // Return result return(form); }
/// <summary> /// Updates a configurable page zone. Creates new elements and removes old elements. Re-orders existing elements. /// </summary> /// <param name="tenantId">Website that page belongs to.</param> /// <param name="pageId">Page identifier.</param> /// <param name="pageZoneId">Page zone identifier.</param> /// <param name="pageZoneElements">New page zone contents.</param> /// <param name="unitOfWork">Unit of work.</param> public void UpdateZone(long tenantId, long pageId, long pageZoneId, List <PageZoneElementInfo> pageZoneElements, IUnitOfWork unitOfWork = null) { // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; // Do the page update try { // Get page, master page and page zone that we administering Page page = Read(tenantId, pageId, unitOfWork ?? localUnitOfWork); MasterPage masterPage = _masterPageService.Read(tenantId, page.MasterPageId, unitOfWork ?? localUnitOfWork); PageZone pageZone = page.PageZones.Where(z => z.PageZoneId == pageZoneId).FirstOrDefault(); // Perform validation _pageValidator.ValidateUpdateZone(masterPage, page, pageZoneId, pageZoneElements); // Construct new list of page zone elements List <PageZoneElement> newPageZoneElements = new List <PageZoneElement>(); foreach (PageZoneElementInfo info in pageZoneElements) { // Create new page zone element? if (info.PageZoneElementId == 0) { IElementSettings element = _elementService.New(tenantId, info.ElementTypeId); element.Name = info.Name; long elementId = _elementService.Create(element, unitOfWork ?? localUnitOfWork); newPageZoneElements.Add(new PageZoneElement { TenantId = tenantId, PageId = pageId, PageZoneId = pageZoneId, PageZoneElementId = 0, SortOrder = info.SortOrder, ElementTypeId = info.ElementTypeId, ElementId = elementId, Element = element }); } // Update existing page zone element? if (info.PageZoneElementId != 0) { IElementSettings element = _elementService.New(tenantId, info.ElementTypeId); element.Name = info.Name; PageZoneElement pageZoneElement = pageZone.PageZoneElements.Where(e => e.PageZoneElementId == info.PageZoneElementId).First(); pageZoneElement.SortOrder = info.SortOrder; pageZoneElement.Element = element; newPageZoneElements.Add(pageZoneElement); } } // Do the page zone element update _pageRepository.UpdatePageZoneElements(tenantId, pageId, pageZoneId, newPageZoneElements, unitOfWork ?? localUnitOfWork); // Get elements that might be removed List <ElementKeyValue> elementsToDelete = new List <ElementKeyValue>(); foreach (PageZoneElement pageZoneElement in pageZone.PageZoneElements) { PageZoneElementInfo pageZoneElementInfo = pageZoneElements.Where(e => e.PageZoneElementId == pageZoneElement.PageZoneElementId).FirstOrDefault(); if (pageZoneElementInfo == null) { elementsToDelete.Add(new ElementKeyValue { ElementId = pageZoneElement.ElementId, ElementTypeId = pageZoneElement.ElementTypeId }); } } // Delete elements if no longer in use foreach (ElementKeyValue elementKeyValue in elementsToDelete) { if (!ElementInUse(tenantId, elementKeyValue.ElementId, unitOfWork ?? localUnitOfWork)) { _elementService.Delete(tenantId, elementKeyValue.ElementTypeId, elementKeyValue.ElementId, unitOfWork ?? localUnitOfWork); } } // Commit work if local unit of work in place if (localUnitOfWork != null) { localUnitOfWork.Commit(); } } catch (ValidationErrorException) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } catch (Exception ex) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex); } }
/// <summary> /// Retrieves form for master page zone update. /// </summary> /// <param name="context">Form context.</param> /// <returns>Form object.</returns> private Form GetMasterPageZoneForm(string context) { // Get tenant identifier long tenantId = _authenticationService.TenantId; // Get identifier of master page zone that is being updated string[] parts = context.Split('|'); long masterPageId = Convert.ToInt64(parts[0]); long masterPageZoneId = Convert.ToInt64(parts[1]); // Get existing master page details MasterPage masterPage = _masterPageService.Read(tenantId, masterPageId); MasterPageZone masterPageZone = masterPage.MasterPageZones.Where(mpz => mpz.MasterPageZoneId == masterPageZoneId).FirstOrDefault(); // Construct form Form form = new Form { Fields = new Dictionary<string, IFormField>(), Id = FormId.ToString(), Context = context, SubmitLabel = MasterPageResource.UpdateZoneButtonLabel }; form.Fields.Add("name", new TextField { Name = "zoneName", Label = MasterPageResource.ZoneNameLabel, Required = true, RequiredErrorMessage = MasterPageResource.ZoneNameRequiredMessage, MaxLength = MasterPageLengths.ZoneNameMaxLength, MaxLengthErrorMessage = string.Format(MasterPageResource.ZoneNameMaxLengthMessage, "name", MasterPageLengths.ZoneNameMaxLength) }); form.Fields.Add("adminType", new SelectListField<string> { Name = "adminType", Label = MasterPageResource.AdminTypeLabel, Items = new List<ListFieldItem<string>> { new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneAdminType>(MasterPageZoneAdminType.Static), Value = ((int)MasterPageZoneAdminType.Static).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneAdminType>(MasterPageZoneAdminType.Editable), Value = ((int)MasterPageZoneAdminType.Editable).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneAdminType>(MasterPageZoneAdminType.Configurable), Value = ((int)MasterPageZoneAdminType.Configurable).ToString() } }, Required = true, RequiredErrorMessage = MasterPageResource.AdminTypeRequiredMessage }); form.Fields.Add("contentType", new SelectListField<string> { Name = "contentType", Label = MasterPageResource.ContentTypeLabel, Items = new List<ListFieldItem<string>> { new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneContentType>(MasterPageZoneContentType.Standard), Value = ((int)MasterPageZoneContentType.Standard).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneContentType>(MasterPageZoneContentType.Main), Value = ((int)MasterPageZoneContentType.Main).ToString() }, new ListFieldItem<string> { Name = _dataAnnotationsService.GetEnumDisplayName<MasterPageZoneContentType>(MasterPageZoneContentType.Comment), Value = ((int)MasterPageZoneContentType.Comment).ToString() } }, Required = true, RequiredErrorMessage = MasterPageResource.ContentTypeRequiredMessage }); form.Fields.Add("beginRender", new MultiLineTextField { Name = "beginRenderZone", Label = MasterPageResource.ZoneBeginRenderLabel, Rows = 4 }); form.Fields.Add("endRender", new MultiLineTextField { Name = "endRenderZone", Label = MasterPageResource.ZoneEndRenderLabel, Rows = 4, Value = masterPageZone.EndRender }); // Create sub forms form.SubForms = new Dictionary<string, Form>(); form.SubForms.Add("zoneElement", GetMasterPageZoneElementForm(context)); form.SubForms.Add("zoneElementNew", GetMasterPageZoneElementNewForm(context)); form.SubForms.Add("zoneElementType", GetMasterPageZoneElementTypeForm(context)); // Set master page zone as form data form.Data = JsonConvert.SerializeObject(masterPageZone); // Return form return form; }
/// <summary> /// Retrieves form for creating new page. /// </summary> /// <param name="context">Form context.</param> /// <returns>View model used to render form.</returns> private Form GetCreateForm(string context) { // Get website identifier long tenantId = _authenticationService.TenantId; // Get type of page to create string[] parts = context.Split('|'); long masterPageId = Convert.ToInt64(parts[1]); // Get the master page on which new page will be based MasterPage masterPage = _masterPageService.Read(tenantId, masterPageId); // Get list of possible parent pages List <Page> parentPages = _pageService.ListMasterPageParentPages(masterPage); // Construct form Form form = new Form { Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context }; form.Fields.Add("name", new TextField { Name = "name", Label = PageResource.NameLabel, Value = masterPage.PageName, Required = true, RequiredErrorMessage = PageResource.NameRequiredMessage, MaxLength = PageLengths.NameMaxLength, MaxLengthErrorMessage = string.Format(PageResource.NameMaxLengthMessage, "name", PageLengths.NameMaxLength) }); form.Fields.Add("description", new MultiLineTextField { Name = "description", Label = PageResource.DescriptionLabel, Rows = 4 }); if (parentPages.Count > 1) { form.Fields.Add("parent", new SelectListField <string> { Name = "parent", Label = PageResource.ParentLabel, Required = true, RequiredErrorMessage = PageResource.ParentRequiredMessage, Items = new List <ListFieldItem <string> > { new ListFieldItem <string> { Name = PageResource.SelectParentLabel, Value = null } } }); foreach (Page page in parentPages) { ((SelectListField <string>)form.Fields["parent"]).Items.Add(new ListFieldItem <string> { Name = page.Name, Value = page.PageId.ToString() }); } } if (masterPage.Taggable) { form.Fields.Add("tags", new MultiLineTextField { Name = "tags", Label = PageResource.TagsLabel, Rows = 4 }); } if (masterPage.HasImage) { form.Fields.Add("upload", new UploadField { Name = "upload", Label = PageResource.UploadLabel }); } form.SubmitLabel = string.Format(PageResource.CreatePageButtonLabel, masterPage.Name.ToLower()); // Return result return(form); }