Esempio n. 1
0
        /// <summary>
        /// Processes the PreSave event when this value is for
        /// <see cref="Field.Types.StructureContentEditorFieldType"/>. Detect any
        /// changes to the internal content and apply them to the database as well.
        /// </summary>
        /// <param name="dbContext">The database context.</param>
        /// <param name="entry">The entry.</param>
        private void PreSaveStructuredContent(Rock.Data.DbContext dbContext, DbEntityEntry entry)
        {
            if (dbContext is RockContext rockContext)
            {
                string content    = entry.State == EntityState.Added || entry.State == EntityState.Modified ? Value : string.Empty;
                string oldContent = entry.State == EntityState.Modified ? entry.OriginalValues["Value"] as string : string.Empty;

                var helper  = new StructuredContentHelper(content);
                var changes = helper.DetectChanges(oldContent);

                helper.ApplyDatabaseChanges(changes, rockContext);
            }
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public override bool ApplyDatabaseChanges(StructuredContentHelper helper, StructuredContentChanges changes, RockContext rockContext)
        {
            var imageChanges = changes.GetData <ImageChangeData>();

            if (imageChanges == null)
            {
                return(false);
            }

            var addedBinaryFileIds   = imageChanges?.NewFileIds?.Except(imageChanges.OldFileIds)?.ToList();
            var removedBinaryFileIds = imageChanges?.OldFileIds?.Except(imageChanges.NewFileIds)?.ToList();

            bool needSave          = false;
            var  binaryFileService = new BinaryFileService(rockContext);

            // If there are any newly added binary files then mark them as
            // permanent so they will persist in the database.
            if (addedBinaryFileIds != null && addedBinaryFileIds.Count > 0)
            {
                var filesToAdd = binaryFileService.Queryable()
                                 .Where(b => addedBinaryFileIds.Contains(b.Id))
                                 .ToList();

                filesToAdd.ForEach(b => b.IsTemporary = false);
                needSave = true;
            }

            // If there are any removed binary files then mark them as temporary
            // so they will be later removed by the cleanup job.
            if (removedBinaryFileIds != null && removedBinaryFileIds.Count > 0)
            {
                var filesToRemove = binaryFileService.Queryable()
                                    .Where(b => removedBinaryFileIds.Contains(b.Id))
                                    .ToList();

                filesToRemove.ForEach(b => b.IsTemporary = true);
                needSave = true;
            }

            return(needSave);
        }
        /// <inheritdoc/>
        public override string GetHtmlValue(string value, Dictionary <string, string> configurationValues)
        {
            var helper = new StructuredContentHelper(value);

            return(helper.Render());
        }