public async Task <IActionResult> ReadMasterPageZoneAsync(long masterPageId, long masterPageZoneId)
        {
            MasterPageZone masterPageZone = await _masterPageService.ReadMasterPageZoneAsync(TenantId, masterPageId, masterPageZoneId);

            if (masterPageZone == null)
            {
                return(NotFound());
            }
            return(Ok(masterPageZone));
        }
Example #2
0
        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);
        }
        public async Task <MasterPageZone> ReadMasterPageZoneAsync(long tenantId, long masterPageId, long masterPageZoneId)
        {
            using (SqlConnection connection = new SqlConnection(_options.Value.SqlConnectionString))
            {
                connection.Open();

                using (GridReader gr = await connection.QueryMultipleAsync(
                           @"SELECT TenantId, MasterPageId, MasterPageZoneId, SortOrder, AdminType, ContentType, BeginRender, EndRender, Name
	                    FROM cms.MasterPageZone WHERE TenantId = @TenantId AND MasterPageId = @MasterPageId AND MasterPageZoneId = @MasterPageZoneId
                      SELECT ElementTypeId FROM cms.MasterPageZoneElementType
                        WHERE TenantId = @TenantId AND MasterPageId = @MasterPageId AND MasterPageZoneId = @MasterPageZoneId",
                           new { TenantId = tenantId, MasterPageId = masterPageId, MasterPageZoneId = masterPageZoneId }
                           )
                       )
                {
                    MasterPageZone masterPageZone = await gr.ReadFirstOrDefaultAsync <MasterPageZone>();

                    masterPageZone.ElementTypeIds = await gr.ReadAsync <Guid>();

                    return(masterPageZone);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Gets master page from a template page.
        /// </summary>
        /// <param name="tenantId">Website identifier.</param>
        /// <param name="page">The page that is about to be created. Contains hierarchy of pages created so far.</param>
        /// <param name="templatePage">Template page.</param>
        /// <param name="templateElements">Maintains dictionary of template elements and their corresponding master element copies.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        private MasterPage GetMasterPageFromTemplatePage(long tenantId, Page page, TemplatePage templatePage, Dictionary <ElementKeyValue, ElementKeyValue> templateElements, IUnitOfWork unitOfWork)
        {
            // Get ancestor page ID?
            long?ancestorPageId = null;

            if (templatePage.AncestorPageLevel != null)
            {
                PageLevel ancestorPageLevel = templatePage.AncestorPageLevel.Value;
                int       numericPageLevel  = (int)ancestorPageLevel;
                while (numericPageLevel > 0)
                {
                    ancestorPageId = page.ParentPageId;
                    page           = page.ParentPage;
                    numericPageLevel--;
                }
            }

            // Create master page from template page
            MasterPage masterPage = new MasterPage
            {
                Administration           = templatePage.Administration,
                AncestorPageId           = ancestorPageId,
                AncestorPageLevel        = templatePage.AncestorPageLevel,
                Creatable                = templatePage.Creatable,
                Deletable                = templatePage.Deletable,
                HasOccurred              = templatePage.HasOccurred,
                HasImage                 = templatePage.HasImage,
                ThumbnailImageWidth      = templatePage.ThumbnailImageWidth,
                ThumbnailImageHeight     = templatePage.ThumbnailImageHeight,
                ThumbnailImageResizeMode = templatePage.ThumbnailImageResizeMode,
                PreviewImageWidth        = templatePage.PreviewImageWidth,
                PreviewImageHeight       = templatePage.PreviewImageHeight,
                PreviewImageResizeMode   = templatePage.PreviewImageResizeMode,
                ImageMinWidth            = templatePage.ImageMinWidth,
                ImageMinHeight           = templatePage.ImageMinHeight,
                Name            = templatePage.Name,
                PageName        = templatePage.PageName,
                PageDescription = templatePage.PageDescription,
                PageType        = templatePage.PageType,
                Taggable        = templatePage.Taggable,
                TenantId        = tenantId,
                BeginRender     = templatePage.BeginRender,
                EndRender       = templatePage.EndRender,
                MasterPageZones = new List <MasterPageZone>()
            };

            // Create master page zones from template page zones
            foreach (TemplatePageZone templatePageZone in templatePage.TemplatePageZones)
            {
                // Create master page zone
                MasterPageZone masterPageZone = new MasterPageZone
                {
                    Name        = templatePageZone.Name,
                    AdminType   = templatePageZone.AdminType,
                    ContentType = templatePageZone.ContentType,
                    TenantId    = tenantId,
                    BeginRender = templatePageZone.BeginRender,
                    EndRender   = templatePageZone.EndRender,
                    SortOrder   = templatePageZone.SortOrder,
                    MasterPageZoneElementTypes = new List <MasterPageZoneElementType>(),
                    MasterPageZoneElements     = new List <MasterPageZoneElement>()
                };
                masterPage.MasterPageZones.Add(masterPageZone);

                // Create master page zone element types from template page zone element types
                for (int index = 0; index < templatePageZone.TemplatePageZoneElementTypes.Count; index++)
                {
                    TemplatePageZoneElementType templatePageZoneElementType = templatePageZone.TemplatePageZoneElementTypes[index];
                    masterPageZone.MasterPageZoneElementTypes.Add(new MasterPageZoneElementType
                    {
                        TenantId      = tenantId,
                        ElementTypeId = templatePageZoneElementType.ElementTypeId
                    });
                }

                // Create master page zone elements from template page zone elements
                for (int index = 0; index < templatePageZone.TemplatePageZoneElements.Count; index++)
                {
                    // Template elements are copied to create master elements
                    TemplatePageZoneElement templatePageZoneElement = templatePageZone.TemplatePageZoneElements[index];
                    ElementKeyValue         templateElementKeyValue = new ElementKeyValue
                    {
                        ElementTypeId = templatePageZoneElement.ElementTypeId,
                        ElementId     = templatePageZoneElement.ElementId
                    };

                    // Where template elements are re-used, so master element copies should be re-used
                    if (!templateElements.ContainsKey(templateElementKeyValue))
                    {
                        long masterElementId = _elementService.Copy(templatePage.TenantId, templateElementKeyValue.ElementId, tenantId, templateElementKeyValue.ElementTypeId, unitOfWork);
                        templateElements.Add(templateElementKeyValue, new ElementKeyValue {
                            ElementTypeId = templateElementKeyValue.ElementTypeId, ElementId = masterElementId
                        });
                    }

                    // Get master element key value
                    ElementKeyValue masterElementKeyValue = templateElements[templateElementKeyValue];
                    masterPageZone.MasterPageZoneElements.Add(new MasterPageZoneElement
                    {
                        ElementId = masterElementKeyValue.ElementId,
                        Element   = new ElementSettings {
                            TenantId = tenantId, ElementId = masterElementKeyValue.ElementId, ElementTypeId = masterElementKeyValue.ElementTypeId
                        },
                        SortOrder   = index,
                        TenantId    = tenantId,
                        Parent      = masterPageZone,
                        BeginRender = templatePageZoneElement.BeginRender,
                        EndRender   = templatePageZoneElement.EndRender
                    });
                }
            }

            // Return the result
            return(masterPage);
        }
Example #5
0
        /// <summary>
        /// Validates update of page zone.
        /// </summary>
        /// <param name="masterPage">Master page associated with page whose zone is being updated.</param>
        /// <param name="page">Page whose zone is being updated.</param>
        /// <param name="pageZoneId">Identifier of page zone being updated.</param>
        /// <param name="pageZoneElements">Determines the content in a page zone.</param>
        /// <param name="keyPrefix">Validation key prefix.</param>
        public void ValidateUpdateZone(MasterPage masterPage, Page page, long pageZoneId, List <PageZoneElementInfo> pageZoneElements, string keyPrefix = null)
        {
            // Check that at least one item of content is specified
            if (pageZoneElements == null || pageZoneElements.Count == 0)
            {
                throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneElementsRequiredMessage));
            }

            // Check page zone exists
            PageZone pageZone = page.PageZones.Where(z => z.PageZoneId == pageZoneId).FirstOrDefault();

            if (pageZone == null)
            {
                throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneInvalidMessage));
            }

            // Check master page zone exists
            MasterPageZone masterPageZone = masterPage.MasterPageZones.Where(z => z.MasterPageZoneId == pageZone.MasterPageZoneId).FirstOrDefault();

            if (masterPageZone == null)
            {
                throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneInvalidMessage));
            }

            // Check that master page zone is configurable
            if (masterPageZone.AdminType != MasterPageZoneAdminType.Configurable)
            {
                throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneInvalidMessage));
            }

            // Check that element types in update request match allowed element types in zone configuration
            Dictionary <Guid, MasterPageZoneElementType> elementTypesById = masterPageZone.MasterPageZoneElementTypes.GroupBy(t => t.ElementTypeId).ToDictionary(t => t.Key, t => t.First());

            foreach (PageZoneElementInfo pageZoneElementInfo in pageZoneElements)
            {
                if (!elementTypesById.ContainsKey(pageZoneElementInfo.ElementTypeId))
                {
                    throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneInvalidMessage));
                }
            }

            // Check that element names are all specified
            foreach (PageZoneElementInfo pageZoneElementInfo in pageZoneElements)
            {
                if (string.IsNullOrWhiteSpace(pageZoneElementInfo.Name))
                {
                    throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneElementNameRequiredMessage));
                }
                if (pageZoneElementInfo.Name.Trim().Length > ElementLengths.NameMaxLength)
                {
                    throw new ValidationErrorException(new ValidationError(null, string.Format(PageResource.PageZoneElementNameMaxLengthMessage, "name", ElementLengths.NameMaxLength)));
                }
                pageZoneElementInfo.Name = pageZoneElementInfo.Name.Trim();
            }

            // Check that all page zone elements specified are found in page zone
            foreach (PageZoneElementInfo pageZoneElementInfo in pageZoneElements)
            {
                if (pageZoneElementInfo.PageZoneElementId != 0)
                {
                    PageZoneElement pageZoneElement = pageZone.PageZoneElements.Where(e => e.PageZoneElementId == pageZoneElementInfo.PageZoneElementId).FirstOrDefault();
                    if (pageZoneElement == null)
                    {
                        throw new ValidationErrorException(new ValidationError(null, PageResource.PageZoneElementInvalidMessage));
                    }
                }
            }
        }