Exemplo n.º 1
0
        /// <summary>
        /// Shows the results.
        /// </summary>
        private void ShowResults()
        {
            RockContext rockContext = new RockContext();

            var qry = new EventItemOccurrenceService(rockContext).Queryable().Where(e => e.EventItem.IsActive);

            int?eventCalendarId = this.PageParameter("EventCalendarId").AsIntegerOrNull();

            if (eventCalendarId.HasValue)
            {
                qry = qry.Where(e => e.EventItem.EventCalendarItems.Any(x => x.EventCalendarId == eventCalendarId));
            }

            // filter by Campus (filter)
            if (cpCampusPicker.SelectedCampusId.HasValue)
            {
                int campusId = cpCampusPicker.SelectedCampusId.Value;

                // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                qry = qry.Where(a => a.CampusId == null || a.CampusId == campusId);
            }

            // retrieve occurrences into a List so we can do additional filtering against the Calendar data
            List <EventItemOccurrence> itemOccurrences = qry.ToList();

            // filter by date range
            var dateRange = new DateRange(pDateRange.LowerValue, pDateRange.UpperValue);

            if (dateRange.Start != null && dateRange.End != null)
            {
                itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
            }
            else
            {
                // default show all future
                itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, DateTime.Now.AddDays(365)).Count() == 0);
            }

            // sort results
            itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).ToList();

            // make lava merge fields
            var mergeFields = new Dictionary <string, object>();

            mergeFields.Add("EventDetailPage", LinkedPageRoute("EventDetailPage"));

            mergeFields.Add("EventItemOccurrences", itemOccurrences);

            if (eventCalendarId.HasValue)
            {
                mergeFields.Add("EventCalendar", new EventCalendarService(rockContext).Get(eventCalendarId.Value));
            }

            lResults.Text = GetAttributeValue("ResultsLavaTemplate").ResolveMergeFields(mergeFields);
        }
        /// <summary>
        /// Gets the occurrences that should be displayed.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <returns>
        /// An enumerable of <see cref="EventItemOccurrence" /> items to be displayed.
        /// </returns>
        private IEnumerable <EventItemOccurrence> GetOccurrences(RockContext rockContext)
        {
            if (!Audience.HasValue)
            {
                return(null);
            }

            // get event occurrences
            var qry = new EventItemOccurrenceService(rockContext).Queryable()
                      .Where(e => e.EventItem.EventItemAudiences.Any(a => a.DefinedValue.Guid == Audience.Value) && e.EventItem.IsActive);

            // filter by campus
            var campusIds = GetFilteredCampuses().Select(a => a.Id).ToList();

            if (campusIds.Any())
            {
                // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                qry = qry.Where(e => !e.CampusId.HasValue || campusIds.Contains(e.CampusId.Value));
            }

            // filter by calendar
            if (Calendar.HasValue)
            {
                qry = qry.Where(e => e.EventItem.EventCalendarItems.Any(c => c.EventCalendar.Guid == Calendar));
            }

            // retrieve occurrences
            var itemOccurrences = qry.ToList();

            // filter by date range
            var dateRange = DateRange;

            if (dateRange.Start != null && dateRange.End != null)
            {
                itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
            }
            else
            {
                // default show all future
                itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, RockDateTime.Now.AddDays(365)).Count() == 0);
            }

            // limit results
            int maxItems = GetAttributeValue("MaxOccurrences").AsInteger();

            itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).Take(maxItems).ToList();

            return(itemOccurrences);
        }
        private void LoadContent()
        {
            var eventItemGuid = GetAttributeValue("EventItem").AsGuid();

            if (eventItemGuid != Guid.Empty)
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService(rockContext).Queryable()
                          .Where(e => e.EventItem.Guid == eventItemGuid);

                // filter occurrences for campus
                if (GetAttributeValue("UseCampusContext").AsBoolean())
                {
                    var campusEntityType = EntityTypeCache.Read("Rock.Model.Campus");
                    var contextCampus    = RockPage.GetCurrentContext(campusEntityType) as Campus;

                    if (contextCampus != null)
                    {
                        qry = qry.Where(e => e.CampusId == contextCampus.Id);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(GetAttributeValue("Campuses")))
                    {
                        var selectedCampuses = Array.ConvertAll(GetAttributeValue("Campuses").Split(','), s => new Guid(s)).ToList();
                        qry = qry.Where(e => selectedCampuses.Contains(e.Campus.Guid));
                    }
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(GetAttributeValue("DateRange"));
                if (dateRange.Start != null && dateRange.End != null)
                {
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
                }
                else
                {
                    // default show all future (max 1 year)
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, RockDateTime.Now.AddDays(365)).Count() == 0);
                }

                // limit results
                int maxItems = GetAttributeValue("MaxOccurrences").AsInteger();
                itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).Take(maxItems).ToList();

                // load event item
                var eventItem = new EventItemService(rockContext).Get(eventItemGuid);

                // make lava merge fields
                var mergeFields = new Dictionary <string, object>();
                mergeFields.Add("RegistrationPage", LinkedPageRoute("RegistrationPage"));
                mergeFields.Add("EventItem", eventItem);
                mergeFields.Add("EventItemOccurrences", itemOccurrences);

                // add context to merge fields
                var contextEntityTypes = RockPage.GetContextEntityTypes();

                var contextObjects = new Dictionary <string, object>();
                foreach (var conextEntityType in contextEntityTypes)
                {
                    var contextObject = RockPage.GetCurrentContext(conextEntityType);
                    contextObjects.Add(conextEntityType.FriendlyName, contextObject);
                }

                mergeFields.Add("Context", contextObjects);

                lContent.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeFields);

                // show debug info
                if (GetAttributeValue("EnableDebug").AsBoolean() && IsUserAuthorized(Authorization.EDIT))
                {
                    lDebug.Visible = true;
                    lDebug.Text    = @"<div class='alert alert-info'>Due to the size of the lava members the debug info for this block has been supressed. Below are high-level details of
                                    the merge objects available.
                                    <ul>
                                        <li>EventItemOccurrences - A list of EventItemOccurrences. View the EvenItemOccurrence model for these properties.</li>
                                        <li>EventItem - The EventItem that was selected. View the EvenItem model for these properties.</li>
                                        <li>RegistrationPage  - String that contains the relative path to the registration page.</li>
                                        <li>Global Attribute  - Access to the Global Attributes.</li>
                                    </ul>
                                    </div>";
                }
                else
                {
                    lDebug.Visible = false;
                    lDebug.Text    = string.Empty;
                }
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No event item is configured for this block.</div>";
            }
        }
