///<summary>Saves the the currently edited Wikipage as a draft. This method is copied from Save_Click with a few modifications.</summary> private void SaveDraft_Click(bool showMsgBox = true) { if (showMsgBox && WikiPageCur.IsNew) { MsgBox.Show(this, "You may not save a new Wiki page as a draft. Save the Wiki page, then create a draft."); return; } if (!MarkupL.ValidateMarkup(textContent, true, showMsgBox)) { return; } if (showMsgBox && _isInvalidPreview) { MsgBox.Show(this, "This page is in an invalid state and cannot be saved as a draft."); return; } WikiPageCur.PageContent = WikiPages.ConvertTitlesToPageNums(textContent.Text); WikiPageCur.UserNum = Security.CurUser.UserNum; Regex regex = new Regex(@"\[\[(keywords:).+?\]\]"); //only grab first match Match m = regex.Match(textContent.Text); WikiPageCur.KeyWords = m.Value.Replace("[[keywords:", "").TrimEnd(']'); //will be empty string if no match if (WikiPageCur.IsDraft) //If it's already a draft, overwrite the current one. { WikiPageCur.DateTimeSaved = DateTime.Now; try { WikiPages.UpdateDraft(WikiPageCur); } catch (Exception ex) { //should never happen due to the if Draft check above. if (showMsgBox) { MessageBox.Show(ex.Message); } return; } } else //otherwise, set it as a draft, then insert it. { WikiPageCur.IsDraft = true; WikiPages.InsertAsDraft(WikiPageCur); } //HasSaved not set so that the user will stay in FormWikiDrafts when this window closes, causing the grid to update. Close(); }
///<summary>Before calling this, make sure to increment/decrement the historyNavBack index to keep track of the position in history. If loading a new page, decrement historyNavBack before calling this function. </summary> private void LoadWikiPage(string pageTitle) { //This is called from 11 different places, any time the program needs to refresh a page from the db. //It's also called from the browser_Navigating event when a "wiki:" link is clicked. WikiPage wpage = WikiPages.GetByTitle(pageTitle); if (wpage == null) { string errorMsg = ""; if (!WikiPages.IsWikiPageTitleValid(pageTitle, out errorMsg)) { MsgBox.Show(this, "That page does not exist and cannot be made because the page title contains invalid characters."); return; } if (!MsgBox.Show(this, MsgBoxButtons.YesNo, "That page does not exist. Would you like to create it?")) { return; } Action <string> onWikiSaved = new Action <string>((pageTitleNew) => { //Insert the pageTitleNew where the pageTitle exists in the existing WikiPageCur //(guaranteed to be unique because all INVALID WIKIPAGE LINK's have an ID at the end) string pageContent = WikiPages.GetWikiPageContentWithWikiPageTitles(WikiPageCur.PageContent); WikiPageCur.PageContent = pageContent.Replace(pageTitle, pageTitleNew); WikiPageCur.PageContent = WikiPages.ConvertTitlesToPageNums(WikiPageCur.PageContent); WikiPages.Update(WikiPageCur); }); AddWikiPage(onWikiSaved); return; } WikiPageCur = wpage; try { webBrowserWiki.DocumentText = WikiPages.TranslateToXhtml(WikiPageCur.PageContent, false); } catch (Exception ex) { webBrowserWiki.DocumentText = ""; MessageBox.Show(this, Lan.g(this, "This page is broken and cannot be viewed. Error message:") + " " + ex.Message); } Text = "Wiki - " + WikiPageCur.PageTitle; #region historyMaint //This region is duplicated in webBrowserWiki_Navigating() for external links. Modifications here will need to be reflected there. int indexInHistory = historyNav.Count - (1 + historyNavBack); //historyNavBack number of pages before the last page in history. This is the index of the page we are loading. if (historyNav.Count == 0) //empty history { historyNavBack = 0; historyNav.Add("wiki:" + pageTitle); } else if (historyNavBack < 0) //historyNavBack could be negative here. This means before the action that caused this load, we were not navigating through history, simply set back to 0 and add to historyNav[] if necessary. { historyNavBack = 0; if (historyNav[historyNav.Count - 1] != "wiki:" + pageTitle) { historyNav.Add("wiki:" + pageTitle); } } else if (historyNavBack >= 0 && historyNav[indexInHistory] != "wiki:" + pageTitle) //branching from page in history { historyNav.RemoveRange(indexInHistory, historyNavBack + 1); //remove "forward" history. branching off in a new direction historyNavBack = 0; historyNav.Add("wiki:" + pageTitle); } #endregion }
private void Save_Click() { if (!MarkupL.ValidateMarkup(textContent, true)) { return; } if (_isInvalidPreview) { MsgBox.Show(this, "This page is in an invalid state and cannot be saved."); return; } WikiPage wikiPageDB = WikiPages.GetByTitle(WikiPageCur.PageTitle); if (wikiPageDB != null && WikiPageCur.DateTimeSaved < wikiPageDB.DateTimeSaved) { if (WikiPageCur.IsDraft) { if (!MsgBox.Show(this, MsgBoxButtons.OKCancel, "The wiki page has been edited since this draft was last saved. Overwrite and continue?")) { return; } } else if (!MsgBox.Show(this, MsgBoxButtons.OKCancel, "This page has been modified and saved since it was opened on this computer. Save anyway?")) { return; } } List <string> listInvalidWikiPages = WikiPages.GetMissingPageTitles(textContent.Text); string message = Lan.g(this, "This page has the following invalid wiki page link(s):") + "\r\n" + string.Join("\r\n", listInvalidWikiPages.Take(10)) + "\r\n" + (listInvalidWikiPages.Count > 10 ? "...\r\n\r\n" : "\r\n") + Lan.g(this, "Create the wikipage(s) automatically?"); if (listInvalidWikiPages.Count != 0 && MsgBox.Show(this, MsgBoxButtons.YesNo, message)) { foreach (string title in listInvalidWikiPages) { WikiPage wp = new WikiPage(); wp.PageTitle = title; wp.UserNum = Security.CurUser.UserNum; wp.PageContent = "[[" + WikiPageCur.WikiPageNum + "]]\r\n" //link back + "<h1>" + title + "</h1>\r\n"; //page title WikiPages.InsertAndArchive(wp); } } WikiPageCur.PageContent = WikiPages.ConvertTitlesToPageNums(textContent.Text); WikiPageCur.UserNum = Security.CurUser.UserNum; Regex regex = new Regex(@"\[\[(keywords:).+?\]\]"); //only grab first match Match m = regex.Match(textContent.Text); WikiPageCur.KeyWords = m.Value.Replace("[[keywords:", "").TrimEnd(']'); //will be empty string if no match if (WikiPageCur.IsDraft) { WikiPages.DeleteDraft(WikiPageCur); //remove the draft from the database. WikiPageCur.IsDraft = false; //no longer a draft if (wikiPageDB != null) //wikiPageDb should never be null, otherwise there was no way to get to this draft. //We need to set the draft copy of the wiki page to the WikiPageNum of the real page. //This is because the draft is the most up to date copy of the wiki page, and is about to be saved, however, we want to maintain any //links there may be to the real wiki page, since we link by WikiPageNum instead of PageTitle (see JobNum 4429 for more info) { WikiPageCur.WikiPageNum = wikiPageDB.WikiPageNum; } } WikiPages.InsertAndArchive(WikiPageCur); //If the form was given an action, fire it. _onWikiSaved?.Invoke(WikiPageCur.PageTitle); FormWiki formWiki = (FormWiki)this.OwnerForm; if (formWiki != null && !formWiki.IsDisposed) { formWiki.RefreshPage(WikiPageCur.PageTitle); } HasSaved = true; Close(); //should be dialog result?? }