Example #1
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            EventCalendar eventCalendar;
            using ( var rockContext = new RockContext() )
            {
                EventCalendarService eventCalendarService = new EventCalendarService( rockContext );
                EventCalendarContentChannelService eventCalendarContentChannelService = new EventCalendarContentChannelService( rockContext );
                ContentChannelService contentChannelService = new ContentChannelService( rockContext );
                AttributeService attributeService = new AttributeService( rockContext );
                AttributeQualifierService qualifierService = new AttributeQualifierService( rockContext );

                int eventCalendarId = int.Parse( hfEventCalendarId.Value );

                if ( eventCalendarId == 0 )
                {
                    eventCalendar = new EventCalendar();
                    eventCalendarService.Add( eventCalendar );
                }
                else
                {
                    eventCalendar = eventCalendarService.Get( eventCalendarId );
                }

                eventCalendar.IsActive = cbActive.Checked;
                eventCalendar.Name = tbName.Text;
                eventCalendar.Description = tbDescription.Text;
                eventCalendar.IconCssClass = tbIconCssClass.Text;

                if ( !eventCalendar.IsValid )
                {
                    // Controls will render the error messages
                    return;
                }

                // need WrapTransaction due to Attribute saves
                rockContext.WrapTransaction( () =>
                {
                    rockContext.SaveChanges();

                    var dbChannelGuids = eventCalendarContentChannelService.Queryable()
                        .Where( c => c.EventCalendarId == eventCalendar.Id )
                        .Select( c => c.Guid )
                        .ToList();

                    var uiChannelGuids = ContentChannelsState.Select( c => c.Key ).ToList();

                    var toDelete = eventCalendarContentChannelService
                        .Queryable()
                        .Where( c =>
                            dbChannelGuids.Contains( c.Guid ) &&
                            !uiChannelGuids.Contains( c.Guid ));

                    eventCalendarContentChannelService.DeleteRange( toDelete );
                    contentChannelService.Queryable()
                        .Where( c =>
                            uiChannelGuids.Contains( c.Guid ) &&
                            !dbChannelGuids.Contains( c.Guid ) )
                        .ToList()
                        .ForEach( c =>
                        {
                            var eventCalendarContentChannel = new EventCalendarContentChannel();
                            eventCalendarContentChannel.EventCalendarId = eventCalendar.Id;
                            eventCalendarContentChannel.ContentChannelId = c.Id;
                            eventCalendarContentChannelService.Add( eventCalendarContentChannel );
                        } );

                    rockContext.SaveChanges();

                    /* Save Attributes */
                    string qualifierValue = eventCalendar.Id.ToString();
                    SaveAttributes( new EventCalendarItem().TypeId, "EventCalendarId", qualifierValue, AttributesState, rockContext );

                    // Reload calendar and make sure that the person who may have just added a calendar has security to view/edit/administrate the calendar
                    eventCalendar = eventCalendarService.Get( eventCalendar.Id );
                    if ( eventCalendar != null )
                    {
                        if ( !eventCalendar.IsAuthorized( Authorization.VIEW, CurrentPerson ) )
                        {
                            eventCalendar.AllowPerson( Authorization.VIEW, CurrentPerson, rockContext );
                        }
                        if ( !eventCalendar.IsAuthorized( Authorization.EDIT, CurrentPerson ) )
                        {
                            eventCalendar.AllowPerson( Authorization.EDIT, CurrentPerson, rockContext );
                        }
                        if ( !eventCalendar.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ) )
                        {
                            eventCalendar.AllowPerson( Authorization.ADMINISTRATE, CurrentPerson, rockContext );
                        }
                    }

                } );
            }

            // Redirect back to same page so that item grid will show any attributes that were selected to show on grid
            var qryParams = new Dictionary<string, string>();
            qryParams["EventCalendarId"] = eventCalendar.Id.ToString();
            NavigateToPage( RockPage.Guid, qryParams );
        }