Exemplo n.º 4
0
        private void LoadContent()
        {
            var eventItemGuid = GetAttributeValue("EventItem").AsGuid();

            if (eventItemGuid != Guid.Empty)
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService(rockContext).Queryable()
                          .Where(e => e.EventItem.Guid == eventItemGuid);

                // filter occurrences for campus
                if (GetAttributeValue("UseCampusContext").AsBoolean())
                {
                    var campusEntityType = EntityTypeCache.Get("Rock.Model.Campus");
                    var contextCampus    = RockPage.GetCurrentContext(campusEntityType) as Campus;

                    if (contextCampus != null)
                    {
                        // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                        qry = qry.Where(e => e.CampusId == null || e.CampusId == contextCampus.Id);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(GetAttributeValue("Campuses")))
                    {
                        var selectedCampusGuids = GetAttributeValue("Campuses").Split(',').AsGuidList();
                        var selectedCampusIds   = selectedCampusGuids.Select(a => CampusCache.Get(a)).Where(a => a != null).Select(a => a.Id);

                        // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                        qry = qry.Where(e => e.CampusId == null || selectedCampusIds.Contains(e.CampusId.Value));
                    }
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(GetAttributeValue("DateRange"));
                if (dateRange.Start != null && dateRange.End != null)
                {
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
                }
                else
                {
                    // default show all future (max 1 year)
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, RockDateTime.Now.AddDays(365)).Count() == 0);
                }

                // limit results
                int maxItems = GetAttributeValue("MaxOccurrences").AsInteger();
                itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).Take(maxItems).ToList();

                // load event item
                var eventItem = new EventItemService(rockContext).Get(eventItemGuid);

                // make lava merge fields
                var mergeFields = new Dictionary <string, object>();
                mergeFields.Add("RegistrationPage", LinkedPageRoute("RegistrationPage"));
                mergeFields.Add("EventItem", eventItem);
                mergeFields.Add("EventItemOccurrences", itemOccurrences);

                // add context to merge fields
                var contextEntityTypes = RockPage.GetContextEntityTypes();

                var contextObjects = new Dictionary <string, object>();
                foreach (var conextEntityType in contextEntityTypes)
                {
                    var contextObject = RockPage.GetCurrentContext(conextEntityType);
                    contextObjects.Add(conextEntityType.FriendlyName, contextObject);
                }

                mergeFields.Add("Context", contextObjects);

                lContent.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeFields);
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No event item is configured for this block.</div>";
            }
        }
