/// <summary> /// Queues ContentChannelItems of this ContentChannel to have their indexes deleted /// </summary> /// <param name="contentChannelId">The content channel identifier.</param> public void DeleteIndexedDocumentsByContentChannel(int contentChannelId) { var contentChannelItemIds = new ContentChannelItemService(new RockContext()).Queryable() .Where(i => i.ContentChannelId == contentChannelId).Select(a => a.Id).ToList(); int contentChannelItemEntityTypeId = EntityTypeCache.GetId <Rock.Model.ContentChannelItem>().Value; foreach (var contentChannelItemId in contentChannelItemIds) { var deleteEntityTypeIndexMsg = new DeleteEntityTypeIndex.Message { EntityTypeId = contentChannelItemEntityTypeId, EntityId = contentChannelItemId }; deleteEntityTypeIndexMsg.Send(); } }
/// <summary> /// Bulks the index documents by content channel. /// </summary> /// <param name="contentChannelId">The content channel identifier.</param> public void BulkIndexDocumentsByContentChannel(int contentChannelId) { List <ContentChannelItemIndex> indexableChannelItems = new List <ContentChannelItemIndex>(); // return all approved content channel items that are in content channels that should be indexed RockContext rockContext = new RockContext(); var contentChannelItems = new ContentChannelItemService(rockContext).Queryable() .Where(i => i.ContentChannelId == contentChannelId && (i.ContentChannel.RequiresApproval == false || i.ContentChannel.ContentChannelType.DisableStatus || i.Status == ContentChannelItemStatus.Approved)); foreach (var item in contentChannelItems) { var indexableChannelItem = ContentChannelItemIndex.LoadByModel(item); indexableChannelItems.Add(indexableChannelItem); } IndexContainer.IndexDocuments(indexableChannelItems); }
/// <summary> /// Queues ContentChannelItems of this ContentChannel to have their indexes updated /// </summary> /// <param name="contentChannelId">The content channel identifier.</param> public void BulkIndexDocumentsByContentChannel(int contentChannelId) { // return all approved content channel items that are in content channels that should be indexed var contentChannelItemIds = new ContentChannelItemService(new RockContext()).Queryable() .Where(i => i.ContentChannelId == contentChannelId && (i.ContentChannel.RequiresApproval == false || i.ContentChannel.ContentChannelType.DisableStatus || i.Status == ContentChannelItemStatus.Approved)) .Select(a => a.Id).ToList(); int contentChannelItemEntityTypeId = EntityTypeCache.GetId <Rock.Model.ContentChannelItem>().Value; foreach (var contentChannelItemId in contentChannelItemIds) { var transaction = new IndexEntityTransaction { EntityId = contentChannelItemId, EntityTypeId = contentChannelItemEntityTypeId }; transaction.Enqueue(); } }
/// <summary> /// Adds the synced content channel item for the media element by /// the given identifier. /// </summary> /// <param name="mediaElement">The media element.</param> /// <param name="rockContext">The <see cref="RockContext"/> to perform the save in.</param> /// <remarks>Must be called on a MediaElement that has a valid <see cref="MediaElement.MediaFolder"/> property.</remarks> private static void AddSyncedContentChannelItem(MediaElement mediaElement, RockContext rockContext) { try { // Freak accident, either we are in a transaction or the // media element got deleted before we started executing. if (mediaElement == null || mediaElement.MediaFolder == null) { return; } // No sync, just exit. if (!mediaElement.MediaFolder.IsContentChannelSyncEnabled) { return; } var contentChannelTypeId = mediaElement.MediaFolder.ContentChannel?.ContentChannelTypeId; var contentChannelId = mediaElement.MediaFolder.ContentChannelId; var attributeId = mediaElement.MediaFolder.ContentChannelAttributeId; var status = mediaElement.MediaFolder.ContentChannelItemStatus; // Missing required information. if (!contentChannelTypeId.HasValue || !contentChannelId.HasValue || !attributeId.HasValue || !status.HasValue) { return; } var attribute = AttributeCache.Get(attributeId.Value); // Shouldn't happen, but make sure we don't hit a NRE later. if (attribute == null) { return; } var contentChannelItemService = new ContentChannelItemService(rockContext); var contentChannelItem = new ContentChannelItem(); contentChannelItemService.Add(contentChannelItem); contentChannelItem.ContentChannelId = contentChannelId.Value; contentChannelItem.ContentChannelTypeId = contentChannelTypeId.Value; contentChannelItem.Title = mediaElement.Name; contentChannelItem.Content = mediaElement.Description; contentChannelItem.StartDateTime = mediaElement.SourceCreatedDateTime ?? mediaElement.CreatedDateTime ?? RockDateTime.Now; contentChannelItem.Status = status.Value; // We want both the content channel item and it's attributes // to be saved either together or not at all. rockContext.WrapTransaction(() => { rockContext.SaveChanges(); // 2.5x faster than LoadAttributes/SaveAttributeValues. Rock.Attribute.Helper.SaveAttributeValue(contentChannelItem, attribute, mediaElement.Guid.ToString(), rockContext); }); } catch (Exception ex) { var exception = new Exception("Error while creating synced content channel item for media element.", ex); ExceptionLogService.LogException(exception); } }
/// <summary> /// Gets the unique slug /// </summary> /// <param name="slug">The slug.</param> /// <param name="contentChannelItemSlugId">The content channel item slug identifier.</param> /// <param name="contentChannelItemId">The content channel item identifier.</param> /// <returns></returns> public string GetUniqueContentSlug(string slug, int?contentChannelItemSlugId, int?contentChannelItemId) { // Slug must be unique within a content channel but may be duplicated across content channels // Get ContentChannel.Id int?contentChannelId = null; if (contentChannelItemId != null) { var contentChannelItem = new ContentChannelItemService(( RockContext )this.Context).Get(contentChannelItemId.Value); contentChannelId = contentChannelItem.ContentChannelId; } bool isValid = false; slug = MakeSlugValid(slug); // If MakeSlugValid removes all the characters then just return null. if (slug.IsNullOrWhiteSpace()) { return(null); } int intialSlugLength = slug.Length; int paddedNumber = 0; do { string customSlug = slug; if (paddedNumber > 0) { string paddedString = string.Format("-{0}", paddedNumber); if (intialSlugLength + paddedString.Length > 75) { customSlug = slug.Left(intialSlugLength + paddedString.Length - 75) + paddedString; } else { customSlug = slug + paddedString; } } var qry = this .Queryable() .Where(b => ((contentChannelItemSlugId.HasValue && b.Id != contentChannelItemSlugId.Value) || !contentChannelItemSlugId.HasValue)) .Where(b => b.Slug == customSlug); if (contentChannelId != null) { qry = qry.Where(b => b.ContentChannelItem.ContentChannelId == contentChannelId); } isValid = !qry.Any(); if (!isValid) { paddedNumber += 1; } else { slug = customSlug; } } while (!isValid); return(slug); }