Ejemplo n.º 1
0
        /// <summary>
        /// Deletes any saved history items.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="entityId">The entity identifier.</param>
        public static void DeleteChanges(RockContext rockContext, Type modelType, int entityId)
        {
            var entityType = EntityTypeCache.Get(modelType);

            if (entityType != null)
            {
                var historyService = new HistoryService(rockContext);
                foreach (var history in historyService.Queryable()
                         .Where(h =>
                                h.EntityTypeId == entityType.Id &&
                                h.EntityId == entityId))
                {
                    historyService.Delete(history);
                }

                rockContext.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deletes any saved history items.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="entityId">The entity identifier.</param>
        public static void DeleteChanges( RockContext rockContext, Type modelType, int entityId )
        {
            var entityType = EntityTypeCache.Read( modelType );
            if ( entityType != null  )
            {
                var historyService = new HistoryService( rockContext );
                foreach( var history in historyService.Queryable()
                    .Where( h =>
                        h.EntityTypeId == entityType.Id &&
                        h.EntityId == entityId ) )
                {
                    historyService.Delete( history );
                }

                rockContext.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the timeline HTML.
        /// </summary>
        /// <param name="timelineLavaTemplate">The timeline lava template.</param>
        /// <param name="primaryEntityType">Type of the primary entity.</param>
        /// <param name="entityId">The entity identifier.</param>
        /// <param name="secondaryEntityType">Type of the secondary entity.</param>
        /// <param name="additionalMergeFields">The additional merge fields.</param>
        /// <returns></returns>
        public string GetTimelineHtml(string timelineLavaTemplate, EntityTypeCache primaryEntityType, int entityId, EntityTypeCache secondaryEntityType, Dictionary <string, object> additionalMergeFields)
        {
            RockContext    rockContext    = this.Context as RockContext;
            HistoryService historyService = new HistoryService(rockContext);

            // change this to adjust the granularity of the GetHistorySummaryByDateTime
            TimeSpan dateSummaryGranularity = TimeSpan.FromDays(1);

            if (primaryEntityType == null)
            {
                return(null);
            }

            var entityTypeIdPrimary = primaryEntityType.Id;

            var primaryEntity = historyService.GetEntityQuery(entityTypeIdPrimary).FirstOrDefault(a => a.Id == entityId);
            var historyQry    = historyService.Queryable().Where(a => a.CreatedDateTime.HasValue);

            if (secondaryEntityType == null)
            {
                // get history records where the primaryentity is the Entity
                historyQry = historyQry.Where(a => a.EntityTypeId == entityTypeIdPrimary && a.EntityId == entityId);
            }
            else
            {
                // get history records where the primaryentity is the Entity OR the primaryEntity is the RelatedEntity and the Entity is the Secondary Entity
                // For example, for GroupHistory, Set PrimaryEntityType to Group and SecondaryEntityType to GroupMember, then get history where the Group is History.Entity or the Group is the RelatedEntity and GroupMember is the EntityType
                var entityTypeIdSecondary = secondaryEntityType.Id;
                historyQry = historyQry.Where(a =>
                                              (a.EntityTypeId == entityTypeIdPrimary && a.EntityId == entityId) ||
                                              (a.RelatedEntityTypeId == entityTypeIdPrimary && a.EntityTypeId == entityTypeIdSecondary && a.RelatedEntityId == entityId));
            }

            var historySummaryList       = historyService.GetHistorySummary(historyQry);
            var historySummaryByDateList = historyService.GetHistorySummaryByDateTime(historySummaryList, dateSummaryGranularity);

            historySummaryByDateList = historySummaryByDateList.OrderByDescending(a => a.SummaryDateTime).ToList();
            var historySummaryByDateByVerbList = historyService.GetHistorySummaryByDateTimeAndVerb(historySummaryByDateList);

            var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(null, null, new Rock.Lava.CommonMergeFieldsOptions {
                GetLegacyGlobalMergeFields = false
            });

            mergeFields.Add("PrimaryEntity", primaryEntity);
            mergeFields.Add("PrimaryEntityTypeName", primaryEntityType.FriendlyName);
            if (secondaryEntityType != null)
            {
                mergeFields.Add("SecondaryEntityTypeName", secondaryEntityType.FriendlyName);
            }

            mergeFields.Add("HistorySummaryByDateByVerbList", historySummaryByDateByVerbList);
            if (additionalMergeFields != null)
            {
                foreach (var additionalMergeField in additionalMergeFields)
                {
                    mergeFields.AddOrIgnore(additionalMergeField.Key, additionalMergeField.Value);
                }
            }

            string timelineHtml = timelineLavaTemplate.ResolveMergeFields(mergeFields);

            return(timelineHtml);
        }