public PageTemplatesDto GetPageTemplates(string templatedPageTitle)
        {
            var pageTemplateDto = new PageTemplatesDto();

            var primaryTeamMembership = new TeamMembership {TeamId = 1};
                //WebSiteModuleManager.EnsureCurrentUser().TeamMemberships.FirstOrDefault(t => t.PrimaryTeamMembership);

            if (primaryTeamMembership == null)
                return SetDefaultTemplateValues();


            var templateList = new List<PageTemplateDto>();
            using (ConsoleRepository)
            {
                var page = ConsoleRepository.Query<TemplatedPage>().FirstOrDefault(tp => tp.PageTitle == templatedPageTitle);
                
                var pageTemplates = ConsoleRepository.Query<PageTemplate>(pt => pt.Template, pt => pt.TemplatedPage, pt => pt.Team)
                    .Where(pt => pt.TemplatedPageId == page.Id && pt.TeamId == primaryTeamMembership.TeamId).ToList();

                if (pageTemplates == null || pageTemplates.Count == 0)
                    throw new Exception("Page Templates not found");
                
                var pageStructureTemplate = pageTemplates.FirstOrDefault(pt => pt.Template.IsPageStructureTemplate);
                pageTemplateDto.PageStructureTemplateUrl = pageStructureTemplate.Template.Url;

                pageTemplates.Remove(pageStructureTemplate);
                pageTemplates.ForEach(p => templateList.Add(new PageTemplateDto
                {
                    TemplateUrl = p.Template.Url,
                    TemplateAfterRenderDomFunction = p.Template.AfterRenderDomFunction,
                    IsVisible = p.IsVisible,
                    IsReadOnly = p.IsReadOnly,
                    PageSectionId = p.PageSectionId
                }));

                pageTemplateDto.SectionTemplates = templateList;
            }

            return pageTemplateDto;
        }
        private void SetTemplateSection(PageTemplatesDto pageTemplatesDto, int index, List<PageTemplate> pageSectionTemplates)
        {
            var templateTitle = pageTemplatesDto.SectionTemplates[index].TemplateTitle;
            var newPageSectionTemplate =
            ConsoleRepository.Query<Template>()
                             .FirstOrDefault(t => t.Title.ToUpper() == templateTitle.Trim().ToUpper() && t.IsPageStructureTemplate == false);

            if (newPageSectionTemplate != null)
            {
                ConsoleRepository.Attach<PageTemplate>(pageSectionTemplates[index]);
                pageSectionTemplates[index].Template = newPageSectionTemplate;
                pageSectionTemplates[index].IsVisible = pageTemplatesDto.SectionTemplates[index].IsVisible;
                pageSectionTemplates[index].PageSectionId = pageTemplatesDto.SectionTemplates[index].PageSectionId;
            }
        }
        public PageTemplatesDto GetAllTemplatesForPageByTeamAndPageId(int teamId, int pageId)
        {
            var pageTemplateDto = new PageTemplatesDto();

            var templateList = new List<PageTemplateDto>();
            using (ConsoleRepository)
            {
                var pageTemplates = ConsoleRepository.Query<PageTemplate>(pt => pt.Template, pt => pt.TemplatedPage, pt => pt.Team)
                    .Where(pt => pt.TemplatedPageId == pageId && pt.TeamId == teamId).ToList();

                var pageStructureTemplate = pageTemplates.FirstOrDefault(pt => pt.Template.IsPageStructureTemplate);

                if (pageStructureTemplate != null)
                {
                    pageTemplateDto.PageStructureTemplateId = pageStructureTemplate.Template.Id;
                    pageTemplateDto.PageStructureTemplateTitle = pageStructureTemplate.Template.Title;
                    pageTemplateDto.PageStructureTemplateUrl = pageStructureTemplate.Template.Url;

                    pageTemplates.Remove(pageStructureTemplate);
                    pageTemplates.ForEach(p => templateList.Add(new PageTemplateDto
                    {
                        TemplateTitle = p.Template.Title,
                        TemplateUrl = p.Template.Url,
                        TemplateAfterRenderDomFunction = p.Template.AfterRenderDomFunction,
                        IsVisible = p.IsVisible,
                        IsReadOnly = p.IsReadOnly,
                        PageSectionId = p.PageSectionId
                    }));

                    pageTemplateDto.SectionTemplates = templateList;
                }
                else
                {
                    return pageTemplateDto;
                }
            }

            return pageTemplateDto;
        }
        public void UpdatePageTemplates(PageTemplatesDto pageTemplatesDto)
        {
            using (ConsoleRepository)
            {

                var pageStructureTemplate = ConsoleRepository.Query<PageTemplate>(pt => pt.Template, pt => pt.TemplatedPage, pt => pt.Team)
                    .FirstOrDefault(pt => pt.TemplatedPageId == pageTemplatesDto.PageId && pt.TeamId == pageTemplatesDto.TeamId && pt.Template.IsPageStructureTemplate);

                if (pageStructureTemplate != null)
                {
                    var newPageStructureTemplate =
                        ConsoleRepository.Query<Template>()
                                         .FirstOrDefault(t => t.Id == pageTemplatesDto.PageStructureTemplateId && t.IsPageStructureTemplate);

                    // Update Page Structure Template
                    if (newPageStructureTemplate != null)
                    {
                        ConsoleRepository.Attach<PageTemplate>(pageStructureTemplate);
                        pageStructureTemplate.Template = newPageStructureTemplate;
                    }
                    else
                    {
                        throw new Exception("UpdatePageTemplates - new pageStructureTemplate not found");
                    }

                    // Update Page Section Templates
                    var pageSectionTemplates = ConsoleRepository.Query<PageTemplate>(pt => pt.Template, pt => pt.TemplatedPage, pt => pt.Team)
                    .Where(pt => pt.TemplatedPageId == pageTemplatesDto.PageId && pt.TeamId == pageTemplatesDto.TeamId && pt.Template.IsPageStructureTemplate == false).ToList();

                    if (pageSectionTemplates.Count > 0)
                    {
                        // TODO: turn this into a foreach...
                        for (var index = 0; index < 8; index++) // Up to 8 template sections...
                        {
                            SetTemplateSection(pageTemplatesDto, index, pageSectionTemplates);
                        }
                    }
                    else
                    {
                        throw new Exception("UpdatePageTemplates - no SectionTemplates found");
                    }
                }
                else
                {
                    throw new Exception("UpdatePageTemplates - pageStructureTemplate not found");
                }

                ConsoleRepository.SaveChanges();
            }
        }
        public ActionResult UpdatePageTemplates(PageTemplatesDto pageTemplatesDto)
        {
            TemplatesModule.UpdatePageTemplates(pageTemplatesDto);

            return new JsonNetResult
            {
                Data = Json(new { Result = "Successfully updated Page Templates" })
            };

        }