Exemplo n.º 5
0
        private void LoadContent()
        {
            var audienceGuid = GetAttributeValue("Audience").AsGuid();

            if (audienceGuid != Guid.Empty)
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService(rockContext).Queryable()
                          .Where(e => e.EventItem.EventItemAudiences.Any(a => a.DefinedValue.Guid == audienceGuid) && e.EventItem.IsActive);

                // filter occurrences for campus (always include the "All Campuses" events)
                if (GetAttributeValue("UseCampusContext").AsBoolean())
                {
                    var campusEntityType = EntityTypeCache.Read("Rock.Model.Campus");
                    var contextCampus    = RockPage.GetCurrentContext(campusEntityType) as Campus;

                    if (contextCampus != null)
                    {
                        qry = qry.Where(e => e.CampusId == contextCampus.Id || !e.CampusId.HasValue);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(GetAttributeValue("Campuses")))
                    {
                        var selectedCampuses = Array.ConvertAll(GetAttributeValue("Campuses").Split(','), s => new Guid(s)).ToList();
                        qry = qry.Where(e => selectedCampuses.Contains(e.Campus.Guid) || !e.CampusId.HasValue);
                    }
                }

                // filter by calendar
                var calendarGuid = GetAttributeValue("Calendar").AsGuid();

                if (calendarGuid != Guid.Empty)
                {
                    qry = qry.Where(e => e.EventItem.EventCalendarItems.Any(c => c.EventCalendar.Guid == calendarGuid));
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(GetAttributeValue("DateRange"));
                if (dateRange.Start != null && dateRange.End != null)
                {
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
                }
                else
                {
                    // default show all future
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, DateTime.Now.AddDays(365)).Count() == 0);
                }

                // limit results
                int maxItems = GetAttributeValue("MaxOccurrences").AsInteger();
                itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).Take(maxItems).ToList();

                // make lava merge fields
                var mergeFields = new Dictionary <string, object>();

                var contextObjects = new Dictionary <string, object>();
                foreach (var contextEntityType in RockPage.GetContextEntityTypes())
                {
                    var contextEntity = RockPage.GetCurrentContext(contextEntityType);
                    if (contextEntity != null && contextEntity is DotLiquid.ILiquidizable)
                    {
                        var type = Type.GetType(contextEntityType.AssemblyName ?? contextEntityType.Name);
                        if (type != null)
                        {
                            contextObjects.Add(type.Name, contextEntity);
                        }
                    }
                }

                if (contextObjects.Any())
                {
                    mergeFields.Add("Context", contextObjects);
                }

                mergeFields.Add("ListTitle", GetAttributeValue("ListTitle"));
                mergeFields.Add("EventDetailPage", LinkedPageUrl("EventDetailPage", null));
                mergeFields.Add("RegistrationPage", LinkedPageUrl("RegistrationPage", null));
                mergeFields.Add("EventItemOccurrences", itemOccurrences);

                lContent.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeFields);

                // show debug info
                if (GetAttributeValue("EnableDebug").AsBoolean() && IsUserAuthorized(Authorization.EDIT))
                {
                    lDebug.Visible = true;
                    lDebug.Text    = @"<div class='alert alert-info'>Due to the size of the lava members the debug info for this block has been supressed. Below are high-level details of
                                    the merge objects available.
                                    <ul>
                                        <li>List Title - The title to pass to lava.
                                        <li>EventItemOccurrences - A list of EventItemOccurrences. View the EvenItemOccurrence model for these properties.</li>
                                        <li>RegistrationPage  - String that contains the relative path to the registration page.</li>
                                        <li>EventDetailPage  - String that contains the relative path to the event detail page.</li>
                                        <li>Global Attribute  - Access to the Global Attributes.</li>
                                    </ul>
                                    </div>";
                }
                else
                {
                    lDebug.Visible = false;
                    lDebug.Text    = string.Empty;
                }
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No audience is configured for this block.</div>";
            }
        }
        private void LoadContent()
        {
            var eventItemGuid = GetAttributeValue( "EventItem" ).AsGuid();

            if ( eventItemGuid != Guid.Empty )
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService( rockContext ).Queryable()
                                            .Where( e => e.EventItem.Guid == eventItemGuid );

                // filter occurrences for campus
                if ( GetAttributeValue( "UseCampusContext" ).AsBoolean() )
                {
                    var campusEntityType = EntityTypeCache.Read( "Rock.Model.Campus" );
                    var contextCampus = RockPage.GetCurrentContext( campusEntityType ) as Campus;

                    if ( contextCampus != null )
                    {
                        qry = qry.Where( e => e.CampusId == contextCampus.Id );
                    }
                }
                else
                {
                    if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "Campuses" ) ) )
                    {
                        var selectedCampuses = Array.ConvertAll( GetAttributeValue( "Campuses" ).Split( ',' ), s => new Guid( s ) ).ToList();
                        qry = qry.Where( e => selectedCampuses.Contains( e.Campus.Guid ) );
                    }
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( GetAttributeValue( "DateRange" ) );
                if ( dateRange.Start != null && dateRange.End != null )
                {
                    itemOccurrences.RemoveAll( o => o.GetStartTimes( dateRange.Start.Value, dateRange.End.Value ).Count() == 0 );
                }
                else
                {
                    // default show all future (max 1 year)
                    itemOccurrences.RemoveAll( o => o.GetStartTimes( RockDateTime.Now, RockDateTime.Now.AddDays(365) ).Count() == 0 );
                }

                // limit results
                int maxItems = GetAttributeValue( "MaxOccurrences" ).AsInteger();
                itemOccurrences = itemOccurrences.OrderBy( i => i.NextStartDateTime ).Take( maxItems ).ToList();

                // load event item
                var eventItem = new EventItemService( rockContext ).Get( eventItemGuid );

                // make lava merge fields
                var mergeFields = new Dictionary<string, object>();
                mergeFields.Add( "RegistrationPage", LinkedPageUrl( "RegistrationPage", null ) );
                mergeFields.Add( "EventItem", eventItem);
                mergeFields.Add( "EventItemOccurrences", itemOccurrences );

                // add context to merge fields
                var contextEntityTypes = RockPage.GetContextEntityTypes();

                var contextObjects = new Dictionary<string, object>();
                foreach (var conextEntityType in contextEntityTypes)
                {
                    var contextObject = RockPage.GetCurrentContext(conextEntityType);
                    contextObjects.Add(conextEntityType.FriendlyName, contextObject);
                }

                mergeFields.Add("Context", contextObjects);

                lContent.Text = GetAttributeValue( "LavaTemplate" ).ResolveMergeFields( mergeFields );

                // show debug info
                if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) )
                {
                    lDebug.Visible = true;
                    lDebug.Text = @"<div class='alert alert-info'>Due to the size of the lava members the debug info for this block has been supressed. Below are high-level details of
                                    the merge objects available.
                                    <ul>
                                        <li>EventItemOccurrences - A list of EventItemOccurrences. View the EvenItemOccurrence model for these properties.</li>
                                        <li>EventItem - The EventItem that was selected. View the EvenItem model for these properties.</li>
                                        <li>RegistrationPage  - String that contains the relative path to the registration page.</li>
                                        <li>Global Attribute  - Access to the Global Attributes.</li>
                                    </ul>
                                    </div>";
                }
                else
                {
                    lDebug.Visible = false;
                    lDebug.Text = string.Empty;
                }
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No event item is configured for this block.</div>";
            }
        }
