/// <summary>
        /// Handles the DeleteClick event of the gContentChannelItems control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs"/> instance containing the event data.</param>
        protected void gContentChannelItems_DeleteClick(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            var rockContext                   = new RockContext();
            var contentItemService            = new ContentChannelItemService(rockContext);
            var contentItemAssociationService = new ContentChannelItemAssociationService(rockContext);
            var contentItemSlugService        = new ContentChannelItemSlugService(rockContext);

            ContentChannelItem contentItem = contentItemService.Get(e.RowKeyId);

            if (contentItem != null)
            {
                string errorMessage;
                if (!contentItemService.CanDelete(contentItem, out errorMessage))
                {
                    mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                rockContext.WrapTransaction(() =>
                {
                    contentItemAssociationService.DeleteRange(contentItem.ChildItems);
                    contentItemAssociationService.DeleteRange(contentItem.ParentItems);
                    contentItemService.Delete(contentItem);
                    rockContext.SaveChanges();
                });
            }

            BindContentChannelItemsGrid();

            // edit whatever the first item is, or create a new one
            var contentChannel       = GetContentChannel();
            var contentChannelItemId = new ContentChannelItemService(rockContext).Queryable().Where(a => a.ContentChannelId == contentChannel.Id).OrderBy(a => a.Order).ThenBy(a => a.Title).Select(a => ( int? )a.Id).FirstOrDefault();

            EditContentChannelItem(contentChannelItemId);
        }
Пример #2
0
        public override MobileBlock GetMobile(string parameter)
        {
            RockContext rockContext = new RockContext();
            ContentChannelItemService contentChannelItemService = new ContentChannelItemService(rockContext);
            var item = contentChannelItemService.Get(parameter.AsInteger());

            if (item != null && item.IsAuthorized("View", CurrentPerson) &&
                (string.IsNullOrWhiteSpace(GetAttributeValue("ContentChannel")) || item.ContentChannel.Guid == GetAttributeValue("ContentChannel").AsGuid()))
            {
                CustomAttributes["Success"]  = "true";
                CustomAttributes["Title"]    = ProcessLava(GetAttributeValue("TitleLava"), item);
                CustomAttributes["Markdown"] = ProcessLava(GetAttributeValue("MarkdownLava"), item);
                CustomAttributes["Image"]    = ProcessLava(GetAttributeValue("ImageLava"), item);
            }
            else
            {
                CustomAttributes["Success"] = "false";
            }

            return(new MobileBlock()
            {
                BlockType = "Avalanche.Blocks.MobileContentItem",
                Attributes = CustomAttributes
            });
        }
Пример #3
0
        /// <summary>
        /// Handles the Click event of the deleteField control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        void gContentChannelItems_Delete(object sender, RowEventArgs e)
        {
            var rockContext                   = new RockContext();
            var contentItemService            = new ContentChannelItemService(rockContext);
            var contentItemAssociationService = new ContentChannelItemAssociationService(rockContext);
            var contentItemSlugService        = new ContentChannelItemSlugService(rockContext);

            var contentItem = contentItemService.Get(e.RowKeyId);

            if (contentItem != null)
            {
                string errorMessage;
                if (!contentItemService.CanDelete(contentItem, out errorMessage))
                {
                    mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                rockContext.WrapTransaction(() =>
                {
                    contentItemAssociationService.DeleteRange(contentItem.ChildItems);
                    contentItemAssociationService.DeleteRange(contentItem.ParentItems);
                    contentItemService.Delete(contentItem);
                    rockContext.SaveChanges();
                });
            }

            GetData();
        }
Пример #4
0
        public object GetStructuredContent(Guid itemGuid)
        {
            using (var rockContext = new RockContext())
            {
                var contentChannelItemService = new ContentChannelItemService(rockContext);
                var noteTypeId = NoteTypeCache.Get(SystemGuid.NoteType.CONTENT_CHANNEL_ITEM_STRUCTURED_CONTENT_USER_VALUE).Id;

                var item = contentChannelItemService.Get(itemGuid);
                Dictionary <string, string> userValues = null;

                if (item != null && RequestContext.CurrentPerson != null)
                {
                    var noteService = new NoteService(rockContext);
                    var note        = noteService.Queryable()
                                      .AsNoTracking()
                                      .Where(a => a.NoteTypeId == noteTypeId && a.EntityId == item.Id)
                                      .Where(a => a.CreatedByPersonAliasId.HasValue && a.CreatedByPersonAlias.PersonId == RequestContext.CurrentPerson.Id)
                                      .FirstOrDefault();

                    if (note != null)
                    {
                        userValues = note.Text.FromJsonOrNull <Dictionary <string, string> >();
                    }
                }

                return(new
                {
                    DocumentJson = item?.StructuredContent,
                    UserValues = userValues
                });
            }
        }
Пример #5
0
        public object SaveUserValues(Guid itemGuid, Dictionary <string, string> userValues)
        {
            if (RequestContext.CurrentPerson == null)
            {
                return(ActionStatusCode(System.Net.HttpStatusCode.Unauthorized));
            }

            using (var rockContext = new RockContext())
            {
                var contentChannelItemService = new ContentChannelItemService(rockContext);
                var noteService = new NoteService(rockContext);
                var noteTypeId  = NoteTypeCache.Get(SystemGuid.NoteType.CONTENT_CHANNEL_ITEM_STRUCTURED_CONTENT_USER_VALUE).Id;

                var item = contentChannelItemService.Get(itemGuid);

                if (item == null)
                {
                    return(ActionNotFound());
                }

                var note = noteService.Queryable()
                           .Where(a => a.NoteTypeId == noteTypeId && a.EntityId == item.Id)
                           .Where(a => a.CreatedByPersonAliasId.HasValue && a.CreatedByPersonAlias.PersonId == RequestContext.CurrentPerson.Id)
                           .FirstOrDefault();

                if (note == null)
                {
                    note = new Note
                    {
                        NoteTypeId = noteTypeId,
                        EntityId   = item.Id
                    };

                    noteService.Add(note);
                }

                note.Text = userValues.ToJson();

                rockContext.SaveChanges();
            }

            return(ActionOk());
        }
Пример #6
0
        protected void lbDeleteChildItem_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                int childItemId = hfRemoveChildItem.ValueAsInt();

                var service   = new ContentChannelItemService(rockContext);
                var childItem = service.Get(childItemId);
                if (childItem != null)
                {
                    service.Delete(childItem);
                    rockContext.SaveChanges();
                }
            }

            BindChildItemsGrid(GetContentItem());

            HideDialog();
        }
        /// <summary>
        /// Saves the content channel item.
        /// </summary>
        private void SaveContentChannelItem()
        {
            RockContext rockContext = new RockContext();
            ContentChannelItemService contentChannelItemService = new ContentChannelItemService(rockContext);
            ContentChannelItem        contentChannelItem        = null;
            int contentChannelItemId = hfContentChannelItemId.Value.AsInteger();

            if (contentChannelItemId != 0)
            {
                contentChannelItem = contentChannelItemService.Get(contentChannelItemId);
            }

            if (contentChannelItem == null)
            {
                ContentChannelCache contentChannel = this.GetContentChannel();

                contentChannelItem = new ContentChannelItem();
                contentChannelItem.ContentChannelTypeId = contentChannel.ContentChannelTypeId;
                contentChannelItem.ContentChannelId     = contentChannel.Id;
                contentChannelItem.Order = (contentChannelItemService.Queryable().Where(a => a.ContentChannelId == contentChannel.Id).Max(a => ( int? )a.Order) ?? 0) + 1;
                contentChannelItemService.Add(contentChannelItem);
            }

            contentChannelItem.LoadAttributes(rockContext);
            avcContentChannelItemAttributes.GetEditValues(contentChannelItem);

            contentChannelItem.Title   = tbContentChannelItemTitle.Text;
            contentChannelItem.Content = htmlContentChannelItemContent.Text;

            rockContext.SaveChanges();

            // just in case this is a new contentChannelItem, set the hfContentChannelItemId to the Id after SaveChanges.
            hfContentChannelItemId.Value = contentChannelItem.Id.ToString();

            contentChannelItem.SaveAttributeValues(rockContext);

            RemoveCacheItem(OUTPUT_CACHE_KEY);
            RemoveCacheItem(ITEM_CACHE_KEY);

            BindContentChannelItemsGrid();
        }
