private void ShowView() { var rockContext = new RockContext(); var missingConfiguration = new List <string>(); var contentChannelGuid = GetAttributeValue("ContentChannel").AsGuidOrNull(); if (!contentChannelGuid.HasValue) { missingConfiguration.Add("The content channel has not yet been configured."); } var blockTitleTemplate = GetAttributeValue("BlockTitleTemplate"); if (blockTitleTemplate.IsNullOrWhiteSpace()) { missingConfiguration.Add("The block title template appears to be blank."); } var bodyTemplate = GetAttributeValue("BodyTemplate"); if (bodyTemplate.IsNullOrWhiteSpace()) { missingConfiguration.Add("The body template appears to be blank."); } if (missingConfiguration.Count > 0) { StringBuilder message = new StringBuilder(); message.Append("Currently, there are some missing configuration items. These items are summarized below: <ul>"); foreach (var configurationItem in missingConfiguration) { message.Append(string.Format("<li>{0}</li>", configurationItem)); } message.Append("</ul>"); nbMessages.Text = message.ToString(); nbMessages.NotificationBoxType = NotificationBoxType.Validation; return; } var enabledLavaCommands = GetAttributeValue("EnabledLavaCommands"); var blockTitleIconCssClass = GetAttributeValue("BlockTitleIconCssClass"); var metricValueCount = GetAttributeValue("MetricValueCount").AsInteger(); var cacheDuration = GetAttributeValue("CacheDuration").AsInteger(); string cacheTags = GetAttributeValue("CacheTags") ?? string.Empty; var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(RockPage, CurrentPerson); var cacheKey = "internal-commmunication-view-" + this.BlockId.ToString(); ContentChannelItem contentChannelItem = null; List <MetricResult> metrics = null; var showPrev = false; CachedBlockData cachedItem = null; if (cacheDuration > 0 && _currentPage == 0) { var serializedCachedItem = RockCache.Get(cacheKey, true); if (serializedCachedItem != null && serializedCachedItem is string && !string.IsNullOrWhiteSpace(( string )serializedCachedItem)) { cachedItem = (( string )serializedCachedItem).FromJsonOrNull <CachedBlockData>(); } } if (cachedItem != null) { contentChannelItem = cachedItem.ContentChannelItem; metrics = cachedItem.Metrics; showPrev = cachedItem.ShowPrev; } else { var channel = ContentChannelCache.Get(contentChannelGuid.Value); // Get latest content channel items, get two so we know if a previous one exists for paging var contentChannelItemsQry = new ContentChannelItemService(rockContext) .Queryable() .AsNoTracking() .Where(i => i.ContentChannel.Guid == contentChannelGuid && i.Status == ContentChannelItemStatus.Approved && i.StartDateTime <= RockDateTime.Now); if (channel.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange) { if (channel.ContentChannelType.IncludeTime) { contentChannelItemsQry = contentChannelItemsQry.Where(c => !c.ExpireDateTime.HasValue || c.ExpireDateTime >= RockDateTime.Now); } else { contentChannelItemsQry = contentChannelItemsQry.Where(c => !c.ExpireDateTime.HasValue || c.ExpireDateTime > RockDateTime.Today); } } var contentChannelItems = contentChannelItemsQry.OrderByDescending(i => i.StartDateTime) .Take(2) .Skip(_currentPage) .ToList(); if (contentChannelItems.IsNull() || contentChannelItems.Count == 0) { nbMessages.Text = "It appears that there are no active communications to display for this content channel."; nbMessages.NotificationBoxType = NotificationBoxType.Info; return; } contentChannelItem = contentChannelItems.First(); showPrev = (contentChannelItems.Count > 1); // Get metrics var metricCategories = Rock.Attribute.MetricCategoriesFieldAttribute.GetValueAsGuidPairs(GetAttributeValue("Metrics")); var metricGuids = metricCategories.Select(a => a.MetricGuid).ToList(); metrics = new MetricService(rockContext).GetByGuids(metricGuids) .Select(m => new MetricResult { Id = m.Id, Title = m.Title, Description = m.Description, IconCssClass = m.IconCssClass, UnitsLabel = m.YAxisLabel, LastRunDateTime = m.MetricValues.OrderByDescending(v => v.MetricValueDateTime).Select(v => v.MetricValueDateTime).FirstOrDefault(), LastValue = m.MetricValues.OrderByDescending(v => v.MetricValueDateTime).Select(v => v.YValue).FirstOrDefault() }).ToList(); // Get metric values for each metric if requested if (metricValueCount > 0) { foreach (var metric in metrics) { metric.MetricValues = new MetricValueService(rockContext).Queryable() .Where(v => v.MetricId == metric.Id) .OrderByDescending(v => v.MetricValueDateTime) .Select(v => new MetricValue { DateTime = v.MetricValueDateTime, Value = v.YValue, Note = v.Note }) .Take(metricValueCount) .ToList(); } } // Set Cache if (cacheDuration > 0 && _currentPage == 0) { var cachedData = new CachedBlockData(); cachedData.ContentChannelItem = contentChannelItem.Clone(false); cachedData.ShowPrev = showPrev; cachedData.Metrics = metrics; var expiration = RockDateTime.Now.AddSeconds(cacheDuration); RockCache.AddOrUpdate(cacheKey, string.Empty, cachedData.ToJson(), expiration, cacheTags); } } mergeFields.Add("Item", contentChannelItem); mergeFields.Add("Metrics", metrics); lBlockTitleIcon.Text = string.Format("<i class='{0}'></i>", blockTitleIconCssClass); lBlockTitle.Text = blockTitleTemplate.ResolveMergeFields(mergeFields, enabledLavaCommands); lBlockBody.Text = bodyTemplate.ResolveMergeFields(mergeFields, enabledLavaCommands); // Determine if we can page backwards btnPrevious.Visible = showPrev; // Determine if we can page forwards btnNext.Visible = (_currentPage > 0); // Set the current page hidden field hfCurrentPage.Value = _currentPage.ToString(); }