Example #1
0
        void PurplePageAddButton_Click(object sender, EventArgs e)
        {
            // What to do now?
            Webpage newpage = new Webpage();

            FindAdminControls();

            newpage.ParentID           = this.Webpage.WebpageID;
            newpage.Filename           = webpageFilename.Text;
            newpage.Title              = webpagePageTitle.Text;
            newpage.MenuTitle          = webpageMenuTitle.Text;
            newpage.Url                = ((this.Webpage.IsSiteRoot) ? "" : this.Webpage.Url + "/") + newpage.Filename;
            newpage.FullUrl            = newpage.Url;
            newpage.ForceSsl           = false;
            newpage.ShowInMenu         = true;
            newpage.MasterPageFilename = webpageMasterPageFilename.SelectedValue + ".master"; // this.Webpage.MasterPageFilename; //  PurpleSettings.DefaultMasterPageFilename;
            newpage.MenuType           = (MenuType)Enum.Parse(typeof(MenuType), webpageMenuType.SelectedValue);

            WebpageArea area = new WebpageArea();

            area.ContentPlaceHolderID = ContentPlaceHolders[0].ID;
            area.ContentHtml          = "New Page";
            newpage.Areas.Add(area);

            // save
            Webpages.AddWebpage(newpage);

            // see the finished page
            Response.Redirect("/" + newpage.Url + "?pagemode=edit");
        }
        public override Webpage CreateWebpage(RequestContext requestContext)
        {
            string url = requestContext.RouteData.Values["url"] as string;


            // special redirect for site.com/default.aspx
            if (url == "default.aspx")
            {
                HttpContext.Current.Response.RedirectPermanent("/");
            }


            // redirects for / or no /
            string prefix = "/";

            if (PurpleSettings.TrailingSlash && !url.EndsWith("/"))
            {
                requestContext.HttpContext.Response.RedirectPermanent(prefix + url + "/" + HttpContext.Current.Request.Url.Query);
            }
            else if (!PurpleSettings.TrailingSlash && url.EndsWith("/") && url != "/")
            {
                requestContext.HttpContext.Response.RedirectPermanent(prefix + url.TrimEnd(new char[] { '/' }) + HttpContext.Current.Request.Url.Query);
            }


            // always check this
            if (url.EndsWith("/"))
            {
                url = url.TrimEnd(new char[] { '/' });
            }
            Webpage webpage = Webpages.GetPublishedWebpage(url);

            // run SSL check
            if (webpage != null && webpage.ForceSsl && !requestContext.HttpContext.Request.IsSecureConnection)
            {
                requestContext.HttpContext.Response.Redirect(PurpleSettings.SecureUrl + url);
            }

            return(webpage);
        }
Example #3
0
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            string url = (values[parameterName] as string).ToLower();

            return(Webpages.ValidateRouteUrl(url));
        }
Example #4
0
        void PurplePageSaveButton_Click(object sender, EventArgs e)
        {
            // What to do now?
            Webpage webpage = this.Webpage;

            FindAdminControls();

            // update main values
            webpage.Filename = webpageFilename.Text;
            webpage.FullUrl  = ((!webpage.Parent.IsSiteRoot) ? webpage.Parent.Url + "/" : "") + webpage.Filename;

            string url = webpageUrl.Text;

            // TODO: seriously need some better checking here
            webpage.Url = (!String.IsNullOrWhiteSpace(url)) ? url : webpage.FullUrl;

            webpage.Title              = webpagePageTitle.Text;
            webpage.MenuTitle          = webpageMenuTitle.Text;
            webpage.MetaDescription    = webpageMetaDescription.Text;
            webpage.MetaKeywords       = webpageMetaKeywords.Text;
            webpage.MasterPageFilename = webpageMasterPageFilename.SelectedValue + ".master";

            webpage.ForceSsl   = webpageForceSsl.Checked;
            webpage.ShowInMenu = webpageShowInMenu.Checked;


            webpage.CommonAreaHeader   = webpageCommonAreaHeader.Text;
            webpage.IgnoreParentHeader = webpageIgnoreParentHeader.Checked;
            DateTime expiresDate = DateTime.MinValue;

            if (DateTime.TryParse(webpageContentExpirationDate.Text, out expiresDate))
            {
                webpage.ContentExpirationDate = expiresDate;
            }
            else
            {
                webpage.ContentExpirationDate = DateTime.MinValue;
            }


            webpage.MenuType = (MenuType)Enum.Parse(typeof(MenuType), webpageMenuType.SelectedValue);

            int menuSortOrder = 0;

            if (Int32.TryParse(webpageSortOrder.Text, out menuSortOrder))
            {
                webpage.SortOrder = menuSortOrder;
            }

            // permissions
            List <string> pageEditors = new List <string>();

            foreach (ListItem li in webpageEditors.Items)
            {
                if (li.Selected)
                {
                    pageEditors.Add(li.Value);
                }
            }
            webpage.Editors = String.Join(",", pageEditors.ToArray());


            // update modules
            webpage.Areas.Clear();
            foreach (ContentPlaceHolder placeholder in ContentPlaceHolders)
            {
                int sortOrder = 1;
                // each sub control should be a editarea.ascx control
                foreach (Control control in placeholder.Controls)
                {
                    WebpageArea area = new WebpageArea();

                    string html = (control.FindControl("HtmlContent") as TextBox).Text;

                    area.ContentPlaceHolderID = placeholder.ID;
                    area.ContentHtml          = html;
                    DropDownList controlList = control.FindControl("ControlName") as DropDownList;
                    area.ControlName = (!String.IsNullOrWhiteSpace(controlList.SelectedValue)) ? controlList.SelectedValue + ".ascx" : "";
                    area.SortOrder   = sortOrder;

                    webpage.Areas.Add(area);
                    sortOrder++;
                }
            }

            // save
            webpage.IsPublished = true;
            Webpages.UpdateWebpage(webpage);

            // see the finished page
            Response.Redirect("/" + webpage.Url);
        }
