Example #1
0
        /// <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);
            }
        }
Example #2
0
        /// <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);
                    }
                }
            }
        }