/// <summary> /// Gets the content channel item using the first page parameter or ContentChannelQueryParameter /// </summary> /// <returns></returns> private ContentChannelItem GetContentChannelItemFromKey(string contentChannelItemKey) { Guid?contentChannelGuid = GetAttributeValue("ContentChannel").AsGuidOrNull(); ContentChannelItem contentChannelItem = null; if (string.IsNullOrEmpty(contentChannelItemKey)) { return(null); } // // Look up the ContentChannelItem from either the Id, Guid, or // Slug depending on the datatype of the ContentChannelQueryParameter value // int? contentChannelItemId = contentChannelItemKey.AsIntegerOrNull(); Guid?contentChannelItemGuid = contentChannelItemKey.AsGuidOrNull(); var rockContext = new RockContext(); if (contentChannelItemId.HasValue) { contentChannelItem = new ContentChannelItemService(rockContext).Get(contentChannelItemId.Value); } else if (contentChannelItemGuid.HasValue) { contentChannelItem = new ContentChannelItemService(rockContext).Get(contentChannelItemGuid.Value); } else { var contentChannelQuery = new ContentChannelItemService(rockContext).Queryable(); if (contentChannelGuid.HasValue) { contentChannelQuery = contentChannelQuery.Where(c => c.ContentChannel.Guid == contentChannelGuid); } contentChannelItem = contentChannelQuery .Where(a => a.ContentChannelItemSlugs.Any(s => s.Slug == contentChannelItemKey)) .FirstOrDefault(); } return(contentChannelItem); }
/// <summary> /// Gets the content channel item using the first page parameter or ContentChannelQueryParameter /// </summary> /// <returns></returns> private ContentChannelItem GetContentChannelItem(string contentChannelItemKey) { int? itemCacheDuration = GetAttributeValue("ItemCacheDuration").AsIntegerOrNull(); Guid?contentChannelGuid = GetAttributeValue("ContentChannel").AsGuidOrNull(); ContentChannelItem contentChannelItem = null; if (string.IsNullOrEmpty(contentChannelItemKey)) { // nothing specified, so don't show anything return(null); } string itemCacheKey = ITEM_CACHE_KEY_PREFIX + contentChannelGuid + "_" + contentChannelItemKey; if (itemCacheDuration.HasValue && itemCacheDuration.Value > 0) { contentChannelItem = GetCacheItem(itemCacheKey) as ContentChannelItem; if (contentChannelItem != null) { return(contentChannelItem); } } // look up the ContentChannelItem from either the Id, Guid, or Slug depending on the datatype of the ContentChannelQueryParameter value int? contentChannelItemId = contentChannelItemKey.AsIntegerOrNull(); Guid?contentChannelItemGuid = contentChannelItemKey.AsGuidOrNull(); var rockContext = new RockContext(); if (contentChannelItemId.HasValue) { contentChannelItem = new ContentChannelItemService(rockContext).Get(contentChannelItemId.Value); } else if (contentChannelItemGuid.HasValue) { contentChannelItem = new ContentChannelItemService(rockContext).Get(contentChannelItemGuid.Value); } else { var contentChannelQuery = new ContentChannelItemService(rockContext).Queryable(); if (contentChannelGuid.HasValue) { contentChannelQuery = contentChannelQuery.Where(c => c.ContentChannel.Guid == contentChannelGuid); } contentChannelItem = contentChannelQuery .Where(a => a.ContentChannelItemSlugs.Any(s => s.Slug == contentChannelItemKey)) .FirstOrDefault(); } if (contentChannelItem != null && itemCacheDuration.HasValue && itemCacheDuration.Value > 0) { string cacheTags = GetAttributeValue("CacheTags") ?? string.Empty; var cacheKeys = GetCacheItem(CACHEKEYS_CACHE_KEY) as HashSet <string> ?? new HashSet <string>(); cacheKeys.Add(itemCacheKey); AddCacheItem(CACHEKEYS_CACHE_KEY, cacheKeys, TimeSpan.MaxValue, cacheTags); AddCacheItem(itemCacheKey, contentChannelItem, itemCacheDuration.Value, cacheTags); } return(contentChannelItem); }
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(); }
private List <ContentChannelItem> GetItems(RockContext rockContext, ContentChannel selectedChannel, out bool isFiltered) { isFiltered = false; var items = new List <ContentChannelItem>(); var itemQry = new ContentChannelItemService(rockContext).Queryable() .Where(i => i.ContentChannelId == selectedChannel.Id); var drp = new DateRangePicker(); drp.DelimitedValues = gfFilter.GetUserPreference("Date Range"); if (drp.LowerValue.HasValue) { isFiltered = true; if (selectedChannel.ContentChannelType.DateRangeType == ContentChannelDateType.SingleDate) { itemQry = itemQry.Where(i => i.StartDateTime >= drp.LowerValue.Value); } else { itemQry = itemQry.Where(i => i.ExpireDateTime.HasValue && i.ExpireDateTime.Value >= drp.LowerValue.Value); } } if (drp.UpperValue.HasValue) { isFiltered = true; DateTime upperDate = drp.UpperValue.Value.Date.AddDays(1); itemQry = itemQry.Where(i => i.StartDateTime <= upperDate); } var status = gfFilter.GetUserPreference("Status").ConvertToEnumOrNull <ContentChannelItemStatus>(); if (status.HasValue) { isFiltered = true; itemQry = itemQry.Where(i => i.Status == status); } string title = gfFilter.GetUserPreference("Title"); if (!string.IsNullOrWhiteSpace(title)) { isFiltered = true; itemQry = itemQry.Where(i => i.Title.Contains(title)); } int?personId = gfFilter.GetUserPreference("Created By").AsIntegerOrNull(); if (personId.HasValue && personId.Value != 0) { isFiltered = true; itemQry = itemQry.Where(i => i.CreatedByPersonAlias.PersonId == personId); } // Filter query by any configured attribute filters if (AvailableAttributes != null && AvailableAttributes.Any()) { var attributeValueService = new AttributeValueService(rockContext); var parameterExpression = attributeValueService.ParameterExpression; foreach (var attribute in AvailableAttributes) { var filterControl = phAttributeFilters.FindControl("filter_" + attribute.Id.ToString()); if (filterControl == null) { continue; } var filterValues = attribute.FieldType.Field.GetFilterValues(filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter); var filterIsDefault = attribute.FieldType.Field.IsEqualToValue(filterValues, attribute.DefaultValue); var expression = attribute.FieldType.Field.AttributeFilterExpression(attribute.QualifierValues, filterValues, parameterExpression); if (expression == null) { continue; } var attributeValues = attributeValueService .Queryable() .Where(v => v.Attribute.Id == attribute.Id); var filteredAttributeValues = attributeValues.Where(parameterExpression, expression, null); isFiltered = true; if (filterIsDefault) { itemQry = itemQry.Where(w => !attributeValues.Any(v => v.EntityId == w.Id) || filteredAttributeValues.Select(v => v.EntityId).Contains(w.Id)); } else { itemQry = itemQry.Where(w => filteredAttributeValues.Select(v => v.EntityId).Contains(w.Id)); } } } foreach (var item in itemQry.ToList()) { if (item.IsAuthorized(Rock.Security.Authorization.VIEW, CurrentPerson)) { items.Add(item); } else { isFiltered = true; } } if (selectedChannel.ItemsManuallyOrdered && !isFiltered) { return(items.OrderBy(i => i.Order).ToList()); } else { return(items); } }
private List<ContentChannelItem> GetItems( RockContext rockContext, ContentChannel selectedChannel, out bool isFiltered ) { isFiltered = false; var items = new List<ContentChannelItem>(); var itemQry = new ContentChannelItemService( rockContext ).Queryable() .Where( i => i.ContentChannelId == selectedChannel.Id ); var drp = new DateRangePicker(); drp.DelimitedValues = gfFilter.GetUserPreference( "Date Range" ); if ( drp.LowerValue.HasValue ) { isFiltered = true; if ( selectedChannel.ContentChannelType.DateRangeType == ContentChannelDateType.SingleDate ) { itemQry = itemQry.Where( i => i.StartDateTime >= drp.LowerValue.Value ); } else { itemQry = itemQry.Where( i => i.ExpireDateTime.HasValue && i.ExpireDateTime.Value >= drp.LowerValue.Value ); } } if ( drp.UpperValue.HasValue ) { isFiltered = true; DateTime upperDate = drp.UpperValue.Value.Date.AddDays( 1 ); itemQry = itemQry.Where( i => i.StartDateTime <= upperDate ); } var status = gfFilter.GetUserPreference( "Status" ).ConvertToEnumOrNull<ContentChannelItemStatus>(); if ( status.HasValue ) { isFiltered = true; itemQry = itemQry.Where( i => i.Status == status ); } string title = gfFilter.GetUserPreference( "Title" ); if ( !string.IsNullOrWhiteSpace( title ) ) { isFiltered = true; itemQry = itemQry.Where( i => i.Title.Contains( title ) ); } int? personId = gfFilter.GetUserPreference( "Created By" ).AsIntegerOrNull(); if ( personId.HasValue && personId.Value != 0 ) { isFiltered = true; itemQry = itemQry.Where( i => i.CreatedByPersonAlias.PersonId == personId ); } // Filter query by any configured attribute filters if ( AvailableAttributes != null && AvailableAttributes.Any() ) { var attributeValueService = new AttributeValueService( rockContext ); var parameterExpression = attributeValueService.ParameterExpression; foreach ( var attribute in AvailableAttributes ) { var filterControl = phAttributeFilters.FindControl( "filter_" + attribute.Id.ToString() ); if ( filterControl != null ) { var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter ); var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression ); if ( expression != null ) { var attributeValues = attributeValueService .Queryable() .Where( v => v.Attribute.Id == attribute.Id ); attributeValues = attributeValues.Where( parameterExpression, expression, null ); isFiltered = true; itemQry = itemQry.Where( w => attributeValues.Select( v => v.EntityId ).Contains( w.Id ) ); } } } } foreach ( var item in itemQry.ToList() ) { if ( item.IsAuthorized( Rock.Security.Authorization.VIEW, CurrentPerson ) ) { items.Add( item ); } else { isFiltered = true; } } if ( selectedChannel.ItemsManuallyOrdered && !isFiltered ) { return items.OrderBy( i => i.Order ).ToList(); } else { return items; } }