Beispiel #1
0
        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);
        }