/// <summary> /// Handles the Click event of the btnSave 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 btnSave_Click(object sender, EventArgs e) { if (Page.IsValid) { PageShortLink link = null; using (var rockContext = new RockContext()) { var service = new PageShortLinkService(rockContext); var errors = new List <string>(); int?linkId = hfShortLinkId.Value.AsIntegerOrNull(); if (linkId.HasValue) { link = service.Get(linkId.Value); } if (link == null) { link = new PageShortLink(); service.Add(link); } int? siteId = ddlSite.SelectedValueAsInt(); string token = tbToken.Text.Trim(); string url = tbUrl.Text.RemoveCrLf().Trim(); if (!siteId.HasValue) { errors.Add("Please select a valid site."); } int minTokenLength = GetAttributeValue("MinimumTokenLength").AsIntegerOrNull() ?? 7; if (token.IsNullOrWhiteSpace() || token.Length < minTokenLength) { errors.Add(string.Format("Please enter a token that is a least {0} characters long.", minTokenLength)); } else if (siteId.HasValue && !service.VerifyUniqueToken(siteId.Value, link.Id, tbToken.Text)) { errors.Add("The selected token is already being used. Please enter a different token."); } if (url.IsNullOrWhiteSpace()) { errors.Add("Please enter a valid URL."); } if (errors.Any()) { nbError.Text = "Please correct the following:<ul><li>" + errors.AsDelimited("</li><li>") + "</li></ul>"; nbError.Visible = true; return; } link.SiteId = siteId.Value; link.Token = token; link.Url = url; if (!link.IsValid) { return; } rockContext.SaveChanges(); } Dictionary <string, string> qryParams = new Dictionary <string, string>(); qryParams["ShortLinkId"] = link.Id.ToString(); NavigateToPage(RockPage.Guid, qryParams); } }
/// <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.Get(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.Get(GetAttributeValue(action, "Attribute").AsGuid(), rockContext); if (attribute != null) { string shortLink = link.ShortLinkUrl; SetWorkflowAttributeValue(action, attribute.Guid, shortLink); action.AddLogEntry(string.Format("Set '{0}' attribute to '{1}'.", attribute.Name, shortLink)); } return(true); }
/// <summary> /// Handles the OnSave event of the masterPage 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 masterPage_OnSave(object sender, EventArgs e) { Page.Validate(BlockValidationGroup); if (Page.IsValid) { PageShortLink link = null; using (var rockContext = new RockContext()) { var service = new PageShortLinkService(rockContext); var errors = new List <string>(); int? siteId = ddlSite.SelectedValueAsInt(); string token = tbToken.Text.Trim(); string url = tbUrl.Text.Trim(); if (!siteId.HasValue) { errors.Add("Please select a valid site."); } if (token.IsNullOrWhiteSpace() || token.Length < _minTokenLength) { errors.Add(string.Format("Please enter a token that is a least {0} characters long.", _minTokenLength)); } else if (siteId.HasValue && !service.VerifyUniqueToken(siteId.Value, 0, tbToken.Text)) { errors.Add("The selected token is already being used. Please enter a different token."); } if (url.IsNullOrWhiteSpace()) { errors.Add("Please enter a valid URL."); } if (errors.Any()) { nbError.Text = "Please correct the following:<ul><li>" + errors.AsDelimited("</li><li>") + "</li></ul>"; nbError.Visible = true; return; } link = new PageShortLink(); link.SiteId = siteId.Value; link.Token = token; link.Url = url; service.Add(link); rockContext.SaveChanges(); } if (link != null) { using (var rockContext = new RockContext()) { string script = "window.parent.Rock.controls.modal.close();"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "close-modal", script, true); } } } }