示例#1
0
        /// <summary>
        /// Gets the data to display.
        /// </summary>
        private void GetData()
        {
            var rockContext = new RockContext();
            var itemService = new ContentChannelItemService(rockContext);

            // Get all of the content channels
            var contentChannelsQry = new ContentChannelService(rockContext).Queryable("ContentChannelType").AsNoTracking();

            List <Guid> contentChannelGuidsFilter      = GetAttributeValue(AttributeKey.ContentChannelsFilter).SplitDelimitedValues().AsGuidList();
            List <Guid> contentChannelTypeGuidsInclude = GetAttributeValue(AttributeKey.ContentChannelTypesInclude).SplitDelimitedValues().AsGuidList();
            List <Guid> contentChannelTypeGuidsExclude = GetAttributeValue(AttributeKey.ContentChannelTypesExclude).SplitDelimitedValues().AsGuidList();

            if (contentChannelGuidsFilter.Any())
            {
                // if contentChannelGuidsFilter is specified, only get those content channels.
                // NOTE: This take precedence over all the other Include/Exclude settings.
                contentChannelsQry = contentChannelsQry.Where(a => contentChannelGuidsFilter.Contains(a.Guid));
            }
            else if (contentChannelTypeGuidsInclude.Any())
            {
                // if contentChannelTypeGuidsInclude is specified, only get contentChannelTypes that are in the contentChannelTypeGuidsInclude
                // NOTE: no need to factor in contentChannelTypeGuidsExclude since included would take precedence and the excluded ones would already not be included
                contentChannelsQry = contentChannelsQry.Where(a => contentChannelTypeGuidsInclude.Contains(a.ContentChannelType.Guid) || a.ContentChannelType.ShowInChannelList);
            }
            else if (contentChannelTypeGuidsExclude.Any())
            {
                contentChannelsQry = contentChannelsQry.Where(a => !contentChannelTypeGuidsExclude.Contains(a.ContentChannelType.Guid) && a.ContentChannelType.ShowInChannelList);
            }
            else
            {
                contentChannelsQry = contentChannelsQry.Where(a => a.ContentChannelType.ShowInChannelList);
            }


            if (GetAttributeValue(AttributeKey.ShowCategoryFilter).AsBoolean())
            {
                int?categoryId          = null;
                var categoryGuid        = PageParameter("CategoryGuid").AsGuidOrNull();
                var selectedChannelGuid = PageParameter("ContentChannelGuid").AsGuidOrNull();

                if (selectedChannelGuid.HasValue)
                {
                    categoryId = CategoryCache.Get(categoryGuid.GetValueOrDefault())?.Id;
                }
                else
                {
                    categoryId = ddlCategory.SelectedValueAsId();
                }

                SetUserPreference(CATEGORY_FILTER_SETTING, categoryId.ToString());
                ddlCategory.SetValue(categoryId);

                var parentCategoryGuid = GetAttributeValue(AttributeKey.ParentCategory).AsGuidOrNull();
                if (ddlCategory.Visible && categoryId.HasValue)
                {
                    contentChannelsQry = contentChannelsQry.Where(a => a.Categories.Any(c => c.Id == categoryId));
                }
                else if (parentCategoryGuid.HasValue)
                {
                    var parentCategoryId = CategoryCache.GetId(parentCategoryGuid.Value);
                    contentChannelsQry = contentChannelsQry.Where(a => a.Categories.Any(c => c.ParentCategoryId == parentCategoryId));
                }
            }

            var contentChannelsList = contentChannelsQry.OrderBy(w => w.Name).ToList();

            // Create variable for storing authorized channels and the count of active items
            var channelCounts = new Dictionary <int, int>();

            foreach (var channel in contentChannelsList)
            {
                if (channel.IsAuthorized(Authorization.VIEW, CurrentPerson))
                {
                    channelCounts.Add(channel.Id, 0);
                }
            }

            // Get the pending approval item counts for each channel (if the channel requires approval)
            itemService.Queryable()
            .Where(i =>
                   channelCounts.Keys.Contains(i.ContentChannelId) &&
                   i.Status == ContentChannelItemStatus.PendingApproval && i.ContentChannel.RequiresApproval)
            .GroupBy(i => i.ContentChannelId)
            .Select(i => new
            {
                Id    = i.Key,
                Count = i.Count()
            })
            .ToList()
            .ForEach(i => channelCounts[i.Id] = i.Count);

            // Create a query to return channel, the count of items, and the selected class
            var qry = contentChannelsList
                      .Where(c => channelCounts.Keys.Contains(c.Id))
                      .Select(c => new
            {
                Channel = c,
                Count   = channelCounts[c.Id],
                Class   = (SelectedChannelId.HasValue && SelectedChannelId.Value == c.Id) ? "active" : ""
            });

            // If displaying active only, update query to exclude those content channels without any items
            if (tglStatus.Checked)
            {
                qry = qry.Where(c => c.Count > 0);
            }

            var contentChannels = qry.ToList();

            rptChannels.DataSource = contentChannels;
            rptChannels.DataBind();

            ContentChannel selectedChannel = null;

            if (SelectedChannelId.HasValue)
            {
                selectedChannel = contentChannelsList
                                  .Where(w =>
                                         w.Id == SelectedChannelId.Value &&
                                         channelCounts.Keys.Contains(SelectedChannelId.Value))
                                  .FirstOrDefault();
            }

            if (selectedChannel != null && contentChannels.Count > 0)
            {
                // show the content item panel
                divItemPanel.Visible = true;

                BindAttributes(selectedChannel);
                AddDynamicControls(selectedChannel);

                bool isFiltered = false;
                var  items      = GetItems(rockContext, selectedChannel, out isFiltered);

                var reorderFieldColumn = gContentChannelItems.ColumnsOfType <ReorderField>().FirstOrDefault();

                if (selectedChannel.ItemsManuallyOrdered && !isFiltered)
                {
                    if (reorderFieldColumn != null)
                    {
                        reorderFieldColumn.Visible = true;
                    }

                    gContentChannelItems.AllowSorting = false;
                }
                else
                {
                    if (reorderFieldColumn != null)
                    {
                        reorderFieldColumn.Visible = false;
                    }

                    gContentChannelItems.AllowSorting = true;

                    SortProperty sortProperty = gContentChannelItems.SortProperty;
                    if (sortProperty != null)
                    {
                        items = items.AsQueryable().Sort(sortProperty).ToList();
                    }
                    else
                    {
                        items = items.OrderByDescending(p => p.StartDateTime).ToList();
                    }
                }

                // Find any possible tags for the items
                var itemTags = new Dictionary <Guid, string>();
                if (selectedChannel.IsTaggingEnabled)
                {
                    itemTags = items.ToDictionary(i => i.Guid, v => "");
                    var entityTypeId = EntityTypeCache.Get(Rock.SystemGuid.EntityType.CONTENT_CHANNEL_ITEM.AsGuid()).Id;
                    var testedTags   = new Dictionary <int, string>();

                    foreach (var taggedItem in new TaggedItemService(rockContext)
                             .Queryable().AsNoTracking()
                             .Where(i =>
                                    i.EntityTypeId == entityTypeId &&
                                    itemTags.Keys.Contains(i.EntityGuid))
                             .OrderBy(i => i.Tag.Name))
                    {
                        if (!testedTags.ContainsKey(taggedItem.TagId))
                        {
                            testedTags.Add(taggedItem.TagId, taggedItem.Tag.IsAuthorized(Authorization.VIEW, CurrentPerson) ? taggedItem.Tag.Name : string.Empty);
                        }

                        if (testedTags[taggedItem.TagId].IsNotNullOrWhiteSpace())
                        {
                            itemTags[taggedItem.EntityGuid] += string.Format("<span class='tag'>{0}</span>", testedTags[taggedItem.TagId]);
                        }
                    }
                }

                gContentChannelItems.ObjectList = new Dictionary <string, object>();
                items.ForEach(i => gContentChannelItems.ObjectList.Add(i.Id.ToString(), i));

                var gridList = items.Select(i => new
                {
                    i.Id,
                    i.Guid,
                    i.Title,
                    i.StartDateTime,
                    i.ExpireDateTime,
                    i.Priority,
                    Status              = DisplayStatus(i.Status),
                    DateStatus          = DisplayDateStatus(i.StartDateTime),
                    Tags                = itemTags.GetValueOrNull(i.Guid),
                    Occurrences         = i.EventItemOccurrences.Any(),
                    CreatedByPersonName = i.CreatedByPersonAlias != null ? String.Format("<a href={0}>{1}</a>", ResolveRockUrl(string.Format("~/Person/{0}", i.CreatedByPersonAlias.PersonId)), i.CreatedByPersonName) : String.Empty
                }).ToList();

                // only show the Event Occurrences item if any of the displayed content channel items have any occurrences (and the block setting is enabled)
                var eventOccurrencesColumn = gContentChannelItems.ColumnsWithDataField("Occurrences").FirstOrDefault();
                eventOccurrencesColumn.Visible = gridList.Any(a => a.Occurrences == true);

                gContentChannelItems.DataSource = gridList;
                gContentChannelItems.DataBind();

                lContentChannelItems.Text = selectedChannel.Name + " Items";
            }
            else
            {
                divItemPanel.Visible = false;
            }
        }
