moduledesign.class.inc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // Copyright (C) 2015-2016 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. require_once(APPROOT.'core/designdocument.class.inc.php');
  26. /**
  27. * Class ModuleDesign
  28. *
  29. * Usage from within a module:
  30. *
  31. * // Fetch the design
  32. * $oDesign = new ModuleDesign('tagada');
  33. *
  34. * // Read data from the root node
  35. * $oRoot = $oDesign->documentElement;
  36. * $oProperties = $oRoot->GetUniqueElement('properties');
  37. * $prop1 = $oProperties->GetChildText('property1');
  38. * $prop2 = $oProperties->GetChildText('property2');
  39. *
  40. * // Read data by searching the entire DOM
  41. * foreach ($oDesign->GetNodes('/module_design/bricks/brick') as $oBrickNode)
  42. * {
  43. * $sId = $oBrickNode->getAttribute('id');
  44. * $sType = $oBrickNode->getAttribute('xsi:type');
  45. * }
  46. *
  47. * // Search starting a given node
  48. * $oBricks = $oDesign->documentElement->GetUniqueElement('bricks');
  49. * foreach ($oBricks->GetNodes('brick') as $oBrickNode)
  50. * {
  51. * ...
  52. * }
  53. */
  54. class ModuleDesign extends \Combodo\iTop\DesignDocument
  55. {
  56. /**
  57. * @param string|null $sDesignSourceId Identifier of the section module_design (generally a module name), null to build an empty design
  58. * @throws Exception
  59. */
  60. public function __construct($sDesignSourceId = null)
  61. {
  62. parent::__construct();
  63. if (!is_null($sDesignSourceId))
  64. {
  65. $this->LoadFromCompiledDesigns($sDesignSourceId);
  66. }
  67. }
  68. /**
  69. * Gets the data where the compiler has left them...
  70. * @param $sDesignSourceId String Identifier of the section module_design (generally a module name)
  71. * @throws Exception
  72. */
  73. protected function LoadFromCompiledDesigns($sDesignSourceId)
  74. {
  75. $sDesignDir = APPROOT.'env-'.utils::GetCurrentEnvironment().'/core/module_designs/';
  76. $sFile = $sDesignDir.$sDesignSourceId.'.xml';
  77. if (!file_exists($sFile))
  78. {
  79. $aFiles = glob($sDesignDir.'/*.xml');
  80. if (count($aFiles) == 0)
  81. {
  82. $sAvailable = 'none!';
  83. }
  84. else
  85. {
  86. var_dump($aFiles);
  87. $aAvailable = array();
  88. foreach ($aFiles as $sFile)
  89. {
  90. $aAvailable[] = "'".basename($sFile, '.xml')."'";
  91. }
  92. $sAvailable = implode(', ', $aAvailable);
  93. }
  94. throw new Exception("Could not load module design '$sDesignSourceId'. Available designs: $sAvailable");
  95. }
  96. // Silently keep track of errors
  97. libxml_use_internal_errors(true);
  98. libxml_clear_errors();
  99. $this->load($sFile);
  100. //$bValidated = $oDocument->schemaValidate(APPROOT.'setup/itop_design.xsd');
  101. $aErrors = libxml_get_errors();
  102. if (count($aErrors) > 0)
  103. {
  104. $aDisplayErrors = array();
  105. foreach($aErrors as $oXmlError)
  106. {
  107. $aDisplayErrors[] = 'Line '.$oXmlError->line.': '.$oXmlError->message;
  108. }
  109. throw new Exception("Invalid XML in '$sFile'. Errors: ".implode(', ', $aDisplayErrors));
  110. }
  111. }
  112. }