Exemplo n.º 7
0
        private void LoadContent()
        {
            var eventItemGuid = GetAttributeValue( "EventItem" ).AsGuid();

            if ( eventItemGuid != Guid.Empty )
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService( rockContext ).Queryable()
                                            .Where( e => e.EventItem.Guid == eventItemGuid );

                // filter occurrences for campus
                if ( GetAttributeValue( "UseCampusContext" ).AsBoolean() )
                {
                    var campusEntityType = EntityTypeCache.Read( "Rock.Model.Campus" );
                    var contextCampus = RockPage.GetCurrentContext( campusEntityType ) as Campus;

                    qry = qry.Where( e => e.CampusId == contextCampus.Id );
                }
                else
                {
                    if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "Campuses" ) ) )
                    {
                        var selectedCampuses = Array.ConvertAll( GetAttributeValue( "Campuses" ).Split( ',' ), s => new Guid( s ) ).ToList();
                        qry = qry.Where( e => selectedCampuses.Contains( e.Campus.Guid ) );
                    }
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( GetAttributeValue( "DateRange" ) );
                if ( dateRange.Start != null && dateRange.End != null )
                {
                    foreach ( var occurrence in itemOccurrences )
                    {
                        if ( occurrence.GetStartTimes( dateRange.Start.Value, dateRange.End.Value ).Count() == 0 )
                        {
                            itemOccurrences.Remove( occurrence );
                        }
                    }
                }

                // limit results
                int maxItems = GetAttributeValue( "MaxOccurrences" ).AsInteger();
                itemOccurrences = itemOccurrences.OrderBy( i => i.NextStartDateTime ).Take( maxItems ).ToList();

                // make lava merge fields
                var mergeFields = new Dictionary<string, object>();
                mergeFields.Add( "RegistrationPage", LinkedPageUrl( "RegistrationPage", null ) );
                mergeFields.Add( "EventItem", new EventItemService( rockContext ).Get( eventItemGuid ) );
                mergeFields.Add( "EventItemOccurrences", itemOccurrences );

                lContent.Text = GetAttributeValue( "LavaTemplate" ).ResolveMergeFields( mergeFields );

                // show debug info
                if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) )
                {
                    lDebug.Visible = true;
                    lDebug.Text = mergeFields.lavaDebugInfo();
                }
                else
                {
                    lDebug.Visible = false;
                    lDebug.Text = string.Empty;
                }
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No event item is configured for this block.</div>";
            }
        }
        private void LoadContent()
        {
            var audienceGuid = GetAttributeValue("Audience").AsGuid();

            if (audienceGuid != Guid.Empty)
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService(rockContext).Queryable()
                          .Where(e => e.EventItem.EventItemAudiences.Any(a => a.DefinedValue.Guid == audienceGuid) && e.EventItem.IsActive);

                // filter occurrences for campus (always include the "All Campuses" events)
                if (GetAttributeValue("UseCampusContext").AsBoolean())
                {
                    var campusEntityType = EntityTypeCache.Read("Rock.Model.Campus");
                    var contextCampus    = RockPage.GetCurrentContext(campusEntityType) as Campus;

                    if (contextCampus != null)
                    {
                        // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                        qry = qry.Where(e => e.CampusId == contextCampus.Id || !e.CampusId.HasValue);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(GetAttributeValue("Campuses")))
                    {
                        var selectedCampusGuids = GetAttributeValue("Campuses").Split(',').AsGuidList();
                        var selectedCampusIds   = selectedCampusGuids.Select(a => CampusCache.Read(a)).Where(a => a != null).Select(a => a.Id);

                        // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                        qry = qry.Where(e => e.CampusId == null || selectedCampusIds.Contains(e.CampusId.Value));
                    }
                }

                // filter by calendar
                var calendarGuid = GetAttributeValue("Calendar").AsGuid();

                if (calendarGuid != Guid.Empty)
                {
                    qry = qry.Where(e => e.EventItem.EventCalendarItems.Any(c => c.EventCalendar.Guid == calendarGuid));
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(GetAttributeValue("DateRange"));
                if (dateRange.Start != null && dateRange.End != null)
                {
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
                }
                else
                {
                    // default show all future
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, DateTime.Now.AddDays(365)).Count() == 0);
                }

                // limit results
                int maxItems = GetAttributeValue("MaxOccurrences").AsInteger();
                itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).Take(maxItems).ToList();

                // make lava merge fields
                var mergeFields = new Dictionary <string, object>();

                var contextObjects = new Dictionary <string, object>();
                foreach (var contextEntityType in RockPage.GetContextEntityTypes())
                {
                    var contextEntity = RockPage.GetCurrentContext(contextEntityType);
                    if (contextEntity != null && contextEntity is DotLiquid.ILiquidizable)
                    {
                        var type = Type.GetType(contextEntityType.AssemblyName ?? contextEntityType.Name);
                        if (type != null)
                        {
                            contextObjects.Add(type.Name, contextEntity);
                        }
                    }
                }

                if (contextObjects.Any())
                {
                    mergeFields.Add("Context", contextObjects);
                }

                mergeFields.Add("ListTitle", GetAttributeValue("ListTitle"));
                mergeFields.Add("EventDetailPage", LinkedPageRoute("EventDetailPage"));
                mergeFields.Add("RegistrationPage", LinkedPageRoute("RegistrationPage"));
                mergeFields.Add("EventItemOccurrences", itemOccurrences);

                lContent.Text = GetAttributeValue("LavaTemplate").ResolveMergeFields(mergeFields);
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No audience is configured for this block.</div>";
            }
        }
        /// <summary>
        /// Shows the results.
        /// </summary>
        private void ShowResults()
        {
            RockContext rockContext = new RockContext();

            var qry = new EventItemOccurrenceService(rockContext).Queryable().Where(e => e.EventItem.IsActive && e.EventItem.IsApproved);

            if (EventCalendarId.HasValue)
            {
                qry = qry.Where(e => e.EventItem.EventCalendarItems.Any(x => x.EventCalendarId == EventCalendarId.Value));
            }

            int? showCampusFilter = GetAttributeValue(AttributeKey.ShowCampusFilter).AsIntegerOrNull();
            bool showCampus       = IsCampusEnabled(showCampusFilter);

            if (showCampus)
            {
                if (cpCampusPicker.Visible && cpCampusPicker.SelectedCampusId.HasValue)
                {
                    int campusId = cpCampusPicker.SelectedCampusId.Value;

                    // If an EventItemOccurrence's CampusId is null, then the occurrence is an 'All Campuses' event occurrence, so include those
                    qry = qry.Where(a => a.CampusId == null || a.CampusId == campusId);
                }
                else
                {
                    var campusIds = cpCampusPicker
                                    .Items
                                    .Cast <ListItem>()
                                    .Select(i => i.Value)
                                    .AsIntegerList();
                    qry = qry.Where(a => a.CampusId == null || campusIds.Contains(a.CampusId.Value));
                }
            }
            else if (GetAttributeValue(AttributeKey.UseCampusContext).AsBoolean())
            {
                var campusEntityType = EntityTypeCache.Get(typeof(Campus));
                var currentCampus    = RockPage.GetCurrentContext(campusEntityType) as Campus;
                if (currentCampus != null)
                {
                    qry = qry.Where(a => a.CampusId == null || a.CampusId == currentCampus.Id);
                }
            }

            if (cblAudience.Visible)
            {
                // Filter by Category
                List <int> audiences = cblAudience.SelectedValuesAsInt;
                if (audiences.Any())
                {
                    qry = qry.Where(i => i.EventItem.EventItemAudiences.Any(c => audiences.Contains(c.DefinedValueId)));
                }
            }

            // retrieve occurrences into a List so we can do additional filtering against the Calendar data
            List <EventItemOccurrence> itemOccurrences = qry.ToList();

            if (pDateRange.Visible)
            {
                // filter by date range
                var dateRange = new DateRange(pDateRange.LowerValue, pDateRange.UpperValue);
                if (dateRange.Start != null && dateRange.End != null)
                {
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(dateRange.Start.Value, dateRange.End.Value).Count() == 0);
                }
                else
                {
                    // default show all future
                    itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, RockDateTime.Now.AddDays(365)).Count() == 0);
                }
            }
            else
            {
                // default show all future
                itemOccurrences.RemoveAll(o => o.GetStartTimes(RockDateTime.Now, RockDateTime.Now.AddDays(365)).Count() == 0);
            }

            // sort results
            itemOccurrences = itemOccurrences.OrderBy(i => i.NextStartDateTime).ToList();

            // make lava merge fields
            var mergeFields = new Dictionary <string, object>();

            mergeFields.Add("EventDetailPage", LinkedPageRoute(AttributeKey.EventDetailPage));

            mergeFields.Add("EventItemOccurrences", itemOccurrences);

            if (EventCalendarId.HasValue)
            {
                mergeFields.Add("EventCalendar", new EventCalendarService(rockContext).Get(EventCalendarId.Value));
            }

            lResults.Text = GetAttributeValue(AttributeKey.ResultsLavaTemplate).ResolveMergeFields(mergeFields);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Binds the campus grid.
        /// </summary>
        private void BindCampusGrid()
        {
            if (_eventItem != null)
            {
                pnlEventCalendarCampusItems.Visible = true;

                var rockContext = new RockContext();

                var qry = new EventItemOccurrenceService(rockContext)
                          .Queryable().AsNoTracking()
                          .Where(c => c.EventItemId == _eventItem.Id);

                // Filter by Campus
                List <int> campusIds = cblCampus.SelectedValuesAsInt;
                if (campusIds.Any())
                {
                    qry = qry
                          .Where(i =>
                                 !i.CampusId.HasValue ||
                                 campusIds.Contains(i.CampusId.Value));
                }

                SortProperty sortProperty = gCalendarItemOccurrenceList.SortProperty;

                // Sort and query db
                List <EventItemOccurrence> eventItemOccurrences = null;
                if (sortProperty != null)
                {
                    // If sorting on date, wait until after checking to see if date range was specified
                    if (sortProperty.Property == "Date")
                    {
                        eventItemOccurrences = qry.ToList();
                    }
                    else
                    {
                        eventItemOccurrences = qry.Sort(sortProperty).ToList();
                    }
                }
                else
                {
                    eventItemOccurrences = qry.ToList().OrderBy(a => a.NextStartDateTime).ToList();
                }

                // Contact filter
                if (!string.IsNullOrWhiteSpace(tbContact.Text))
                {
                    eventItemOccurrences = eventItemOccurrences
                                           .Where(i =>
                                                  i.ContactPersonAlias != null &&
                                                  i.ContactPersonAlias.Person != null &&
                                                  i.ContactPersonAlias.Person.FullName.Contains(tbContact.Text))
                                           .ToList();
                }

                // Now that items have been loaded and ordered from db, calculate the next start date for each item
                var eventItemOccurrencesWithDates = eventItemOccurrences
                                                    .Select(i => new EventItemOccurrenceWithDates
                {
                    EventItemOccurrence = i,
                    NextStartDateTime   = i.NextStartDateTime,
                })
                                                    .ToList();

                var dateCol = gCalendarItemOccurrenceList.Columns.OfType <BoundField>().Where(c => c.DataField == "Date").FirstOrDefault();

                // if a date range was specified, need to get all dates for items and filter based on any that have an occurrence withing the date range
                DateTime?lowerDateRange = drpDate.LowerValue;
                DateTime?upperDateRange = drpDate.UpperValue;
                if (lowerDateRange.HasValue || upperDateRange.HasValue)
                {
                    // If only one value was included, default the other to be a years difference
                    lowerDateRange = lowerDateRange ?? upperDateRange.Value.AddYears(-1).AddDays(1);
                    upperDateRange = upperDateRange ?? lowerDateRange.Value.AddYears(1).AddDays(-1);

                    // Get the start datetimes within the selected date range
                    eventItemOccurrencesWithDates.ForEach(i => i.StartDateTimes = i.EventItemOccurrence.GetStartTimes(lowerDateRange.Value, upperDateRange.Value.AddDays(1)));

                    // Filter out calendar items with no dates within range
                    eventItemOccurrencesWithDates = eventItemOccurrencesWithDates.Where(i => i.StartDateTimes.Any()).ToList();

                    // Update the Next Start Date to be the next date in range instead
                    dateCol.HeaderText = "Next Date In Range";
                    eventItemOccurrencesWithDates.ForEach(i => i.NextStartDateTime = i.StartDateTimes.Min());
                }
                else
                {
                    dateCol.HeaderText = "Next Start Date";
                }

                // Now sort on date if that is what was selected
                if (sortProperty != null && sortProperty.Property == "Date")
                {
                    if (sortProperty.Direction == SortDirection.Ascending)
                    {
                        eventItemOccurrencesWithDates = eventItemOccurrencesWithDates.OrderBy(a => a.NextStartDateTime).ToList();
                    }
                    else
                    {
                        eventItemOccurrencesWithDates = eventItemOccurrencesWithDates.OrderByDescending(a => a.NextStartDateTime).ToList();
                    }
                }

                gCalendarItemOccurrenceList.EntityTypeId = EntityTypeCache.Get <Rock.Model.EventItemOccurrence>().Id;
                gCalendarItemOccurrenceList.ObjectList   = new Dictionary <string, object>();
                eventItemOccurrencesWithDates.ForEach(i => gCalendarItemOccurrenceList.ObjectList.Add(i.EventItemOccurrence.Id.ToString(), i.EventItemOccurrence));
                gCalendarItemOccurrenceList.DataSource = eventItemOccurrencesWithDates
                                                         .Select(c => new
                {
                    c.EventItemOccurrence.Id,
                    c.EventItemOccurrence.Guid,
                    Campus   = c.EventItemOccurrence.Campus != null ? c.EventItemOccurrence.Campus.Name : "All Campuses",
                    Date     = c.NextStartDateTime.HasValue ? c.NextStartDateTime.Value.ToShortDateString() : "N/A",
                    Location = c.EventItemOccurrence.Location,
                    RegistrationInstanceId = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().RegistrationInstanceId : ( int? )null,
                    RegistrationInstance   = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().RegistrationInstance : null,
                    GroupId      = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().GroupId : ( int? )null,
                    Group        = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().Group : null,
                    ContentItems = FormatContentItems(c.EventItemOccurrence.ContentChannelItems.Select(i => i.ContentChannelItem).ToList()),
                    Contact      = c.EventItemOccurrence.ContactPersonAlias != null ? c.EventItemOccurrence.ContactPersonAlias.Person.FullName : "",
                    Phone        = c.EventItemOccurrence.ContactPhone,
                    Email        = c.EventItemOccurrence.ContactEmail,
                })
                                                         .ToList();
                gCalendarItemOccurrenceList.DataBind();
            }
            else
            {
                pnlEventCalendarCampusItems.Visible = false;
            }
        }
        private void LoadContent()
        {
            var audienceGuid = GetAttributeValue( "Audience" ).AsGuid();

            if ( audienceGuid != Guid.Empty )
            {
                lMessages.Text = string.Empty;
                RockContext rockContext = new RockContext();

                // get event occurrences
                var qry = new EventItemOccurrenceService( rockContext ).Queryable()
                                            .Where(e => e.EventItem.EventItemAudiences.Any(a => a.DefinedValue.Guid == audienceGuid) && e.EventItem.IsActive);

                // filter occurrences for campus (always include the "All Campuses" events)
                if ( GetAttributeValue( "UseCampusContext" ).AsBoolean() )
                {
                    var campusEntityType = EntityTypeCache.Read( "Rock.Model.Campus" );
                    var contextCampus = RockPage.GetCurrentContext( campusEntityType ) as Campus;

                    if ( contextCampus != null )
                    {
                        qry = qry.Where( e => e.CampusId == contextCampus.Id || !e.CampusId.HasValue );
                    }
                }
                else
                {
                    if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "Campuses" ) ) )
                    {
                        var selectedCampuses = Array.ConvertAll( GetAttributeValue( "Campuses" ).Split( ',' ), s => new Guid( s ) ).ToList();
                        qry = qry.Where( e => selectedCampuses.Contains(e.Campus.Guid) || !e.CampusId.HasValue );
                    }
                }

                // filter by calendar
                var calendarGuid = GetAttributeValue( "Calendar" ).AsGuid();

                if ( calendarGuid != Guid.Empty )
                {
                    qry = qry.Where( e => e.EventItem.EventCalendarItems.Any( c => c.EventCalendar.Guid == calendarGuid ) );
                }

                // retrieve occurrences
                var itemOccurrences = qry.ToList();

                // filter by date range
                var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( GetAttributeValue( "DateRange" ) );
                if ( dateRange.Start != null && dateRange.End != null )
                {
                    itemOccurrences.RemoveAll( o => o.GetStartTimes( dateRange.Start.Value, dateRange.End.Value ).Count() == 0 );
                }
                else
                {
                    // default show all future
                    itemOccurrences.RemoveAll( o => o.GetStartTimes( RockDateTime.Now, DateTime.Now.AddDays( 365 ) ).Count() == 0 );
                }

                // limit results
                int maxItems = GetAttributeValue( "MaxOccurrences" ).AsInteger();
                itemOccurrences = itemOccurrences.OrderBy( i => i.NextStartDateTime ).Take( maxItems ).ToList();

                // make lava merge fields
                var mergeFields = new Dictionary<string, object>();

                var contextObjects = new Dictionary<string, object>();
                foreach (var contextEntityType in RockPage.GetContextEntityTypes())
                {
                    var contextEntity = RockPage.GetCurrentContext(contextEntityType);
                    if (contextEntity != null && contextEntity is DotLiquid.ILiquidizable)
                    {
                        var type = Type.GetType(contextEntityType.AssemblyName ?? contextEntityType.Name);
                        if (type != null)
                        {
                            contextObjects.Add(type.Name, contextEntity);
                        }
                    }

                }

                if (contextObjects.Any())
                {
                    mergeFields.Add("Context", contextObjects);
                }

                mergeFields.Add( "ListTitle", GetAttributeValue("ListTitle") );
                mergeFields.Add( "EventDetailPage", LinkedPageRoute( "EventDetailPage" ) );
                mergeFields.Add( "RegistrationPage", LinkedPageRoute( "RegistrationPage" ) );
                mergeFields.Add( "EventItemOccurrences", itemOccurrences );

                lContent.Text = GetAttributeValue( "LavaTemplate" ).ResolveMergeFields( mergeFields );

                // show debug info
                if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) )
                {
                    lDebug.Visible = true;
                    lDebug.Text = @"<div class='alert alert-info'>Due to the size of the lava members the debug info for this block has been supressed. Below are high-level details of
                                    the merge objects available.
                                    <ul>
                                        <li>List Title - The title to pass to lava.
                                        <li>EventItemOccurrences - A list of EventItemOccurrences. View the EvenItemOccurrence model for these properties.</li>
                                        <li>RegistrationPage  - String that contains the relative path to the registration page.</li>
                                        <li>EventDetailPage  - String that contains the relative path to the event detail page.</li>
                                        <li>Global Attribute  - Access to the Global Attributes.</li>
                                    </ul>
                                    </div>";
                }
                else
                {
                    lDebug.Visible = false;
                    lDebug.Text = string.Empty;
                }
            }
            else
            {
                lMessages.Text = "<div class='alert alert-warning'>No audience is configured for this block.</div>";
            }
        }
        /// <summary>
        /// Binds the campus grid.
        /// </summary>
        private void BindCampusGrid()
        {
            if ( _eventItem != null )
            {
                pnlEventCalendarCampusItems.Visible = true;

                var rockContext = new RockContext();

                var qry = new EventItemOccurrenceService( rockContext )
                    .Queryable().AsNoTracking()
                    .Where( c => c.EventItemId == _eventItem.Id );

                // Filter by Campus
                List<int> campusIds = cblCampus.SelectedValuesAsInt;
                if ( campusIds.Any() )
                {
                    qry = qry
                        .Where( i =>
                            !i.CampusId.HasValue ||
                            campusIds.Contains( i.CampusId.Value ) );
                }

                SortProperty sortProperty = gCalendarItemOccurrenceList.SortProperty;

                // Sort and query db
                List<EventItemOccurrence> eventItemOccurrences = null;
                if ( sortProperty != null )
                {
                    // If sorting on date, wait until after checking to see if date range was specified
                    if ( sortProperty.Property == "Date" )
                    {
                        eventItemOccurrences = qry.ToList();
                    }
                    else
                    {
                        eventItemOccurrences = qry.Sort( sortProperty ).ToList();
                    }
                }
                else
                {
                    eventItemOccurrences = qry.ToList().OrderBy( a => a.NextStartDateTime ).ToList();
                }

                // Contact filter
                if ( !string.IsNullOrWhiteSpace( tbContact.Text ) )
                {
                    eventItemOccurrences = eventItemOccurrences
                        .Where( i =>
                            i.ContactPersonAlias != null &&
                            i.ContactPersonAlias.Person != null &&
                            i.ContactPersonAlias.Person.FullName.Contains( tbContact.Text ) )
                        .ToList();
                }

                // Now that items have been loaded and ordered from db, calculate the next start date for each item
                var eventItemOccurrencesWithDates = eventItemOccurrences
                    .Select( i => new EventItemOccurrenceWithDates
                    {
                        EventItemOccurrence = i,
                        NextStartDateTime = i.NextStartDateTime,
                    } )
                    .ToList();

                var dateCol = gCalendarItemOccurrenceList.Columns.OfType<BoundField>().Where( c => c.DataField == "Date" ).FirstOrDefault();

                // if a date range was specified, need to get all dates for items and filter based on any that have an occurrence withing the date range
                DateTime? lowerDateRange = drpDate.LowerValue;
                DateTime? upperDateRange = drpDate.UpperValue;
                if ( lowerDateRange.HasValue || upperDateRange.HasValue )
                {
                    // If only one value was included, default the other to be a years difference
                    lowerDateRange = lowerDateRange ?? upperDateRange.Value.AddYears( -1 ).AddDays( 1 );
                    upperDateRange = upperDateRange ?? lowerDateRange.Value.AddYears( 1 ).AddDays( -1 );

                    // Get the start datetimes within the selected date range
                    eventItemOccurrencesWithDates.ForEach( i => i.StartDateTimes = i.EventItemOccurrence.GetStartTimes( lowerDateRange.Value, upperDateRange.Value.AddDays( 1 ) ) );

                    // Filter out calendar items with no dates within range
                    eventItemOccurrencesWithDates = eventItemOccurrencesWithDates.Where( i => i.StartDateTimes.Any() ).ToList();

                    // Update the Next Start Date to be the next date in range instead
                    dateCol.HeaderText = "Next Date In Range";
                    eventItemOccurrencesWithDates.ForEach( i => i.NextStartDateTime = i.StartDateTimes.Min() );
                }
                else
                {
                    dateCol.HeaderText = "Next Start Date";
                }

                // Now sort on date if that is what was selected
                if ( sortProperty != null && sortProperty.Property == "Date" )
                {
                    if ( sortProperty.Direction == SortDirection.Ascending )
                    {
                        eventItemOccurrencesWithDates = eventItemOccurrencesWithDates.OrderBy( a => a.NextStartDateTime ).ToList();
                    }
                    else
                    {
                        eventItemOccurrencesWithDates = eventItemOccurrencesWithDates.OrderByDescending( a => a.NextStartDateTime ).ToList();
                    }
                }

                gCalendarItemOccurrenceList.DataSource = eventItemOccurrencesWithDates
                    .Select( c => new
                    {
                        c.EventItemOccurrence.Id,
                        c.EventItemOccurrence.Guid,
                        Campus = c.EventItemOccurrence.Campus != null ? c.EventItemOccurrence.Campus.Name : "All Campuses",
                        Date = c.NextStartDateTime.HasValue ? c.NextStartDateTime.Value.ToShortDateString() : "N/A",
                        Location = c.EventItemOccurrence.Location,
                        RegistrationInstanceId = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().RegistrationInstanceId : (int?)null,
                        RegistrationInstance = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().RegistrationInstance : null,
                        GroupId = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().GroupId : (int?)null,
                        Group = c.EventItemOccurrence.Linkages.Any() ? c.EventItemOccurrence.Linkages.FirstOrDefault().Group : null,
                        ContentItems = FormatContentItems( c.EventItemOccurrence.ContentChannelItems.Select( i => i.ContentChannelItem ).ToList() ),
                        Contact = c.EventItemOccurrence.ContactPersonAlias != null ? c.EventItemOccurrence.ContactPersonAlias.Person.FullName : "",
                        Phone = c.EventItemOccurrence.ContactPhone,
                        Email = c.EventItemOccurrence.ContactEmail,
                    } )
                    .ToList();
                gCalendarItemOccurrenceList.DataBind();
            }
            else
            {
                pnlEventCalendarCampusItems.Visible = false;
            }
        }