Exemplo n.º 1
0
        /// <summary>
        /// Executes the specified workflow.
        /// </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 service = new PageShortLinkService(rockContext);

            // Get the merge fields
            var mergeFields = GetMergeFields(action);

            // Get the site
            int       siteId = GetAttributeValue(action, "Site", true).AsInteger();
            SiteCache site   = SiteCache.Read(siteId);

            if (site == null)
            {
                errorMessages.Add(string.Format("Invalid Site Value"));
                return(false);
            }

            // Get the token
            string token = GetAttributeValue(action, "Token", true).ResolveMergeFields(mergeFields);

            if (token.IsNullOrWhiteSpace())
            {
                int tokenLen = GetAttributeValue(action, "RandomTokenLength").AsIntegerOrNull() ?? 7;
                token = service.GetUniqueToken(site.Id, tokenLen);
            }

            // Get the target url
            string url = GetAttributeValue(action, "Url", true).ResolveMergeFields(mergeFields).RemoveCrLf().Trim();

            if (url.IsNullOrWhiteSpace())
            {
                errorMessages.Add("A valid Target Url was not specified.");
                return(false);
            }

            // Save the short link
            var link = service.GetByToken(token, site.Id);

            if (link != null)
            {
                if (!GetAttributeValue(action, "Overwrite").AsBoolean())
                {
                    errorMessages.Add(string.Format("The selected token ('{0}') already exists. Please specify a unique token, or configure action to allow token re-use.", token));
                    return(false);
                }
                else
                {
                    link.Url = url;
                }
            }
            else
            {
                link        = new PageShortLink();
                link.SiteId = site.Id;
                link.Token  = token;
                link.Url    = url;
                service.Add(link);
            }
            rockContext.SaveChanges();

            // Save the resulting short link url
            var attribute = AttributeCache.Read(GetAttributeValue(action, "Attribute").AsGuid(), rockContext);

            if (attribute != null)
            {
                string domain    = new SiteService(rockContext).GetDefaultDomainUri(site.Id).ToString();
                string shortLink = domain.EnsureTrailingForwardslash() + token;

                SetWorkflowAttributeValue(action, attribute.Guid, shortLink);
                action.AddLogEntry(string.Format("Set '{0}' attribute to '{1}'.", attribute.Name, shortLink));
            }

            return(true);
        }