moduledesign.class.inc.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Module specific customizations:
  20. * The customizations are done in XML, within a module_design section (itop_design/module_designs/module_design)
  21. * The module reads the cusomtizations by the mean of the ModuleDesign API
  22. * @package Core
  23. */
  24. require_once(APPROOT.'application/utils.inc.php');
  25. /**
  26. * Class ModuleDesign
  27. *
  28. * Usage from within a module:
  29. *
  30. * // Fetch the design
  31. * $oDesign = new ModuleDesign('tagada');
  32. *
  33. * // Read data from the root node
  34. * $oRoot = $oDesign->documentElement;
  35. * $oProperties = $oRoot->GetUniqueElement('properties');
  36. * $prop1 = $oProperties->GetChildText('property1');
  37. * $prop2 = $oProperties->GetChildText('property2');
  38. *
  39. * // Read data by searching the entire DOM
  40. * foreach ($oDesign->GetNodes('/module_design/bricks/brick') as $oBrickNode)
  41. * {
  42. * $sId = $oBrickNode->getAttribute('id');
  43. * $sType = $oBrickNode->getAttribute('xsi:type');
  44. * }
  45. *
  46. * // Search starting a given node
  47. * $oBricks = $oDesign->documentElement->GetUniqueElement('bricks');
  48. * foreach ($oBricks->GetNodes('brick') as $oBrickNode)
  49. * {
  50. * ...
  51. * }
  52. */
  53. class ModuleDesign extends DOMDocument
  54. {
  55. /**
  56. * @param string|null $sDesignSourceId Identifier of the section module_design (generally a module name), null to build an empty design
  57. * @throws Exception
  58. */
  59. public function __construct($sDesignSourceId = null)
  60. {
  61. parent::__construct('1.0', 'UTF-8');
  62. $this->Init();
  63. if (!is_null($sDesignSourceId))
  64. {
  65. $this->LoadFromCompiledDesigns($sDesignSourceId);
  66. }
  67. }
  68. /**
  69. * Overloadable. Called prior to data loading.
  70. */
  71. protected function Init()
  72. {
  73. $this->registerNodeClass('DOMElement', 'ModuleDesignElement');
  74. $this->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  75. $this->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  76. }
  77. /**
  78. * Gets the data where the compiler has left them...
  79. * @param $sDesignSourceId Identifier of the section module_design (generally a module name)
  80. * @throws Exception
  81. */
  82. protected function LoadFromCompiledDesigns($sDesignSourceId)
  83. {
  84. $sDesignDir = APPROOT.'env-'.utils::GetCurrentEnvironment().'/core/module_designs/';
  85. $sFile = $sDesignDir.$sDesignSourceId.'.xml';
  86. if (!file_exists($sFile))
  87. {
  88. $aFiles = glob($sDesignDir.'/*.xml');
  89. if (count($aFiles) == 0)
  90. {
  91. $sAvailable = 'none!';
  92. }
  93. else
  94. {
  95. var_dump($aFiles);
  96. $aAvailable = array();
  97. foreach ($aFiles as $sFile)
  98. {
  99. $aAvailable[] = "'".basename($sFile, '.xml')."'";
  100. }
  101. $sAvailable = implode(', ', $aAvailable);
  102. }
  103. throw new Exception("Could not load module design '$sDesignSourceId'. Available designs: $sAvailable");
  104. }
  105. // Silently keep track of errors
  106. libxml_use_internal_errors(true);
  107. libxml_clear_errors();
  108. $this->load($sFile);
  109. //$bValidated = $oDocument->schemaValidate(APPROOT.'setup/itop_design.xsd');
  110. $aErrors = libxml_get_errors();
  111. if (count($aErrors) > 0)
  112. {
  113. $aDisplayErrors = array();
  114. foreach($aErrors as $oXmlError)
  115. {
  116. $aDisplayErrors[] = 'Line '.$oXmlError->line.': '.$oXmlError->message;
  117. }
  118. throw new Exception("Invalid XML in '$sFile'. Errors: ".implode(', ', $aDisplayErrors));
  119. }
  120. }
  121. /**
  122. * Overload of the standard API
  123. */
  124. public function load($filename, $options = 0)
  125. {
  126. parent::load($filename, LIBXML_NOBLANKS);
  127. }
  128. /**
  129. * Overload of the standard API
  130. */
  131. public function save($filename, $options = 0)
  132. {
  133. $this->documentElement->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  134. return parent::save($filename, LIBXML_NOBLANKS);
  135. }
  136. /**
  137. * Create an HTML representation of the DOM, for debugging purposes
  138. * @param bool|false $bReturnRes Echoes or returns the HTML representation
  139. * @return mixed void or the HTML representation of the DOM
  140. */
  141. public function Dump($bReturnRes = false)
  142. {
  143. $sXml = $this->saveXML();
  144. if ($bReturnRes)
  145. {
  146. return $sXml;
  147. }
  148. else
  149. {
  150. echo "<pre>\n";
  151. echo htmlentities($sXml);
  152. echo "</pre>\n";
  153. }
  154. }
  155. /**
  156. * Quote and escape strings for use within an XPath expression
  157. * Usage: DesignDocument::GetNodes('class[@id='.DesignDocument::XPathQuote($sId).']');
  158. * @param $sValue The value to be quoted
  159. * @return string to be used within an XPath
  160. */
  161. public static function XPathQuote($sValue)
  162. {
  163. if (strpos($sValue, '"') !== false)
  164. {
  165. $aParts = explode('"', $sValue);
  166. $sRet = 'concat("'.implode('", \'"\', "', $aParts).'")';
  167. }
  168. else
  169. {
  170. $sRet = '"'.$sValue.'"';
  171. }
  172. return $sRet;
  173. }
  174. /**
  175. * Extracts some nodes from the DOM
  176. * @param string $sXPath A XPath expression
  177. * @param DesignNode|null $oContextNode The node to start the search from
  178. * @return DOMNodeList
  179. */
  180. public function GetNodes($sXPath, $oContextNode = null)
  181. {
  182. $oXPath = new DOMXPath($this);
  183. if (is_null($oContextNode))
  184. {
  185. $oResult = $oXPath->query($sXPath);
  186. }
  187. else
  188. {
  189. $oResult = $oXPath->query($sXPath, $oContextNode);
  190. }
  191. return $oResult;
  192. }
  193. /**
  194. * An alternative to getNodePath, that gives the id of nodes instead of the position within the children
  195. */
  196. public static function GetItopNodePath($oNode)
  197. {
  198. if ($oNode instanceof DOMDocument) return '';
  199. $sId = $oNode->getAttribute('id');
  200. $sNodeDesc = ($sId != '') ? $oNode->nodeName.'['.$sId.']' : $oNode->nodeName;
  201. return self::GetItopNodePath($oNode->parentNode).'/'.$sNodeDesc;
  202. }
  203. }
  204. /**
  205. * ModuleDesignElement: helper to read/change the DOM
  206. * @package ModelFactory
  207. */
  208. class ModuleDesignElement extends DOMElement
  209. {
  210. /**
  211. * Extracts some nodes from the DOM
  212. * @param string $sXPath A XPath expression
  213. * @return DOMNodeList
  214. */
  215. public function GetNodes($sXPath)
  216. {
  217. return $this->ownerDocument->GetNodes($sXPath, $this);
  218. }
  219. /**
  220. * Create an HTML representation of the DOM, for debugging purposes
  221. * @param bool|false $bReturnRes Echoes or returns the HTML representation
  222. * @return mixed void or the HTML representation of the DOM
  223. */
  224. public function Dump($bReturnRes = false)
  225. {
  226. $oDoc = new iTopDesignDocument();
  227. $oClone = $oDoc->importNode($this->cloneNode(true), true);
  228. $oDoc->appendChild($oClone);
  229. $sXml = $oDoc->saveXML($oClone);
  230. if ($bReturnRes)
  231. {
  232. return $sXml;
  233. }
  234. else
  235. {
  236. echo "<pre>\n";
  237. echo htmlentities($sXml);
  238. echo "</pre>\n";
  239. }
  240. }
  241. /**
  242. * Returns the node directly under the given node
  243. * @param $sTagName
  244. * @param bool|true $bMustExist
  245. * @return null
  246. * @throws DOMFormatException
  247. */
  248. public function GetUniqueElement($sTagName, $bMustExist = true)
  249. {
  250. $oNode = null;
  251. foreach($this->childNodes as $oChildNode)
  252. {
  253. if ($oChildNode->nodeName == $sTagName)
  254. {
  255. $oNode = $oChildNode;
  256. break;
  257. }
  258. }
  259. if ($bMustExist && is_null($oNode))
  260. {
  261. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  262. }
  263. return $oNode;
  264. }
  265. /**
  266. * Returns the node directly under the current node, or null if missing
  267. * @param $sTagName
  268. * @return null
  269. * @throws DOMFormatException
  270. */
  271. public function GetOptionalElement($sTagName)
  272. {
  273. return $this->GetUniqueElement($sTagName, false);
  274. }
  275. /**
  276. * Returns the TEXT of the current node (possibly from several child nodes)
  277. * @param null $sDefault
  278. * @return null|string
  279. */
  280. public function GetText($sDefault = null)
  281. {
  282. $sText = null;
  283. foreach($this->childNodes as $oChildNode)
  284. {
  285. if ($oChildNode instanceof DOMText)
  286. {
  287. if (is_null($sText)) $sText = '';
  288. $sText .= $oChildNode->wholeText;
  289. }
  290. }
  291. if (is_null($sText))
  292. {
  293. return $sDefault;
  294. }
  295. else
  296. {
  297. return $sText;
  298. }
  299. }
  300. /**
  301. * Get the TEXT value from a child node
  302. * @param string $sTagName
  303. * @param string|null $sDefault
  304. * @return string
  305. */
  306. public function GetChildText($sTagName, $sDefault = null)
  307. {
  308. $sRet = $sDefault;
  309. if ($oChild = $this->GetOptionalElement($sTagName))
  310. {
  311. $sRet = $oChild->GetText($sDefault);
  312. }
  313. return $sRet;
  314. }
  315. }