public virtual IEnumerable <IContentBlock> GetContentBlocks(string zoneName) { var workContext = EngineContext.Current.Resolve <IWebWorkContext>(); Guid?pageId = workContext.GetState <Guid?>("CurrentPageId"); var contentBlocks = contentBlockService.GetContentBlocks(zoneName, workContext.CurrentCultureCode, pageId: pageId); return(contentBlocks.Where(x => IsVisible(x)).ToList()); }
public async Task <ActionResult> Index(string slug) { // Hack to make it search the correct path for the view if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area")) { this.ControllerContext.RouteData.DataTokens.Add("area", CmsConstants.Areas.Pages); } int tenantId = WorkContext.CurrentTenant.Id; var currentCulture = WorkContext.CurrentCultureCode; //TODO: To support localized routes, we should probably first try get a single record by slug, // then if there's only 1, fine.. return it.. if more than one.. then add cultureCode as // we currently do... // First try get the latest published version for the current culture PageVersion pageVersion; using (var connection = pageVersionService.OpenConnection()) { pageVersion = await connection.Query() .Include(x => x.Page) .Where(x => x.TenantId == tenantId && x.Status == VersionStatus.Published && x.CultureCode == currentCulture && x.Slug == slug) .OrderByDescending(x => x.DateModifiedUtc) .FirstOrDefaultAsync(); } // If there isn't one... if (pageVersion == null) { // ...then try get the last archived one for the current culture // NOTE: there's no need to worry about the last one being a draft before being archived, because // we ONLY archive the published ones, not drafts.. so getting the last archived one will be the last published one using (var connection = pageVersionService.OpenConnection()) { pageVersion = await connection.Query() .Include(x => x.Page) .Where(x => x.TenantId == tenantId && x.Status == VersionStatus.Archived && x.CultureCode == currentCulture && x.Slug == slug) .OrderByDescending(x => x.DateModifiedUtc) .FirstOrDefaultAsync(); } } // If there isn't one... if (pageVersion == null) { // ...then try get the latest published version for the invariant culture using (var connection = pageVersionService.OpenConnection()) { pageVersion = await connection.Query() .Include(x => x.Page) .Where(x => x.TenantId == tenantId && x.Status == VersionStatus.Published && x.CultureCode == null && x.Slug == slug) .OrderByDescending(x => x.DateModifiedUtc) .FirstOrDefaultAsync(); } } // If there isn't one... if (pageVersion == null) { // ...then try get the last archived one for the invariant culture (TODO: What if last archived was a draft??) using (var connection = pageVersionService.OpenConnection()) { pageVersion = await connection.Query() .Include(x => x.Page) .Where(x => x.TenantId == tenantId && x.Status == VersionStatus.Archived && x.CultureCode == null && x.Slug == slug) .OrderByDescending(x => x.DateModifiedUtc) .FirstOrDefaultAsync(); } } if (pageVersion != null && pageVersion.Page.IsEnabled) { // If there are access restrictions if (!await PageSecurityHelper.CheckUserHasAccessToPage(pageVersion.Page, User)) { return(new HttpUnauthorizedResult()); } // Else no restrictions (available for anyone to view) WorkContext.SetState("CurrentPageId", pageVersion.PageId); WorkContext.Breadcrumbs.Add(pageVersion.Title); var pageType = await pageTypeService.FindOneAsync(pageVersion.Page.PageTypeId); var korePageType = pageTypeService.GetKorePageType(pageType.Name); korePageType.InstanceName = pageVersion.Title; korePageType.InstanceParentId = pageVersion.Page.ParentId; korePageType.LayoutPath = string.IsNullOrWhiteSpace(pageType.LayoutPath) ? KoreWebConstants.DefaultFrontendLayoutPath : pageType.LayoutPath; korePageType.InitializeInstance(pageVersion); var contentBlocks = contentBlockService.GetContentBlocks(pageVersion.PageId, WorkContext.CurrentCultureCode); korePageType.ReplaceContentTokens(x => InsertContentBlocks(x, contentBlocks.Where(y => IsVisible(y)))); return(View(korePageType.DisplayTemplatePath, korePageType)); } return(HttpNotFound()); }