예제 #1
0
        private EventItemOccurrence EventItemOccurrenceAddOrUpdateInstance(RockContext rockContext, Guid scheduleGuid, bool deleteExistingInstance)
        {
            // Get existing schedules.
            var scheduleService = new ScheduleService(rockContext);
            var schedule        = scheduleService.Get(scheduleGuid);

            // Get Event "Rock Solid Finances".
            var eventItemService           = new EventItemService(rockContext);
            var eventItemOccurrenceService = new EventItemOccurrenceService(rockContext);

            var financeEvent = eventItemService.Get(EventFinancesClassGuid.AsGuid());

            // Add a new occurrence of this event.
            var financeEvent1 = eventItemOccurrenceService.Get(FinancesClassOccurrenceTestGuid.AsGuid());

            if (financeEvent1 != null && deleteExistingInstance)
            {
                eventItemOccurrenceService.Delete(financeEvent1);
                rockContext.SaveChanges();
            }
            financeEvent1 = new EventItemOccurrence();

            var mainCampusId = CampusCache.GetId(MainCampusGuidString.AsGuid());

            financeEvent1.Location   = "Meeting Room 1";
            financeEvent1.ForeignKey = TestDataForeignKey;
            financeEvent1.ScheduleId = schedule.Id;
            financeEvent1.Guid       = FinancesClassOccurrenceTestGuid.AsGuid();
            financeEvent1.CampusId   = mainCampusId;

            financeEvent.EventItemOccurrences.Add(financeEvent1);

            return(financeEvent1);
        }
        /// <summary>
        /// Handles the Delete event of the gCalendarItemOccurrenceList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gCalendarItemOccurrenceList_Delete(object sender, RowEventArgs e)
        {
            using (RockContext rockContext = new RockContext())
            {
                EventItemOccurrenceService eventItemOccurrenceService = new EventItemOccurrenceService(rockContext);
                EventItemOccurrence        eventItemOccurrence        = eventItemOccurrenceService.Get(e.RowKeyId);
                if (eventItemOccurrence != null)
                {
                    string errorMessage;
                    if (!eventItemOccurrenceService.CanDelete(eventItemOccurrence, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    eventItemOccurrenceService.Delete(eventItemOccurrence);
                    rockContext.SaveChanges();
                }
            }

            BindCampusGrid();
        }
예제 #3
0
        /// <summary>
        /// Handles the Click event of the btnDelete 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 btnDelete_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                EventItemOccurrenceService eventItemOccurrenceService = new EventItemOccurrenceService(rockContext);
                EventItemOccurrence        eventItemOccurrence        = eventItemOccurrenceService.Get(hfEventItemOccurrenceId.Value.AsInteger());

                if (eventItemOccurrence != null)
                {
                    string errorMessage;
                    if (!eventItemOccurrenceService.CanDelete(eventItemOccurrence, out errorMessage))
                    {
                        mdDeleteWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    eventItemOccurrenceService.Delete(eventItemOccurrence);

                    rockContext.SaveChanges();
                }
            }

            NavigateToParentPage();
        }
        /// <summary>
        /// Handles the Click event of the btnDelete 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 btnDelete_Click( object sender, EventArgs e )
        {
            using ( var rockContext = new RockContext() )
            {
                EventItemOccurrenceService eventItemOccurrenceService = new EventItemOccurrenceService( rockContext );
                EventItemOccurrence eventItemOccurrence = eventItemOccurrenceService.Get( hfEventItemOccurrenceId.Value.AsInteger() );

                if ( eventItemOccurrence != null )
                {
                    string errorMessage;
                    if ( !eventItemOccurrenceService.CanDelete( eventItemOccurrence, out errorMessage ) )
                    {
                        mdDeleteWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    eventItemOccurrenceService.Delete( eventItemOccurrence );

                    rockContext.SaveChanges();
                }
            }

            NavigateToParentPage();
        }
예제 #5
0
파일: SyncHelper.cs 프로젝트: moayyaed/Rock
        /// <summary>
        /// Syncs an eSpace event into Rock.
        /// </summary>
        /// <param name="eSpaceClient">The eSpace client</param>
        /// <param name="eSpaceEvent">The eSpace event</param>
        /// <param name="occurrencesFilter">The filter to use when syncing occurrences</param>
        /// <returns>The synced Rock event</returns>
        public static async Task SyncEvent(
            Client eSpaceClient,
            ESpace.Event eSpaceEvent,
            GetEventOccurrencesOptions occurrencesFilter,
            EventCalendarCache globalCalendar,
            EventCalendarCache publicCalendar,
            EventCalendarCache privateCalendar,
            string occurrenceApprovedAttributeKey
            )
        {
            using (var rockContext = new RockContext())
            {
                // Create our services
                var eventItemService           = new EventItemService(rockContext);
                var eventItemOccurrenceService = new EventItemOccurrenceService(rockContext);
                var eventItemAudienceService   = new EventItemAudienceService(rockContext);
                var scheduleService            = new ScheduleService(rockContext);
                var personService = new PersonService(rockContext);

                // Get or create the linked Rock event
                var rockEvent = eventItemService.GetOrCreateByForeignId(
                    "EventCalendarItems,EventItemOccurrences",
                    ForeignKey_eSpaceEventId,
                    eSpaceEvent.EventId.Value,
                    out var _
                    );

                // Track if we needed to update anything
                var changed = false;

                // Update the Name
                if (rockEvent.Name != eSpaceEvent.EventName)
                {
                    changed        = true;
                    rockEvent.Name = eSpaceEvent.EventName;
                }

                // Update the Active State
                var eSpaceEventIsActive = eSpaceEvent.Status != ESpaceStatus_Draft;
                if (rockEvent.IsActive != eSpaceEventIsActive)
                {
                    changed            = true;
                    rockEvent.IsActive = eSpaceEventIsActive;
                }

                // Update the Approval State
                var eSpaceEventIsApproved = eSpaceEvent.Status == ESpaceStatus_Approved;
                if (rockEvent.IsApproved != eSpaceEventIsApproved)
                {
                    changed = true;
                    rockEvent.IsApproved = eSpaceEventIsApproved;

                    if (eSpaceEventIsApproved)
                    {
                        rockEvent.ApprovedOnDateTime = DateTime.Now;
                    }
                    else
                    {
                        rockEvent.ApprovedOnDateTime = null;
                    }
                }

                // Update the Summary
                if (rockEvent.Summary != eSpaceEvent.Description)
                {
                    changed           = true;
                    rockEvent.Summary = eSpaceEvent.Description;
                }

                // Update the details Url
                var eSpaceEventPublicLink = eSpaceEvent.PublicLink?.ToString();
                if (rockEvent.DetailsUrl != eSpaceEventPublicLink)
                {
                    changed = true;
                    rockEvent.DetailsUrl = eSpaceEventPublicLink;
                }

                // Update the audiences
                var eSpaceCategories = MatchCategories(eSpaceEvent.Categories);


                // Check global calendar
                if (globalCalendar != null)
                {
                    rockEvent.AddToCalendar(globalCalendar, out var addedToCalendar);
                    changed = changed || addedToCalendar;
                }

                var eventCalendarItemService = new EventCalendarItemService(rockContext);

                // Check public calendar
                if (publicCalendar != null)
                {
                    if (eSpaceEvent.IsPublic ?? false)
                    {
                        rockEvent.AddToCalendar(publicCalendar, out var addedToCalendar);
                        changed = changed || addedToCalendar;
                    }
                    else
                    {
                        rockEvent.RemoveFromCalendar(eventCalendarItemService, publicCalendar, out var removedFromCalendar);
                        changed = changed || removedFromCalendar;
                    }
                }

                // Check private calendar
                if (privateCalendar != null)
                {
                    if (!(eSpaceEvent.IsPublic ?? false))
                    {
                        rockEvent.AddToCalendar(privateCalendar, out var addedToCalendar);
                        changed = changed || addedToCalendar;
                    }
                    else
                    {
                        rockEvent.RemoveFromCalendar(eventCalendarItemService, privateCalendar, out var removedFromCalendar);
                        changed = changed || removedFromCalendar;
                    }
                }

                // Fetch the occurrences for the event
                if (occurrencesFilter == null)
                {
                    occurrencesFilter = new GetEventOccurrencesOptions {
                        StartDate = DateTime.Now
                    }
                }
                ;
                occurrencesFilter.EventId = eSpaceEvent.EventId;
                var eSpaceEventOccurrences = await eSpaceClient.GetEventOccurrences(occurrencesFilter);

                // Calculate some stuff for the occurrences
                var campusLocations = MatchLocations(eSpaceEvent.IsOffSite ?? false ? eSpaceEvent.PublicLocations : eSpaceEvent.Locations);
                var contactPerson   = personService.FindPerson(eSpaceEvent.Contacts.FirstOrDefault());

                var firstESpaceOccurrence = eSpaceEventOccurrences.FirstOrDefault();
                if (firstESpaceOccurrence != null)
                {
                    // Update the Description
                    if (rockEvent.Description != firstESpaceOccurrence.PublicHtmlNotes)
                    {
                        rockEvent.Description = firstESpaceOccurrence.PublicHtmlNotes;
                        changed = true;
                    }
                }

                var syncedRockOccurrences = new List <EventItemOccurrence>();;
                var rockOccurrencesWithAttributeChanges = new List <EventItemOccurrence>();

                // Update each occurrence
                foreach (var eSpaceOccurrence in eSpaceEventOccurrences)
                {
                    foreach (var campusLocation in campusLocations)
                    {
                        var rockOccurrence = SyncOccurrence(eSpaceEvent, eSpaceOccurrence, rockEvent, campusLocation, contactPerson, occurrenceApprovedAttributeKey, out var occurrenceChanged, out var occurrenceAttributeChanged);
                        changed = changed || occurrenceChanged;
                        syncedRockOccurrences.Add(rockOccurrence);
                        if (occurrenceAttributeChanged)
                        {
                            rockOccurrencesWithAttributeChanges.Add(rockOccurrence);
                        }
                    }
                }

                // Remove any desynced occurrences
                var removedOccurrences = rockEvent.EventItemOccurrences.Except(syncedRockOccurrences).ToList();
                foreach (var occurrence in removedOccurrences)
                {
                    rockEvent.EventItemOccurrences.Remove(occurrence);
                    if (occurrence.Schedule != null)
                    {
                        scheduleService.Delete(occurrence.Schedule);
                        occurrence.Schedule = null;
                    }
                    eventItemOccurrenceService.Delete(occurrence);
                    changed = true;
                }

                // If anything was updated, save it
                if (changed)
                {
                    rockContext.SaveChanges();
                }

                // If any occurrences had attributes modified, save them
                foreach (var rockOccurrence in rockOccurrencesWithAttributeChanges)
                {
                    rockOccurrence.SaveAttributeValues();
                }
            }
        }
        /// <summary>
        /// Handles the Delete event of the gCalendarItemOccurrenceList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gCalendarItemOccurrenceList_Delete( object sender, RowEventArgs e )
        {
            using ( RockContext rockContext = new RockContext() )
            {
                EventItemOccurrenceService eventItemOccurrenceService = new EventItemOccurrenceService( rockContext );
                EventItemOccurrence eventItemOccurrence = eventItemOccurrenceService.Get( e.RowKeyId );
                if ( eventItemOccurrence != null )
                {
                    string errorMessage;
                    if ( !eventItemOccurrenceService.CanDelete( eventItemOccurrence, out errorMessage ) )
                    {
                        mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                        return;
                    }

                    eventItemOccurrenceService.Delete( eventItemOccurrence );
                    rockContext.SaveChanges();
                }
            }

            BindCampusGrid();
        }