// add elements to the <macro> root node, corresponding to parameters private void AddMacroXmlNode(XmlDocument umbracoXml, XmlDocument macroXml, string macroPropertyAlias, string macroPropertyType, string macroPropertyValue) { XmlNode macroXmlNode = macroXml.CreateNode(XmlNodeType.Element, macroPropertyAlias, string.Empty); var x = new XmlDocument(); // if no value is passed, then use the current "pageID" as value var contentId = macroPropertyValue == string.Empty ? UmbracoContext.Current.PageId.ToString() : macroPropertyValue; TraceInfo("umbracoMacro", "Xslt node adding search start (" + macroPropertyAlias + ",'" + macroPropertyValue + "')"); switch (macroPropertyType) { case "contentTree": var nodeId = macroXml.CreateAttribute("nodeID"); nodeId.Value = contentId; macroXmlNode.Attributes.SetNamedItem(nodeId); // Get subs try { macroXmlNode.AppendChild(macroXml.ImportNode(umbracoXml.GetElementById(contentId), true)); } catch { } break; case "contentCurrent": var importNode = macroPropertyValue == string.Empty ? umbracoXml.GetElementById(contentId) : umbracoXml.GetElementById(macroPropertyValue); var currentNode = macroXml.ImportNode(importNode, true); // remove all sub content nodes foreach (XmlNode n in currentNode.SelectNodes("node|*[@isDoc]")) currentNode.RemoveChild(n); macroXmlNode.AppendChild(currentNode); break; case "contentSubs": // disable that one, it does not work anyway... //x.LoadXml("<nodes/>"); //x.FirstChild.AppendChild(x.ImportNode(umbracoXml.GetElementById(contentId), true)); //macroXmlNode.InnerXml = TransformMacroXml(x, "macroGetSubs.xsl"); break; case "contentAll": macroXmlNode.AppendChild(macroXml.ImportNode(umbracoXml.DocumentElement, true)); break; case "contentRandom": XmlNode source = umbracoXml.GetElementById(contentId); if (source != null) { var sourceList = source.SelectNodes("node|*[@isDoc]"); if (sourceList.Count > 0) { int rndNumber; var r = library.GetRandom(); lock (r) { rndNumber = r.Next(sourceList.Count); } var node = macroXml.ImportNode(sourceList[rndNumber], true); // remove all sub content nodes foreach (XmlNode n in node.SelectNodes("node|*[@isDoc]")) node.RemoveChild(n); macroXmlNode.AppendChild(node); } else TraceWarn("umbracoMacro", "Error adding random node - parent (" + macroPropertyValue + ") doesn't have children!"); } else TraceWarn("umbracoMacro", "Error adding random node - parent (" + macroPropertyValue + ") doesn't exists!"); break; case "mediaCurrent": var c = new Content(int.Parse(macroPropertyValue)); macroXmlNode.AppendChild(macroXml.ImportNode(c.ToXml(umbraco.content.Instance.XmlContent, false), true)); break; default: macroXmlNode.InnerText = HttpContext.Current.Server.HtmlDecode(macroPropertyValue); break; } macroXml.FirstChild.AppendChild(macroXmlNode); }
private void addMacroXmlNode(XmlDocument umbracoXML, XmlDocument macroXML, String macroPropertyAlias, String macroPropertyType, String macroPropertyValue) { XmlNode macroXmlNode = macroXML.CreateNode(XmlNodeType.Element, macroPropertyAlias, string.Empty); var x = new XmlDocument(); int currentID = -1; // If no value is passed, then use the current pageID as value if (macroPropertyValue == string.Empty) { var umbPage = (page)HttpContext.Current.Items["umbPageObject"]; if (umbPage == null) return; currentID = umbPage.PageID; } TraceInfo("umbracoMacro", "Xslt node adding search start (" + macroPropertyAlias + ",'" + macroPropertyValue + "')"); switch (macroPropertyType) { case "contentTree": XmlAttribute nodeID = macroXML.CreateAttribute("nodeID"); if (macroPropertyValue != string.Empty) nodeID.Value = macroPropertyValue; else nodeID.Value = currentID.ToString(); macroXmlNode.Attributes.SetNamedItem(nodeID); // Get subs try { macroXmlNode.AppendChild(macroXML.ImportNode(umbracoXML.GetElementById(nodeID.Value), true)); } catch { break; } break; case "contentCurrent": x.LoadXml("<nodes/>"); XmlNode currentNode; if (macroPropertyValue != string.Empty) currentNode = macroXML.ImportNode(umbracoXML.GetElementById(macroPropertyValue), true); else currentNode = macroXML.ImportNode(umbracoXML.GetElementById(currentID.ToString()), true); // remove all sub content nodes foreach (XmlNode n in currentNode.SelectNodes("node|*[@isDoc]")) currentNode.RemoveChild(n); macroXmlNode.AppendChild(currentNode); break; case "contentSubs": x.LoadXml("<nodes/>"); if (macroPropertyValue != string.Empty) x.FirstChild.AppendChild(x.ImportNode(umbracoXML.GetElementById(macroPropertyValue), true)); else x.FirstChild.AppendChild(x.ImportNode(umbracoXML.GetElementById(currentID.ToString()), true)); macroXmlNode.InnerXml = transformMacroXML(x, "macroGetSubs.xsl"); break; case "contentAll": x.ImportNode(umbracoXML.DocumentElement.LastChild, true); break; case "contentRandom": XmlNode source = umbracoXML.GetElementById(macroPropertyValue); if (source != null) { XmlNodeList sourceList = source.SelectNodes("node|*[@isDoc]"); if (sourceList.Count > 0) { int rndNumber; Random r = library.GetRandom(); lock (r) { rndNumber = r.Next(sourceList.Count); } XmlNode node = macroXML.ImportNode(sourceList[rndNumber], true); // remove all sub content nodes foreach (XmlNode n in node.SelectNodes("node|*[@isDoc]")) node.RemoveChild(n); macroXmlNode.AppendChild(node); break; } else TraceWarn("umbracoMacro", "Error adding random node - parent (" + macroPropertyValue + ") doesn't have children!"); } else TraceWarn("umbracoMacro", "Error adding random node - parent (" + macroPropertyValue + ") doesn't exists!"); break; case "mediaCurrent": var c = new Content(int.Parse(macroPropertyValue)); macroXmlNode.AppendChild(macroXML.ImportNode(c.ToXml(content.Instance.XmlContent, false), true)); break; default: macroXmlNode.InnerText = HttpContext.Current.Server.HtmlDecode(macroPropertyValue); break; } macroXML.FirstChild.AppendChild(macroXmlNode); }