示例#2
0
        private void GetData()
        {
            var rockContext = new RockContext();
            var itemService = new ContentChannelItemService(rockContext);

            // Get all of the content channels
            var contentChannelsQry = new ContentChannelService(rockContext).Queryable("ContentChannelType");

            List <Guid> contentChannelTypeGuidsInclude = GetAttributeValue("ContentChannelTypesInclude").SplitDelimitedValues().AsGuidList();
            List <Guid> contentChannelTypeGuidsExclude = GetAttributeValue("ContentChannelTypesExclude").SplitDelimitedValues().AsGuidList();

            if (contentChannelTypeGuidsInclude.Any())
            {
                // if contentChannelTypeGuidsInclude is specified, only get contentChannelTypes that are in the contentChannelTypeGuidsInclude
                // NOTE: no need to factor in contentChannelTypeGuidsExclude since included would take precendance and the excluded ones would already not be included
                contentChannelsQry = contentChannelsQry.Where(a => contentChannelTypeGuidsInclude.Contains(a.ContentChannelType.Guid));
            }
            else if (contentChannelTypeGuidsExclude.Any())
            {
                contentChannelsQry = contentChannelsQry.Where(a => !contentChannelTypeGuidsExclude.Contains(a.ContentChannelType.Guid));
            }

            var contentChannelsList = contentChannelsQry.OrderBy(w => w.Name).ToList();

            // Create variable for storing authorized channels and the count of active items
            var channelCounts = new Dictionary <int, int>();

            foreach (var channel in contentChannelsList)
            {
                if (channel.IsAuthorized(Authorization.VIEW, CurrentPerson))
                {
                    channelCounts.Add(channel.Id, 0);
                }
            }

            // Get the pending approval item counts for each channel (if the channel requires approval)
            itemService.Queryable()
            .Where(i =>
                   channelCounts.Keys.Contains(i.ContentChannelId) &&
                   i.Status == ContentChannelItemStatus.PendingApproval && i.ContentChannel.RequiresApproval)
            .GroupBy(i => i.ContentChannelId)
            .Select(i => new {
                Id    = i.Key,
                Count = i.Count()
            })
            .ToList()
            .ForEach(i => channelCounts[i.Id] = i.Count);

            // Create a query to return channel, the count of items, and the selected class
            var qry = contentChannelsList
                      .Where(c => channelCounts.Keys.Contains(c.Id))
                      .Select(c => new
            {
                Channel = c,
                Count   = channelCounts[c.Id],
                Class   = (SelectedChannelId.HasValue && SelectedChannelId.Value == c.Id) ? "active" : ""
            });

            // If displaying active only, update query to exclude those content channels without any items
            if (tglStatus.Checked)
            {
                qry = qry.Where(c => c.Count > 0);
            }

            var contentChannels = qry.ToList();

            rptChannels.DataSource = contentChannels;
            rptChannels.DataBind();

            ContentChannel selectedChannel = null;

            if (SelectedChannelId.HasValue)
            {
                selectedChannel = contentChannelsList
                                  .Where(w =>
                                         w.Id == SelectedChannelId.Value &&
                                         channelCounts.Keys.Contains(SelectedChannelId.Value))
                                  .FirstOrDefault();
            }

            if (selectedChannel != null && contentChannels.Count > 0)
            {
                // show the content item panel
                divItemPanel.Visible = true;

                BindAttributes(selectedChannel);
                AddDynamicControls(selectedChannel);

                var itemQry = itemService.Queryable()
                              .Where(i => i.ContentChannelId == selectedChannel.Id);

                var drp = new DateRangePicker();
                drp.DelimitedValues = gfFilter.GetUserPreference("Date Range");
                if (drp.LowerValue.HasValue)
                {
                    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)
                {
                    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)
                {
                    itemQry = itemQry.Where(i => i.Status == status);
                }

                string title = gfFilter.GetUserPreference("Title");
                if (!string.IsNullOrWhiteSpace(title))
                {
                    itemQry = itemQry.Where(i => i.Title.Contains(title));
                }

                int?personId = gfFilter.GetUserPreference("Created By").AsIntegerOrNull();
                if (personId.HasValue && personId.Value != 0)
                {
                    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);

                                itemQry = itemQry.Where(w => attributeValues.Select(v => v.EntityId).Contains(w.Id));
                            }
                        }
                    }
                }

                var items = new List <ContentChannelItem>();
                foreach (var item in itemQry.ToList())
                {
                    if (item.IsAuthorized(Rock.Security.Authorization.VIEW, CurrentPerson))
                    {
                        items.Add(item);
                    }
                }

                SortProperty sortProperty = gContentChannelItems.SortProperty;
                if (sortProperty != null)
                {
                    items = items.AsQueryable().Sort(sortProperty).ToList();
                }
                else
                {
                    items = items.OrderByDescending(p => p.StartDateTime).ToList();
                }

                gContentChannelItems.ObjectList = new Dictionary <string, object>();
                items.ForEach(i => gContentChannelItems.ObjectList.Add(i.Id.ToString(), i));

                gContentChannelItems.DataSource = items.Select(i => new
                {
                    i.Id,
                    i.Guid,
                    i.Title,
                    i.StartDateTime,
                    i.ExpireDateTime,
                    i.Priority,
                    Status              = DisplayStatus(i.Status),
                    Occurrences         = i.EventItemOccurrences.Any(),
                    CreatedByPersonName = i.CreatedByPersonAlias != null ? String.Format("<a href={0}>{1}</a>", ResolveRockUrl(string.Format("~/Person/{0}", i.CreatedByPersonAlias.PersonId)), i.CreatedByPersonName) : String.Empty
                }).ToList();
                gContentChannelItems.DataBind();

                lContentChannelItems.Text = selectedChannel.Name + " Items";
            }
            else
            {
                divItemPanel.Visible = false;
            }
        }
        private void GetData()
        {
            var rockContext = new RockContext();
            var itemService = new ContentChannelItemService(rockContext);

            // Get all of the content channels
            var contentChannelsQry = new ContentChannelService(rockContext).Queryable("ContentChannelType");

            List <Guid> contentChannelTypeGuidsInclude = GetAttributeValue("ContentChannelTypesInclude").SplitDelimitedValues().AsGuidList();
            List <Guid> contentChannelTypeGuidsExclude = GetAttributeValue("ContentChannelTypesExclude").SplitDelimitedValues().AsGuidList();

            if (contentChannelTypeGuidsInclude.Any())
            {
                // if contentChannelTypeGuidsInclude is specified, only get contentChannelTypes that are in the contentChannelTypeGuidsInclude
                // NOTE: no need to factor in contentChannelTypeGuidsExclude since included would take precendance and the excluded ones would already not be included
                contentChannelsQry = contentChannelsQry.Where(a => contentChannelTypeGuidsInclude.Contains(a.ContentChannelType.Guid));
            }
            else if (contentChannelTypeGuidsExclude.Any())
            {
                contentChannelsQry = contentChannelsQry.Where(a => !contentChannelTypeGuidsExclude.Contains(a.ContentChannelType.Guid));
            }

            var contentChannelsList = contentChannelsQry.OrderBy(w => w.Name).ToList();

            // Create variable for storing authorized channels and the count of active items
            var channelCounts = new Dictionary <int, int>();

            foreach (var channel in contentChannelsList)
            {
                if (channel.IsAuthorized(Authorization.VIEW, CurrentPerson))
                {
                    channelCounts.Add(channel.Id, 0);
                }
            }

            // Get the pending approval item counts for each channel (if the channel requires approval)
            itemService.Queryable()
            .Where(i =>
                   channelCounts.Keys.Contains(i.ContentChannelId) &&
                   i.Status == ContentChannelItemStatus.PendingApproval && i.ContentChannel.RequiresApproval)
            .GroupBy(i => i.ContentChannelId)
            .Select(i => new {
                Id    = i.Key,
                Count = i.Count()
            })
            .ToList()
            .ForEach(i => channelCounts[i.Id] = i.Count);

            // Create a query to return channel, the count of items, and the selected class
            var qry = contentChannelsList
                      .Where(c => channelCounts.Keys.Contains(c.Id))
                      .Select(c => new
            {
                Channel = c,
                Count   = channelCounts[c.Id],
                Class   = (SelectedChannelId.HasValue && SelectedChannelId.Value == c.Id) ? "active" : ""
            });

            // If displaying active only, update query to exclude those content channels without any items
            if (tglStatus.Checked)
            {
                qry = qry.Where(c => c.Count > 0);
            }

            var contentChannels = qry.ToList();

            rptChannels.DataSource = contentChannels;
            rptChannels.DataBind();

            ContentChannel selectedChannel = null;

            if (SelectedChannelId.HasValue)
            {
                selectedChannel = contentChannelsList
                                  .Where(w =>
                                         w.Id == SelectedChannelId.Value &&
                                         channelCounts.Keys.Contains(SelectedChannelId.Value))
                                  .FirstOrDefault();
            }

            if (selectedChannel != null && contentChannels.Count > 0)
            {
                // show the content item panel
                divItemPanel.Visible = true;

                BindAttributes(selectedChannel);
                AddDynamicControls(selectedChannel);

                bool isFiltered = false;
                var  items      = GetItems(rockContext, selectedChannel, out isFiltered);

                if (selectedChannel.ItemsManuallyOrdered && !isFiltered)
                {
                    gContentChannelItems.Columns[0].Visible = true;
                    gContentChannelItems.AllowSorting       = false;
                }
                else
                {
                    gContentChannelItems.Columns[0].Visible = false;
                    gContentChannelItems.AllowSorting       = true;

                    SortProperty sortProperty = gContentChannelItems.SortProperty;
                    if (sortProperty != null)
                    {
                        items = items.AsQueryable().Sort(sortProperty).ToList();
                    }
                    else
                    {
                        items = items.OrderByDescending(p => p.StartDateTime).ToList();
                    }
                }

                gContentChannelItems.ObjectList = new Dictionary <string, object>();
                items.ForEach(i => gContentChannelItems.ObjectList.Add(i.Id.ToString(), i));

                gContentChannelItems.DataSource = items.Select(i => new
                {
                    i.Id,
                    i.Guid,
                    i.Title,
                    i.StartDateTime,
                    i.ExpireDateTime,
                    i.Priority,
                    Status              = DisplayStatus(i.Status),
                    Occurrences         = i.EventItemOccurrences.Any(),
                    CreatedByPersonName = i.CreatedByPersonAlias != null ? String.Format("<a href={0}>{1}</a>", ResolveRockUrl(string.Format("~/Person/{0}", i.CreatedByPersonAlias.PersonId)), i.CreatedByPersonName) : String.Empty
                }).ToList();
                gContentChannelItems.DataBind();

                lContentChannelItems.Text = selectedChannel.Name + " Items";
            }
            else
            {
                divItemPanel.Visible = false;
            }
        }