void PreEditPage(PageEntry pe)
 {
     if (Request.Form.Count == 0)
     {
         Page page = null;
         if (SprocketPath.Sections.Length <= 3)
         {
             page = new Page(0, 0, "Untitled Page", "", "", "", true, "", "text/html", DateTime.UtcNow, null);
         }
         else
         {
             long pageID;
             if (long.TryParse(SprocketPath.Sections[3], out pageID))
             {
                 try
                 {
                     DatabaseManager.DatabaseEngine.GetConnection();
                     page = ContentManager.Instance.DataProvider.SelectPage(pageID);
                     FormValues.Set("Revision", null, page.RevisionInformation, false);
                     FormValues.Set("AllRevisions", null, page.RevisionInformation, false);
                 }
                 finally
                 {
                     DatabaseManager.DatabaseEngine.ReleaseConnection();
                 }
             }
         }
         FormValues.Set("Page", page == null ? "The requested page was not found." : null, page, page == null);
     }
 }
Exemple #2
0
        public Result ValidateFormField(string fieldName)
        {
            object value  = null;
            string val    = HttpContext.Current.Request.Form[fieldName] ?? "";
            Result result = new Result();

            switch (fieldName)
            {
            case "PageID": value = pageID = long.TryParse(val, out pageID) ? pageID : 0; break;

            case "RevisionID": value = revisionID = long.TryParse(val, out revisionID) ? pageID : 0; break;

            case "PageCode": value = pageCode = val.Trim(); break;

            case "PageName": value = pageName = (val == String.Empty ? "Untitled Page" : val).Trim(); break;

            case "Requestable": value = requestable = StringUtilities.BoolFromString(val); break;

            case "RequestPath":
                value = requestPath = val.Trim().Replace(" ", "-");
                if (!Uri.IsWellFormedUriString("http://localhost/" + requestPath + "/", UriKind.Absolute))
                {
                    result.SetFailed("The specified URL is badly formed. Make sure you haven't typed in any invalid characters.");
                }
                break;

            case "TemplateName":
                value = templateName = val.Trim();
                if (templateName != String.Empty)
                {
                    if (ContentManager.Templates[templateName] == null)
                    {
                        result.SetFailed("The template \"" + templateName + "\" is not valid. It may have been deleted.");
                    }
                }
                break;

            case "PublishDate":
            {
                if (!DateTime.TryParseExact(val, "yyyy-MM-dd hh:mmtt", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AllowWhiteSpaces, out publishDate))
                {
                    if (!DateTime.TryParse(val, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowWhiteSpaces, out publishDate))
                    {
                        result.SetFailed("The publish date should be in the format yyyy-mm-dd hh:mmtt (see the grey help text for that field)");
                    }
                }
            }
            break;

            case "ExpiryDate":
            {
                DateTime dt = DateTime.MinValue;
                if (val.Trim() == String.Empty)
                {
                    expiryDate = null;
                }
                else if (!DateTime.TryParseExact(val, "yyyy-MM-dd hh:mmtt", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AllowWhiteSpaces, out dt))
                {
                    if (!DateTime.TryParse(val, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowWhiteSpaces, out dt))
                    {
                        result.SetFailed("The expiry date should be in the format yyyy-mm-dd hh:mmtt (see the grey help text for that field)");
                    }
                }
                if (dt != DateTime.MinValue)
                {
                    expiryDate = dt;
                }
            }
            break;

            case "ContentType": value = contentType = val.Trim(); break;
            }
            FormValues.Set(fieldName, result.Message, value, !result.Succeeded);
            return(result);
        }
        void SavePage()
        {
            Result result = new Result();
            long   pageID = -1;

            long.TryParse(Request.Form["PageID"], out pageID);
            bool wasDraft = false;
            Page page;

            if (pageID == 0)
            {
                page = new Page();
            }
            else
            {
                page = ContentManager.Instance.DataProvider.SelectPage(pageID);
            }
            if (page == null)
            {
                WebUtility.Redirect("admin/pages/edit/notfound");
                return;
            }
            else if (pageID != 0)
            {
                if (page.RevisionInformation.Draft)
                {
                    wasDraft = true;
                }
            }

            // load the page's existing content for comparison against newly-submitted content
            List <PreparedPageAdminSection> sectionlist = page.AdminSectionList;

            string   prevTemplateName = page.TemplateName;
            Template prevTemplate     = ContentManager.Templates[prevTemplateName];

            // read in the selected page categories and cause an error if any requirements were not satisfied
            Dictionary <string, List <string> > pagecategories = new Dictionary <string, List <string> >();

            if (prevTemplate != null)
            {
                foreach (CategorySet catset in prevTemplate.CategorySets)
                {
                    if (catset.AllowMultiple)
                    {
                        List <string> cats = new List <string>();
                        if (Request.Form["_$CategorySet_" + catset.Name] != null)
                        {
                            foreach (string val in Request.Form.GetValues("_$CategorySet_" + catset.Name))
                            {
                                cats.Add(val);
                            }
                        }

                        foreach (Category cat in catset.Categories)
                        {
                            if (cats.Contains(cat.Text))
                            {
                                List <string> catlist;
                                if (!pagecategories.TryGetValue(catset.Name, out catlist))
                                {
                                    catlist = new List <string>();
                                    pagecategories.Add(catset.Name, catlist);
                                }
                                catlist.Add(cat.Text);
                            }
                        }

                        if (!pagecategories.ContainsKey(catset.Name) && catset.Required)
                        {
                            result.Merge(new Result("You must make at least one selection from the \"" + catset.Name + "\" category."));
                        }
                    }
                    else
                    {
                        string val   = Request.Form["_$CategorySet_" + catset.Name];
                        bool   found = false;
                        foreach (Category cat in catset.Categories)
                        {
                            if (cat.Text == val)
                            {
                                found = true;
                                break;
                            }
                        }
                        if (!found && catset.Required)
                        {
                            result.Merge(new Result("You must make a selection from the \"" + catset.Name + "\" category."));
                        }
                        else if (found)
                        {
                            List <string> catlist;
                            if (!pagecategories.TryGetValue(catset.Name, out catlist))
                            {
                                catlist = new List <string>();
                                pagecategories.Add(catset.Name, catlist);
                            }
                            catlist.Add(val);
                        }
                    }
                }
            }
            page.CategorySelections = pagecategories;

            result.Merge(page.ValidateFormField("PageName"));
            result.Merge(page.ValidateFormField("Requestable"));
            result.Merge(page.ValidateFormField("RequestPath"));
            result.Merge(page.ValidateFormField("TemplateName"));
            result.Merge(page.ValidateFormField("ContentType"));
            result.Merge(page.ValidateFormField("PublishDate"));
            result.Merge(page.ValidateFormField("ExpiryDate"));
            FormValues.Set("Page", result.Message, page, !result.Succeeded);

            bool success = false;

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    DatabaseManager.DatabaseEngine.GetConnection();

                    if (result.Succeeded)
                    {
                        if (page.PageID == 0)
                        {
                            result = page.SaveRevision("Created new page \"" + page.PageName + "\".", true, false, false);
                        }
                        else
                        {
                            result = page.SaveRevision(Request.Form["RevisionNotes"].Trim(),
                                                       StringUtilities.BoolFromString(Request.Form["Draft"]),
                                                       StringUtilities.BoolFromString(Request.Form["Hidden"]),
                                                       StringUtilities.BoolFromString(Request.Form["Deleted"]));
                        }
                        if (result.Succeeded)
                        {
                            // now save all fields of the selected template. use the previously-selected template for field selection
                            // as it will reflect any fields that were available in the form in that has just been submitted. if the
                            // page is newly-created or has just come from a state where no template was selected, then there will be
                            // no fields and thus we can skip this step.
                            if (prevTemplateName != String.Empty && page.PageID != 0 && sectionlist.Count > 0)
                            {
                                foreach (PreparedPageAdminSection section in sectionlist)
                                {
                                    int rank = 0;
                                    foreach (EditFieldInfo fld in section.FieldList)
                                    {
                                        if (fld.DataHandler == null)
                                        {
                                            continue;
                                        }

                                        // todo: implement optional validation and thus use the returned Result value from ReadAdminField,
                                        // combined with FormValues.Set() and associated expressions in the page editor html file.
                                        IEditFieldData data;
                                        fld.Handler.ReadAdminField(out data);
                                        bool isdifferent = fld.Handler.IsContentDifferent(fld.Data, data) || fld.DataID == 0;
                                        if (isdifferent)
                                        {
                                            fld.Data   = data;
                                            fld.DataID = 0;
                                        }
                                        fld.Rank = rank++;
                                        // we stil loop through each field to make sure the values are read from the form. if a failure has
                                        // occurred at some point though, the following checks ensure we don't bother trying to hit the db.
                                        if (result.Succeeded)
                                        {
                                            result.Merge(ContentManager.Instance.DataProvider.StoreEditFieldInfo(page.RevisionID, fld));
                                        }
                                        if (result.Succeeded && isdifferent)
                                        {
                                            result.Merge(fld.DataHandler.SaveData(fld.DataID, fld.Data));
                                        }
                                    }
                                }
                            }
                            // if this was a draft page and now it's not, delete the old draft revisions as they're no longer required
                            if (result.Succeeded && wasDraft && !page.RevisionInformation.Draft)
                            {
                                result = ContentManager.Instance.DataProvider.DeleteDraftRevisions(page.PageID);
                            }

                            if (result.Succeeded)
                            {
                                success = true;
                                scope.Complete();
                            }
                            else
                            {
                                FormValues.Set("Page", result.Message, null, true);
                            }
                        }
                    }
                }
            }
            finally
            {
                DatabaseManager.DatabaseEngine.ReleaseConnection();
            }
            if (success)
            {
                WebUtility.Redirect("admin/pages/edit/" + page.PageID + "/?saved");
            }
        }