Пример #8
0
        /// <summary>
        /// Handles the Delete event of the gItems 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 gItems_Delete(object sender, RowEventArgs e)
        {
            var rockContext = new RockContext();
            ContentChannelItemService contentItemService = new ContentChannelItemService(rockContext);

            ContentChannelItem contentItem = contentItemService.Get(e.RowKeyId);

            if (contentItem != null)
            {
                string errorMessage;
                if (!contentItemService.CanDelete(contentItem, out errorMessage))
                {
                    mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                contentItemService.Delete(contentItem);
                rockContext.SaveChanges();
            }

            BindGrids();
        }
Пример #9
0
    public TheDailyItem GetDailyItem(int id)
    {
        var rockContext = new RockContext();

        var contentItemService = new ContentChannelItemService(rockContext);

        ContentChannelItem i = null;

        var attrService = new AttributeService(rockContext);

        var dailyItem = new  TheDailyItem();

        i = contentItemService.Get(id);

        if (i != null)
        {
            i.LoadAttributes();

            i.Content = DotLiquid.StandardFilters.StripHtml(i.Content).Replace("\n\n", "\r\n\r\n");

            var attributes = i.Attributes;

            var pdfAttr = i.Attributes["PDF"];

            var binaryFile = new BinaryFileService(new RockContext()).Get(pdfAttr.Id);
            var pdfUrl = binaryFile.Url;

            var scriptureAttr = i.Attributes["ScriptureCards"];

            binaryFile = new BinaryFileService(new RockContext()).Get(scriptureAttr.Id);
            var scriptureUrl = binaryFile.Url;

            dailyItem  = (new TheDailyItem { Id = i.Id, Title = i.Title, Content = i.Content, DailyPDF = pdfUrl, ScriptiureCards = scriptureUrl });

        }

        return dailyItem;
    }
        /// <summary>
        /// Handles the Click event of the deleteField control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        void gContentChannelItems_Delete( object sender, RowEventArgs e )
        {
            var rockContext = new RockContext();
            var contentItemService = new ContentChannelItemService( rockContext );
            var contentItem = contentItemService.Get( e.RowKeyId );
            if ( contentItem != null )
            {
                string errorMessage;
                if ( !contentItemService.CanDelete( contentItem, out errorMessage ) )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                    return;
                }

                contentItemService.Delete( contentItem );
                rockContext.SaveChanges();
            }

            GetData();
        }
