compiler.class.inc.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. <?php
  2. // Copyright (C) 2011-2013 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. require_once(APPROOT.'setup/setuputils.class.inc.php');
  19. class DOMFormatException extends Exception
  20. {
  21. }
  22. /**
  23. * Compiler class
  24. */
  25. class MFCompiler
  26. {
  27. protected $oFactory;
  28. protected $aRootClasses;
  29. protected $aLog;
  30. public function __construct($oModelFactory)
  31. {
  32. $this->oFactory = $oModelFactory;
  33. $this->aLog = array();
  34. }
  35. protected function Log($sText)
  36. {
  37. $this->aLog[] = $sText;
  38. }
  39. protected function DumpLog($oPage)
  40. {
  41. foreach ($this->aLog as $sText)
  42. {
  43. $oPage->p($sText);
  44. }
  45. }
  46. public function GetLog()
  47. {
  48. return $this->aLog;
  49. }
  50. public function Compile($sTargetDir, $oP = null, $bUseSymbolicLinks = false)
  51. {
  52. $sFinalTargetDir = $sTargetDir;
  53. if ($bUseSymbolicLinks)
  54. {
  55. // Skip the creation of a temporary dictionary, not compatible with symbolic links
  56. $sTempTargetDir = $sFinalTargetDir;
  57. }
  58. else
  59. {
  60. // Create a temporary directory
  61. // Once the compilation is 100% successful, then move the results into the target directory
  62. $sTempTargetDir = tempnam(SetupUtils::GetTmpDir(), 'itop-');
  63. unlink($sTempTargetDir); // I need a directory, not a file...
  64. SetupUtils::builddir($sTempTargetDir); // Here is the directory
  65. }
  66. try
  67. {
  68. $this->DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks);
  69. }
  70. catch (Exception $e)
  71. {
  72. if ($sTempTargetDir != $sFinalTargetDir)
  73. {
  74. // Cleanup the temporary directory
  75. SetupUtils::rrmdir($sTempTargetDir);
  76. }
  77. throw $e;
  78. }
  79. if ($sTempTargetDir != $sFinalTargetDir)
  80. {
  81. // Move the results to the target directory
  82. SetupUtils::movedir($sTempTargetDir, $sFinalTargetDir);
  83. }
  84. }
  85. protected function DoCompile($sTempTargetDir, $sFinalTargetDir, $oP = null, $bUseSymbolicLinks = false)
  86. {
  87. $aAllClasses = array(); // flat list of classes
  88. // Determine the target modules for the MENUS
  89. //
  90. $aMenuNodes = array();
  91. $aMenusByModule = array();
  92. foreach ($this->oFactory->ListActiveChildNodes('menus', 'menu') as $oMenuNode)
  93. {
  94. $sMenuId = $oMenuNode->getAttribute('id');
  95. $aMenuNodes[$sMenuId] = $oMenuNode;
  96. $sModuleMenu = $oMenuNode->getAttribute('_created_in');
  97. $aMenusByModule[$sModuleMenu][] = $sMenuId;
  98. }
  99. // Determine the target module (exactly one!) for USER RIGHTS
  100. //
  101. $sUserRightsModule = '';
  102. $oUserRightsNode = $this->oFactory->GetNodes('user_rights')->item(0);
  103. if ($oUserRightsNode)
  104. {
  105. $sUserRightsModule = $oUserRightsNode->getAttribute('_created_in');
  106. $this->Log("User Rights module found: $sUserRightsModule");
  107. }
  108. // List root classes
  109. //
  110. $this->aRootClasses = array();
  111. foreach ($this->oFactory->ListRootClasses() as $oClass)
  112. {
  113. $this->Log("Root class (with child classes): ".$oClass->getAttribute('id'));
  114. $this->aRootClasses[$oClass->getAttribute('id')] = $oClass;
  115. }
  116. // Compile, module by module
  117. //
  118. $aModules = $this->oFactory->GetLoadedModules();
  119. foreach($aModules as $foo => $oModule)
  120. {
  121. $sModuleName = $oModule->GetName();
  122. $sModuleVersion = $oModule->GetVersion();
  123. $sModuleRootDir = $oModule->GetRootDir();
  124. if ($sModuleRootDir != '')
  125. {
  126. $sModuleRootDir = realpath($sModuleRootDir);
  127. $sRelativeDir = basename($sModuleRootDir);
  128. // Push the other module files
  129. SetupUtils::copydir($sModuleRootDir, $sTempTargetDir.'/'.$sRelativeDir, $bUseSymbolicLinks);
  130. }
  131. $sCompiledCode = '';
  132. $oConstants = $this->oFactory->ListConstants($sModuleName);
  133. if ($oConstants->length > 0)
  134. {
  135. foreach($oConstants as $oConstant)
  136. {
  137. $sCompiledCode .= $this->CompileConstant($oConstant)."\n";
  138. }
  139. }
  140. $oClasses = $this->oFactory->ListClasses($sModuleName);
  141. $iClassCount = $oClasses->length;
  142. if ($iClassCount == 0)
  143. {
  144. $this->Log("Found module without classes declared: $sModuleName");
  145. }
  146. else
  147. {
  148. foreach($oClasses as $oClass)
  149. {
  150. $sClass = $oClass->getAttribute("id");
  151. $aAllClasses[] = $sClass;
  152. try
  153. {
  154. $sCompiledCode .= $this->CompileClass($oClass, $sTempTargetDir, $sFinalTargetDir, $sRelativeDir, $oP);
  155. }
  156. catch (DOMFormatException $e)
  157. {
  158. throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
  159. }
  160. }
  161. }
  162. if (!array_key_exists($sModuleName, $aMenusByModule))
  163. {
  164. $this->Log("Found module without menus declared: $sModuleName");
  165. }
  166. else
  167. {
  168. $sCompiledCode .=
  169. <<<EOF
  170. //
  171. // Menus
  172. //
  173. global \$__comp_menus__; // ensure that the global variable is indeed global !
  174. EOF;
  175. // Preliminary: determine parent menus not defined within the current module
  176. $aMenusToLoad = array();
  177. $aParentMenus = array();
  178. foreach($aMenusByModule[$sModuleName] as $sMenuId)
  179. {
  180. $oMenuNode = $aMenuNodes[$sMenuId];
  181. if ($sParent = $oMenuNode->GetChildText('parent', null))
  182. {
  183. $aMenusToLoad[] = $sParent;
  184. $aParentMenus[] = $sParent;
  185. }
  186. // Note: the order matters: the parents must be defined BEFORE
  187. $aMenusToLoad[] = $sMenuId;
  188. }
  189. $aMenusToLoad = array_unique($aMenusToLoad);
  190. foreach($aMenusToLoad as $sMenuId)
  191. {
  192. $oMenuNode = $aMenuNodes[$sMenuId];
  193. if ($oMenuNode->getAttribute("xsi:type") == 'MenuGroup')
  194. {
  195. // Note: this algorithm is wrong
  196. // 1 - the module may appear empty in the current module, while children are defined in other modules
  197. // 2 - check recursively that child nodes are not empty themselves
  198. // Future algorithm:
  199. // a- browse the modules and build the menu tree
  200. // b- browse the tree and blacklist empty menus
  201. // c- before compiling, discard if blacklisted
  202. if (!in_array($oMenuNode->getAttribute("id"), $aParentMenus))
  203. {
  204. // Discard empty menu groups
  205. continue;
  206. }
  207. }
  208. try
  209. {
  210. $sCompiledCode .= $this->CompileMenu($oMenuNode, $sTempTargetDir, $sFinalTargetDir, $sRelativeDir, $oP);
  211. }
  212. catch (DOMFormatException $e)
  213. {
  214. throw new Exception("Failed to process menu '$sMenuId', from '$sModuleRootDir': ".$e->getMessage());
  215. }
  216. }
  217. }
  218. // User rights
  219. //
  220. if ($sModuleName == $sUserRightsModule)
  221. {
  222. $sCompiledCode .= $this->CompileUserRights($oUserRightsNode);
  223. }
  224. // Create (overwrite if existing) the compiled file
  225. //
  226. if (strlen($sCompiledCode) > 0)
  227. {
  228. // We have compiled something: write the result file
  229. //
  230. $sResultFile = $sTempTargetDir.'/'.$sRelativeDir.'/model.'.$sModuleName.'.php';
  231. if (is_file($sResultFile))
  232. {
  233. $this->Log("Updating $sResultFile for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  234. }
  235. else
  236. {
  237. $sResultDir = dirname($sResultFile);
  238. if (!is_dir($sResultDir))
  239. {
  240. $this->Log("Creating directory $sResultDir");
  241. mkdir($sResultDir, 0777, true);
  242. }
  243. $this->Log("Creating $sResultFile for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  244. }
  245. // Compile the module into a single file
  246. //
  247. $sId = $sModuleName;
  248. $sCurrDate = date(DATE_ISO8601);
  249. $sAuthor = 'iTop compiler';
  250. $sLicence = 'http://opensource.org/licenses/AGPL-3.0';
  251. $sFileHeader =
  252. <<<EOF
  253. <?php
  254. //
  255. // File generated by ... on the $sCurrDate
  256. // Please do not edit manually
  257. //
  258. /**
  259. * Classes and menus for $sModuleName (version $sModuleVersion)
  260. *
  261. * @author $sAuthor
  262. * @license $sLicence
  263. */
  264. EOF;
  265. $ret = file_put_contents($sResultFile, $sFileHeader.$sCompiledCode);
  266. if ($ret === false)
  267. {
  268. $iLen = strlen($sFileHeader.$sCompiledCode);
  269. $fFree = @disk_free_space(dirname($sResultFile));
  270. $aErr = error_get_last();
  271. throw new Exception("Failed to write '$sResultFile'. Last error: '{$aErr['message']}', content to write: $iLen bytes, available free space on disk: $fFree.");
  272. }
  273. }
  274. else
  275. {
  276. $this->Log("Compilation of module $sModuleName in version $sModuleVersion produced not code at all. No file written.");
  277. }
  278. } // foreach module
  279. // Compile the dictionaries -out of the modules
  280. //
  281. $sDictDir = $sTempTargetDir.'/dictionaries';
  282. if (!is_dir($sDictDir))
  283. {
  284. $this->Log("Creating directory $sDictDir");
  285. mkdir($sDictDir, 0777, true);
  286. }
  287. $oDictionaries = $this->oFactory->ListActiveChildNodes('dictionaries', 'dictionary');
  288. foreach($oDictionaries as $oDictionaryNode)
  289. {
  290. $this->CompileDictionary($oDictionaryNode, $sTempTargetDir, $sFinalTargetDir);
  291. }
  292. // Compile the branding
  293. //
  294. $oBrandingNode = $this->oFactory->GetNodes('branding')->item(0);
  295. $this->CompileBranding($oBrandingNode, $sTempTargetDir, $sFinalTargetDir);
  296. } // DoCompile()
  297. /**
  298. * Helper to form a valid ZList from the array built by GetNodeAsArrayOfItems()
  299. */
  300. protected function ArrayOfItemsToZList(&$aItems)
  301. {
  302. $aTransformed = array();
  303. foreach ($aItems as $key => $value)
  304. {
  305. if (is_null($value))
  306. {
  307. $aTransformed[] = $key;
  308. }
  309. else
  310. {
  311. if (is_array($value))
  312. {
  313. $this->ArrayOfItemsToZList($value);
  314. }
  315. $aTransformed[$key] = $value;
  316. }
  317. }
  318. $aItems = $aTransformed;
  319. }
  320. /**
  321. * Helper to format the flags for an attribute, in a given state
  322. * @param object $oAttNode DOM node containing the information to build the flags
  323. * Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
  324. */
  325. protected function FlagsToPHP($oAttNode)
  326. {
  327. static $aNodeAttributeToFlag = array(
  328. 'mandatory' => 'OPT_ATT_MANDATORY',
  329. 'read_only' => 'OPT_ATT_READONLY',
  330. 'must_prompt' => 'OPT_ATT_MUSTPROMPT',
  331. 'must_change' => 'OPT_ATT_MUSTCHANGE',
  332. 'hidden' => 'OPT_ATT_HIDDEN',
  333. );
  334. $aFlags = array();
  335. foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
  336. {
  337. $bFlag = ($oAttNode->GetOptionalElement($sNodeAttribute) != null);
  338. if ($bFlag)
  339. {
  340. $aFlags[] = $sFlag;
  341. }
  342. }
  343. $sRes = implode(' | ', $aFlags);
  344. return $sRes;
  345. }
  346. /**
  347. * Helper to format the tracking level for linkset (direct or indirect attributes)
  348. * @param string $sTrackingLevel Value set from within the XML
  349. * Returns string PHP flag
  350. */
  351. protected function TrackingLevelToPHP($sAttType, $sTrackingLevel)
  352. {
  353. static $aXmlToPHP_Links = array(
  354. 'none' => 'LINKSET_TRACKING_NONE',
  355. 'list' => 'LINKSET_TRACKING_LIST',
  356. 'details' => 'LINKSET_TRACKING_DETAILS',
  357. 'all' => 'LINKSET_TRACKING_ALL',
  358. );
  359. static $aXmlToPHP_Others = array(
  360. 'none' => 'ATTRIBUTE_TRACKING_NONE',
  361. 'all' => 'ATTRIBUTE_TRACKING_ALL',
  362. );
  363. switch ($sAttType)
  364. {
  365. case 'AttributeLinkedSetIndirect':
  366. case 'AttributeLinkedSet':
  367. $aXmlToPHP = $aXmlToPHP_Links;
  368. break;
  369. default:
  370. $aXmlToPHP = $aXmlToPHP_Others;
  371. }
  372. if (!array_key_exists($sTrackingLevel, $aXmlToPHP))
  373. {
  374. throw new DOMFormatException("Tracking level: unknown value '$sTrackingLevel', expecting a value in {".implode(', ', array_keys($aXmlToPHP))."}");
  375. }
  376. return $aXmlToPHP[$sTrackingLevel];
  377. }
  378. /**
  379. * Helper to format the edit-mode for direct linkset
  380. * @param string $sEditMode Value set from within the XML
  381. * Returns string PHP flag
  382. */
  383. protected function EditModeToPHP($sEditMode)
  384. {
  385. static $aXmlToPHP = array(
  386. 'none' => 'LINKSET_EDITMODE_NONE',
  387. 'add_only' => 'LINKSET_EDITMODE_ADDONLY',
  388. 'actions' => 'LINKSET_EDITMODE_ACTIONS',
  389. 'in_place' => 'LINKSET_EDITMODE_INPLACE',
  390. 'add_remove' => 'LINKSET_EDITMODE_ADDREMOVE',
  391. );
  392. if (!array_key_exists($sEditMode, $aXmlToPHP))
  393. {
  394. throw new DOMFormatException("Edit mode: unknown value '$sEditMode'");
  395. }
  396. return $aXmlToPHP[$sEditMode];
  397. }
  398. /**
  399. * Format a path (file or url) as an absolute path or relative to the module or the app
  400. */
  401. protected function PathToPHP($sPath, $sModuleRelativeDir, $bIsUrl = false)
  402. {
  403. if ($sPath == '')
  404. {
  405. $sPHP = "''";
  406. }
  407. elseif (substr($sPath, 0, 2) == '$$')
  408. {
  409. // Absolute
  410. $sPHP = self::QuoteForPHP(substr($sPath, 2));
  411. }
  412. elseif (substr($sPath, 0, 1) == '$')
  413. {
  414. // Relative to the application
  415. if ($bIsUrl)
  416. {
  417. $sPHP = "utils::GetAbsoluteUrlAppRoot().".self::QuoteForPHP(substr($sPath, 1));
  418. }
  419. else
  420. {
  421. $sPHP = "APPROOT.".self::QuoteForPHP(substr($sPath, 1));
  422. }
  423. }
  424. else
  425. {
  426. // Relative to the module
  427. if ($bIsUrl)
  428. {
  429. $sPHP = "utils::GetAbsoluteUrlAppRoot().".self::QuoteForPHP($sModuleRelativeDir.''.$sPath);
  430. }
  431. else
  432. {
  433. $sPHP = "dirname(__FILE__).'/$sPath'";
  434. }
  435. }
  436. return $sPHP;
  437. }
  438. protected function GetPropString($oNode, $sTag, $sDefault = null)
  439. {
  440. $val = $oNode->GetChildText($sTag);
  441. if (is_null($val))
  442. {
  443. if (is_null($sDefault))
  444. {
  445. return null;
  446. }
  447. else
  448. {
  449. $val = $sDefault;
  450. }
  451. }
  452. return "'".$val."'";
  453. }
  454. protected function GetPropBoolean($oNode, $sTag, $bDefault = null)
  455. {
  456. $val = $oNode->GetChildText($sTag);
  457. if (is_null($val))
  458. {
  459. if (is_null($bDefault))
  460. {
  461. return null;
  462. }
  463. else
  464. {
  465. return $bDefault ? 'true' : 'false';
  466. }
  467. }
  468. return $val == 'true' ? 'true' : 'false';
  469. }
  470. protected function GetPropNumber($oNode, $sTag, $nDefault = null)
  471. {
  472. $val = $oNode->GetChildText($sTag);
  473. if (is_null($val))
  474. {
  475. if (is_null($nDefault))
  476. {
  477. return null;
  478. }
  479. else
  480. {
  481. $val = $nDefault;
  482. }
  483. }
  484. return (string)$val;
  485. }
  486. /**
  487. * Adds quotes and escape characters
  488. */
  489. protected function QuoteForPHP($sStr, $bSimpleQuotes = false)
  490. {
  491. if ($bSimpleQuotes)
  492. {
  493. $sEscaped = str_replace(array('\\', "'"), array('\\\\', "\\'"), $sStr);
  494. $sRet = "'$sEscaped'";
  495. }
  496. else
  497. {
  498. $sEscaped = str_replace(array('\\', '"', "\n"), array('\\\\', '\\"', '\\n'), $sStr);
  499. $sRet = '"'.$sEscaped.'"';
  500. }
  501. return $sRet;
  502. }
  503. protected function CompileConstant($oConstant)
  504. {
  505. $sName = $oConstant->getAttribute('id');
  506. $sType = $oConstant->getAttribute('xsi:type');
  507. $sText = $oConstant->GetText(null);
  508. switch ($sType)
  509. {
  510. case 'integer':
  511. if (is_null($sText))
  512. {
  513. // No data given => null
  514. $sScalar = 'null';
  515. }
  516. else
  517. {
  518. $sScalar = (string)(int)$sText;
  519. }
  520. break;
  521. case 'float':
  522. if (is_null($sText))
  523. {
  524. // No data given => null
  525. $sScalar = 'null';
  526. }
  527. else
  528. {
  529. $sScalar = (string)(float)$sText;
  530. }
  531. break;
  532. case 'bool':
  533. if (is_null($sText))
  534. {
  535. // No data given => null
  536. $sScalar = 'null';
  537. }
  538. else
  539. {
  540. $sScalar = ($sText == 'true') ? 'true' : 'false';
  541. }
  542. break;
  543. case 'string':
  544. default:
  545. $sScalar = $this->QuoteForPHP($sText, true);
  546. }
  547. $sPHPDefine = "define('$sName', $sScalar);";
  548. return $sPHPDefine;
  549. }
  550. protected function CompileClass($oClass, $sTempTargetDir, $sFinalTargetDir, $sModuleRelativeDir, $oP)
  551. {
  552. $sClass = $oClass->getAttribute('id');
  553. $oProperties = $oClass->GetUniqueElement('properties');
  554. // Class caracteristics
  555. //
  556. $aClassParams = array();
  557. $aClassParams['category'] = $this->GetPropString($oProperties, 'category', '');
  558. $aClassParams['key_type'] = "'autoincrement'";
  559. if ((bool) $this->GetPropNumber($oProperties, 'is_link', 0))
  560. {
  561. $aClassParams['is_link'] = 'true';
  562. }
  563. if ($oNaming = $oProperties->GetOptionalElement('naming'))
  564. {
  565. $oNameAttributes = $oNaming->GetUniqueElement('attributes');
  566. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  567. $aNameAttCodes = array();
  568. foreach($oAttributes as $oAttribute)
  569. {
  570. $aNameAttCodes[] = $oAttribute->getAttribute('id');
  571. }
  572. if (count($aNameAttCodes) > 1)
  573. {
  574. // New style...
  575. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  576. }
  577. elseif (count($aNameAttCodes) == 1)
  578. {
  579. // New style...
  580. $sNameAttCode = "'$aNameAttCodes[0]'";
  581. }
  582. else
  583. {
  584. $sNameAttCode = "''";
  585. }
  586. }
  587. else
  588. {
  589. $sNameAttCode = "''";
  590. }
  591. $aClassParams['name_attcode'] = $sNameAttCode;
  592. $oLifecycle = $oClass->GetOptionalElement('lifecycle');
  593. if ($oLifecycle)
  594. {
  595. $sStateAttCode = $oLifecycle->GetChildText('attribute');
  596. }
  597. else
  598. {
  599. $sStateAttCode = "";
  600. }
  601. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  602. if ($oReconciliation = $oProperties->GetOptionalElement('reconciliation'))
  603. {
  604. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  605. $aReconcAttCodes = array();
  606. foreach($oReconcAttributes as $oAttribute)
  607. {
  608. $aReconcAttCodes[] = $oAttribute->getAttribute('id');
  609. }
  610. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  611. }
  612. else
  613. {
  614. $sReconcKeys = "array()";
  615. }
  616. $aClassParams['reconc_keys'] = $sReconcKeys;
  617. $aClassParams['db_table'] = $this->GetPropString($oProperties, 'db_table', '');
  618. $aClassParams['db_key_field'] = $this->GetPropString($oProperties, 'db_key_field', 'id');
  619. if (array_key_exists($sClass, $this->aRootClasses))
  620. {
  621. $sDefaultFinalClass = 'finalclass';
  622. }
  623. else
  624. {
  625. $sDefaultFinalClass = '';
  626. }
  627. $aClassParams['db_finalclass_field'] = $this->GetPropString($oProperties, 'db_final_class_field', $sDefaultFinalClass);
  628. if (($sDisplayTemplate = $oProperties->GetChildText('display_template')) && (strlen($sDisplayTemplate) > 0))
  629. {
  630. $sDisplayTemplate = $sModuleRelativeDir.'/'.$sDisplayTemplate;
  631. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  632. }
  633. $this->CompileFiles($oProperties, $sTempTargetDir.'/'.$sModuleRelativeDir, $sFinalTargetDir.'/'.$sModuleRelativeDir, '');
  634. if (($sIcon = $oProperties->GetChildText('icon')) && (strlen($sIcon) > 0))
  635. {
  636. $sIcon = $sModuleRelativeDir.'/'.$sIcon;
  637. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  638. }
  639. $oOrder = $oProperties->GetOptionalElement('order');
  640. if ($oOrder)
  641. {
  642. $oColumnsNode = $oOrder->GetUniqueElement('columns');
  643. $oColumns = $oColumnsNode->getElementsByTagName('column');
  644. $aSortColumns = array();
  645. foreach($oColumns as $oColumn)
  646. {
  647. $aSortColumns[] = "'".$oColumn->getAttribute('id')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  648. }
  649. if (count($aSortColumns) > 0)
  650. {
  651. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  652. }
  653. }
  654. if ($oIndexes = $oProperties->GetOptionalElement('indexes'))
  655. {
  656. $aIndexes = array();
  657. foreach($oIndexes->getElementsByTagName('index') as $oIndex)
  658. {
  659. $sIndexId = $oIndex->getAttribute('id');
  660. $oAttributes = $oIndex->GetUniqueElement('attributes');
  661. foreach($oAttributes->getElementsByTagName('attribute') as $oAttribute)
  662. {
  663. $aIndexes[$sIndexId][] = $oAttribute->getAttribute('id');
  664. }
  665. }
  666. $aClassParams['indexes'] = var_export($aIndexes, true);
  667. }
  668. // Finalize class params declaration
  669. //
  670. $aClassParamsPHP = array();
  671. foreach($aClassParams as $sKey => $sPHPValue)
  672. {
  673. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  674. }
  675. $sClassParams = implode("\n", $aClassParamsPHP);
  676. // Comment on top of the class declaration
  677. //
  678. $sCodeComment = $oProperties->GetChildText('comment');
  679. // Fields
  680. //
  681. $sAttributes = '';
  682. foreach($this->oFactory->ListFields($oClass) as $oField)
  683. {
  684. // $oField
  685. $sAttCode = $oField->getAttribute('id');
  686. $sAttType = $oField->getAttribute('xsi:type');
  687. $aDependencies = array();
  688. $oDependencies = $oField->GetOptionalElement('dependencies');
  689. if (!is_null($oDependencies))
  690. {
  691. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  692. foreach($oDepNodes as $oDepAttribute)
  693. {
  694. $aDependencies[] = "'".$oDepAttribute->getAttribute('id')."'";
  695. }
  696. }
  697. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  698. $aParameters = array();
  699. if ($sAttType == 'AttributeLinkedSetIndirect')
  700. {
  701. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  702. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  703. $aParameters['ext_key_to_remote'] = $this->GetPropString($oField, 'ext_key_to_remote', '');
  704. $aParameters['allowed_values'] = 'null';
  705. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  706. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  707. $aParameters['duplicates'] = $this->GetPropBoolean($oField, 'duplicates', false);
  708. $aParameters['depends_on'] = $sDependencies;
  709. }
  710. elseif ($sAttType == 'AttributeLinkedSet')
  711. {
  712. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  713. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  714. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  715. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  716. $sEditMode = $oField->GetChildText('edit_mode');
  717. if (!is_null($sEditMode))
  718. {
  719. $aParameters['edit_mode'] = $this->EditModeToPHP($sEditMode);
  720. }
  721. if ($sOql = $oField->GetChildText('filter'))
  722. {
  723. $sEscapedOql = self::QuoteForPHP($sOql);
  724. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)";
  725. }
  726. else
  727. {
  728. $aParameters['allowed_values'] = 'null';
  729. }
  730. $aParameters['depends_on'] = $sDependencies;
  731. }
  732. elseif ($sAttType == 'AttributeExternalKey')
  733. {
  734. $aParameters['targetclass'] = $this->GetPropString($oField, 'target_class', '');
  735. // deprecated: $aParameters['jointype'] = 'null';
  736. if ($sOql = $oField->GetChildText('filter'))
  737. {
  738. $sEscapedOql = self::QuoteForPHP($sOql);
  739. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  740. }
  741. else
  742. {
  743. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  744. }
  745. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  746. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  747. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  748. $aParameters['depends_on'] = $sDependencies;
  749. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  750. $aParameters['min_autocomplete_chars'] = $this->GetPropNumber($oField, 'min_autocomplete_chars');
  751. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  752. }
  753. elseif ($sAttType == 'AttributeHierarchicalKey')
  754. {
  755. if ($sOql = $oField->GetChildText('filter'))
  756. {
  757. $sEscapedOql = self::QuoteForPHP($sOql);
  758. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  759. }
  760. else
  761. {
  762. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  763. }
  764. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  765. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  766. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  767. $aParameters['depends_on'] = $sDependencies;
  768. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  769. $aParameters['min_autocomplete_chars'] = $this->GetPropNumber($oField, 'min_autocomplete_chars');
  770. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  771. }
  772. elseif ($sAttType == 'AttributeExternalField')
  773. {
  774. $aParameters['allowed_values'] = 'null';
  775. $aParameters['extkey_attcode'] = $this->GetPropString($oField, 'extkey_attcode', '');
  776. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode', '');
  777. }
  778. elseif ($sAttType == 'AttributeURL')
  779. {
  780. $aParameters['target'] = $this->GetPropString($oField, 'target', '');
  781. $aParameters['allowed_values'] = 'null';
  782. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  783. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  784. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  785. $aParameters['depends_on'] = $sDependencies;
  786. }
  787. elseif ($sAttType == 'AttributeEnum')
  788. {
  789. $oValues = $oField->GetUniqueElement('values');
  790. $oValueNodes = $oValues->getElementsByTagName('value');
  791. $aValues = array();
  792. foreach($oValueNodes as $oValue)
  793. {
  794. // new style... $aValues[] = self::QuoteForPHP($oValue->textContent);
  795. $aValues[] = $oValue->textContent;
  796. }
  797. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  798. $sValues = '"'.implode(',', $aValues).'"';
  799. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  800. $aParameters['display_style'] = $this->GetPropString($oField, 'display_style', 'list');
  801. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  802. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  803. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  804. $aParameters['depends_on'] = $sDependencies;
  805. }
  806. elseif ($sAttType == 'AttributeBlob')
  807. {
  808. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  809. $aParameters['depends_on'] = $sDependencies;
  810. }
  811. elseif ($sAttType == 'AttributeStopWatch')
  812. {
  813. $oStates = $oField->GetUniqueElement('states');
  814. $oStateNodes = $oStates->getElementsByTagName('state');
  815. $aStates = array();
  816. foreach($oStateNodes as $oState)
  817. {
  818. $aStates[] = '"'.$oState->GetAttribute('id').'"';
  819. }
  820. $aParameters['states'] = 'array('.implode(', ', $aStates).')';
  821. $aParameters['goal_computing'] = $this->GetPropString($oField, 'goal', 'DefaultMetricComputer'); // Optional, no deadline by default
  822. $aParameters['working_time_computing'] = $this->GetPropString($oField, 'working_time', ''); // Blank (different than DefaultWorkingTimeComputer)
  823. $oThresholds = $oField->GetUniqueElement('thresholds');
  824. $oThresholdNodes = $oThresholds->getElementsByTagName('threshold');
  825. $aThresholds = array();
  826. foreach($oThresholdNodes as $oThreshold)
  827. {
  828. $iPercent = $this->GetPropNumber($oThreshold, 'percent');
  829. $oActions = $oThreshold->GetUniqueElement('actions');
  830. $oActionNodes = $oActions->getElementsByTagName('action');
  831. $aActions = array();
  832. foreach($oActionNodes as $oAction)
  833. {
  834. $oParams = $oAction->GetOptionalElement('params');
  835. $aActionParams = array();
  836. if ($oParams)
  837. {
  838. $oParamNodes = $oParams->getElementsByTagName('param');
  839. foreach($oParamNodes as $oParam)
  840. {
  841. $aActionParams[] = self::QuoteForPHP($oParam->textContent);
  842. }
  843. }
  844. $sActionParams = 'array('.implode(', ', $aActionParams).')';
  845. $sVerb = $this->GetPropString($oAction, 'verb');
  846. $aActions[] = "array('verb' => $sVerb, 'params' => $sActionParams)";
  847. }
  848. $sActions = 'array('.implode(', ', $aActions).')';
  849. $aThresholds[] = $iPercent." => array('percent' => $iPercent, 'actions' => $sActions)";
  850. }
  851. $aParameters['thresholds'] = 'array('.implode(', ', $aThresholds).')';
  852. }
  853. elseif ($sAttType == 'AttributeSubItem')
  854. {
  855. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode');
  856. $aParameters['item_code'] = $this->GetPropString($oField, 'item_code');
  857. }
  858. else
  859. {
  860. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  861. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  862. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  863. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  864. $aParameters['depends_on'] = $sDependencies;
  865. }
  866. // Optional parameters (more for historical reasons)
  867. // Added if present...
  868. //
  869. $aParameters['validation_pattern'] = $this->GetPropString($oField, 'validation_pattern');
  870. $aParameters['width'] = $this->GetPropNumber($oField, 'width');
  871. $aParameters['height'] = $this->GetPropNumber($oField, 'height');
  872. $aParameters['digits'] = $this->GetPropNumber($oField, 'digits');
  873. $aParameters['decimals'] = $this->GetPropNumber($oField, 'decimals');
  874. $aParameters['always_load_in_tables'] = $this->GetPropBoolean($oField, 'always_load_in_tables', false);
  875. $sTrackingLevel = $oField->GetChildText('tracking_level');
  876. if (!is_null($sTrackingLevel))
  877. {
  878. $aParameters['tracking_level'] = $this->TrackingLevelToPHP($sAttType, $sTrackingLevel);
  879. }
  880. $aParams = array();
  881. foreach($aParameters as $sKey => $sValue)
  882. {
  883. if (!is_null($sValue))
  884. {
  885. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  886. }
  887. }
  888. $sParams = implode(', ', $aParams);
  889. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  890. }
  891. // Lifecycle
  892. //
  893. $sLifecycle = '';
  894. if ($oLifecycle)
  895. {
  896. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  897. $sLifecycle .= "\t\t//\n";
  898. $oStimuli = $oLifecycle->GetUniqueElement('stimuli');
  899. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  900. {
  901. $sStimulus = $oStimulus->getAttribute('id');
  902. $sStimulusClass = $oStimulus->getAttribute('xsi:type');
  903. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  904. }
  905. $oStates = $oLifecycle->GetUniqueElement('states');
  906. foreach ($oStates->getElementsByTagName('state') as $oState)
  907. {
  908. $sState = $oState->getAttribute('id');
  909. $oInitialStatePath = $oState->GetOptionalElement('initial_state_path');
  910. if ($oInitialStatePath)
  911. {
  912. $aInitialStatePath = array();
  913. foreach ($oInitialStatePath->getElementsByTagName('state_ref') as $oIntermediateState)
  914. {
  915. $aInitialStatePath[] = "'".$oIntermediateState->GetText()."'";
  916. }
  917. $sInitialStatePath = 'Array('.implode(', ', $aInitialStatePath).')';
  918. }
  919. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  920. $sLifecycle .= " \"".$sState."\",\n";
  921. $sLifecycle .= " array(\n";
  922. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  923. $sLifecycle .= " \"attribute_list\" => array(\n";
  924. $oFlags = $oState->GetUniqueElement('flags');
  925. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  926. {
  927. $sFlags = $this->FlagsToPHP($oAttributeNode);
  928. if (strlen($sFlags) > 0)
  929. {
  930. $sAttCode = $oAttributeNode->GetAttribute('id');
  931. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  932. }
  933. }
  934. $sLifecycle .= " ),\n";
  935. if (!is_null($oInitialStatePath))
  936. {
  937. $sLifecycle .= " \"initial_state_path\" => $sInitialStatePath,\n";
  938. }
  939. $sLifecycle .= " )\n";
  940. $sLifecycle .= " );\n";
  941. $oTransitions = $oState->GetUniqueElement('transitions');
  942. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  943. {
  944. $sStimulus = $oTransition->GetChildText('stimulus');
  945. $sTargetState = $oTransition->GetChildText('target');
  946. $oActions = $oTransition->GetUniqueElement('actions');
  947. $aVerbs = array();
  948. foreach ($oActions->getElementsByTagName('action') as $oAction)
  949. {
  950. $sVerb = $oAction->GetChildText('verb');
  951. $aVerbs[] = "'$sVerb'";
  952. }
  953. $sActions = implode(', ', $aVerbs);
  954. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  955. }
  956. }
  957. }
  958. // ZLists
  959. //
  960. $aListRef = array(
  961. 'details' => 'details',
  962. 'standard_search' => 'search',
  963. 'list' => 'list'
  964. );
  965. $oPresentation = $oClass->GetUniqueElement('presentation');
  966. $sZlists = '';
  967. foreach ($aListRef as $sListCode => $sListTag)
  968. {
  969. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  970. if ($oListNode)
  971. {
  972. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  973. $this->ArrayOfItemsToZList($aAttributes);
  974. $sZAttributes = var_export($aAttributes, true);
  975. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  976. }
  977. }
  978. // Methods
  979. $sMethods = "";
  980. $oMethods = $oClass->GetUniqueElement('methods');
  981. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  982. {
  983. $sMethodCode = $oMethod->GetChildText('code');
  984. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  985. {
  986. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  987. }
  988. else
  989. {
  990. $sMethods .= "\n\n".$sMethodCode."\n";
  991. }
  992. }
  993. // Let's make the whole class declaration
  994. //
  995. $sPHP = "\n\n$sCodeComment\n";
  996. $sParentClass = $oClass->GetChildText('php_parent');
  997. $oPhpParent = $oClass->GetUniqueElement('php_parent', false);
  998. if ($oPhpParent)
  999. {
  1000. $sParentClass = $oPhpParent->GetChildText('name', '');
  1001. if ($sParentClass == '')
  1002. {
  1003. throw new Exception("Failed to process class '".$oClass->getAttribute('id')."', missing required tag 'name' under 'php_parent'.");
  1004. }
  1005. $sIncludeFile = $oPhpParent->GetChildText('file', '');
  1006. if ($sIncludeFile != '')
  1007. {
  1008. $sPHP .= "\nrequire_once('$sIncludeFile'); // Implementation of the class $sParentClass\n";
  1009. }
  1010. //TODO fix this !!!
  1011. // $sFullPath = $this->sSourceDir.'/'.$sModuleRelativeDir.'/'.$sIncludeFile;
  1012. // if (!file_exists($sFullPath))
  1013. // {
  1014. // throw new Exception("Failed to process class '".$oClass->getAttribute('id')."', from '$sModuleRelativeDir'. The required include file: '$sFullPath' does not exist.");
  1015. // }
  1016. }
  1017. else
  1018. {
  1019. $sParentClass = $oClass->GetChildText('parent', 'DBObject');
  1020. }
  1021. if ($oProperties->GetChildText('abstract') == 'true')
  1022. {
  1023. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  1024. }
  1025. else
  1026. {
  1027. $sPHP .= 'class '.$oClass->getAttribute('id');
  1028. }
  1029. $sPHP .= " extends $sParentClass\n";
  1030. $sPHP .=
  1031. <<<EOF
  1032. {
  1033. public static function Init()
  1034. {
  1035. \$aParams = array
  1036. (
  1037. $sClassParams
  1038. );
  1039. MetaModel::Init_Params(\$aParams);
  1040. MetaModel::Init_InheritAttributes();
  1041. $sAttributes
  1042. $sLifecycle
  1043. $sZlists
  1044. }
  1045. $sMethods
  1046. }
  1047. EOF;
  1048. return $sPHP;
  1049. }// function CompileClass()
  1050. protected function CompileMenu($oMenu, $sTempTargetDir, $sFinalTargetDir, $sModuleRelativeDir, $oP)
  1051. {
  1052. $this->CompileFiles($oMenu, $sTempTargetDir.'/'.$sModuleRelativeDir, $sFinalTargetDir.'/'.$sModuleRelativeDir, $sModuleRelativeDir);
  1053. $sMenuId = $oMenu->getAttribute("id");
  1054. $sMenuClass = $oMenu->getAttribute("xsi:type");
  1055. $sParent = $oMenu->GetChildText('parent', null);
  1056. if ($sParent)
  1057. {
  1058. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  1059. }
  1060. else
  1061. {
  1062. $sParentSpec = '-1';
  1063. }
  1064. $fRank = (float) $oMenu->GetChildText('rank');
  1065. switch($sMenuClass)
  1066. {
  1067. case 'WebPageMenuNode':
  1068. $sUrl = $oMenu->GetChildText('url');
  1069. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  1070. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  1071. break;
  1072. case 'DashboardMenuNode':
  1073. $sTemplateFile = $oMenu->GetChildText('definition_file', '');
  1074. if ($sTemplateFile != '')
  1075. {
  1076. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  1077. }
  1078. else
  1079. {
  1080. $oDashboardDefinition = $oMenu->GetOptionalElement('definition');
  1081. if ($oDashboardDefinition == null)
  1082. {
  1083. throw(new DOMFormatException('Missing definition for Dashboard menu "'.$sMenuId.'" expecting either a tag "definition_file" or "definition".'));
  1084. }
  1085. $sFileName = strtolower(str_replace(array(':', '/', '\\', '*'), '_', $sMenuId)).'_dashboard_menu.xml';
  1086. $sTemplateSpec = $this->PathToPHP($sFileName, $sModuleRelativeDir);
  1087. $oXMLDoc = new DOMDocument('1.0', 'UTF-8');
  1088. $oXMLDoc->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  1089. $oXMLDoc->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  1090. $oRootNode = $oXMLDoc->createElement('dashboard'); // make sure that the document is not empty
  1091. $oRootNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  1092. $oXMLDoc->appendChild($oRootNode);
  1093. foreach($oDashboardDefinition->childNodes as $oNode)
  1094. {
  1095. $oDefNode = $oXMLDoc->importNode($oNode, true); // layout, cells, etc Nodes and below
  1096. $oRootNode->appendChild($oDefNode);
  1097. }
  1098. $oXMLDoc->save($sTempTargetDir.'/'.$sModuleRelativeDir.'/'.$sFileName);
  1099. }
  1100. $sNewMenu = "new DashboardMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  1101. break;
  1102. case 'ShortcutContainerMenuNode':
  1103. $sNewMenu = "new ShortcutContainerMenuNode('$sMenuId', $sParentSpec, $fRank);";
  1104. break;
  1105. case 'OQLMenuNode':
  1106. $sOQL = self::QuoteForPHP($oMenu->GetChildText('oql'));
  1107. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  1108. $sNewMenu = "new OQLMenuNode('$sMenuId', $sOQL, $sParentSpec, $fRank, $bSearch);";
  1109. break;
  1110. case 'NewObjectMenuNode':
  1111. $sClass = $oMenu->GetChildText('class');
  1112. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  1113. break;
  1114. case 'SearchMenuNode':
  1115. $sClass = $oMenu->GetChildText('class');
  1116. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  1117. break;
  1118. case 'TemplateMenuNode':
  1119. $sTemplateFile = $oMenu->GetChildText('template_file');
  1120. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  1121. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  1122. {
  1123. $sEnableAction = $oMenu->GetChildText('enable_action', 'null');
  1124. $sEnablePermission = $oMenu->GetChildText('enable_permission', 'UR_ALLOWED_YES');
  1125. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  1126. if ($sEnableStimulus != null)
  1127. {
  1128. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  1129. }
  1130. else
  1131. {
  1132. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  1133. }
  1134. }
  1135. else
  1136. {
  1137. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  1138. }
  1139. break;
  1140. case 'MenuGroup':
  1141. default:
  1142. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  1143. {
  1144. $sEnableAction = $oMenu->GetChildText('enable_action', 'null');
  1145. $sEnablePermission = $oMenu->GetChildText('enable_permission', 'UR_ALLOWED_YES');
  1146. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  1147. if ($sEnableStimulus != null)
  1148. {
  1149. $sNewMenu = "new $sMenuClass('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  1150. }
  1151. else
  1152. {
  1153. $sNewMenu = "new $sMenuClass('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  1154. }
  1155. }
  1156. else
  1157. {
  1158. $sNewMenu = "new $sMenuClass('$sMenuId', $fRank);";
  1159. }
  1160. }
  1161. $sIndent = '';
  1162. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  1163. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  1164. {
  1165. $sAutoReload = self::QuoteForPHP($sAutoReload);
  1166. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
  1167. }
  1168. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  1169. if ($sAdminOnly && ($sAdminOnly == '1'))
  1170. {
  1171. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  1172. $sPHP .= $sIndent."{\n";
  1173. foreach($aPHPMenu as $sPHPLine)
  1174. {
  1175. $sPHP .= $sIndent." $sPHPLine\n";
  1176. }
  1177. $sPHP .= $sIndent."}\n";
  1178. }
  1179. else
  1180. {
  1181. $sPHP = '';
  1182. foreach($aPHPMenu as $sPHPLine)
  1183. {
  1184. $sPHP .= $sIndent.$sPHPLine."\n";
  1185. }
  1186. }
  1187. return $sPHP;
  1188. } // function CompileMenu
  1189. /**
  1190. * Helper to compute the grant, taking any existing grant into account
  1191. */
  1192. protected function CumulateGrant(&$aGrants, $sKey, $bGrant)
  1193. {
  1194. if (isset($aGrants[$sKey]))
  1195. {
  1196. if (!$bGrant)
  1197. {
  1198. $aGrants[$sKey] = false;
  1199. }
  1200. }
  1201. else
  1202. {
  1203. $aGrants[$sKey] = $bGrant;
  1204. }
  1205. }
  1206. protected function CompileUserRights($oUserRightsNode)
  1207. {
  1208. static $aActionsInShort = array(
  1209. 'read' => 'r',
  1210. 'bulk read' => 'br',
  1211. 'write' => 'w',
  1212. 'bulk write' => 'bw',
  1213. 'delete' => 'd',
  1214. 'bulk delete' => 'bd',
  1215. );
  1216. // Preliminary : create an index so that links will be taken into account implicitely
  1217. $aLinkToClasses = array();
  1218. $oClasses = $this->oFactory->ListAllClasses();
  1219. foreach($oClasses as $oClass)
  1220. {
  1221. $bIsLink = false;
  1222. $oProperties = $oClass->GetOptionalElement('properties');
  1223. if ($oProperties)
  1224. {
  1225. $bIsLink = (bool) $this->GetPropNumber($oProperties, 'is_link', 0);
  1226. }
  1227. if ($bIsLink)
  1228. {
  1229. foreach($this->oFactory->ListFields($oClass) as $oField)
  1230. {
  1231. $sAttType = $oField->getAttribute('xsi:type');
  1232. if (($sAttType == 'AttributeExternalKey') || ($sAttType == 'AttributeHierarchicalKey'))
  1233. {
  1234. $sOnTargetDel = $oField->GetChildText('on_target_delete');
  1235. if ($sOnTargetDel == 'DEL_AUTO')
  1236. {
  1237. $sTargetClass = $oField->GetChildText('target_class');
  1238. $aLinkToClasses[$oClass->getAttribute('id')][] = $sTargetClass;
  1239. }
  1240. }
  1241. }
  1242. }
  1243. }
  1244. // Groups
  1245. //
  1246. $aGroupClasses = array();
  1247. $oGroups = $oUserRightsNode->GetUniqueElement('groups');
  1248. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  1249. {
  1250. $sGroupId = $oGroup->getAttribute("id");
  1251. $aClasses = array();
  1252. $oClasses = $oGroup->GetUniqueElement('classes');
  1253. foreach($oClasses->getElementsByTagName('class') as $oClass)
  1254. {
  1255. $sClass = $oClass->getAttribute("id");
  1256. $aClasses[] = $sClass;
  1257. //$bSubclasses = $this->GetPropBoolean($oClass, 'subclasses', true);
  1258. //if ($bSubclasses)...
  1259. }
  1260. $aGroupClasses[$sGroupId] = $aClasses;
  1261. }
  1262. // Profiles and grants
  1263. //
  1264. $aProfiles = array();
  1265. // Hardcode the administrator profile
  1266. $aProfiles[1] = array(
  1267. 'name' => 'Administrator',
  1268. 'description' => 'Has the rights on everything (bypassing any control)'
  1269. );
  1270. $aGrants = array();
  1271. $oProfiles = $oUserRightsNode->GetUniqueElement('profiles');
  1272. foreach($oProfiles->getElementsByTagName('profile') as $oProfile)
  1273. {
  1274. $iProfile = $oProfile->getAttribute("id");
  1275. $sName = $oProfile->GetChildText('name');
  1276. $sDescription = $oProfile->GetChildText('description');
  1277. $oGroups = $oProfile->GetUniqueElement('groups');
  1278. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  1279. {
  1280. $sGroupId = $oGroup->getAttribute("id");
  1281. $aActions = array();
  1282. $oActions = $oGroup->GetUniqueElement('actions');
  1283. foreach($oActions->getElementsByTagName('action') as $oAction)
  1284. {
  1285. $sAction = $oAction->getAttribute("id");
  1286. $sType = $oAction->getAttribute("xsi:type");
  1287. $sGrant = $oAction->GetText();
  1288. $bGrant = ($sGrant == 'allow');
  1289. if ($sGroupId == '*')
  1290. {
  1291. $aGrantClasses = array('*');
  1292. }
  1293. else
  1294. {
  1295. $aGrantClasses = $aGroupClasses[$sGroupId];
  1296. }
  1297. foreach ($aGrantClasses as $sClass)
  1298. {
  1299. if ($sType == 'stimulus')
  1300. {
  1301. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'_s_'.$sAction, $bGrant);
  1302. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'+_s_'.$sAction, $bGrant); // subclasses inherit this grant
  1303. }
  1304. else
  1305. {
  1306. $sAction = $aActionsInShort[$sType];
  1307. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'_'.$sAction, $bGrant);
  1308. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'+_'.$sAction, $bGrant); // subclasses inherit this grant
  1309. }
  1310. }
  1311. }
  1312. }
  1313. $aProfiles[$iProfile] = array(
  1314. 'name' => $sName,
  1315. 'description' => $sDescription
  1316. );
  1317. }
  1318. $sProfiles = var_export($aProfiles, true);
  1319. $sGrants = var_export($aGrants, true);
  1320. $sLinkToClasses = var_export($aLinkToClasses, true);
  1321. $sPHP =
  1322. <<<EOF
  1323. //
  1324. // List of constant profiles
  1325. // - used by the class URP_Profiles at setup (create/update/delete records)
  1326. // - used by the addon UserRightsProfile to determine user rights
  1327. //
  1328. class ProfilesConfig
  1329. {
  1330. protected static \$aPROFILES = $sProfiles;
  1331. protected static \$aGRANTS = $sGrants;
  1332. protected static \$aLINKTOCLASSES = $sLinkToClasses;
  1333. // Now replaced by MetaModel::GetLinkClasses (working with 1.x)
  1334. // This function could be deprecated
  1335. public static function GetLinkClasses()
  1336. {
  1337. return self::\$aLINKTOCLASSES;
  1338. }
  1339. public static function GetProfileActionGrant(\$iProfileId, \$sClass, \$sAction)
  1340. {
  1341. // Search for a grant, starting from the most explicit declaration,
  1342. // then searching for less and less explicit declaration
  1343. // 1 - The class itself
  1344. //
  1345. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_'.\$sAction;
  1346. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1347. {
  1348. return self::\$aGRANTS[\$sGrantKey];
  1349. }
  1350. // 2 - The parent classes, up to the root class
  1351. //
  1352. foreach (MetaModel::EnumParentClasses(\$sClass, ENUM_PARENT_CLASSES_EXCLUDELEAF, false /*bRootFirst*/) as \$sParent)
  1353. {
  1354. \$sGrantKey = \$iProfileId.'_'.\$sParent.'+_'.\$sAction;
  1355. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1356. {
  1357. return self::\$aGRANTS[\$sGrantKey];
  1358. }
  1359. }
  1360. // 3 - The related classes (if the current is an N-N link with AUTO_DEL)
  1361. //
  1362. if (array_key_exists(\$sClass, self::\$aLINKTOCLASSES))
  1363. {
  1364. // Get the grant for the remote classes. The resulting grant is:
  1365. // - One YES => YES
  1366. // - 100% undefined => undefined
  1367. // - otherwise => NO
  1368. //
  1369. // Having write allowed on the remote class implies write + delete on the N-N link class
  1370. if (\$sAction == 'd')
  1371. {
  1372. \$sRemoteAction = 'w';
  1373. }
  1374. elseif (\$sAction == 'bd')
  1375. {
  1376. \$sRemoteAction = 'bw';
  1377. }
  1378. else
  1379. {
  1380. \$sRemoteAction = \$sAction;
  1381. }
  1382. foreach (self::\$aLINKTOCLASSES[\$sClass] as \$sRemoteClass)
  1383. {
  1384. \$bUndefined = true;
  1385. \$bGrant = self::GetProfileActionGrant(\$iProfileId, \$sRemoteClass, \$sAction);
  1386. if (\$bGrant === true)
  1387. {
  1388. return true;
  1389. }
  1390. if (\$bGrant === false)
  1391. {
  1392. \$bUndefined = false;
  1393. }
  1394. }
  1395. if (!\$bUndefined)
  1396. {
  1397. return false;
  1398. }
  1399. }
  1400. // 4 - All
  1401. //
  1402. \$sGrantKey = \$iProfileId.'_*_'.\$sAction;
  1403. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1404. {
  1405. return self::\$aGRANTS[\$sGrantKey];
  1406. }
  1407. // Still undefined for this class
  1408. return null;
  1409. }
  1410. public static function GetProfileStimulusGrant(\$iProfileId, \$sClass, \$sStimulus)
  1411. {
  1412. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_s_'.\$sStimulus;
  1413. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1414. {
  1415. return self::\$aGRANTS[\$sGrantKey];
  1416. }
  1417. \$sGrantKey = \$iProfileId.'_*_s_'.\$sStimulus;
  1418. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1419. {
  1420. return self::\$aGRANTS[\$sGrantKey];
  1421. }
  1422. return null;
  1423. }
  1424. // returns an array of id => array of column => php value(so-called "real value")
  1425. public static function GetProfilesValues()
  1426. {
  1427. return self::\$aPROFILES;
  1428. }
  1429. }
  1430. EOF;
  1431. return $sPHP;
  1432. } // function CompileUserRights
  1433. protected function CompileDictionary($oDictionaryNode, $sTempTargetDir, $sFinalTargetDir)
  1434. {
  1435. $sLang = $oDictionaryNode->getAttribute('id');
  1436. $sEnglishLanguageDesc = $oDictionaryNode->GetChildText('english_description');
  1437. $sLocalizedLanguageDesc = $oDictionaryNode->GetChildText('localized_description');
  1438. $aEntriesPHP = array();
  1439. $oEntries = $oDictionaryNode->GetUniqueElement('entries');
  1440. foreach($oEntries->getElementsByTagName('entry') as $oEntry)
  1441. {
  1442. $sStringCode = $oEntry->getAttribute('id');
  1443. $sValue = $oEntry->GetText();
  1444. $aEntriesPHP[] = "\t'$sStringCode' => ".self::QuoteForPHP($sValue, true).",";
  1445. }
  1446. $sEntriesPHP = implode("\n", $aEntriesPHP);
  1447. $sEscEnglishLanguageDesc = self::QuoteForPHP($sEnglishLanguageDesc);
  1448. $sEscLocalizedLanguageDesc = self::QuoteForPHP($sLocalizedLanguageDesc);
  1449. $sPHPDict =
  1450. <<<EOF
  1451. <?php
  1452. //
  1453. // Dictionary built by the compiler for the language "$sLang"
  1454. //
  1455. Dict::Add('$sLang', $sEscEnglishLanguageDesc, $sEscLocalizedLanguageDesc, array(
  1456. $sEntriesPHP
  1457. ));
  1458. EOF;
  1459. $sSafeLang = str_replace(' ', '-', strtolower(trim($sLang)));
  1460. $sDictFile = $sTempTargetDir.'/dictionaries/'.$sSafeLang.'.dict.php';
  1461. file_put_contents($sDictFile, $sPHPDict);
  1462. }
  1463. // Transform the file references into the corresponding filename (and create the file in the relevant directory)
  1464. //
  1465. protected function CompileFiles($oNode, $sTempTargetDir, $sFinalTargetDir, $sRelativePath)
  1466. {
  1467. $oFileRefs = $oNode->GetNodes(".//fileref");
  1468. foreach ($oFileRefs as $oFileRef)
  1469. {
  1470. $sFileId = $oFileRef->getAttribute('ref');
  1471. if ($sFileId !== '')
  1472. {
  1473. $oNodes = $this->oFactory->GetNodes("/itop_design/files/file[@id='$sFileId']");
  1474. if ($oNodes->length == 0)
  1475. {
  1476. throw new DOMFormatException('Could not find the file with ref '.$sFileId);
  1477. }
  1478. $sName = $oNodes->item(0)->GetChildText('name');
  1479. $sData = base64_decode($oNodes->item(0)->GetChildText('data'));
  1480. $aPathInfo = pathinfo($sName);
  1481. $sFile = $sFileId.'.'.$aPathInfo['extension'];
  1482. $sFilePath = $sTempTargetDir.'/images/'.$sFile;
  1483. @mkdir($sTempTargetDir.'/images');
  1484. file_put_contents($sFilePath, $sData);
  1485. if (!file_exists($sFilePath))
  1486. {
  1487. throw new Exception('Could not write icon file '.$sFilePath);
  1488. }
  1489. $oParentNode = $oFileRef->parentNode;
  1490. $oParentNode->removeChild($oFileRef);
  1491. $oTextNode = $oParentNode->ownerDocument->createTextNode($sRelativePath.'/images/'.$sFile);
  1492. $oParentNode->appendChild($oTextNode);
  1493. }
  1494. }
  1495. }
  1496. protected function CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, $sNodeName, $sTargetFile)
  1497. {
  1498. if (($sIcon = $oBrandingNode->GetChildText($sNodeName)) && (strlen($sIcon) > 0))
  1499. {
  1500. if (substr($sIcon, 0, 8) == 'branding')
  1501. {
  1502. $sSourceFile = $sTempTargetDir.'/'.$sIcon;
  1503. }
  1504. else
  1505. {
  1506. $sSourceFile = APPROOT.$sIcon;
  1507. }
  1508. $sTargetFile = $sTempTargetDir.'/branding/'.$sTargetFile.'.png';
  1509. if (!file_exists($sSourceFile))
  1510. {
  1511. throw new Exception("Branding $sNodeName: could not find the file $sIcon ($sSourceFile)");
  1512. }
  1513. // Note: rename makes sense only when the file given as a file ref, otherwise it may be an item of the application (thus it must be kept there)
  1514. copy($sSourceFile, $sTargetFile);
  1515. }
  1516. }
  1517. protected function CompileBranding($oBrandingNode, $sTempTargetDir, $sFinalTargetDir)
  1518. {
  1519. if ($oBrandingNode)
  1520. {
  1521. // Transform file refs into files in the images folder
  1522. SetupUtils::builddir($sTempTargetDir.'/branding');
  1523. $this->CompileFiles($oBrandingNode, $sTempTargetDir.'/branding', $sFinalTargetDir.'/branding', 'branding');
  1524. $this->CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, 'main_logo', 'main-logo');
  1525. $this->CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, 'login_logo', 'login-logo');
  1526. $this->CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, 'portal_logo', 'portal-logo');
  1527. // Cleanup the images directory (made by CompileFiles)
  1528. SetupUtils::rrmdir($sTempTargetDir.'/branding/images');
  1529. }
  1530. }
  1531. }
  1532. ?>