/// <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(); ContentChannelItem contentItem = GetContentItem(rockContext); if (contentItem != null && contentItem.IsAuthorized(Authorization.EDIT, CurrentPerson)) { contentItem.Title = tbTitle.Text; contentItem.Content = contentItem.ContentChannel.ContentControlType == ContentControlType.HtmlEditor ? htmlContent.Text : ceContent.Text; contentItem.Priority = nbPriority.Text.AsInteger(); contentItem.StartDateTime = dtpStart.SelectedDateTime ?? RockDateTime.Now; contentItem.ExpireDateTime = (contentItem.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange) ? dtpExpire.SelectedDateTime : null; int newStatusID = hfStatus.Value.AsIntegerOrNull() ?? contentItem.Status.ConvertToInt(); int oldStatusId = contentItem.Status.ConvertToInt(); if (newStatusID != oldStatusId && contentItem.IsAuthorized(Authorization.APPROVE, CurrentPerson)) { contentItem.Status = hfStatus.Value.ConvertToEnum <ContentChannelItemStatus>(ContentChannelItemStatus.PendingApproval); if (contentItem.Status == ContentChannelItemStatus.PendingApproval) { contentItem.ApprovedDateTime = null; contentItem.ApprovedByPersonAliasId = null; } else { contentItem.ApprovedDateTime = RockDateTime.Now; contentItem.ApprovedByPersonAliasId = CurrentPersonAliasId; } } contentItem.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributes, contentItem); if (!Page.IsValid || !contentItem.IsValid) { // Controls will render the error messages return; } rockContext.WrapTransaction(() => { rockContext.SaveChanges(); contentItem.SaveAttributeValues(rockContext); }); ReturnToParentPage(); } }
protected void lbPersonPaging_Click(object sender, EventArgs e) { var rockContext = new RockContext(); var personService = new PersonService(rockContext); var person = personService.Queryable().FirstOrDefault(a => a.Guid == personGuid); var attributeKey = GetAttributeValue("PersonAttributeKey"); var contentChannel = new ContentChannelService(rockContext).Get(GetAttributeValue("PersonPagingContentChannel").AsGuid()); var contentChannelItem = new ContentChannelItem { Title = string.Empty, Status = ContentChannelItemStatus.Approved, Content = string.Empty, ContentChannelId = contentChannel.Id, ContentChannelTypeId = contentChannel.ContentChannelTypeId, Priority = 0, StartDateTime = DateTime.Now, Order = 0, Guid = new Guid() }; contentChannelItem.LoadAttributes(); if (!contentChannelItem.Attributes.ContainsKey(attributeKey)) { nbWarning.Text = "The selected Content Channel is not configured with provided Attribute Key."; nbWarning.Visible = true; } else { var contentChannelItems = new ContentChannelItemService(rockContext).Queryable().AsNoTracking().Where(i => i.ContentChannelId.Equals(contentChannel.Id)).ToList(); var exists = false; foreach (var item in contentChannelItems) { item.LoadAttributes(); if (item.AttributeValues[attributeKey].Value.Equals(person.PrimaryAlias.Guid.ToString())) { exists = true; } } if (exists) { nbWarning.Text = string.Format("{0} is already on the list.", person.FullName); nbWarning.Visible = true; } else { contentChannelItem.AttributeValues[attributeKey].Value = person.PrimaryAlias.Guid.ToString(); rockContext.WrapTransaction(() => { rockContext.ContentChannelItems.Add(contentChannelItem); rockContext.SaveChanges(); contentChannelItem.SaveAttributeValues(rockContext); }); nbWarning.Text = string.Format("{0} added to the list.", person.FullName); nbWarning.Visible = true; } } }
/// <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(); ContentChannelItem contentItem = GetContentItem(rockContext); if (contentItem != null && contentItem.IsAuthorized(Authorization.EDIT, CurrentPerson)) { contentItem.Title = tbTitle.Text; contentItem.Content = contentItem.ContentChannel.ContentControlType == ContentControlType.HtmlEditor ? htmlContent.Text : ceContent.Text; contentItem.Priority = nbPriority.Text.AsInteger(); if (contentItem.ContentChannelType.IncludeTime) { contentItem.StartDateTime = dtpStart.SelectedDateTime ?? RockDateTime.Now; contentItem.ExpireDateTime = (contentItem.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange) ? dtpExpire.SelectedDateTime : null; } else { contentItem.StartDateTime = dpStart.SelectedDate ?? RockDateTime.Today; contentItem.ExpireDateTime = (contentItem.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange) ? dpExpire.SelectedDate : null; } int newStatusID = hfStatus.Value.AsIntegerOrNull() ?? contentItem.Status.ConvertToInt(); int oldStatusId = contentItem.Status.ConvertToInt(); if (newStatusID != oldStatusId && contentItem.IsAuthorized(Authorization.APPROVE, CurrentPerson)) { contentItem.Status = hfStatus.Value.ConvertToEnum <ContentChannelItemStatus>(ContentChannelItemStatus.PendingApproval); if (contentItem.Status == ContentChannelItemStatus.PendingApproval) { contentItem.ApprovedDateTime = null; contentItem.ApprovedByPersonAliasId = null; } else { contentItem.ApprovedDateTime = RockDateTime.Now; contentItem.ApprovedByPersonAliasId = CurrentPersonAliasId; } } contentItem.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributes, contentItem); if (!Page.IsValid || !contentItem.IsValid) { // Controls will render the error messages return; } rockContext.WrapTransaction(() => { rockContext.SaveChanges(); contentItem.SaveAttributeValues(rockContext); int?eventItemOccurrenceId = PageParameter("EventItemOccurrenceId").AsIntegerOrNull(); if (eventItemOccurrenceId.HasValue) { var occurrenceChannelItemService = new EventItemOccurrenceChannelItemService(rockContext); var occurrenceChannelItem = occurrenceChannelItemService .Queryable() .Where(c => c.ContentChannelItemId == contentItem.Id && c.EventItemOccurrenceId == eventItemOccurrenceId.Value) .FirstOrDefault(); if (occurrenceChannelItem == null) { occurrenceChannelItem = new EventItemOccurrenceChannelItem(); occurrenceChannelItem.ContentChannelItemId = contentItem.Id; occurrenceChannelItem.EventItemOccurrenceId = eventItemOccurrenceId.Value; occurrenceChannelItemService.Add(occurrenceChannelItem); rockContext.SaveChanges(); } } }); ReturnToParentPage(); } }
private int SyncVimeo(RockContext rockContext) { ContentChannelItem contentItem = GetContentItem(rockContext); if (contentItem != null) { if (contentItem.Attributes == null) { contentItem.LoadAttributes(); } long videoId = _vimeoId; if (_vimeoId == 0) { videoId = this.tbVimeoId.Text.AsInteger(); } if (contentItem.AttributeValues.ContainsKey(_vimeoIdKey)) { contentItem.AttributeValues[_vimeoIdKey].Value = videoId.ToString().AsInteger().ToString(); } else { contentItem.SetAttributeValue(_durationAttributeKey, videoId.ToString().AsInteger()); } var client = new VimeoClient(_accessToken); var vimeo = new Video(); var width = GetAttributeValue("ImageWidth").AsInteger(); var video = vimeo.GetVideoInfo(client, videoId, width); var cbName = cblSyncOptions.Items.FindByValue("Name"); if (cbName != null && cbName.Selected == true) { contentItem.Title = video.name; } var cbDescription = cblSyncOptions.Items.FindByValue("Description"); if (cbDescription != null && cbDescription.Selected == true) { contentItem.Content = video.description; } var cbImage = cblSyncOptions.Items.FindByValue("Image"); if (cbImage != null && cbImage.Selected == true) { if (contentItem.AttributeValues.ContainsKey(_imageAttributeKey)) { contentItem.AttributeValues[_imageAttributeKey].Value = video.imageUrl; } else { contentItem.SetAttributeValue(_imageAttributeKey, video.imageUrl); } } var cbDuration = cblSyncOptions.Items.FindByValue("Duration"); if (cbDuration != null && cbDuration.Selected == true) { if (contentItem.AttributeValues.ContainsKey(_durationAttributeKey)) { contentItem.AttributeValues[_durationAttributeKey].Value = video.duration.ToString(); } else { contentItem.SetAttributeValue(_durationAttributeKey, video.duration.ToString()); } } var cbHDVideo = cblSyncOptions.Items.FindByValue("HD Video"); if (cbHDVideo != null && cbHDVideo.Selected == true && !string.IsNullOrWhiteSpace(video.hdLink)) { if (contentItem.AttributeValues.ContainsKey(_hdVideoAttributeKey)) { contentItem.AttributeValues[_hdVideoAttributeKey].Value = video.hdLink; } else { contentItem.SetAttributeValue(_hdVideoAttributeKey, video.hdLink); } } var cbSDVideo = cblSyncOptions.Items.FindByValue("SD Video"); if (cbSDVideo != null && cbSDVideo.Selected == true && !string.IsNullOrWhiteSpace(video.sdLink)) { if (contentItem.AttributeValues.ContainsKey(_sdVideoAttributeKey)) { contentItem.AttributeValues[_sdVideoAttributeKey].Value = video.sdLink; } else { contentItem.SetAttributeValue(_sdVideoAttributeKey, video.sdLink); } } var cbHLSVideo = cblSyncOptions.Items.FindByValue("HLS Video"); if (cbHLSVideo != null && cbHLSVideo.Selected == true && !string.IsNullOrWhiteSpace(video.hlsLink)) { if (contentItem.AttributeValues.ContainsKey(_hlsVideoAttributeKey)) { contentItem.AttributeValues[_hlsVideoAttributeKey].Value = video.hlsLink; } else { contentItem.SetAttributeValue(_hlsVideoAttributeKey, video.hlsLink); } } // Save Everything rockContext.WrapTransaction(() => { rockContext.SaveChanges(); contentItem.SaveAttributeValues(rockContext); }); } return(contentItem.Id); }
/// <summary> /// Executes the specified workflow, setting the startDateTime to now (if none was given) and leaving /// the expireDateTime as null (if none was given). /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages) { errorMessages = new List <string>(); var mergeFields = GetMergeFields(action); // Get the content channel Guid contentChannelGuid = GetAttributeValue(action, "ContentChannel").AsGuid(); ContentChannel contentChannel = new ContentChannelService(rockContext).Get(contentChannelGuid); if (contentChannel == null) { errorMessages.Add("Invalid Content Channel attribute or value!"); return(false); } // Get the Content string contentValue = GetAttributeValue(action, "Content", true); string content = string.Empty; Guid? contentGuid = contentValue.AsGuidOrNull(); if (contentGuid.HasValue) { var attribute = AttributeCache.Get(contentGuid.Value, rockContext); if (attribute != null) { string contentAttributeValue = action.GetWorklowAttributeValue(contentGuid.Value); if (!string.IsNullOrWhiteSpace(contentAttributeValue)) { if (attribute.FieldType.Class == "Rock.Field.Types.TextFieldType" || attribute.FieldType.Class == "Rock.Field.Types.MemoFieldType") { content = contentAttributeValue; } } } } else { content = contentValue; } // Get the Start Date Time (check if the attribute value is a guid first) DateTime startDateTime = RockDateTime.Now; string startAttributeValue = GetAttributeValue(action, "StartDateTime"); Guid startDateTimeAttributeGuid = startAttributeValue.AsGuid(); if (!startDateTimeAttributeGuid.IsEmpty()) { var attribute = AttributeCache.Get(startDateTimeAttributeGuid, rockContext); if (attribute != null) { string attributeValue = action.GetWorklowAttributeValue(startDateTimeAttributeGuid); if (!string.IsNullOrWhiteSpace(attributeValue)) { if (attribute.FieldType.Class == "Rock.Field.Types.TextFieldType" || attribute.FieldType.Class == "Rock.Field.Types.DateTimeFieldType") { if (!DateTime.TryParse(attributeValue, out startDateTime)) { startDateTime = RockDateTime.Now; errorMessages.Add(string.Format("Could not parse the start date provided {0}.", attributeValue)); } } } } } // otherwise check just the plain value and then perform lava merge on it. else if (!string.IsNullOrWhiteSpace(startAttributeValue)) { string mergedStartAttributeValue = startAttributeValue.ResolveMergeFields(mergeFields); if (!DateTime.TryParse(mergedStartAttributeValue, out startDateTime)) { startDateTime = RockDateTime.Now; errorMessages.Add(string.Format("Could not parse the start date provided {0}.", startAttributeValue)); } } // Get the Expire Date Time (check if the attribute value is a guid first) DateTime?expireDateTime = null; string expireAttributeValue = GetAttributeValue(action, "ExpireDateTime"); Guid expireDateTimeAttributeGuid = expireAttributeValue.AsGuid(); if (!expireDateTimeAttributeGuid.IsEmpty()) { var attribute = AttributeCache.Get(expireDateTimeAttributeGuid, rockContext); if (attribute != null) { DateTime aDateTime; string attributeValue = action.GetWorklowAttributeValue(expireDateTimeAttributeGuid); if (!string.IsNullOrWhiteSpace(attributeValue)) { if (attribute.FieldType.Class == "Rock.Field.Types.TextFieldType" || attribute.FieldType.Class == "Rock.Field.Types.DateTimeFieldType") { if (DateTime.TryParse(attributeValue, out aDateTime)) { expireDateTime = aDateTime; } else { errorMessages.Add(string.Format("Could not parse the expire date provided {0}.", attributeValue)); } } } } } // otherwise check just the text value and then perform lava merge on it. else if (!string.IsNullOrWhiteSpace(expireAttributeValue)) { string mergedExpireAttributeValue = expireAttributeValue.ResolveMergeFields(mergeFields); DateTime aDateTime; if (DateTime.TryParse(mergedExpireAttributeValue, out aDateTime)) { expireDateTime = aDateTime; } else { errorMessages.Add(string.Format("Could not parse the expire date provided {0}.", expireAttributeValue)); } } // Get the Content Channel Item Status var channelItemStatus = this.GetAttributeValue(action, "Status").ConvertToEnum <ContentChannelItemStatus>(ContentChannelItemStatus.PendingApproval); // Save the new content channel item var contentChannelItemService = new ContentChannelItemService(rockContext); var contentChannelItem = new ContentChannelItem(); contentChannelItem.ContentChannelId = contentChannel.Id; contentChannelItem.ContentChannelTypeId = contentChannel.ContentChannelTypeId; contentChannelItem.Title = GetAttributeValue(action, "Title").ResolveMergeFields(mergeFields); contentChannelItem.Content = content.ResolveMergeFields(mergeFields); contentChannelItem.StartDateTime = startDateTime; contentChannelItem.ExpireDateTime = expireDateTime; contentChannelItem.Status = channelItemStatus; contentChannelItemService.Add(contentChannelItem); rockContext.SaveChanges(); Dictionary <string, string> sourceKeyMap = null; var itemAttributeKeys = GetAttributeValue(action, "ItemAttributeKey"); if (!string.IsNullOrWhiteSpace(itemAttributeKeys)) { // TODO Find a way upstream to stop an additional being appended to the value sourceKeyMap = itemAttributeKeys.AsDictionaryOrNull(); } sourceKeyMap = sourceKeyMap ?? new Dictionary <string, string>(); // Load the content channel item attributes if we're going to add some values if (sourceKeyMap.Count > 0) { contentChannelItem.LoadAttributes(rockContext); foreach (var keyPair in sourceKeyMap) { // Does the source key exist as an attribute in the this workflow? if (action.Activity.Workflow.Attributes.ContainsKey(keyPair.Key)) { if (contentChannelItem.Attributes.ContainsKey(keyPair.Value)) { var value = action.Activity.Workflow.AttributeValues[keyPair.Key].Value; contentChannelItem.SetAttributeValue(keyPair.Value, value); } else { errorMessages.Add(string.Format("'{0}' is not an attribute key in the content channel: '{1}'", keyPair.Value, contentChannel.Name)); } } else { errorMessages.Add(string.Format("'{0}' is not an attribute key in this workflow: '{1}'", keyPair.Key, action.Activity.Workflow.Name)); } } contentChannelItem.SaveAttributeValues(rockContext); } return(true); }
/// <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(); ContentChannelItem contentItem = GetContentItem(rockContext); if (contentItem != null && (IsUserAuthorized(Authorization.EDIT) || contentItem.IsAuthorized(Authorization.EDIT, CurrentPerson))) { contentItem.Title = tbTitle.Text; contentItem.Content = htmlContent.Text; contentItem.Priority = nbPriority.Text.AsInteger(); // If this is a new item and the channel is manually sorted then we need to set the order to the next number if (contentItem.Id == 0 && new ContentChannelService(rockContext).IsManuallySorted(contentItem.ContentChannelId)) { contentItem.Order = new ContentChannelItemService(rockContext).GetNextItemOrderValueForContentChannel(contentItem.ContentChannelId); } if (contentItem.ContentChannelType.IncludeTime) { contentItem.StartDateTime = dtpStart.SelectedDateTime ?? RockDateTime.Now; contentItem.ExpireDateTime = (contentItem.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange) ? dtpExpire.SelectedDateTime : null; } else { contentItem.StartDateTime = dpStart.SelectedDate ?? RockDateTime.Today; contentItem.ExpireDateTime = (contentItem.ContentChannelType.DateRangeType == ContentChannelDateType.DateRange) ? dpExpire.SelectedDate : null; } if (contentItem.ContentChannelType.DisableStatus) { // if DisableStatus == True, just set the status to Approved contentItem.Status = ContentChannelItemStatus.Approved; } else { int newStatusID = hfStatus.Value.AsIntegerOrNull() ?? contentItem.Status.ConvertToInt(); int oldStatusId = contentItem.Status.ConvertToInt(); if (newStatusID != oldStatusId && contentItem.IsAuthorized(Authorization.APPROVE, CurrentPerson)) { contentItem.Status = hfStatus.Value.ConvertToEnum <ContentChannelItemStatus>(ContentChannelItemStatus.PendingApproval); if (contentItem.Status == ContentChannelItemStatus.PendingApproval) { contentItem.ApprovedDateTime = null; contentItem.ApprovedByPersonAliasId = null; } else { contentItem.ApprovedDateTime = RockDateTime.Now; contentItem.ApprovedByPersonAliasId = CurrentPersonAliasId; } } // remove approved status if they do not have approve access when editing if (!contentItem.IsAuthorized(Authorization.APPROVE, CurrentPerson)) { contentItem.ApprovedDateTime = null; contentItem.ApprovedByPersonAliasId = null; contentItem.Status = ContentChannelItemStatus.PendingApproval; } } contentItem.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributes, contentItem); if (!Page.IsValid || !contentItem.IsValid) { // Controls will render the error messages return; } rockContext.WrapTransaction(() => { rockContext.SaveChanges(); contentItem.SaveAttributeValues(rockContext); if (contentItem.ContentChannel.IsTaggingEnabled) { taglTags.EntityGuid = contentItem.Guid; taglTags.SaveTagValues(CurrentPersonAlias); } int?eventItemOccurrenceId = PageParameter("EventItemOccurrenceId").AsIntegerOrNull(); if (eventItemOccurrenceId.HasValue) { var occurrenceChannelItemService = new EventItemOccurrenceChannelItemService(rockContext); var occurrenceChannelItem = occurrenceChannelItemService .Queryable() .Where(c => c.ContentChannelItemId == contentItem.Id && c.EventItemOccurrenceId == eventItemOccurrenceId.Value) .FirstOrDefault(); if (occurrenceChannelItem == null) { occurrenceChannelItem = new EventItemOccurrenceChannelItem(); occurrenceChannelItem.ContentChannelItemId = contentItem.Id; occurrenceChannelItem.EventItemOccurrenceId = eventItemOccurrenceId.Value; occurrenceChannelItemService.Add(occurrenceChannelItem); rockContext.SaveChanges(); } } }); ReturnToParentPage(); } }