Example #5
0
        protected override void OnInit(EventArgs e)
        {
            FormMode formMode = FormMode.Container;
            PageMode pageMode = PageMode.Render;

            // local variable
            Webpage webpage = this.Webpage;

            if ((Request.QueryString["pagemode"] + "").ToLower() == "edit")
            {
                pageMode = PageMode.Edit;
            }
            //else if (Request.QueryString["pagemode"] + "" == "newpage")
            //	pageMode = PageMode.NewPage;


            bool userHasPermissions = GetUserPermissions();

            if (!userHasPermissions)
            {
                pageMode = PageMode.Render;
            }


            // get and clear place holders
            ContentPlaceHolders = FindControls <ContentPlaceHolder>(this.Body);

            if (ContentPlaceHolders.Count == 0)
            {
                throw new Exception("The masterpage '{0}' has no <asp:Content runat=\"Server\" /> controls");
            }

            ContentPlaceHolders.ForEach(p => p.Controls.Clear());

            // update meta data
            this.Title           = webpage.Title + ((!webpage.IsSiteRoot) ? PurpleSettings.PageSuffix : string.Empty);
            this.MetaDescription = webpage.MetaDescription;
            this.MetaKeywords    = webpage.MetaKeywords;

            if (userHasPermissions)
            {
                HtmlLink adminStylesLink = new HtmlLink()
                {
                    Href = PurpleSettings.CmsPath + "assets/admincontrols/admin.css"
                };
                adminStylesLink.Attributes.Add("rel", "stylesheet");
                this.Header.Controls.Add(adminStylesLink);
            }


            if (pageMode == PageMode.Render)
            {
                // add the admin controls first so the form is already there
                if (userHasPermissions)
                {
                    AddFormToBody();

                    // add admin controls
                    AdminControl = LoadControl(PurpleSettings.CmsPath + "assets/admincontrols/adminpage.ascx");
                    FindAdminControls();

                    // set master selector
                    PopulateMasterPageList();

                    webpageAddButton.Click += new EventHandler(PurplePageAddButton_Click);

                    // fill in the details
                    webpageFilenamePrefix.Text = (!webpage.IsSiteRoot) ? webpage.Url + "/" : "";

                    // versions
                    webpageVersionsRepeater.DataSource = Webpages.GetWebpageRevision(webpage.WebpageID);
                    webpageVersionsRepeater.DataBind();

                    _form.Controls.Add(AdminControl);
                }

                // add common header to first control if needed
                string commonHeader = webpage.CommonAreaHeader;
                if (string.IsNullOrWhiteSpace(commonHeader) && !webpage.IgnoreParentHeader)
                {
                    // go back until the parent is null or we find a
                    Webpage parent = webpage.Parent;
                    do
                    {
                        if (!string.IsNullOrWhiteSpace(parent.CommonAreaHeader))
                        {
                            commonHeader = parent.CommonAreaHeader;
                            break;
                        }


                        parent = parent.Parent;
                    } while (parent != null && !parent.IsSiteRoot);
                }
                // add it if it exists
                if (!String.IsNullOrWhiteSpace(commonHeader))
                {
                    ContentPlaceHolders[0].Controls.Add(new LiteralControl(commonHeader));
                }


                // go through the webpage areas
                foreach (WebpageArea area in webpage.Areas)
                {
                    // get the right placeholder
                    ContentPlaceHolder placeholder = ContentPlaceHolders.Find(c => c.ID == area.ContentPlaceHolderID);

                    // use default
                    if (placeholder == null)
                    {
                        placeholder = ContentPlaceHolders[0];
                    }


                    // build control/HTML parts
                    string content = area.ContentHtml;

                    // fix extra paragraph tags for controls
                    content = _controlFixRegex.Replace(content, "[control:${1}]");

                    // override with single control
                    if (!String.IsNullOrWhiteSpace(area.ControlName))
                    {
                        content = "[control:" + area.ControlName + "]";
                    }


                    // search for controls, load them and create them
                    int             currentPosition = 0;
                    MatchCollection controlMatches  = _controlRegex.Matches(content);

                    if (controlMatches.Count == 0)
                    {
                        // Plain HTML
                        placeholder.Controls.Add(new LiteralControl(area.ContentHtml));
                    }
                    else
                    {
                        foreach (Match controlMatch in controlMatches)
                        {
                            // Add literal for content before custom tag should it exist.
                            if (controlMatch.Index > currentPosition)
                            {
                                placeholder.Controls.Add(new LiteralControl(content.Substring(currentPosition, controlMatch.Index - currentPosition)));
                            }

                            // Now lets add our user control.
                            try
                            {
                                string  all         = controlMatch.Groups[1].Value.Trim();
                                Control usercontrol = null;

                                if (!all.EndsWith(".ascx", StringComparison.OrdinalIgnoreCase))
                                {
                                    int index = all.IndexOf(".ascx", StringComparison.OrdinalIgnoreCase) + 5;
                                    usercontrol = LoadControl("~/cms/content/controls/" + all.Substring(0, index));

                                    string   parameters      = Server.HtmlDecode(all.Substring(index));
                                    Type     type            = usercontrol.GetType();
                                    string[] paramCollection = parameters.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

                                    foreach (string param in paramCollection)
                                    {
                                        string name  = param.Split('=')[0].Trim();
                                        string value = param.Split('=')[1].Trim();
                                        System.Reflection.PropertyInfo property = type.GetProperty(name);
                                        property.SetValue(usercontrol, Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture), null);
                                    }
                                }
                                else
                                {
                                    usercontrol = LoadControl("~/cms/content/controls/" + all);
                                }

                                placeholder.Controls.Add(usercontrol);

                                // Now we will update our position.
                                //currentPosition = controlMatch.Index + controlMatch.Groups[0].Length;
                            }
                            catch (Exception controlLoadException)
                            {
                                // Whoopss, can't load that control so lets output something that tells the developer that theres a problem.
                                placeholder.Controls.Add(new LiteralControl("ERROR - UNABLE TO LOAD CONTROL : " + "~/cms/content/controls/" + controlMatch.Groups[1].Value + "<br />" + controlLoadException.ToString()));
                            }

                            currentPosition = controlMatch.Index + controlMatch.Groups[0].Length;
                        }

                        // final text

                        // Add literal for content before custom tag should it exist.
                        if (content.Length > currentPosition)
                        {
                            placeholder.Controls.Add(new LiteralControl(content.Substring(currentPosition)));
                        }



                        // check for form needs
                        bool requiresForm = CheckRequiresForm(placeholder);;
                        if (requiresForm)
                        {
                            if (formMode == FormMode.Container)
                            {
                                // transfer controls to form
                                System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                                while (placeholder.HasControls())
                                {
                                    form.Controls.Add(placeholder.Controls[0]);
                                }

                                // add form
                                placeholder.Controls.Add(form);
                            }
                            else if (formMode == FormMode.WholePage)
                            {
                                AddFormToBody();
                            }
                        }
                    }



                    #region old rendercode

                    /*
                     *                  if (!String.IsNullOrWhiteSpace(area.ControlName))
                     *                  {
                     *
                     *                          // find the ASCX file
                     *                          string controlVPath = PurpleSettings.CmsPath + "/content/controls/" + area.ControlName;
                     *                          string controlPath = Server.MapPath(controlVPath);
                     *
                     *                          if (File.Exists(controlPath))
                     *                          {
                     *                                  Control control = LoadControl(controlVPath);
                     *                                  bool requiresForm = CheckRequiresForm( control); ;
                     *                                  if (requiresForm)
                     *                                  {
                     *                                          if (formMode == FormMode.Container)
                     *                                          {
                     *                                                  System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                     *                                                  placeholder.Controls.Add(form);
                     *                                                  form.Controls.Add(control);
                     *                                          }
                     *                                          else if (formMode == FormMode.WholePage)
                     *                                          {
                     *                                                  AddFormToBody();
                     *                                          }
                     *                                  }
                     *                                  else
                     *                                  {
                     *                                          placeholder.Controls.Add(control);
                     *                                  }
                     *                          }
                     *                          else
                     *                          {
                     *                                  placeholder.Controls.Add(new LiteralControl("<p>No control at :" + controlVPath + "</p>"));
                     *                          }
                     *
                     *                  }
                     *                  else
                     *                  {
                     *
                     *
                     *
                     *  // search for controls, load them and create them
                     *  string content = area.ContentHtml;
                     *  int currentPosition = 0;
                     *  MatchCollection controlMatches = _controlRegex.Matches(content);
                     *
                     *  if (controlMatches.Count == 0)
                     *  {
                     *      // old HTML only code
                     *      placeholder.Controls.Add(new LiteralControl(area.ContentHtml));
                     *  }
                     *  else
                     *  {
                     *
                     *      foreach (Match controlMatch in controlMatches)
                     *      {
                     *          // Add literal for content before custom tag should it exist.
                     *          if (controlMatch.Index > currentPosition)
                     *          {
                     *              placeholder.Controls.Add(new LiteralControl(content.Substring(currentPosition, controlMatch.Index - currentPosition)));
                     *          }
                     *
                     *          // Now lets add our user control.
                     *          try
                     *          {
                     *              string all = controlMatch.Groups[1].Value.Trim();
                     *              Control usercontrol = null;
                     *
                     *              if (!all.EndsWith(".ascx", StringComparison.OrdinalIgnoreCase))
                     *              {
                     *                  int index = all.IndexOf(".ascx", StringComparison.OrdinalIgnoreCase) + 5;
                     *                  usercontrol = LoadControl("~/cms/content/controls/" + all.Substring(0, index));
                     *
                     *                  string parameters = Server.HtmlDecode(all.Substring(index));
                     *                  Type type = usercontrol.GetType();
                     *                  string[] paramCollection = parameters.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                     *
                     *                  foreach (string param in paramCollection)
                     *                  {
                     *                      string name = param.Split('=')[0].Trim();
                     *                      string value = param.Split('=')[1].Trim();
                     *                      System.Reflection.PropertyInfo property = type.GetProperty(name);
                     *                      property.SetValue(usercontrol, Convert.ChangeType(value, property.PropertyType, CultureInfo.InvariantCulture), null);
                     *                  }
                     *              }
                     *              else
                     *              {
                     *                  usercontrol = LoadControl("~/cms/content/controls/" + all);
                     *              }
                     *
                     *              placeholder.Controls.Add(usercontrol);
                     *
                     *              // Now we will update our position.
                     *              //currentPosition = myMatch.Index + myMatch.Groups[0].Length;
                     *          }
                     *          catch (Exception controlLoadException)
                     *          {
                     *              // Whoopss, can't load that control so lets output something that tells the developer that theres a problem.
                     *              placeholder.Controls.Add(new LiteralControl("ERROR - UNABLE TO LOAD CONTROL : " + "~/cms/content/controls/" + controlMatch.Groups[1].Value + "<br />" + controlLoadException.ToString()));
                     *          }
                     *
                     *          currentPosition = controlMatch.Index + controlMatch.Groups[0].Length;
                     *      }
                     *
                     *
                     *      // check for form needs
                     *      bool requiresForm = CheckRequiresForm(placeholder); ;
                     *      if (requiresForm)
                     *      {
                     *          if (formMode == FormMode.Container)
                     *          {
                     *              // transfer controls to form
                     *              System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                     *              while (placeholder.HasControls())
                     *              {
                     *                  form.Controls.Add(placeholder.Controls[0]);
                     *              }
                     *
                     *              // add form
                     *              placeholder.Controls.Add(form);
                     *
                     *          }
                     *          else if (formMode == FormMode.WholePage)
                     *          {
                     *              AddFormToBody();
                     *          }
                     *      }
                     *  }
                     *
                     * }
                     */
                    #endregion
                }
            }
            else if (pageMode == PageMode.Edit)
            {
                // just in case
                AddFormToBody();

                // main edit control
                AdminControl = LoadControl(PurpleSettings.CmsPath + "assets/admincontrols/editpage.ascx");
                _form.Controls.Add(AdminControl);

                // attach all the controls
                FindAdminControls();

                // special cases
                if (webpage.IsSiteRoot)
                {
                    webpageFilename.Enabled = false;
                    webpageUrl.Enabled      = false;
                }


                // main attributes
                webpageFilename.Text        = webpage.Filename;
                webpagePageTitle.Text       = webpage.Title;
                webpageMenuTitle.Text       = webpage.MenuTitle;
                webpageMetaDescription.Text = webpage.MetaDescription;
                webpageMetaKeywords.Text    = webpage.MetaKeywords;
                webpageFilenamePrefix.Text  = (!webpage.IsSiteRoot && !webpage.Parent.IsSiteRoot) ? webpage.Parent.Url + "/" : "";
                webpageUrl.Text             = webpage.Url;
                webpageForceSsl.Checked     = webpage.ForceSsl;
                webpageShowInMenu.Checked   = webpage.ShowInMenu;
                webpageSortOrder.Text       = webpage.SortOrder.ToString();

                webpageCommonAreaHeader.Text      = webpage.CommonAreaHeader;
                webpageContentExpirationDate.Text = webpage.ContentExpirationDate.ToString();
                webpageIgnoreParentHeader.Checked = webpage.IgnoreParentHeader;

                // menu type enum



                // master pages
                PopulateMasterPageList();

                // permissions
                string[] allEditors = System.Web.Security.Roles.GetUsersInRole(PurpleSettings.RoleEditor);
                webpageEditors.DataSource = allEditors;
                webpageEditors.DataBind();

                List <string> pageEditors = webpage.Editors.Split(new char[] { ',' }).ToList <String>();
                foreach (ListItem li in webpageEditors.Items)
                {
                    if (pageEditors.Contains(li.Value))
                    {
                        li.Selected = true;
                    }
                }


                // attach save event
                webpageSaveButton.Click += new EventHandler(PurplePageSaveButton_Click);


                // WEBPAGE AREAS


                // list of control folders
                string        controlBase    = Server.MapPath(PurpleSettings.CmsPath + "content/controls/");
                List <string> controlFolders = new List <string>();
                UtilityMethods.FindFoldersRecursive(controlFolders, controlBase);

                // list of controls
                List <string> availableControls = new List <string>();
                UtilityMethods.FindFilesRecursive(availableControls, "*.ascx", controlBase, true);


                // go through the webpage areas
                foreach (WebpageArea area in webpage.Areas)
                {
                    // get the right placeholder
                    ContentPlaceHolder placeholder = ContentPlaceHolders.Find(c => c.ID == area.ContentPlaceHolderID);

                    // use default
                    if (placeholder == null)
                    {
                        placeholder = ContentPlaceHolders[0];
                    }


                    Control areaControl = LoadControl(PurpleSettings.CmsPath + "assets/admincontrols/editarea.ascx");

                    // grab the html area
                    TextBox htmlContent = areaControl.FindControl("HtmlContent") as TextBox;
                    htmlContent.Text = area.ContentHtml;


                    // fill in the *.ascx controls
                    DropDownList controlName = areaControl.FindControl("ControlName") as DropDownList;
                    controlName.DataSource = availableControls;
                    controlName.DataBind();
                    controlName.Items.Insert(0, new ListItem("-- No Control (use HTML) --", ""));

                    if (!String.IsNullOrWhiteSpace(area.ControlName) && controlName.Items.FindByValue(area.ControlName.Replace(".ascx", "")) != null)
                    {
                        controlName.Items.FindByValue(area.ControlName.Replace(".ascx", "")).Selected = true;
                    }

                    // fill in the upload folders
                    DropDownList controlUploadFolder = areaControl.FindControl("ControlUploadFolder") as DropDownList;
                    controlUploadFolder.DataSource = controlFolders;
                    controlUploadFolder.DataBind();
                    controlUploadFolder.Items.Insert(0, new ListItem("-- Root Folder --", ""));

                    // attach event to the upload button
                    (areaControl.FindControl("ControlUploadButton") as Button).Click += new EventHandler(ControlUploadButton_Click);


                    // add this control
                    placeholder.Controls.Add(areaControl);
                }
            }

            base.OnInit(e);
        }