namespace demo
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Xml;
///
/// Summary description for wiki.
///
public class wiki : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Panel PanelContent;
protected System.Web.UI.WebControls.Panel PanelEdit;
protected System.Web.UI.WebControls.TextBox TextBoxContent;
protected System.Web.UI.WebControls.Label LabelContent;
protected System.Web.UI.WebControls.Button Button1;
private string _wikiRoot = "";
private int _wikiNodeType;
private int _wikiEntryNodeType;
private string _wikiContentAlias = "bodyText";
private umbraco.layoutControls.umbracoPageHolder _ph;
[umbraco.cms.businesslogic.macro.MacroComment("The ID of the document type")]
public int WikiNodeType
{
set {_wikiNodeType = value;}
}
public int WikiEntryNodeType
{
set {_wikiEntryNodeType = value;}
}
public string WikiContentAlias
{
set {_wikiContentAlias = value;}
}
public String WikiRoot
{
set {_wikiRoot = value;}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Test for edit
bool edit = false;
// Get content by finding the umbraco page holder control
_ph = (umbraco.layoutControls.umbracoPageHolder) Page.FindControl("umbPageHolder");
int pageID = _ph.pageID;
// Check for creation
if (Request["umbWikiCreate"] != null)
{
if (Request["umbWikiCreate"] != "")
{
try
{
// Validate user
umbraco.BasePages.BasePage bp = new umbraco.BasePages.BasePage();
bp.ensureContext();
// Create new umbraco document
umbraco.cms.businesslogic.web.Document d = umbraco.cms.businesslogic.web.Document.MakeNew(
Request["umbWikiCreate"],
new umbraco.cms.businesslogic.web.DocumentType(_wikiEntryNodeType),
bp.getUser(),
FindWikiRootDocument(pageID).Id);
// Publish changes
d.Publish(bp.getUser());
umbraco.library.PublishSingleNode(d.Id);
// Redirect to the new page in edit mode
Response.Redirect(umbraco.library.NiceUrl(d.Id) + "?edit=true");
Response.End();
}
catch {}
}
}
if (Request["edit"] != null)
{
if (Request["edit"] == "true")
{
try
{
umbraco.BasePages.BasePage bp = new umbraco.BasePages.BasePage();
bp.ensureContext();
edit = true;
}
catch {}
}
}
if (edit)
{
umbraco.BasePages.BasePage bp = new umbraco.BasePages.BasePage();
bp.ensureContext();
PanelEdit.Visible = true;
PanelContent.Visible = false;
}
else
{
PanelEdit.Visible = false;
PanelContent.Visible = true;
}
// Test for wiki root
if (_wikiRoot == "")
{
// Get currentpage as document object
umbraco.cms.businesslogic.web.Document root = new umbraco.cms.businesslogic.web.Document(pageID);
if (root.ContentType.Id != _wikiNodeType)
{
while (_wikiRoot == "" && root.Level > 1)
{
root = new umbraco.cms.businesslogic.web.Document(root.Parent.Id);
if (root.ContentType.Id == _wikiNodeType)
break;
}
}
_wikiRoot = umbraco.library.NiceUrl(root.Id);
}
if (!IsPostBack)
{
string wikiContent = (string) _ph.Elements[_wikiContentAlias];
TextBoxContent.Text = wikiContent;
LabelContent.Text = "
" + formatWikiWord(_ph.PageName) + "
" + "" + wikiContent.Replace("\n", "
\n") + "
";
}
// Do wiki regexp
string aspxEnding = "";
if (System.Configuration.ConfigurationSettings.AppSettings["umbracoUseDirectoryUrls"].ToLower() != "true")
aspxEnding = ".aspx";
string wikiWord = @"(?" + formatWikiWord(m.Value) + "");
else
LabelContent.Text = LabelContent.Text.Replace(m.Value, "" + m.Value + "");
}
}
///
/// Find the root document from the current wiki, by matching the current pages and its parents with
/// the document type sat in the variable "_wikiNodeType"
///
///
///
private umbraco.cms.businesslogic.web.Document FindWikiRootDocument(int pageID)
{
// Get currentpage as document object
umbraco.cms.businesslogic.web.Document root = new umbraco.cms.businesslogic.web.Document(pageID);
if (root.ContentType.Id != _wikiNodeType)
{
while (_wikiRoot == "" && root.Level > 1)
{
root = new umbraco.cms.businesslogic.web.Document(root.Parent.Id);
if (root.ContentType.Id == _wikiNodeType)
break;
}
}
return root;
}
///
/// Checks if the current url matches a page in the umbraco data storage.
/// A bit nasty code just copy pasted from the runtime part...
///
///
///
private bool wikiPageExists(string url)
{
string [] requestRawUrl = url.Split("/".ToCharArray());
string _pageXPathQuery = "/root/node";
for (int i=0; i<=requestRawUrl.GetUpperBound(0); i++)
{
if (requestRawUrl[i] != "")
_pageXPathQuery += "/node [@urlName= \"" + requestRawUrl[i].Replace(".aspx","").ToLower().Replace(" ", "") + "\"]";
}
XmlNode currentPage = umbraco.content.xmlContent.DocumentElement.SelectSingleNode(_pageXPathQuery);
if (currentPage == null)
return false;
else
return true;
}
///
/// Changes a wiki word into something more readable, like "UmbracoSite" is translated to "Umbraco Site"
///
///
///
private string formatWikiWord(string wikiWord)
{
string tempWord = "";
for (int i=0;i
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
///
/// Called when pressing the save button
///
///
///
private void Button1_Click(object sender, System.EventArgs e)
{
_ph = (umbraco.layoutControls.umbracoPageHolder) Page.FindControl("umbPageHolder");
if (_ph.pageID > 0)
{
try
{
// Validate current user
umbraco.BasePages.BasePage bp = new umbraco.BasePages.BasePage();
bp.ensureContext();
// Get the current document from the business logic
umbraco.cms.businesslogic.web.Document d = new umbraco.cms.businesslogic.web.Document(_ph.pageID);
// Assign the document property to the current value from the textbox (the document property is based
// on the alias assigned through the variable "_wikiContentAlias".
d.getProperty(_wikiContentAlias).Value = TextBoxContent.Text;
// Publish the changes
d.Publish(bp.getUser());
umbraco.library.PublishSingleNode(d.Id);
// Redirect to the updated page
Response.Redirect(umbraco.library.NiceUrl(d.Id));
Response.End();
}
catch (Exception saveE)
{
throw new Exception("Error updating content, user may not be logged in...
" + saveE.ToString());
}
}
}
}
}