Example #2
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            EventCalendar eventCalendar;

            using (var rockContext = new RockContext())
            {
                EventCalendarService eventCalendarService = new EventCalendarService(rockContext);
                EventCalendarContentChannelService eventCalendarContentChannelService = new EventCalendarContentChannelService(rockContext);
                ContentChannelService     contentChannelService = new ContentChannelService(rockContext);
                AttributeService          attributeService      = new AttributeService(rockContext);
                AttributeQualifierService qualifierService      = new AttributeQualifierService(rockContext);

                int eventCalendarId = int.Parse(hfEventCalendarId.Value);

                if (eventCalendarId == 0)
                {
                    eventCalendar = new EventCalendar();
                    eventCalendarService.Add(eventCalendar);
                }
                else
                {
                    eventCalendar = eventCalendarService.Get(eventCalendarId);
                }

                eventCalendar.IsActive     = cbActive.Checked;
                eventCalendar.Name         = tbName.Text;
                eventCalendar.Description  = tbDescription.Text;
                eventCalendar.IconCssClass = tbIconCssClass.Text;

                eventCalendar.LoadAttributes();
                Rock.Attribute.Helper.GetEditValues(phAttributes, eventCalendar);

                if (!eventCalendar.IsValid)
                {
                    // Controls will render the error messages
                    return;
                }

                // need WrapTransaction due to Attribute saves
                rockContext.WrapTransaction(() =>
                {
                    rockContext.SaveChanges();
                    eventCalendar.SaveAttributeValues(rockContext);

                    var dbChannelGuids = eventCalendarContentChannelService.Queryable()
                                         .Where(c => c.EventCalendarId == eventCalendar.Id)
                                         .Select(c => c.Guid)
                                         .ToList();

                    var uiChannelGuids = ContentChannelsState.Select(c => c.Key).ToList();

                    var toDelete = eventCalendarContentChannelService
                                   .Queryable()
                                   .Where(c =>
                                          dbChannelGuids.Contains(c.Guid) &&
                                          !uiChannelGuids.Contains(c.Guid));

                    eventCalendarContentChannelService.DeleteRange(toDelete);
                    contentChannelService.Queryable()
                    .Where(c =>
                           uiChannelGuids.Contains(c.Guid) &&
                           !dbChannelGuids.Contains(c.Guid))
                    .ToList()
                    .ForEach(c =>
                    {
                        var eventCalendarContentChannel              = new EventCalendarContentChannel();
                        eventCalendarContentChannel.EventCalendarId  = eventCalendar.Id;
                        eventCalendarContentChannel.ContentChannelId = c.Id;
                        eventCalendarContentChannelService.Add(eventCalendarContentChannel);
                    });

                    rockContext.SaveChanges();

                    /* Save Event Attributes */
                    string qualifierValue = eventCalendar.Id.ToString();
                    SaveAttributes(new EventCalendarItem().TypeId, "EventCalendarId", qualifierValue, EventAttributesState, rockContext);

                    // Reload calendar and make sure that the person who may have just added a calendar has security to view/edit/administrate the calendar
                    eventCalendar = eventCalendarService.Get(eventCalendar.Id);
                    if (eventCalendar != null)
                    {
                        if (!eventCalendar.IsAuthorized(Authorization.VIEW, CurrentPerson))
                        {
                            eventCalendar.AllowPerson(Authorization.VIEW, CurrentPerson, rockContext);
                        }
                        if (!eventCalendar.IsAuthorized(Authorization.EDIT, CurrentPerson))
                        {
                            eventCalendar.AllowPerson(Authorization.EDIT, CurrentPerson, rockContext);
                        }
                        if (!eventCalendar.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson))
                        {
                            eventCalendar.AllowPerson(Authorization.ADMINISTRATE, CurrentPerson, rockContext);
                        }
                    }
                });
            }

            // Redirect back to same page so that item grid will show any attributes that were selected to show on grid
            var qryParams = new Dictionary <string, string>();

            qryParams["EventCalendarId"] = eventCalendar.Id.ToString();
            NavigateToPage(RockPage.Guid, qryParams);
        }