Пример #11
0
        /// <summary>
        /// Handles the Click event of the lbSave 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 lbSave_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();

            rockContext.WrapTransaction(() =>
            {
                if (contextEntity is Person)
                {
                    var personService = new PersonService(rockContext);
                    var changes       = new History.HistoryChangeList();
                    var _person       = personService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _person.ForeignKey, tbForeignKey.Text);
                    _person.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _person.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _person.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _person.ForeignId.ToString(), tbForeignId.Text);
                    _person.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(Person),
                                Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(),
                                _person.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is FinancialAccount)
                {
                    var accountService = new FinancialAccountService(rockContext);
                    var _account       = accountService.Get(contextEntity.Id);

                    _account.ForeignKey  = tbForeignKey.Text;
                    _account.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _account.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is FinancialBatch)
                {
                    var batchService = new FinancialBatchService(rockContext);
                    var changes      = new History.HistoryChangeList();
                    var _batch       = batchService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _batch.ForeignKey, tbForeignKey.Text);
                    _batch.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _batch.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _batch.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _batch.ForeignId.ToString(), tbForeignId.Text);
                    _batch.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(FinancialBatch),
                                Rock.SystemGuid.Category.HISTORY_FINANCIAL_BATCH.AsGuid(),
                                _batch.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is FinancialPledge)
                {
                    var pledgeService = new FinancialPledgeService(rockContext);
                    var _pledge       = pledgeService.Get(contextEntity.Id);

                    _pledge.ForeignKey  = tbForeignKey.Text;
                    _pledge.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _pledge.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is FinancialTransaction)
                {
                    var transactionService = new FinancialTransactionService(rockContext);
                    var changes            = new History.HistoryChangeList();
                    var _transaction       = transactionService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _transaction.ForeignKey, tbForeignKey.Text);
                    _transaction.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _transaction.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _transaction.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _transaction.ForeignId.ToString(), tbForeignId.Text);
                    _transaction.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(FinancialTransaction),
                                Rock.SystemGuid.Category.HISTORY_FINANCIAL_TRANSACTION.AsGuid(),
                                _transaction.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is FinancialScheduledTransaction)
                {
                    var transactionScheduledService = new FinancialScheduledTransactionService(rockContext);
                    var _scheduledTransaction       = transactionScheduledService.Get(contextEntity.Id);

                    _scheduledTransaction.ForeignKey  = tbForeignKey.Text;
                    _scheduledTransaction.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _scheduledTransaction.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is Group)
                {
                    var groupService = new GroupService(rockContext);
                    var _group       = groupService.Get(contextEntity.Id);

                    _group.ForeignKey  = tbForeignKey.Text;
                    _group.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _group.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is GroupMember)
                {
                    var groupMemberService = new GroupMemberService(rockContext);
                    var changes            = new History.HistoryChangeList();
                    var _groupMember       = groupMemberService.Get(contextEntity.Id);

                    History.EvaluateChange(changes, "Foreign Key", _groupMember.ForeignKey, tbForeignKey.Text);
                    _groupMember.ForeignKey = tbForeignKey.Text;

                    History.EvaluateChange(changes, "Foreign Guid", _groupMember.ForeignGuid.ToString(), tbForeignGuid.Text);
                    _groupMember.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();

                    History.EvaluateChange(changes, "Foreign Id", _groupMember.ForeignId.ToString(), tbForeignId.Text);
                    _groupMember.ForeignId = tbForeignId.Text.AsType <int?>();

                    if (rockContext.SaveChanges() > 0)
                    {
                        if (changes.Any())
                        {
                            HistoryService.SaveChanges(
                                rockContext,
                                typeof(GroupMember),
                                Rock.SystemGuid.Category.HISTORY_PERSON_GROUP_MEMBERSHIP.AsGuid(),
                                _groupMember.Id,
                                changes);
                        }
                    }
                }
                else if (contextEntity is Metric)
                {
                    var metricService = new MetricService(rockContext);
                    var _metric       = metricService.Get(contextEntity.Id);

                    _metric.ForeignKey  = tbForeignKey.Text;
                    _metric.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _metric.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is Location)
                {
                    var locationService = new LocationService(rockContext);
                    var _location       = locationService.Get(contextEntity.Id);

                    _location.ForeignKey  = tbForeignKey.Text;
                    _location.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _location.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is PrayerRequest)
                {
                    var prayerRequestService = new PrayerRequestService(rockContext);
                    var _request             = prayerRequestService.Get(contextEntity.Id);

                    _request.ForeignKey  = tbForeignKey.Text;
                    _request.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _request.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is ContentChannel)
                {
                    var contentChannelService = new ContentChannelService(rockContext);
                    var _channel = contentChannelService.Get(contextEntity.Id);

                    _channel.ForeignKey  = tbForeignKey.Text;
                    _channel.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _channel.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
                else if (contextEntity is ContentChannelItem)
                {
                    var contentChannelItemService = new ContentChannelItemService(rockContext);
                    var _item = contentChannelItemService.Get(contextEntity.Id);

                    _item.ForeignKey  = tbForeignKey.Text;
                    _item.ForeignGuid = tbForeignGuid.Text.AsType <Guid?>();
                    _item.ForeignId   = tbForeignId.Text.AsType <int?>();

                    rockContext.SaveChanges();
                }
            });

            Page.Response.Redirect(Page.Request.Url.ToString(), true);
        }
        /// <summary>
        /// Handles the Delete event of the gItems 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 gItems_Delete( object sender, RowEventArgs e )
        {
            var rockContext = new RockContext();
            var contentItemService = new ContentChannelItemService( rockContext );
            var contentItemAssociationService = new ContentChannelItemAssociationService( rockContext );

            ContentChannelItem contentItem = contentItemService.Get( e.RowKeyId );

            if ( contentItem != null )
            {
                string errorMessage;
                if ( !contentItemService.CanDelete( contentItem, out errorMessage ) )
                {
                    mdGridWarning.Show( errorMessage, ModalAlertType.Information );
                    return;
                }

                rockContext.WrapTransaction( () =>
                {
                    contentItemAssociationService.DeleteRange( contentItem.ChildItems );
                    contentItemAssociationService.DeleteRange( contentItem.ParentItems );
                    contentItemService.Delete( contentItem );
                    rockContext.SaveChanges();
                } );
            }

            BindGrid();
        }
