From 69ccf5a13881b370af185b28a3de259bbdc815db Mon Sep 17 00:00:00 2001 From: PerPloug Date: Mon, 21 Feb 2011 12:49:45 -0100 Subject: [PATCH] AddRestExtension package action, swicth to use XmlHelper instead of doc.load(); --- .../PackageActions/StandardPackageActions.cs | 1604 ++++++++--------- 1 file changed, 801 insertions(+), 803 deletions(-) diff --git a/umbraco/cms/businesslogic/Packager/PackageActions/StandardPackageActions.cs b/umbraco/cms/businesslogic/Packager/PackageActions/StandardPackageActions.cs index faa98054a6..e213275b6f 100644 --- a/umbraco/cms/businesslogic/Packager/PackageActions/StandardPackageActions.cs +++ b/umbraco/cms/businesslogic/Packager/PackageActions/StandardPackageActions.cs @@ -7,860 +7,858 @@ using System.Linq; using umbraco.IO; namespace umbraco.cms.businesslogic.packager.standardPackageActions { - /*Build in standard actions */ + /*Build in standard actions */ - /// - /// This class implements the IPackageAction Interface, used to execute code when packages are installed. - /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. - /// - public class addApplicationTree : umbraco.interfaces.IPackageAction { + /// + /// This class implements the IPackageAction Interface, used to execute code when packages are installed. + /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. + /// + public class addApplicationTree : umbraco.interfaces.IPackageAction { - #region IPackageAction Members + #region IPackageAction Members - /// - /// Executes the specified package action. - /// - /// Name of the package. - /// The XML data. - /// - /// - /// - /// - public bool Execute(string packageName, XmlNode xmlData) { - bool silent = bool.Parse(xmlData.Attributes["silent"].Value); - bool initialize = bool.Parse(xmlData.Attributes["initialize"].Value); - byte sortOrder = byte.Parse(xmlData.Attributes["sortOrder"].Value); + /// + /// Executes the specified package action. + /// + /// Name of the package. + /// The XML data. + /// + /// + /// + /// + public bool Execute(string packageName, XmlNode xmlData) { + bool silent = bool.Parse(xmlData.Attributes["silent"].Value); + bool initialize = bool.Parse(xmlData.Attributes["initialize"].Value); + byte sortOrder = byte.Parse(xmlData.Attributes["sortOrder"].Value); - string applicationAlias = xmlData.Attributes["applicationAlias"].Value; - string treeAlias = xmlData.Attributes["treeAlias"].Value; - string treeTitle = xmlData.Attributes["treeTitle"].Value; - string iconOpened = xmlData.Attributes["iconOpened"].Value; - string iconClosed = xmlData.Attributes["iconClosed"].Value; + string applicationAlias = xmlData.Attributes["applicationAlias"].Value; + string treeAlias = xmlData.Attributes["treeAlias"].Value; + string treeTitle = xmlData.Attributes["treeTitle"].Value; + string iconOpened = xmlData.Attributes["iconOpened"].Value; + string iconClosed = xmlData.Attributes["iconClosed"].Value; - string assemblyName = xmlData.Attributes["assemblyName"].Value; - string type = xmlData.Attributes["treeHandlerType"].Value; - string action = xmlData.Attributes["action"].Value; + string assemblyName = xmlData.Attributes["assemblyName"].Value; + string type = xmlData.Attributes["treeHandlerType"].Value; + string action = xmlData.Attributes["action"].Value; - BusinessLogic.ApplicationTree.MakeNew(silent, initialize, sortOrder, applicationAlias, treeAlias, treeTitle, iconClosed, iconOpened, assemblyName, type, action); + BusinessLogic.ApplicationTree.MakeNew(silent, initialize, sortOrder, applicationAlias, treeAlias, treeTitle, iconClosed, iconOpened, assemblyName, type, action); - return true; - } + return true; + } - /// - /// Undoes the action - /// - /// Name of the package. - /// The XML data. - /// - public bool Undo(string packageName, XmlNode xmlData) { - string treeAlias = xmlData.Attributes["treeAlias"].Value; - BusinessLogic.ApplicationTree.getByAlias(treeAlias).Delete(); - return true; - } + /// + /// Undoes the action + /// + /// Name of the package. + /// The XML data. + /// + public bool Undo(string packageName, XmlNode xmlData) { + string treeAlias = xmlData.Attributes["treeAlias"].Value; + BusinessLogic.ApplicationTree.getByAlias(treeAlias).Delete(); + return true; + } - /// - /// Action alias. - /// - /// - public string Alias() { - return "addApplicationTree"; - } + /// + /// Action alias. + /// + /// + public string Alias() { + return "addApplicationTree"; + } - #endregion + #endregion - - public XmlNode SampleXml() { + public XmlNode SampleXml() { - string sample = ""; - return helper.parseStringToXmlNode(sample); - } + string sample = ""; + return helper.parseStringToXmlNode(sample); + } + } - - } + /// + /// This class implements the IPackageAction Interface, used to execute code when packages are installed. + /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. + /// + public class addApplication : umbraco.interfaces.IPackageAction { - /// - /// This class implements the IPackageAction Interface, used to execute code when packages are installed. - /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. - /// - public class addApplication : umbraco.interfaces.IPackageAction { + #region IPackageAction Members - #region IPackageAction Members + /// + /// Installs a new application in umbraco. + /// + /// Name of the package. + /// The XML data. + /// + /// + /// + /// true if successfull + public bool Execute(string packageName, XmlNode xmlData) { + string name = xmlData.Attributes["appName"].Value; + string alias = xmlData.Attributes["appAlias"].Value; + string icon = xmlData.Attributes["appIcon"].Value; - /// - /// Installs a new application in umbraco. - /// - /// Name of the package. - /// The XML data. - /// - /// - /// - /// true if successfull - public bool Execute(string packageName, XmlNode xmlData) { - string name = xmlData.Attributes["appName"].Value; - string alias = xmlData.Attributes["appAlias"].Value; - string icon = xmlData.Attributes["appIcon"].Value; + BusinessLogic.Application.MakeNew(name, alias, icon); - BusinessLogic.Application.MakeNew(name, alias, icon); + return true; + } - return true; - } + public bool Undo(string packageName, XmlNode xmlData) { + string alias = xmlData.Attributes["appAlias"].Value; + BusinessLogic.Application.getByAlias(alias).Delete(); + return true; + } + /// + /// Action alias. + /// + /// + public string Alias() { + return "addApplication"; + } - public bool Undo(string packageName, XmlNode xmlData) { - string alias = xmlData.Attributes["appAlias"].Value; - BusinessLogic.Application.getByAlias(alias).Delete(); - return true; - } - /// - /// Action alias. - /// - /// - public string Alias() { - return "addApplication"; - } + #endregion - #endregion + public XmlNode SampleXml() { + throw new NotImplementedException(); + } - public XmlNode SampleXml() { - throw new NotImplementedException(); - } + } - } + /// + /// + /// + public class addDashboardSection : umbraco.interfaces.IPackageAction { + #region IPackageAction Members - /// - /// - /// - public class addDashboardSection : umbraco.interfaces.IPackageAction { - #region IPackageAction Members - - /// - /// Installs a dashboard section. This action reuses the action XML, so it has to be valid dashboard markup. - /// - /// Name of the package. - /// The XML data. - /// true if successfull - /// - /// - /// - ///
- /// + /// + /// Installs a dashboard section. This action reuses the action XML, so it has to be valid dashboard markup. + /// + /// Name of the package. + /// The XML data. + /// true if successfull + /// + /// + /// + ///
+ /// /// default /// content - /// - /// + /// + /// /// /usercontrols/dashboard/latestEdits.ascx - /// /usercontrols/umbracoBlog/dashboardBlogPostCreate.ascx - /// - /// /// /usercontrols/umbracoBlog/dashboardBlogPostCreate.ascx - /// - ///
- ///
- ///
- ///
- public bool Execute(string packageName, XmlNode xmlData) { - //this will need a complete section node to work... - - if (xmlData.HasChildNodes) { - string sectionAlias = xmlData.Attributes["dashboardAlias"].Value; - string dbConfig = SystemFiles.DashboardConfig; - - XmlNode section = xmlData.SelectSingleNode("./section"); - XmlDocument dashboardFile = xmlHelper.OpenAsXmlDocument(dbConfig); - - XmlNode importedSection = dashboardFile.ImportNode(section, true); - - XmlAttribute alias = xmlHelper.addAttribute(dashboardFile, "alias", sectionAlias); - importedSection.Attributes.Append(alias); - - dashboardFile.DocumentElement.AppendChild(importedSection); - - dashboardFile.Save(IOHelper.MapPath(dbConfig)); - - return true; - } - - return false; - } - - - public string Alias() { - return "addDashboardSection"; - } - - public bool Undo(string packageName, XmlNode xmlData) { - - string sectionAlias = xmlData.Attributes["dashboardAlias"].Value; - string dbConfig = SystemFiles.DashboardConfig; - XmlDocument dashboardFile = xmlHelper.OpenAsXmlDocument(dbConfig); - - XmlNode section = dashboardFile.SelectSingleNode("//section [@alias = '" + sectionAlias + "']"); - - if(section != null){ - - dashboardFile.SelectSingleNode("/dashBoard").RemoveChild(section); - dashboardFile.Save(IOHelper.MapPath(dbConfig)); - } - - return true; - } - - #endregion - - public XmlNode SampleXml() { - throw new NotImplementedException(); - } - - } - /// - /// This class implements the IPackageAction Interface, used to execute code when packages are installed. - /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. - /// - public class allowDoctype : umbraco.interfaces.IPackageAction { - - #region IPackageAction Members - - /// - /// Allows a documentType to be created below another documentType. - /// - /// Name of the package. - /// The XML data. - /// - /// - /// - /// Returns true on success - public bool Execute(string packageName, XmlNode xmlData) { - string doctypeName = xmlData.Attributes["documentTypeAlias"].Value; - string parentDoctypeName = xmlData.Attributes["parentDocumentTypeAlias"].Value; - - cms.businesslogic.ContentType ct = cms.businesslogic.ContentType.GetByAlias(doctypeName); - cms.businesslogic.ContentType parentct = cms.businesslogic.ContentType.GetByAlias(parentDoctypeName); - - if (ct != null && parentct != null) { - bool containsId = false; - ArrayList tmp = new ArrayList(); - - foreach (int i in parentct.AllowedChildContentTypeIDs.ToList()) { - tmp.Add(i); - if (i == ct.Id) - containsId = true; - } - - if (!containsId) { - - int[] ids = new int[tmp.Count + 1]; - for (int i = 0; i < tmp.Count; i++) ids[i] = (int)tmp[i]; - ids[ids.Length - 1] = ct.Id; - - parentct.AllowedChildContentTypeIDs = ids; - parentct.Save(); - return true; - } - } - return false; - } - - //this has no undo. - /// - /// This action has no undo. - /// - /// Name of the package. - /// The XML data. - /// - public bool Undo(string packageName, XmlNode xmlData) { - return true; - } - - /// - /// Action Alias. - /// - /// - public string Alias() { - return "allowDocumenttype"; - } - - #endregion - - public XmlNode SampleXml() { - throw new NotImplementedException(); - } - - } - - public class addXsltExtension : umbraco.interfaces.IPackageAction { - #region IPackageAction Members - - public bool Execute(string packageName, XmlNode xmlData) { - - string _assembly = xmlData.Attributes["assembly"].Value; - string _type = xmlData.Attributes["type"].Value; - string _alias = xmlData.Attributes["extensionAlias"].Value; - string xeConfig = SystemFiles.XsltextensionsConfig; - - XmlDocument xdoc = new XmlDocument(); - xdoc.PreserveWhitespace = true; - xdoc = xmlHelper.OpenAsXmlDocument(xeConfig); - - XmlNode xn = xdoc.SelectSingleNode("//XsltExtensions"); - - if (xn != null) { - bool insertExt = true; - if (xn.HasChildNodes) { - foreach (XmlNode ext in xn.SelectNodes("//ext")) { - if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) - insertExt = false; - } - } - if (insertExt) { - XmlNode newExt = umbraco.xmlHelper.addTextNode(xdoc, "ext", ""); - newExt.Attributes.Append(umbraco.xmlHelper.addAttribute(xdoc, "assembly", _assembly.Replace("/bin/",""))); - newExt.Attributes.Append(umbraco.xmlHelper.addAttribute(xdoc, "type", _type)); - newExt.Attributes.Append(umbraco.xmlHelper.addAttribute(xdoc, "alias", _alias)); - xn.AppendChild(newExt); - - - xdoc.Save(IOHelper.MapPath(xeConfig)); - return true; - } - } - return false; - } - - public string Alias() { - return "addXsltExtension"; - } - - public bool Undo(string packageName, XmlNode xmlData) { - string _assembly = xmlData.Attributes["assembly"].Value; - string _type = xmlData.Attributes["type"].Value; - string _alias = xmlData.Attributes["extensionAlias"].Value; - string xeConfig = SystemFiles.XsltextensionsConfig; - - XmlDocument xdoc = new XmlDocument(); - xdoc.PreserveWhitespace = true; - xdoc = xmlHelper.OpenAsXmlDocument(xeConfig); - - XmlNode xn = xdoc.SelectSingleNode("//XsltExtensions"); - - if (xn != null) { - bool inserted = false; - if (xn.HasChildNodes) { - foreach (XmlNode ext in xn.SelectNodes("//ext")) { - if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) { - xn.RemoveChild(ext); - inserted = true; - } - } - } - - if (inserted) { - xdoc.Save(IOHelper.MapPath(xeConfig)); - return true; - } - } - return false; - } - - #endregion - - public XmlNode SampleXml() { - throw new NotImplementedException(); - } - - } - - public class addRestExtension : umbraco.interfaces.IPackageAction { - #region IPackageAction Members - - public bool Execute(string packageName, XmlNode xmlData) { - - XmlNodeList _newExts = xmlData.SelectNodes("//ext"); - - if (_newExts.Count > 0) { - - string reConfig = SystemFiles.RestextensionsConfig; - - XmlDocument xdoc = new XmlDocument(); - xdoc.PreserveWhitespace = true; - xdoc.Load(reConfig); - - XmlNode xn = xdoc.SelectSingleNode("//RestExtensions"); - - if (xn != null) { - for (int i = 0; i < _newExts.Count; i++) { - XmlNode newExt = _newExts[i]; - string _alias = newExt.Attributes["alias"].Value; - - bool insertExt = true; - if (xn.HasChildNodes) { - foreach (XmlNode ext in xn.SelectNodes("//ext")) { - if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) - insertExt = false; - } - } - - if (insertExt) { - xn.AppendChild(xdoc.ImportNode(newExt, true)); - } - } - - xdoc.Save(IOHelper.MapPath(reConfig)); - return true; - } - } - return false; - } - - public string Alias() { - return "addRestExtension"; - } - - public bool Undo(string packageName, XmlNode xmlData) { - - XmlNodeList _newExts = xmlData.SelectNodes("//ext"); - - if (_newExts.Count > 0) { - string reConfig = SystemFiles.RestextensionsConfig; - - XmlDocument xdoc = new XmlDocument(); - xdoc.PreserveWhitespace = true; - xdoc.Load(reConfig); - - XmlNode xn = xdoc.SelectSingleNode("//RestExtensions"); - - if (xn != null) { - bool inserted = false; - - for (int i = 0; i < _newExts.Count; i++) { - XmlNode newExt = _newExts[i]; - string _alias = newExt.Attributes["alias"].Value; - if (xn.HasChildNodes) { - foreach (XmlNode ext in xn.SelectNodes("//ext")) { - if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) { - xn.RemoveChild(ext); - inserted = true; - } - } - } - } - - if (inserted) { - xdoc.Save(IOHelper.MapPath(reConfig)); - return true; - } - } - } - return false; - } - - #endregion - - public XmlNode SampleXml() { - throw new NotImplementedException(); - } - - } - - /// - /// This class implements the IPackageAction Interface, used to execute code when packages are installed. - /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. - /// - public class moveRootDocument : umbraco.interfaces.IPackageAction { - #region IPackageAction Members - - /// - /// Executes the specified package action. - /// - /// Name of the package. - /// The XML data. - /// - /// - /// - /// True if executed succesfully - public bool Execute(string packageName, XmlNode xmlData) { - - string documentName = xmlData.Attributes["documentName"].Value; - string parentDocumentType = xmlData.Attributes["parentDocumentType"].Value; - string parentDocumentName = ""; - - if (xmlData.Attributes["parentDocumentName"] != null) - parentDocumentName = xmlData.Attributes["parentDocumentName"].Value; - - int parentDocid = 0; - - ContentType ct = ContentType.GetByAlias(parentDocumentType); - Content[] docs = web.Document.getContentOfContentType(ct); - - if (docs.Length > 0) { - if (String.IsNullOrEmpty(parentDocumentName)) - parentDocid = docs[0].Id; - else { - foreach (Content doc in docs) { - if (doc.Text == parentDocumentName) - parentDocid = doc.Id; - } - } - } - - if (parentDocid > 0) { - web.Document[] rootDocs = web.Document.GetRootDocuments(); - - foreach (web.Document rootDoc in rootDocs) { - if (rootDoc.Text == documentName) { - rootDoc.Move(parentDocid); - rootDoc.PublishWithSubs(new umbraco.BusinessLogic.User(0)); - } - } - } - - - return true; - } - - //this has no undo. - /// - /// This action has no undo. - /// - /// Name of the package. - /// The XML data. - /// - public bool Undo(string packageName, XmlNode xmlData) { - return true; - } - - /// - /// Action alias - /// - /// - public string Alias() { - return "moveRootDocument"; - } - - #endregion - - public XmlNode SampleXml() { - throw new NotImplementedException(); - } - - } - - /// - /// This class implements the IPackageAction Interface, used to execute code when packages are installed. - /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. - /// - public class publishRootDocument : umbraco.interfaces.IPackageAction { - #region IPackageAction Members - - /// - /// Executes the specified package action. - /// - /// Name of the package. - /// The XML data. - /// - /// - /// - /// True if executed succesfully - public bool Execute(string packageName, XmlNode xmlData) { - - string documentName = xmlData.Attributes["documentName"].Value; - - int parentDocid = 0; - - web.Document[] rootDocs = web.Document.GetRootDocuments(); - - foreach (web.Document rootDoc in rootDocs) { - if (rootDoc.Text.Trim() == documentName.Trim() && rootDoc != null && rootDoc.ContentType != null) { - - rootDoc.PublishWithChildrenWithResult(umbraco.BusinessLogic.User.GetUser(0)); - - - break; - } - } - return true; - } - - //this has no undo. - /// - /// This action has no undo. - /// - /// Name of the package. - /// The XML data. - /// - public bool Undo(string packageName, XmlNode xmlData) { - return true; - } - - /// - /// Action alias - /// - /// - public string Alias() { - return "publishRootDocument"; - } - #endregion - - public XmlNode SampleXml() { - throw new NotImplementedException(); - } - - } - - /// - /// This class implements the IPackageAction Interface, used to execute code when packages are installed. - /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. - /// addStringToHtmlElement adds a string to specific HTML element in a specific template, and can either append or prepend it. - /// It uses the action xml node to do this, exemple action xml node: - /// - /// The above will add the string "hello world!" to the first html element with the id "newsSection" in the template "news" - /// - public class addStringToHtmlElement : umbraco.interfaces.IPackageAction { - #region IPackageAction Members - - /// - /// Executes the specified package action. - /// - /// Name of the package. - /// The XML data. - /// - /// - /// - /// True if executed successfully - public bool Execute(string packageName, XmlNode xmlData) { - - BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, "executing addStringToHtmlElement"); - - string templateAlias = xmlData.Attributes["templateAlias"].Value; - string htmlElementId = xmlData.Attributes["htmlElementId"].Value; - string position = xmlData.Attributes["position"].Value; - string value = xmlHelper.GetNodeValue(xmlData); - template.Template tmp = template.Template.GetByAlias(templateAlias); - - if (UmbracoSettings.UseAspNetMasterPages) - value = tmp.EnsureMasterPageSyntax(value); - - _addStringToHtmlElement(tmp, value, templateAlias, htmlElementId, position); - - return true; - } - - - /// - /// Undoes the addStringToHtml Execute() method, by removing the same string from the same template. - /// - /// Name of the package. - /// The XML data. - /// - public bool Undo(string packageName, XmlNode xmlData) { - string templateAlias = xmlData.Attributes["templateAlias"].Value; - string htmlElementId = xmlData.Attributes["htmlElementId"].Value; - string value = xmlHelper.GetNodeValue(xmlData); - template.Template tmp = template.Template.GetByAlias(templateAlias); - - if (UmbracoSettings.UseAspNetMasterPages) - value = tmp.EnsureMasterPageSyntax(value); - - _removeStringFromHtmlElement(tmp, value, templateAlias, htmlElementId); - return true; - } - - /// - /// Action alias. - /// - /// - public string Alias() { - return "addStringToHtmlElement"; - } - - private void _addStringToHtmlElement(template.Template tmp, string value, string templateAlias, string htmlElementId, string position) { - bool hasAspNetContentBeginning = false; - string design = ""; - string directive = ""; - - if (tmp != null) { - try { - XmlDocument templateXml = new XmlDocument(); - templateXml.PreserveWhitespace = true; - - //Make sure that directive is remove before hacked non html4 compatiple replacement action... - design = tmp.Design; - - - splitDesignAndDirective(ref design, ref directive); - - //making sure that the template xml has a root node... - if (tmp.MasterTemplate > 0) - templateXml.LoadXml( helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, "" + design + "", true)); - else - templateXml.LoadXml(helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, design, true)); - - XmlNode xmlElement = templateXml.SelectSingleNode("//* [@id = '" + htmlElementId + "']"); - - if (xmlElement != null) { - - if (position == "beginning") { - xmlElement.InnerXml = "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, value, true) + "\n" + xmlElement.InnerXml; - } else { - xmlElement.InnerXml = xmlElement.InnerXml + "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, value, true) + "\n"; - } - } - - tmp.Design = directive + "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, templateXml.OuterXml, false); - tmp.Save(); - } catch (Exception ex) { - umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, ex.ToString()); - } - } else { - umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, "template not found"); - } - } - - private void _removeStringFromHtmlElement(template.Template tmp, string value, string templateAlias, string htmlElementId) { - bool hasAspNetContentBeginning = false; - string design = ""; - string directive = ""; - - - if (tmp != null) { - try { - XmlDocument templateXml = new XmlDocument(); - templateXml.PreserveWhitespace = true; - - //Make sure that directive is remove before hacked non html4 compatiple replacement action... - design = tmp.Design; - splitDesignAndDirective(ref design, ref directive); - - //making sure that the template xml has a root node... - if (tmp.MasterTemplate > 0) - templateXml.LoadXml( helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, "" + design + "", true)); - else - templateXml.LoadXml( helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, design, true)); - - XmlNode xmlElement = templateXml.SelectSingleNode("//* [@id = '" + htmlElementId + "']"); - + /// + /// + /// /usercontrols/umbracoBlog/dashboardBlogPostCreate.ascx + /// + ///
+ ///
+ ///
+ ///
+ public bool Execute(string packageName, XmlNode xmlData) { + //this will need a complete section node to work... + + if (xmlData.HasChildNodes) { + string sectionAlias = xmlData.Attributes["dashboardAlias"].Value; + string dbConfig = SystemFiles.DashboardConfig; + + XmlNode section = xmlData.SelectSingleNode("./section"); + XmlDocument dashboardFile = xmlHelper.OpenAsXmlDocument(dbConfig); + + XmlNode importedSection = dashboardFile.ImportNode(section, true); + + XmlAttribute alias = xmlHelper.addAttribute(dashboardFile, "alias", sectionAlias); + importedSection.Attributes.Append(alias); + + dashboardFile.DocumentElement.AppendChild(importedSection); + + dashboardFile.Save(IOHelper.MapPath(dbConfig)); + + return true; + } + + return false; + } + + + public string Alias() { + return "addDashboardSection"; + } + + public bool Undo(string packageName, XmlNode xmlData) { + + string sectionAlias = xmlData.Attributes["dashboardAlias"].Value; + string dbConfig = SystemFiles.DashboardConfig; + XmlDocument dashboardFile = xmlHelper.OpenAsXmlDocument(dbConfig); + + XmlNode section = dashboardFile.SelectSingleNode("//section [@alias = '" + sectionAlias + "']"); + + if(section != null){ + + dashboardFile.SelectSingleNode("/dashBoard").RemoveChild(section); + dashboardFile.Save(IOHelper.MapPath(dbConfig)); + } + + return true; + } + + #endregion + + public XmlNode SampleXml() { + throw new NotImplementedException(); + } + + } + /// + /// This class implements the IPackageAction Interface, used to execute code when packages are installed. + /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. + /// + public class allowDoctype : umbraco.interfaces.IPackageAction { + + #region IPackageAction Members + + /// + /// Allows a documentType to be created below another documentType. + /// + /// Name of the package. + /// The XML data. + /// + /// + /// + /// Returns true on success + public bool Execute(string packageName, XmlNode xmlData) { + string doctypeName = xmlData.Attributes["documentTypeAlias"].Value; + string parentDoctypeName = xmlData.Attributes["parentDocumentTypeAlias"].Value; + + cms.businesslogic.ContentType ct = cms.businesslogic.ContentType.GetByAlias(doctypeName); + cms.businesslogic.ContentType parentct = cms.businesslogic.ContentType.GetByAlias(parentDoctypeName); + + if (ct != null && parentct != null) { + bool containsId = false; + ArrayList tmp = new ArrayList(); + + foreach (int i in parentct.AllowedChildContentTypeIDs.ToList()) { + tmp.Add(i); + if (i == ct.Id) + containsId = true; + } + + if (!containsId) { + + int[] ids = new int[tmp.Count + 1]; + for (int i = 0; i < tmp.Count; i++) ids[i] = (int)tmp[i]; + ids[ids.Length - 1] = ct.Id; + + parentct.AllowedChildContentTypeIDs = ids; + parentct.Save(); + return true; + } + } + return false; + } + + //this has no undo. + /// + /// This action has no undo. + /// + /// Name of the package. + /// The XML data. + /// + public bool Undo(string packageName, XmlNode xmlData) { + return true; + } + + /// + /// Action Alias. + /// + /// + public string Alias() { + return "allowDocumenttype"; + } + + #endregion + + public XmlNode SampleXml() { + throw new NotImplementedException(); + } + + } + + public class addXsltExtension : umbraco.interfaces.IPackageAction { + #region IPackageAction Members + + public bool Execute(string packageName, XmlNode xmlData) { + + string _assembly = xmlData.Attributes["assembly"].Value; + string _type = xmlData.Attributes["type"].Value; + string _alias = xmlData.Attributes["extensionAlias"].Value; + string xeConfig = SystemFiles.XsltextensionsConfig; + + XmlDocument xdoc = new XmlDocument(); + xdoc.PreserveWhitespace = true; + xdoc = xmlHelper.OpenAsXmlDocument(xeConfig); + + XmlNode xn = xdoc.SelectSingleNode("//XsltExtensions"); + + if (xn != null) { + bool insertExt = true; + if (xn.HasChildNodes) { + foreach (XmlNode ext in xn.SelectNodes("//ext")) { + if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) + insertExt = false; + } + } + if (insertExt) { + XmlNode newExt = umbraco.xmlHelper.addTextNode(xdoc, "ext", ""); + newExt.Attributes.Append(umbraco.xmlHelper.addAttribute(xdoc, "assembly", _assembly.Replace("/bin/",""))); + newExt.Attributes.Append(umbraco.xmlHelper.addAttribute(xdoc, "type", _type)); + newExt.Attributes.Append(umbraco.xmlHelper.addAttribute(xdoc, "alias", _alias)); + xn.AppendChild(newExt); + + + xdoc.Save(IOHelper.MapPath(xeConfig)); + return true; + } + } + return false; + } + + public string Alias() { + return "addXsltExtension"; + } + + public bool Undo(string packageName, XmlNode xmlData) { + string _assembly = xmlData.Attributes["assembly"].Value; + string _type = xmlData.Attributes["type"].Value; + string _alias = xmlData.Attributes["extensionAlias"].Value; + string xeConfig = SystemFiles.XsltextensionsConfig; + + XmlDocument xdoc = new XmlDocument(); + xdoc.PreserveWhitespace = true; + xdoc = xmlHelper.OpenAsXmlDocument(xeConfig); + + XmlNode xn = xdoc.SelectSingleNode("//XsltExtensions"); + + if (xn != null) { + bool inserted = false; + if (xn.HasChildNodes) { + foreach (XmlNode ext in xn.SelectNodes("//ext")) { + if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) { + xn.RemoveChild(ext); + inserted = true; + } + } + } + + if (inserted) { + xdoc.Save(IOHelper.MapPath(xeConfig)); + return true; + } + } + return false; + } + + #endregion + + public XmlNode SampleXml() { + throw new NotImplementedException(); + } + + } + + public class addRestExtension : umbraco.interfaces.IPackageAction { + #region IPackageAction Members + + public bool Execute(string packageName, XmlNode xmlData) { + + XmlNodeList _newExts = xmlData.SelectNodes("//ext"); + + if (_newExts.Count > 0) { + + string reConfig = SystemFiles.RestextensionsConfig; + + XmlDocument xdoc = new XmlDocument(); + xdoc.PreserveWhitespace = true; + xdoc = xmlHelper.OpenAsXmlDocument(reConfig); + + + XmlNode xn = xdoc.SelectSingleNode("//RestExtensions"); + + if (xn != null) { + for (int i = 0; i < _newExts.Count; i++) { + XmlNode newExt = _newExts[i]; + string _alias = newExt.Attributes["alias"].Value; + + bool insertExt = true; + if (xn.HasChildNodes) { + foreach (XmlNode ext in xn.SelectNodes("//ext")) { + if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) + insertExt = false; + } + } + + if (insertExt) { + xn.AppendChild(xdoc.ImportNode(newExt, true)); + } + } + + xdoc.Save(IOHelper.MapPath(reConfig)); + return true; + } + } + return false; + } + + public string Alias() { + return "addRestExtension"; + } + + public bool Undo(string packageName, XmlNode xmlData) { + + XmlNodeList _newExts = xmlData.SelectNodes("//ext"); + + if (_newExts.Count > 0) { + string reConfig = SystemFiles.RestextensionsConfig; + + XmlDocument xdoc = new XmlDocument(); + xdoc.PreserveWhitespace = true; + xdoc.Load(reConfig); + + XmlNode xn = xdoc.SelectSingleNode("//RestExtensions"); + + if (xn != null) { + bool inserted = false; + + for (int i = 0; i < _newExts.Count; i++) { + XmlNode newExt = _newExts[i]; + string _alias = newExt.Attributes["alias"].Value; + if (xn.HasChildNodes) { + foreach (XmlNode ext in xn.SelectNodes("//ext")) { + if (ext.Attributes["alias"] != null && ext.Attributes["alias"].Value == _alias) { + xn.RemoveChild(ext); + inserted = true; + } + } + } + } + + if (inserted) { + xdoc.Save(IOHelper.MapPath(reConfig)); + return true; + } + } + } + return false; + } + + #endregion + + public XmlNode SampleXml() { + throw new NotImplementedException(); + } + + } + + /// + /// This class implements the IPackageAction Interface, used to execute code when packages are installed. + /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. + /// + public class moveRootDocument : umbraco.interfaces.IPackageAction { + #region IPackageAction Members + + /// + /// Executes the specified package action. + /// + /// Name of the package. + /// The XML data. + /// + /// + /// + /// True if executed succesfully + public bool Execute(string packageName, XmlNode xmlData) { + + string documentName = xmlData.Attributes["documentName"].Value; + string parentDocumentType = xmlData.Attributes["parentDocumentType"].Value; + string parentDocumentName = ""; + + if (xmlData.Attributes["parentDocumentName"] != null) + parentDocumentName = xmlData.Attributes["parentDocumentName"].Value; + + int parentDocid = 0; + + ContentType ct = ContentType.GetByAlias(parentDocumentType); + Content[] docs = web.Document.getContentOfContentType(ct); + + if (docs.Length > 0) { + if (String.IsNullOrEmpty(parentDocumentName)) + parentDocid = docs[0].Id; + else { + foreach (Content doc in docs) { + if (doc.Text == parentDocumentName) + parentDocid = doc.Id; + } + } + } + + if (parentDocid > 0) { + web.Document[] rootDocs = web.Document.GetRootDocuments(); + + foreach (web.Document rootDoc in rootDocs) { + if (rootDoc.Text == documentName) { + rootDoc.Move(parentDocid); + rootDoc.PublishWithSubs(new umbraco.BusinessLogic.User(0)); + } + } + } + + + return true; + } + + //this has no undo. + /// + /// This action has no undo. + /// + /// Name of the package. + /// The XML data. + /// + public bool Undo(string packageName, XmlNode xmlData) { + return true; + } + + /// + /// Action alias + /// + /// + public string Alias() { + return "moveRootDocument"; + } + + #endregion + + public XmlNode SampleXml() { + throw new NotImplementedException(); + } + + } + + /// + /// This class implements the IPackageAction Interface, used to execute code when packages are installed. + /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. + /// + public class publishRootDocument : umbraco.interfaces.IPackageAction { + #region IPackageAction Members + + /// + /// Executes the specified package action. + /// + /// Name of the package. + /// The XML data. + /// + /// + /// + /// True if executed succesfully + public bool Execute(string packageName, XmlNode xmlData) { + + string documentName = xmlData.Attributes["documentName"].Value; + + int parentDocid = 0; + + web.Document[] rootDocs = web.Document.GetRootDocuments(); + + foreach (web.Document rootDoc in rootDocs) { + if (rootDoc.Text.Trim() == documentName.Trim() && rootDoc != null && rootDoc.ContentType != null) { + + rootDoc.PublishWithChildrenWithResult(umbraco.BusinessLogic.User.GetUser(0)); + + + break; + } + } + return true; + } + + //this has no undo. + /// + /// This action has no undo. + /// + /// Name of the package. + /// The XML data. + /// + public bool Undo(string packageName, XmlNode xmlData) { + return true; + } + + /// + /// Action alias + /// + /// + public string Alias() { + return "publishRootDocument"; + } + #endregion + + public XmlNode SampleXml() { + throw new NotImplementedException(); + } + + } + + /// + /// This class implements the IPackageAction Interface, used to execute code when packages are installed. + /// All IPackageActions only takes a PackageName and a XmlNode as input, and executes based on the data in the xmlnode. + /// addStringToHtmlElement adds a string to specific HTML element in a specific template, and can either append or prepend it. + /// It uses the action xml node to do this, exemple action xml node: + /// + /// The above will add the string "hello world!" to the first html element with the id "newsSection" in the template "news" + /// + public class addStringToHtmlElement : umbraco.interfaces.IPackageAction { + #region IPackageAction Members + + /// + /// Executes the specified package action. + /// + /// Name of the package. + /// The XML data. + /// + /// + /// + /// True if executed successfully + public bool Execute(string packageName, XmlNode xmlData) { + + BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, "executing addStringToHtmlElement"); + + string templateAlias = xmlData.Attributes["templateAlias"].Value; + string htmlElementId = xmlData.Attributes["htmlElementId"].Value; + string position = xmlData.Attributes["position"].Value; + string value = xmlHelper.GetNodeValue(xmlData); + template.Template tmp = template.Template.GetByAlias(templateAlias); + + if (UmbracoSettings.UseAspNetMasterPages) + value = tmp.EnsureMasterPageSyntax(value); + + _addStringToHtmlElement(tmp, value, templateAlias, htmlElementId, position); + + return true; + } + + + /// + /// Undoes the addStringToHtml Execute() method, by removing the same string from the same template. + /// + /// Name of the package. + /// The XML data. + /// + public bool Undo(string packageName, XmlNode xmlData) { + string templateAlias = xmlData.Attributes["templateAlias"].Value; + string htmlElementId = xmlData.Attributes["htmlElementId"].Value; + string value = xmlHelper.GetNodeValue(xmlData); + template.Template tmp = template.Template.GetByAlias(templateAlias); + + if (UmbracoSettings.UseAspNetMasterPages) + value = tmp.EnsureMasterPageSyntax(value); + + _removeStringFromHtmlElement(tmp, value, templateAlias, htmlElementId); + return true; + } + + /// + /// Action alias. + /// + /// + public string Alias() { + return "addStringToHtmlElement"; + } + + private void _addStringToHtmlElement(template.Template tmp, string value, string templateAlias, string htmlElementId, string position) { + bool hasAspNetContentBeginning = false; + string design = ""; + string directive = ""; + + if (tmp != null) { + try { + XmlDocument templateXml = new XmlDocument(); + templateXml.PreserveWhitespace = true; + + //Make sure that directive is remove before hacked non html4 compatiple replacement action... + design = tmp.Design; + + + splitDesignAndDirective(ref design, ref directive); + + //making sure that the template xml has a root node... + if (tmp.MasterTemplate > 0) + templateXml.LoadXml( helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, "" + design + "", true)); + else + templateXml.LoadXml(helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, design, true)); + + XmlNode xmlElement = templateXml.SelectSingleNode("//* [@id = '" + htmlElementId + "']"); + + if (xmlElement != null) { + + if (position == "beginning") { + xmlElement.InnerXml = "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, value, true) + "\n" + xmlElement.InnerXml; + } else { + xmlElement.InnerXml = xmlElement.InnerXml + "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, value, true) + "\n"; + } + } + + tmp.Design = directive + "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, templateXml.OuterXml, false); + tmp.Save(); + } catch (Exception ex) { + umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, ex.ToString()); + } + } else { + umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, "template not found"); + } + } + + private void _removeStringFromHtmlElement(template.Template tmp, string value, string templateAlias, string htmlElementId) { + bool hasAspNetContentBeginning = false; + string design = ""; + string directive = ""; + + + if (tmp != null) { + try { + XmlDocument templateXml = new XmlDocument(); + templateXml.PreserveWhitespace = true; + + //Make sure that directive is remove before hacked non html4 compatiple replacement action... + design = tmp.Design; + splitDesignAndDirective(ref design, ref directive); + + //making sure that the template xml has a root node... + if (tmp.MasterTemplate > 0) + templateXml.LoadXml( helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, "" + design + "", true)); + else + templateXml.LoadXml( helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, design, true)); + + XmlNode xmlElement = templateXml.SelectSingleNode("//* [@id = '" + htmlElementId + "']"); + - if (xmlElement != null) { - string repValue = helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, value, true); - xmlElement.InnerXml = xmlElement.InnerXml.Replace(repValue , ""); - } + if (xmlElement != null) { + string repValue = helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, value, true); + xmlElement.InnerXml = xmlElement.InnerXml.Replace(repValue , ""); + } - tmp.Design = directive + "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, templateXml.OuterXml, false); - tmp.Save(); - } catch (Exception ex) { - umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, ex.ToString() ); - } - } else { - umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, "template not found"); - } - } + tmp.Design = directive + "\n" + helper.parseToValidXml(tmp, ref hasAspNetContentBeginning, templateXml.OuterXml, false); + tmp.Save(); + } catch (Exception ex) { + umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, ex.ToString() ); + } + } else { + umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, "template not found"); + } + } - + - private void splitDesignAndDirective(ref string design, ref string directive) { - if (design.StartsWith("<%@")) { - directive = design.Substring(0, design.IndexOf("%>") + 2).Trim(Environment.NewLine.ToCharArray()); - design = design.Substring(design.IndexOf("%>") + 3).Trim(Environment.NewLine.ToCharArray()); - } - } + private void splitDesignAndDirective(ref string design, ref string directive) { + if (design.StartsWith("<%@")) { + directive = design.Substring(0, design.IndexOf("%>") + 2).Trim(Environment.NewLine.ToCharArray()); + design = design.Substring(design.IndexOf("%>") + 3).Trim(Environment.NewLine.ToCharArray()); + } + } - #endregion + #endregion - public XmlNode SampleXml() { - throw new NotImplementedException(); - } + public XmlNode SampleXml() { + throw new NotImplementedException(); + } - } + } - public class removeStringFromTemplate : umbraco.interfaces.IPackageAction { - #region IPackageAction Members + public class removeStringFromTemplate : umbraco.interfaces.IPackageAction { + #region IPackageAction Members - public bool Execute(string packageName, XmlNode xmlData) { - addStringToHtmlElement ast = new addStringToHtmlElement(); - return ast.Undo(packageName, xmlData); - } + public bool Execute(string packageName, XmlNode xmlData) { + addStringToHtmlElement ast = new addStringToHtmlElement(); + return ast.Undo(packageName, xmlData); + } - public string Alias() { - return "removeStringFromHtmlElement"; - } + public string Alias() { + return "removeStringFromHtmlElement"; + } - public bool Undo(string packageName, XmlNode xmlData) { - return true; - } + public bool Undo(string packageName, XmlNode xmlData) { + return true; + } - public XmlNode SampleXml() { - throw new NotImplementedException(); - } + public XmlNode SampleXml() { + throw new NotImplementedException(); + } - #endregion - } + #endregion + } - public class helper { - //Helper method to replace umbraco tags that breaks the xml format.. - public static string parseToValidXml(template.Template templateObj, ref bool hasAspNetContentBeginning, string template, bool toValid) { - string retVal = template; - if (toValid) { - // test for asp:content as the first part of the design - if (retVal.StartsWith("") + 1); - retVal = retVal.Substring(0, retVal.Length - 14); - } - //shorten empty macro tags.. - retVal = retVal.Replace(">", " />"); - retVal = retVal.Replace(">", " />"); + public class helper { + //Helper method to replace umbraco tags that breaks the xml format.. + public static string parseToValidXml(template.Template templateObj, ref bool hasAspNetContentBeginning, string template, bool toValid) { + string retVal = template; + if (toValid) { + // test for asp:content as the first part of the design + if (retVal.StartsWith("") + 1); + retVal = retVal.Substring(0, retVal.Length - 14); + } + //shorten empty macro tags.. + retVal = retVal.Replace(">", " />"); + retVal = retVal.Replace(">", " />"); - retVal = retVal.Replace("", ""); - retVal = retVal.Replace("", ""); - retVal = retVal.Replace("", ""); + } else { + retVal = retVal.Replace("", ""); + retVal = retVal.Replace("", ""); + retVal = retVal.Replace("", ""); - // add asp content element - if (hasAspNetContentBeginning) { - retVal = templateObj.GetMasterContentElement(templateObj.MasterTemplate) + retVal + ""; - } - } + // add asp content element + if (hasAspNetContentBeginning) { + retVal = templateObj.GetMasterContentElement(templateObj.MasterTemplate) + retVal + ""; + } + } - return retVal; - } + return retVal; + } - public static XmlNode parseStringToXmlNode(string value) { - XmlDocument doc = new XmlDocument(); - XmlNode node = xmlHelper.addTextNode(doc, "error", ""); - - try { - doc.LoadXml(value); - return doc.SelectSingleNode("."); - } catch { - return node; - } + public static XmlNode parseStringToXmlNode(string value) { + XmlDocument doc = new XmlDocument(); + XmlNode node = xmlHelper.addTextNode(doc, "error", ""); + + try { + doc.LoadXml(value); + return doc.SelectSingleNode("."); + } catch { + return node; + } - return node; - } - } + return node; + } + } }