Пример #13
0
        /// <summary>
        /// Gets the type of the content.
        /// </summary>
        /// <param name="contentItemId">The content type identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        private ContentChannelItem GetContentItem(RockContext rockContext = null)
        {
            rockContext = rockContext ?? new RockContext();
            var contentItemService         = new ContentChannelItemService(rockContext);
            ContentChannelItem contentItem = null;

            int contentItemId = hfId.Value.AsInteger();

            if (contentItemId != 0)
            {
                contentItem = contentItemService
                              .Queryable("ContentChannel,ContentChannelType")
                              .FirstOrDefault(t => t.Id == contentItemId);
            }

            if (contentItem == null)
            {
                var contentChannel = new ContentChannelService(rockContext).Get(hfChannelId.Value.AsInteger());
                if (contentChannel != null)
                {
                    contentItem = new ContentChannelItem
                    {
                        ContentChannel       = contentChannel,
                        ContentChannelId     = contentChannel.Id,
                        ContentChannelType   = contentChannel.ContentChannelType,
                        ContentChannelTypeId = contentChannel.ContentChannelType.Id,
                        StartDateTime        = RockDateTime.Now
                    };

                    var hierarchy = GetNavHierarchy();
                    if (hierarchy.Any())
                    {
                        var parentItem = contentItemService.Get(hierarchy.Last().AsInteger());
                        if (parentItem != null &&
                            parentItem.IsAuthorized(Authorization.EDIT, CurrentPerson) &&
                            parentItem.ContentChannel.ChildContentChannels.Any(c => c.Id == contentChannel.Id))
                        {
                            var order = parentItem.ChildItems
                                        .Select(a => (int?)a.Order)
                                        .DefaultIfEmpty()
                                        .Max();

                            var assoc = new ContentChannelItemAssociation();
                            assoc.ContentChannelItemId = parentItem.Id;
                            assoc.Order = order.HasValue ? order.Value + 1 : 0;
                            contentItem.ParentItems.Add(assoc);
                        }
                    }

                    if (contentChannel.RequiresApproval)
                    {
                        contentItem.Status = ContentChannelItemStatus.PendingApproval;
                    }
                    else
                    {
                        contentItem.Status                  = ContentChannelItemStatus.Approved;
                        contentItem.ApprovedDateTime        = RockDateTime.Now;
                        contentItem.ApprovedByPersonAliasId = CurrentPersonAliasId;
                    }

                    contentItemService.Add(contentItem);
                }
            }

            return(contentItem);
        }
        /// <summary>
        /// Gets the type of the content.
        /// </summary>
        /// <param name="contentItemId">The content type identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        private ContentChannelItem GetContentItem( RockContext rockContext = null )
        {
            rockContext = rockContext ?? new RockContext();
            var contentItemService = new ContentChannelItemService( rockContext );
            ContentChannelItem contentItem = null;

            int contentItemId = hfId.Value.AsInteger();
            if ( contentItemId != 0 )
            {
                contentItem = contentItemService
                    .Queryable( "ContentChannel,ContentChannelType" )
                    .FirstOrDefault( t => t.Id == contentItemId );
            }

            if ( contentItem == null)
            {
                var contentChannel = new ContentChannelService( rockContext ).Get( hfChannelId.Value.AsInteger() );
                if ( contentChannel != null )
                {
                    contentItem = new ContentChannelItem
                    {
                        ContentChannel = contentChannel,
                        ContentChannelId = contentChannel.Id,
                        ContentChannelType = contentChannel.ContentChannelType,
                        ContentChannelTypeId = contentChannel.ContentChannelType.Id,
                        StartDateTime = RockDateTime.Now
                    };

                    var hierarchy = GetNavHierarchy();
                    if ( hierarchy.Any() )
                    {
                        var parentItem = contentItemService.Get( hierarchy.Last().AsInteger() );
                        if ( parentItem != null &&
                            parentItem.IsAuthorized( Authorization.EDIT, CurrentPerson ) &&
                            parentItem.ContentChannel.ChildContentChannels.Any( c => c.Id == contentChannel.Id ) )
                        {
                            var order = parentItem.ChildItems
                                .Select( a => (int?)a.Order )
                                .DefaultIfEmpty()
                                .Max();

                            var assoc = new ContentChannelItemAssociation();
                            assoc.ContentChannelItemId = parentItem.Id;
                            assoc.Order = order.HasValue ? order.Value + 1 : 0;
                            contentItem.ParentItems.Add( assoc );
                        }
                    }

                    if ( contentChannel.RequiresApproval )
                    {
                        contentItem.Status = ContentChannelItemStatus.PendingApproval;
                    }
                    else
                    {
                        contentItem.Status = ContentChannelItemStatus.Approved;
                        contentItem.ApprovedDateTime = RockDateTime.Now;
                        contentItem.ApprovedByPersonAliasId = CurrentPersonAliasId;
                    }

                    contentItemService.Add( contentItem );
                }
            }

            return contentItem;
        }
        protected void lbDeleteChildItem_Click( object sender, EventArgs e )
        {
            using ( var rockContext = new RockContext() )
            {
                int childItemId = hfRemoveChildItem.ValueAsInt();

                var service = new ContentChannelItemService( rockContext );
                var childItem = service.Get( childItemId );
                if ( childItem != null )
                {
                    service.Delete( childItem );
                    rockContext.SaveChanges();
                }
            }

            BindChildItemsGrid( GetContentItem() );

            HideDialog();
        }