compiler.class.inc.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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->GetNodes('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->GetNodes('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 GetMandatoryPropString($oNode, $sTag)
  455. {
  456. $val = $oNode->GetChildText($sTag);
  457. if (!is_null($val) && ($val !== ''))
  458. {
  459. return "'".$val."'";
  460. }
  461. else
  462. {
  463. throw new DOMFormatException("missing (or empty) mandatory tag '$sTag' under the tag '".$oNode->nodeName."'");
  464. }
  465. }
  466. protected function GetPropBoolean($oNode, $sTag, $bDefault = null)
  467. {
  468. $val = $oNode->GetChildText($sTag);
  469. if (is_null($val))
  470. {
  471. if (is_null($bDefault))
  472. {
  473. return null;
  474. }
  475. else
  476. {
  477. return $bDefault ? 'true' : 'false';
  478. }
  479. }
  480. return $val == 'true' ? 'true' : 'false';
  481. }
  482. protected function GetPropNumber($oNode, $sTag, $nDefault = null)
  483. {
  484. $val = $oNode->GetChildText($sTag);
  485. if (is_null($val))
  486. {
  487. if (is_null($nDefault))
  488. {
  489. return null;
  490. }
  491. else
  492. {
  493. $val = $nDefault;
  494. }
  495. }
  496. return (string)$val;
  497. }
  498. /**
  499. * Adds quotes and escape characters
  500. */
  501. protected function QuoteForPHP($sStr, $bSimpleQuotes = false)
  502. {
  503. if ($bSimpleQuotes)
  504. {
  505. $sEscaped = str_replace(array('\\', "'"), array('\\\\', "\\'"), $sStr);
  506. $sRet = "'$sEscaped'";
  507. }
  508. else
  509. {
  510. $sEscaped = str_replace(array('\\', '"', "\n"), array('\\\\', '\\"', '\\n'), $sStr);
  511. $sRet = '"'.$sEscaped.'"';
  512. }
  513. return $sRet;
  514. }
  515. protected function CompileConstant($oConstant)
  516. {
  517. $sName = $oConstant->getAttribute('id');
  518. $sType = $oConstant->getAttribute('xsi:type');
  519. $sText = $oConstant->GetText(null);
  520. switch ($sType)
  521. {
  522. case 'integer':
  523. if (is_null($sText))
  524. {
  525. // No data given => null
  526. $sScalar = 'null';
  527. }
  528. else
  529. {
  530. $sScalar = (string)(int)$sText;
  531. }
  532. break;
  533. case 'float':
  534. if (is_null($sText))
  535. {
  536. // No data given => null
  537. $sScalar = 'null';
  538. }
  539. else
  540. {
  541. $sScalar = (string)(float)$sText;
  542. }
  543. break;
  544. case 'bool':
  545. if (is_null($sText))
  546. {
  547. // No data given => null
  548. $sScalar = 'null';
  549. }
  550. else
  551. {
  552. $sScalar = ($sText == 'true') ? 'true' : 'false';
  553. }
  554. break;
  555. case 'string':
  556. default:
  557. $sScalar = $this->QuoteForPHP($sText, true);
  558. }
  559. $sPHPDefine = "define('$sName', $sScalar);";
  560. return $sPHPDefine;
  561. }
  562. protected function CompileClass($oClass, $sTempTargetDir, $sFinalTargetDir, $sModuleRelativeDir, $oP)
  563. {
  564. $sClass = $oClass->getAttribute('id');
  565. $oProperties = $oClass->GetUniqueElement('properties');
  566. // Class caracteristics
  567. //
  568. $aClassParams = array();
  569. $aClassParams['category'] = $this->GetPropString($oProperties, 'category', '');
  570. $aClassParams['key_type'] = "'autoincrement'";
  571. if ((bool) $this->GetPropNumber($oProperties, 'is_link', 0))
  572. {
  573. $aClassParams['is_link'] = 'true';
  574. }
  575. if ($oNaming = $oProperties->GetOptionalElement('naming'))
  576. {
  577. $oNameAttributes = $oNaming->GetUniqueElement('attributes');
  578. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  579. $aNameAttCodes = array();
  580. foreach($oAttributes as $oAttribute)
  581. {
  582. $aNameAttCodes[] = $oAttribute->getAttribute('id');
  583. }
  584. if (count($aNameAttCodes) > 1)
  585. {
  586. // New style...
  587. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  588. }
  589. elseif (count($aNameAttCodes) == 1)
  590. {
  591. // New style...
  592. $sNameAttCode = "'$aNameAttCodes[0]'";
  593. }
  594. else
  595. {
  596. $sNameAttCode = "''";
  597. }
  598. }
  599. else
  600. {
  601. $sNameAttCode = "''";
  602. }
  603. $aClassParams['name_attcode'] = $sNameAttCode;
  604. $oLifecycle = $oClass->GetOptionalElement('lifecycle');
  605. if ($oLifecycle)
  606. {
  607. $sStateAttCode = $oLifecycle->GetChildText('attribute');
  608. }
  609. else
  610. {
  611. $sStateAttCode = "";
  612. }
  613. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  614. if ($oReconciliation = $oProperties->GetOptionalElement('reconciliation'))
  615. {
  616. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  617. $aReconcAttCodes = array();
  618. foreach($oReconcAttributes as $oAttribute)
  619. {
  620. $aReconcAttCodes[] = $oAttribute->getAttribute('id');
  621. }
  622. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  623. }
  624. else
  625. {
  626. $sReconcKeys = "array()";
  627. }
  628. $aClassParams['reconc_keys'] = $sReconcKeys;
  629. $aClassParams['db_table'] = $this->GetPropString($oProperties, 'db_table', '');
  630. $aClassParams['db_key_field'] = $this->GetPropString($oProperties, 'db_key_field', 'id');
  631. if (array_key_exists($sClass, $this->aRootClasses))
  632. {
  633. $sDefaultFinalClass = 'finalclass';
  634. }
  635. else
  636. {
  637. $sDefaultFinalClass = '';
  638. }
  639. $aClassParams['db_finalclass_field'] = $this->GetPropString($oProperties, 'db_final_class_field', $sDefaultFinalClass);
  640. if (($sDisplayTemplate = $oProperties->GetChildText('display_template')) && (strlen($sDisplayTemplate) > 0))
  641. {
  642. $sDisplayTemplate = $sModuleRelativeDir.'/'.$sDisplayTemplate;
  643. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  644. }
  645. $this->CompileFiles($oProperties, $sTempTargetDir.'/'.$sModuleRelativeDir, $sFinalTargetDir.'/'.$sModuleRelativeDir, '');
  646. if (($sIcon = $oProperties->GetChildText('icon')) && (strlen($sIcon) > 0))
  647. {
  648. $sIcon = $sModuleRelativeDir.'/'.$sIcon;
  649. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  650. }
  651. $oOrder = $oProperties->GetOptionalElement('order');
  652. if ($oOrder)
  653. {
  654. $oColumnsNode = $oOrder->GetUniqueElement('columns');
  655. $oColumns = $oColumnsNode->getElementsByTagName('column');
  656. $aSortColumns = array();
  657. foreach($oColumns as $oColumn)
  658. {
  659. $aSortColumns[] = "'".$oColumn->getAttribute('id')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  660. }
  661. if (count($aSortColumns) > 0)
  662. {
  663. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  664. }
  665. }
  666. if ($oIndexes = $oProperties->GetOptionalElement('indexes'))
  667. {
  668. $aIndexes = array();
  669. foreach($oIndexes->getElementsByTagName('index') as $oIndex)
  670. {
  671. $sIndexId = $oIndex->getAttribute('id');
  672. $oAttributes = $oIndex->GetUniqueElement('attributes');
  673. foreach($oAttributes->getElementsByTagName('attribute') as $oAttribute)
  674. {
  675. $aIndexes[$sIndexId][] = $oAttribute->getAttribute('id');
  676. }
  677. }
  678. $aClassParams['indexes'] = var_export($aIndexes, true);
  679. }
  680. // Finalize class params declaration
  681. //
  682. $aClassParamsPHP = array();
  683. foreach($aClassParams as $sKey => $sPHPValue)
  684. {
  685. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  686. }
  687. $sClassParams = implode("\n", $aClassParamsPHP);
  688. // Comment on top of the class declaration
  689. //
  690. $sCodeComment = $oProperties->GetChildText('comment');
  691. // Fields
  692. //
  693. $sAttributes = '';
  694. foreach($this->oFactory->ListFields($oClass) as $oField)
  695. {
  696. try
  697. {
  698. // $oField
  699. $sAttCode = $oField->getAttribute('id');
  700. $sAttType = $oField->getAttribute('xsi:type');
  701. $aDependencies = array();
  702. $oDependencies = $oField->GetOptionalElement('dependencies');
  703. if (!is_null($oDependencies))
  704. {
  705. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  706. foreach($oDepNodes as $oDepAttribute)
  707. {
  708. $aDependencies[] = "'".$oDepAttribute->getAttribute('id')."'";
  709. }
  710. }
  711. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  712. $aParameters = array();
  713. if ($sAttType == 'AttributeLinkedSetIndirect')
  714. {
  715. $aParameters['linked_class'] = $this->GetMandatoryPropString($oField, 'linked_class', '');
  716. $aParameters['ext_key_to_me'] = $this->GetMandatoryPropString($oField, 'ext_key_to_me', '');
  717. $aParameters['ext_key_to_remote'] = $this->GetMandatoryPropString($oField, 'ext_key_to_remote', '');
  718. $aParameters['allowed_values'] = 'null';
  719. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  720. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  721. $aParameters['duplicates'] = $this->GetPropBoolean($oField, 'duplicates', false);
  722. $aParameters['depends_on'] = $sDependencies;
  723. }
  724. elseif ($sAttType == 'AttributeLinkedSet')
  725. {
  726. $aParameters['linked_class'] = $this->GetMandatoryPropString($oField, 'linked_class', '');
  727. $aParameters['ext_key_to_me'] = $this->GetMandatoryPropString($oField, 'ext_key_to_me', '');
  728. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  729. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  730. $sEditMode = $oField->GetChildText('edit_mode');
  731. if (!is_null($sEditMode))
  732. {
  733. $aParameters['edit_mode'] = $this->EditModeToPHP($sEditMode);
  734. }
  735. if ($sOql = $oField->GetChildText('filter'))
  736. {
  737. $sEscapedOql = self::QuoteForPHP($sOql);
  738. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)";
  739. }
  740. else
  741. {
  742. $aParameters['allowed_values'] = 'null';
  743. }
  744. $aParameters['depends_on'] = $sDependencies;
  745. }
  746. elseif ($sAttType == 'AttributeExternalKey')
  747. {
  748. $aParameters['targetclass'] = $this->GetPropString($oField, 'target_class', '');
  749. // deprecated: $aParameters['jointype'] = 'null';
  750. if ($sOql = $oField->GetChildText('filter'))
  751. {
  752. $sEscapedOql = self::QuoteForPHP($sOql);
  753. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  754. }
  755. else
  756. {
  757. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  758. }
  759. $aParameters['sql'] = $this->GetMandatoryPropString($oField, 'sql', '');
  760. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  761. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  762. $aParameters['depends_on'] = $sDependencies;
  763. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  764. $aParameters['min_autocomplete_chars'] = $this->GetPropNumber($oField, 'min_autocomplete_chars');
  765. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  766. $aParameters['display_style'] = $this->GetPropString($oField, 'display_style', 'select');
  767. }
  768. elseif ($sAttType == 'AttributeHierarchicalKey')
  769. {
  770. if ($sOql = $oField->GetChildText('filter'))
  771. {
  772. $sEscapedOql = self::QuoteForPHP($sOql);
  773. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  774. }
  775. else
  776. {
  777. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  778. }
  779. $aParameters['sql'] = $this->GetMandatoryPropString($oField, 'sql', '');
  780. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  781. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  782. $aParameters['depends_on'] = $sDependencies;
  783. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  784. $aParameters['min_autocomplete_chars'] = $this->GetPropNumber($oField, 'min_autocomplete_chars');
  785. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  786. }
  787. elseif ($sAttType == 'AttributeExternalField')
  788. {
  789. $aParameters['allowed_values'] = 'null';
  790. $aParameters['extkey_attcode'] = $this->GetMandatoryPropString($oField, 'extkey_attcode', '');
  791. $aParameters['target_attcode'] = $this->GetMandatoryPropString($oField, 'target_attcode', '');
  792. }
  793. elseif ($sAttType == 'AttributeURL')
  794. {
  795. $aParameters['target'] = $this->GetPropString($oField, 'target', '');
  796. $aParameters['allowed_values'] = 'null';
  797. $aParameters['sql'] = $this->GetMandatoryPropString($oField, 'sql', '');
  798. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  799. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  800. $aParameters['depends_on'] = $sDependencies;
  801. }
  802. elseif ($sAttType == 'AttributeEnum')
  803. {
  804. $oValues = $oField->GetUniqueElement('values');
  805. $oValueNodes = $oValues->getElementsByTagName('value');
  806. $aValues = array();
  807. foreach($oValueNodes as $oValue)
  808. {
  809. // new style... $aValues[] = self::QuoteForPHP($oValue->textContent);
  810. $aValues[] = $oValue->textContent;
  811. }
  812. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  813. $sValues = '"'.implode(',', $aValues).'"';
  814. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  815. $aParameters['display_style'] = $this->GetPropString($oField, 'display_style', 'list');
  816. $aParameters['sql'] = $this->GetMandatoryPropString($oField, 'sql', '');
  817. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  818. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  819. $aParameters['depends_on'] = $sDependencies;
  820. }
  821. elseif ($sAttType == 'AttributeBlob')
  822. {
  823. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  824. $aParameters['depends_on'] = $sDependencies;
  825. }
  826. elseif ($sAttType == 'AttributeStopWatch')
  827. {
  828. $oStates = $oField->GetUniqueElement('states');
  829. $oStateNodes = $oStates->getElementsByTagName('state');
  830. $aStates = array();
  831. foreach($oStateNodes as $oState)
  832. {
  833. $aStates[] = '"'.$oState->GetAttribute('id').'"';
  834. }
  835. $aParameters['states'] = 'array('.implode(', ', $aStates).')';
  836. $aParameters['goal_computing'] = $this->GetPropString($oField, 'goal', 'DefaultMetricComputer'); // Optional, no deadline by default
  837. $aParameters['working_time_computing'] = $this->GetPropString($oField, 'working_time', ''); // Blank (different than DefaultWorkingTimeComputer)
  838. $oThresholds = $oField->GetUniqueElement('thresholds');
  839. $oThresholdNodes = $oThresholds->getElementsByTagName('threshold');
  840. $aThresholds = array();
  841. foreach($oThresholdNodes as $oThreshold)
  842. {
  843. $iPercent = $this->GetPropNumber($oThreshold, 'percent');
  844. $oHighlight = $oThreshold->GetUniqueElement('highlight', false);
  845. $sHighlight = '';
  846. if($oHighlight)
  847. {
  848. $sCode = $oHighlight->GetChildText('code');
  849. $bPersistent = $this->GetPropBoolean($oHighlight, 'persistent', false);
  850. $sHighlight = "'highlight' => array('code' => '$sCode', 'persistent' => ".($bPersistent ? 'true' : 'false')."), ";
  851. }
  852. $oActions = $oThreshold->GetUniqueElement('actions');
  853. $oActionNodes = $oActions->getElementsByTagName('action');
  854. $aActions = array();
  855. foreach($oActionNodes as $oAction)
  856. {
  857. $oParams = $oAction->GetOptionalElement('params');
  858. $aActionParams = array();
  859. if ($oParams)
  860. {
  861. $oParamNodes = $oParams->getElementsByTagName('param');
  862. foreach($oParamNodes as $oParam)
  863. {
  864. $sParamType = $oParam->getAttribute('xsi:type');
  865. if ($sParamType == '')
  866. {
  867. $sParamType = 'string';
  868. }
  869. $aActionParams[] = "array('type' => '$sParamType', 'value' => '".self::QuoteForPHP($oParam->textContent)."')";
  870. }
  871. }
  872. $sActionParams = 'array('.implode(', ', $aActionParams).')';
  873. $sVerb = $this->GetPropString($oAction, 'verb');
  874. $aActions[] = "array('verb' => $sVerb, 'params' => $sActionParams)";
  875. }
  876. $sActions = 'array('.implode(', ', $aActions).')';
  877. $aThresholds[] = $iPercent." => array('percent' => $iPercent, $sHighlight 'actions' => $sActions)";
  878. }
  879. $aParameters['thresholds'] = 'array('.implode(', ', $aThresholds).')';
  880. }
  881. elseif ($sAttType == 'AttributeSubItem')
  882. {
  883. $aParameters['target_attcode'] = $this->GetMandatoryPropString($oField, 'target_attcode');
  884. $aParameters['item_code'] = $this->GetMandatoryPropString($oField, 'item_code');
  885. }
  886. else
  887. {
  888. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  889. $aParameters['sql'] = $this->GetMandatoryPropString($oField, 'sql', '');
  890. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  891. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  892. $aParameters['depends_on'] = $sDependencies;
  893. }
  894. // Optional parameters (more for historical reasons)
  895. // Added if present...
  896. //
  897. $aParameters['validation_pattern'] = $this->GetPropString($oField, 'validation_pattern');
  898. $aParameters['width'] = $this->GetPropNumber($oField, 'width');
  899. $aParameters['height'] = $this->GetPropNumber($oField, 'height');
  900. $aParameters['digits'] = $this->GetPropNumber($oField, 'digits');
  901. $aParameters['decimals'] = $this->GetPropNumber($oField, 'decimals');
  902. $aParameters['always_load_in_tables'] = $this->GetPropBoolean($oField, 'always_load_in_tables', false);
  903. $sTrackingLevel = $oField->GetChildText('tracking_level');
  904. if (!is_null($sTrackingLevel))
  905. {
  906. $aParameters['tracking_level'] = $this->TrackingLevelToPHP($sAttType, $sTrackingLevel);
  907. }
  908. $aParams = array();
  909. foreach($aParameters as $sKey => $sValue)
  910. {
  911. if (!is_null($sValue))
  912. {
  913. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  914. }
  915. }
  916. $sParams = implode(', ', $aParams);
  917. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  918. }
  919. catch(Exception $e)
  920. {
  921. throw new DOMFormatException("Field: '$sAttCode', (type: $sAttType), ".$e->getMessage());
  922. }
  923. }
  924. // Lifecycle
  925. //
  926. $sLifecycle = '';
  927. $sHighlightScale = '';
  928. if ($oLifecycle)
  929. {
  930. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  931. $sLifecycle .= "\t\t//\n";
  932. $oStimuli = $oLifecycle->GetUniqueElement('stimuli');
  933. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  934. {
  935. $sStimulus = $oStimulus->getAttribute('id');
  936. $sStimulusClass = $oStimulus->getAttribute('xsi:type');
  937. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  938. }
  939. $oHighlightScale = $oLifecycle->GetUniqueElement('highlight_scale', false);
  940. if ($oHighlightScale)
  941. {
  942. $sHighlightScale = "\t\t// Higlight Scale\n";
  943. $sHighlightScale .= " MetaModel::Init_DefineHighlightScale( array(\n";
  944. $this->CompileFiles($oHighlightScale, $sTempTargetDir.'/'.$sModuleRelativeDir, $sFinalTargetDir.'/'.$sModuleRelativeDir, '');
  945. foreach ($oHighlightScale->getElementsByTagName('item') as $oItem)
  946. {
  947. $sItemCode = $oItem->getAttribute('id');
  948. $fRank = (float)$oItem->GetChildText('rank');
  949. $sColor = $oItem->GetChildText('color');
  950. if (($sIcon = $oItem->GetChildText('icon')) && (strlen($sIcon) > 0))
  951. {
  952. $sIcon = $sModuleRelativeDir.'/'.$sIcon;
  953. $sIcon = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  954. }
  955. switch($sColor)
  956. {
  957. // Known PHP constants: keep the literal value as-is
  958. case 'HILIGHT_CLASS_CRITICAL':
  959. case 'HIGHLIGHT_CLASS_CRITICAL':
  960. $sColor = 'HILIGHT_CLASS_CRITICAL';
  961. break;
  962. case 'HILIGHT_CLASS_OK':
  963. case 'HIGHLIGHT_CLASS_OK':
  964. $sColor = 'HILIGHT_CLASS_OK';
  965. break;
  966. case 'HIGHLIGHT_CLASS_WARNING':
  967. case 'HILIGHT_CLASS_WARNING':
  968. $sColor = 'HILIGHT_CLASS_WARNING';
  969. break;
  970. case 'HIGHLIGHT_CLASS_NONE':
  971. case 'HILIGHT_CLASS_NONE':
  972. $sColor = 'HILIGHT_CLASS_NONE';
  973. break;
  974. default:
  975. // Future extension, specify your own color??
  976. $sColor = "'".addslashes($sColor)."'";
  977. }
  978. $sHighlightScale .= " '$sItemCode' => array('rank' => $fRank, 'color' => $sColor, 'icon' => $sIcon),\n";
  979. }
  980. $sHighlightScale .= " ));\n";
  981. }
  982. $oStates = $oLifecycle->GetUniqueElement('states');
  983. foreach ($oStates->getElementsByTagName('state') as $oState)
  984. {
  985. $sState = $oState->getAttribute('id');
  986. $oInitialStatePath = $oState->GetOptionalElement('initial_state_path');
  987. if ($oInitialStatePath)
  988. {
  989. $aInitialStatePath = array();
  990. foreach ($oInitialStatePath->getElementsByTagName('state_ref') as $oIntermediateState)
  991. {
  992. $aInitialStatePath[] = "'".$oIntermediateState->GetText()."'";
  993. }
  994. $sInitialStatePath = 'Array('.implode(', ', $aInitialStatePath).')';
  995. }
  996. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  997. $sLifecycle .= " \"".$sState."\",\n";
  998. $sLifecycle .= " array(\n";
  999. $sAttributeInherit = $oState->GetChildText('inherit_flags_from', '');
  1000. $sLifecycle .= " \"attribute_inherit\" => '$sAttributeInherit',\n";
  1001. $oHighlight = $oState->GetUniqueElement('highlight', false);
  1002. if ($oHighlight)
  1003. {
  1004. $sCode = $oHighlight->GetChildText('code', '');
  1005. if ($sCode != '')
  1006. {
  1007. $sLifecycle .= " 'highlight' => array('code' => '$sCode'),\n";
  1008. }
  1009. }
  1010. $sLifecycle .= " \"attribute_list\" => array(\n";
  1011. $oFlags = $oState->GetUniqueElement('flags');
  1012. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  1013. {
  1014. $sFlags = $this->FlagsToPHP($oAttributeNode);
  1015. if (strlen($sFlags) > 0)
  1016. {
  1017. $sAttCode = $oAttributeNode->GetAttribute('id');
  1018. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  1019. }
  1020. }
  1021. $sLifecycle .= " ),\n";
  1022. if (!is_null($oInitialStatePath))
  1023. {
  1024. $sLifecycle .= " \"initial_state_path\" => $sInitialStatePath,\n";
  1025. }
  1026. $sLifecycle .= " )\n";
  1027. $sLifecycle .= " );\n";
  1028. $oTransitions = $oState->GetUniqueElement('transitions');
  1029. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  1030. {
  1031. $sStimulus = $oTransition->GetChildText('stimulus');
  1032. $sTargetState = $oTransition->GetChildText('target');
  1033. $oActions = $oTransition->GetUniqueElement('actions');
  1034. $aVerbs = array();
  1035. foreach ($oActions->getElementsByTagName('action') as $oAction)
  1036. {
  1037. $sVerb = $oAction->GetChildText('verb');
  1038. $oParams = $oAction->GetOptionalElement('params');
  1039. $aActionParams = array();
  1040. if ($oParams)
  1041. {
  1042. $oParamNodes = $oParams->getElementsByTagName('param');
  1043. foreach($oParamNodes as $oParam)
  1044. {
  1045. $sParamType = $oParam->getAttribute('xsi:type');
  1046. if ($sParamType == '')
  1047. {
  1048. $sParamType = 'string';
  1049. }
  1050. $aActionParams[] = "array('type' => '$sType', 'value' => '".self::QuoteForPHP($oParam->textContent)."')";
  1051. }
  1052. }
  1053. else
  1054. {
  1055. // Old (pre 2.0.4) format, when no parameter is specified, assume 1 parameter: reference sStimulusCode
  1056. $aActionParams[] = "array('type' => 'reference', 'value' => 'sStimulusCode')";
  1057. }
  1058. $sActionParams = 'array('.implode(', ', $aActionParams).')';
  1059. $aVerbs[] = "array('verb' => '$sVerb', 'params' => $sActionParams)";
  1060. }
  1061. $sActions = implode(', ', $aVerbs);
  1062. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  1063. }
  1064. }
  1065. }
  1066. // ZLists
  1067. //
  1068. $aListRef = array(
  1069. 'details' => 'details',
  1070. 'standard_search' => 'search',
  1071. 'list' => 'list'
  1072. );
  1073. $oPresentation = $oClass->GetUniqueElement('presentation');
  1074. $sZlists = '';
  1075. foreach ($aListRef as $sListCode => $sListTag)
  1076. {
  1077. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  1078. if ($oListNode)
  1079. {
  1080. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  1081. $this->ArrayOfItemsToZList($aAttributes);
  1082. $sZAttributes = var_export($aAttributes, true);
  1083. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  1084. }
  1085. }
  1086. // Methods
  1087. $sMethods = "";
  1088. $oMethods = $oClass->GetUniqueElement('methods');
  1089. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  1090. {
  1091. $sMethodCode = $oMethod->GetChildText('code');
  1092. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  1093. {
  1094. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  1095. }
  1096. else
  1097. {
  1098. $sMethods .= "\n\n".$sMethodCode."\n";
  1099. }
  1100. }
  1101. // Let's make the whole class declaration
  1102. //
  1103. $sPHP = "\n\n$sCodeComment\n";
  1104. $sParentClass = $oClass->GetChildText('php_parent');
  1105. $oPhpParent = $oClass->GetUniqueElement('php_parent', false);
  1106. if ($oPhpParent)
  1107. {
  1108. $sParentClass = $oPhpParent->GetChildText('name', '');
  1109. if ($sParentClass == '')
  1110. {
  1111. throw new Exception("Failed to process class '".$oClass->getAttribute('id')."', missing required tag 'name' under 'php_parent'.");
  1112. }
  1113. $sIncludeFile = $oPhpParent->GetChildText('file', '');
  1114. if ($sIncludeFile != '')
  1115. {
  1116. $sPHP .= "\nrequire_once('$sIncludeFile'); // Implementation of the class $sParentClass\n";
  1117. }
  1118. //TODO fix this !!!
  1119. // $sFullPath = $this->sSourceDir.'/'.$sModuleRelativeDir.'/'.$sIncludeFile;
  1120. // if (!file_exists($sFullPath))
  1121. // {
  1122. // throw new Exception("Failed to process class '".$oClass->getAttribute('id')."', from '$sModuleRelativeDir'. The required include file: '$sFullPath' does not exist.");
  1123. // }
  1124. }
  1125. else
  1126. {
  1127. $sParentClass = $oClass->GetChildText('parent', 'DBObject');
  1128. }
  1129. if ($oProperties->GetChildText('abstract') == 'true')
  1130. {
  1131. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  1132. }
  1133. else
  1134. {
  1135. $sPHP .= 'class '.$oClass->getAttribute('id');
  1136. }
  1137. $sPHP .= " extends $sParentClass\n";
  1138. $sPHP .=
  1139. <<<EOF
  1140. {
  1141. public static function Init()
  1142. {
  1143. \$aParams = array
  1144. (
  1145. $sClassParams
  1146. );
  1147. MetaModel::Init_Params(\$aParams);
  1148. MetaModel::Init_InheritAttributes();
  1149. $sAttributes
  1150. $sLifecycle
  1151. $sHighlightScale
  1152. $sZlists
  1153. }
  1154. $sMethods
  1155. }
  1156. EOF;
  1157. return $sPHP;
  1158. }// function CompileClass()
  1159. protected function CompileMenu($oMenu, $sTempTargetDir, $sFinalTargetDir, $sModuleRelativeDir, $oP)
  1160. {
  1161. $this->CompileFiles($oMenu, $sTempTargetDir.'/'.$sModuleRelativeDir, $sFinalTargetDir.'/'.$sModuleRelativeDir, $sModuleRelativeDir);
  1162. $sMenuId = $oMenu->getAttribute("id");
  1163. $sMenuClass = $oMenu->getAttribute("xsi:type");
  1164. $sParent = $oMenu->GetChildText('parent', null);
  1165. if ($sParent)
  1166. {
  1167. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  1168. }
  1169. else
  1170. {
  1171. $sParentSpec = '-1';
  1172. }
  1173. $fRank = (float) $oMenu->GetChildText('rank');
  1174. switch($sMenuClass)
  1175. {
  1176. case 'WebPageMenuNode':
  1177. $sUrl = $oMenu->GetChildText('url');
  1178. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  1179. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  1180. break;
  1181. case 'DashboardMenuNode':
  1182. $sTemplateFile = $oMenu->GetChildText('definition_file', '');
  1183. if ($sTemplateFile != '')
  1184. {
  1185. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  1186. }
  1187. else
  1188. {
  1189. $oDashboardDefinition = $oMenu->GetOptionalElement('definition');
  1190. if ($oDashboardDefinition == null)
  1191. {
  1192. throw(new DOMFormatException('Missing definition for Dashboard menu "'.$sMenuId.'" expecting either a tag "definition_file" or "definition".'));
  1193. }
  1194. $sFileName = strtolower(str_replace(array(':', '/', '\\', '*'), '_', $sMenuId)).'_dashboard_menu.xml';
  1195. $sTemplateSpec = $this->PathToPHP($sFileName, $sModuleRelativeDir);
  1196. $oXMLDoc = new DOMDocument('1.0', 'UTF-8');
  1197. $oXMLDoc->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  1198. $oXMLDoc->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  1199. $oRootNode = $oXMLDoc->createElement('dashboard'); // make sure that the document is not empty
  1200. $oRootNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  1201. $oXMLDoc->appendChild($oRootNode);
  1202. foreach($oDashboardDefinition->childNodes as $oNode)
  1203. {
  1204. $oDefNode = $oXMLDoc->importNode($oNode, true); // layout, cells, etc Nodes and below
  1205. $oRootNode->appendChild($oDefNode);
  1206. }
  1207. $oXMLDoc->save($sTempTargetDir.'/'.$sModuleRelativeDir.'/'.$sFileName);
  1208. }
  1209. $sNewMenu = "new DashboardMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  1210. break;
  1211. case 'ShortcutContainerMenuNode':
  1212. $sNewMenu = "new ShortcutContainerMenuNode('$sMenuId', $sParentSpec, $fRank);";
  1213. break;
  1214. case 'OQLMenuNode':
  1215. $sOQL = self::QuoteForPHP($oMenu->GetChildText('oql'));
  1216. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  1217. $sNewMenu = "new OQLMenuNode('$sMenuId', $sOQL, $sParentSpec, $fRank, $bSearch);";
  1218. break;
  1219. case 'NewObjectMenuNode':
  1220. $sClass = $oMenu->GetChildText('class');
  1221. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  1222. break;
  1223. case 'SearchMenuNode':
  1224. $sClass = $oMenu->GetChildText('class');
  1225. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  1226. break;
  1227. case 'TemplateMenuNode':
  1228. $sTemplateFile = $oMenu->GetChildText('template_file');
  1229. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  1230. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  1231. {
  1232. $sEnableAction = $oMenu->GetChildText('enable_action', 'null');
  1233. $sEnablePermission = $oMenu->GetChildText('enable_permission', 'UR_ALLOWED_YES');
  1234. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  1235. if ($sEnableStimulus != null)
  1236. {
  1237. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  1238. }
  1239. else
  1240. {
  1241. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  1242. }
  1243. }
  1244. else
  1245. {
  1246. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  1247. }
  1248. break;
  1249. case 'MenuGroup':
  1250. default:
  1251. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  1252. {
  1253. $sEnableAction = $oMenu->GetChildText('enable_action', 'null');
  1254. $sEnablePermission = $oMenu->GetChildText('enable_permission', 'UR_ALLOWED_YES');
  1255. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  1256. if ($sEnableStimulus != null)
  1257. {
  1258. $sNewMenu = "new $sMenuClass('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  1259. }
  1260. else
  1261. {
  1262. $sNewMenu = "new $sMenuClass('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  1263. }
  1264. }
  1265. else
  1266. {
  1267. $sNewMenu = "new $sMenuClass('$sMenuId', $fRank);";
  1268. }
  1269. }
  1270. $sIndent = '';
  1271. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  1272. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  1273. {
  1274. $sAutoReload = self::QuoteForPHP($sAutoReload);
  1275. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
  1276. }
  1277. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  1278. if ($sAdminOnly && ($sAdminOnly == '1'))
  1279. {
  1280. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  1281. $sPHP .= $sIndent."{\n";
  1282. foreach($aPHPMenu as $sPHPLine)
  1283. {
  1284. $sPHP .= $sIndent." $sPHPLine\n";
  1285. }
  1286. $sPHP .= $sIndent."}\n";
  1287. }
  1288. else
  1289. {
  1290. $sPHP = '';
  1291. foreach($aPHPMenu as $sPHPLine)
  1292. {
  1293. $sPHP .= $sIndent.$sPHPLine."\n";
  1294. }
  1295. }
  1296. return $sPHP;
  1297. } // function CompileMenu
  1298. /**
  1299. * Helper to compute the grant, taking any existing grant into account
  1300. */
  1301. protected function CumulateGrant(&$aGrants, $sKey, $bGrant)
  1302. {
  1303. if (isset($aGrants[$sKey]))
  1304. {
  1305. if (!$bGrant)
  1306. {
  1307. $aGrants[$sKey] = false;
  1308. }
  1309. }
  1310. else
  1311. {
  1312. $aGrants[$sKey] = $bGrant;
  1313. }
  1314. }
  1315. protected function CompileUserRights($oUserRightsNode)
  1316. {
  1317. static $aActionsInShort = array(
  1318. 'read' => 'r',
  1319. 'bulk read' => 'br',
  1320. 'write' => 'w',
  1321. 'bulk write' => 'bw',
  1322. 'delete' => 'd',
  1323. 'bulk delete' => 'bd',
  1324. );
  1325. // Preliminary : create an index so that links will be taken into account implicitely
  1326. $aLinkToClasses = array();
  1327. $oClasses = $this->oFactory->ListAllClasses();
  1328. foreach($oClasses as $oClass)
  1329. {
  1330. $bIsLink = false;
  1331. $oProperties = $oClass->GetOptionalElement('properties');
  1332. if ($oProperties)
  1333. {
  1334. $bIsLink = (bool) $this->GetPropNumber($oProperties, 'is_link', 0);
  1335. }
  1336. if ($bIsLink)
  1337. {
  1338. foreach($this->oFactory->ListFields($oClass) as $oField)
  1339. {
  1340. $sAttType = $oField->getAttribute('xsi:type');
  1341. if (($sAttType == 'AttributeExternalKey') || ($sAttType == 'AttributeHierarchicalKey'))
  1342. {
  1343. $sOnTargetDel = $oField->GetChildText('on_target_delete');
  1344. if ($sOnTargetDel == 'DEL_AUTO')
  1345. {
  1346. $sTargetClass = $oField->GetChildText('target_class');
  1347. $aLinkToClasses[$oClass->getAttribute('id')][] = $sTargetClass;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. // Groups
  1354. //
  1355. $aGroupClasses = array();
  1356. $oGroups = $oUserRightsNode->GetUniqueElement('groups');
  1357. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  1358. {
  1359. $sGroupId = $oGroup->getAttribute("id");
  1360. $aClasses = array();
  1361. $oClasses = $oGroup->GetUniqueElement('classes');
  1362. foreach($oClasses->getElementsByTagName('class') as $oClass)
  1363. {
  1364. $sClass = $oClass->getAttribute("id");
  1365. $aClasses[] = $sClass;
  1366. //$bSubclasses = $this->GetPropBoolean($oClass, 'subclasses', true);
  1367. //if ($bSubclasses)...
  1368. }
  1369. $aGroupClasses[$sGroupId] = $aClasses;
  1370. }
  1371. // Profiles and grants
  1372. //
  1373. $aProfiles = array();
  1374. // Hardcode the administrator profile
  1375. $aProfiles[1] = array(
  1376. 'name' => 'Administrator',
  1377. 'description' => 'Has the rights on everything (bypassing any control)'
  1378. );
  1379. $aGrants = array();
  1380. $oProfiles = $oUserRightsNode->GetUniqueElement('profiles');
  1381. foreach($oProfiles->getElementsByTagName('profile') as $oProfile)
  1382. {
  1383. $iProfile = $oProfile->getAttribute("id");
  1384. $sName = $oProfile->GetChildText('name');
  1385. $sDescription = $oProfile->GetChildText('description');
  1386. $oGroups = $oProfile->GetUniqueElement('groups');
  1387. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  1388. {
  1389. $sGroupId = $oGroup->getAttribute("id");
  1390. $aActions = array();
  1391. $oActions = $oGroup->GetUniqueElement('actions');
  1392. foreach($oActions->getElementsByTagName('action') as $oAction)
  1393. {
  1394. $sAction = $oAction->getAttribute("id");
  1395. $sType = $oAction->getAttribute("xsi:type");
  1396. $sGrant = $oAction->GetText();
  1397. $bGrant = ($sGrant == 'allow');
  1398. if ($sGroupId == '*')
  1399. {
  1400. $aGrantClasses = array('*');
  1401. }
  1402. else
  1403. {
  1404. $aGrantClasses = $aGroupClasses[$sGroupId];
  1405. }
  1406. foreach ($aGrantClasses as $sClass)
  1407. {
  1408. if ($sType == 'stimulus')
  1409. {
  1410. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'_s_'.$sAction, $bGrant);
  1411. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'+_s_'.$sAction, $bGrant); // subclasses inherit this grant
  1412. }
  1413. else
  1414. {
  1415. $sAction = $aActionsInShort[$sType];
  1416. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'_'.$sAction, $bGrant);
  1417. $this->CumulateGrant($aGrants, $iProfile.'_'.$sClass.'+_'.$sAction, $bGrant); // subclasses inherit this grant
  1418. }
  1419. }
  1420. }
  1421. }
  1422. $aProfiles[$iProfile] = array(
  1423. 'name' => $sName,
  1424. 'description' => $sDescription
  1425. );
  1426. }
  1427. $sProfiles = var_export($aProfiles, true);
  1428. $sGrants = var_export($aGrants, true);
  1429. $sLinkToClasses = var_export($aLinkToClasses, true);
  1430. $sPHP =
  1431. <<<EOF
  1432. //
  1433. // List of constant profiles
  1434. // - used by the class URP_Profiles at setup (create/update/delete records)
  1435. // - used by the addon UserRightsProfile to determine user rights
  1436. //
  1437. class ProfilesConfig
  1438. {
  1439. protected static \$aPROFILES = $sProfiles;
  1440. protected static \$aGRANTS = $sGrants;
  1441. protected static \$aLINKTOCLASSES = $sLinkToClasses;
  1442. // Now replaced by MetaModel::GetLinkClasses (working with 1.x)
  1443. // This function could be deprecated
  1444. public static function GetLinkClasses()
  1445. {
  1446. return self::\$aLINKTOCLASSES;
  1447. }
  1448. public static function GetProfileActionGrant(\$iProfileId, \$sClass, \$sAction)
  1449. {
  1450. // Search for a grant, starting from the most explicit declaration,
  1451. // then searching for less and less explicit declaration
  1452. // 1 - The class itself
  1453. //
  1454. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_'.\$sAction;
  1455. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1456. {
  1457. return self::\$aGRANTS[\$sGrantKey];
  1458. }
  1459. // 2 - The parent classes, up to the root class
  1460. //
  1461. foreach (MetaModel::EnumParentClasses(\$sClass, ENUM_PARENT_CLASSES_EXCLUDELEAF, false /*bRootFirst*/) as \$sParent)
  1462. {
  1463. \$sGrantKey = \$iProfileId.'_'.\$sParent.'+_'.\$sAction;
  1464. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1465. {
  1466. return self::\$aGRANTS[\$sGrantKey];
  1467. }
  1468. }
  1469. // 3 - The related classes (if the current is an N-N link with AUTO_DEL)
  1470. //
  1471. if (array_key_exists(\$sClass, self::\$aLINKTOCLASSES))
  1472. {
  1473. // Get the grant for the remote classes. The resulting grant is:
  1474. // - One YES => YES
  1475. // - 100% undefined => undefined
  1476. // - otherwise => NO
  1477. //
  1478. // Having write allowed on the remote class implies write + delete on the N-N link class
  1479. if (\$sAction == 'd')
  1480. {
  1481. \$sRemoteAction = 'w';
  1482. }
  1483. elseif (\$sAction == 'bd')
  1484. {
  1485. \$sRemoteAction = 'bw';
  1486. }
  1487. else
  1488. {
  1489. \$sRemoteAction = \$sAction;
  1490. }
  1491. foreach (self::\$aLINKTOCLASSES[\$sClass] as \$sRemoteClass)
  1492. {
  1493. \$bUndefined = true;
  1494. \$bGrant = self::GetProfileActionGrant(\$iProfileId, \$sRemoteClass, \$sAction);
  1495. if (\$bGrant === true)
  1496. {
  1497. return true;
  1498. }
  1499. if (\$bGrant === false)
  1500. {
  1501. \$bUndefined = false;
  1502. }
  1503. }
  1504. if (!\$bUndefined)
  1505. {
  1506. return false;
  1507. }
  1508. }
  1509. // 4 - All
  1510. //
  1511. \$sGrantKey = \$iProfileId.'_*_'.\$sAction;
  1512. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1513. {
  1514. return self::\$aGRANTS[\$sGrantKey];
  1515. }
  1516. // Still undefined for this class
  1517. return null;
  1518. }
  1519. public static function GetProfileStimulusGrant(\$iProfileId, \$sClass, \$sStimulus)
  1520. {
  1521. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_s_'.\$sStimulus;
  1522. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1523. {
  1524. return self::\$aGRANTS[\$sGrantKey];
  1525. }
  1526. \$sGrantKey = \$iProfileId.'_*_s_'.\$sStimulus;
  1527. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1528. {
  1529. return self::\$aGRANTS[\$sGrantKey];
  1530. }
  1531. return null;
  1532. }
  1533. // returns an array of id => array of column => php value(so-called "real value")
  1534. public static function GetProfilesValues()
  1535. {
  1536. return self::\$aPROFILES;
  1537. }
  1538. }
  1539. EOF;
  1540. return $sPHP;
  1541. } // function CompileUserRights
  1542. protected function CompileDictionary($oDictionaryNode, $sTempTargetDir, $sFinalTargetDir)
  1543. {
  1544. $sLang = $oDictionaryNode->getAttribute('id');
  1545. $sEnglishLanguageDesc = $oDictionaryNode->GetChildText('english_description');
  1546. $sLocalizedLanguageDesc = $oDictionaryNode->GetChildText('localized_description');
  1547. $aEntriesPHP = array();
  1548. $oEntries = $oDictionaryNode->GetUniqueElement('entries');
  1549. foreach($oEntries->getElementsByTagName('entry') as $oEntry)
  1550. {
  1551. $sStringCode = $oEntry->getAttribute('id');
  1552. $sValue = $oEntry->GetText();
  1553. $aEntriesPHP[] = "\t'$sStringCode' => ".self::QuoteForPHP($sValue, true).",";
  1554. }
  1555. $sEntriesPHP = implode("\n", $aEntriesPHP);
  1556. $sEscEnglishLanguageDesc = self::QuoteForPHP($sEnglishLanguageDesc);
  1557. $sEscLocalizedLanguageDesc = self::QuoteForPHP($sLocalizedLanguageDesc);
  1558. $sPHPDict =
  1559. <<<EOF
  1560. <?php
  1561. //
  1562. // Dictionary built by the compiler for the language "$sLang"
  1563. //
  1564. Dict::Add('$sLang', $sEscEnglishLanguageDesc, $sEscLocalizedLanguageDesc, array(
  1565. $sEntriesPHP
  1566. ));
  1567. EOF;
  1568. $sSafeLang = str_replace(' ', '-', strtolower(trim($sLang)));
  1569. $sDictFile = $sTempTargetDir.'/dictionaries/'.$sSafeLang.'.dict.php';
  1570. file_put_contents($sDictFile, $sPHPDict);
  1571. }
  1572. // Transform the file references into the corresponding filename (and create the file in the relevant directory)
  1573. //
  1574. protected function CompileFiles($oNode, $sTempTargetDir, $sFinalTargetDir, $sRelativePath)
  1575. {
  1576. $oFileRefs = $oNode->GetNodes(".//fileref");
  1577. foreach ($oFileRefs as $oFileRef)
  1578. {
  1579. $sFileId = $oFileRef->getAttribute('ref');
  1580. if ($sFileId !== '')
  1581. {
  1582. $oNodes = $this->oFactory->GetNodes("/itop_design/files/file[@id='$sFileId']");
  1583. if ($oNodes->length == 0)
  1584. {
  1585. throw new DOMFormatException('Could not find the file with ref '.$sFileId);
  1586. }
  1587. $sName = $oNodes->item(0)->GetChildText('name');
  1588. $sData = base64_decode($oNodes->item(0)->GetChildText('data'));
  1589. $aPathInfo = pathinfo($sName);
  1590. $sFile = $sFileId.'.'.$aPathInfo['extension'];
  1591. $sFilePath = $sTempTargetDir.'/images/'.$sFile;
  1592. @mkdir($sTempTargetDir.'/images');
  1593. file_put_contents($sFilePath, $sData);
  1594. if (!file_exists($sFilePath))
  1595. {
  1596. throw new Exception('Could not write icon file '.$sFilePath);
  1597. }
  1598. $oParentNode = $oFileRef->parentNode;
  1599. $oParentNode->removeChild($oFileRef);
  1600. $oTextNode = $oParentNode->ownerDocument->createTextNode($sRelativePath.'/images/'.$sFile);
  1601. $oParentNode->appendChild($oTextNode);
  1602. }
  1603. }
  1604. }
  1605. protected function CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, $sNodeName, $sTargetFile)
  1606. {
  1607. if (($sIcon = $oBrandingNode->GetChildText($sNodeName)) && (strlen($sIcon) > 0))
  1608. {
  1609. if (substr($sIcon, 0, 8) == 'branding')
  1610. {
  1611. $sSourceFile = $sTempTargetDir.'/'.$sIcon;
  1612. }
  1613. else
  1614. {
  1615. $sSourceFile = APPROOT.$sIcon;
  1616. }
  1617. $sTargetFile = $sTempTargetDir.'/branding/'.$sTargetFile.'.png';
  1618. if (!file_exists($sSourceFile))
  1619. {
  1620. throw new Exception("Branding $sNodeName: could not find the file $sIcon ($sSourceFile)");
  1621. }
  1622. // 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)
  1623. copy($sSourceFile, $sTargetFile);
  1624. }
  1625. }
  1626. protected function CompileBranding($oBrandingNode, $sTempTargetDir, $sFinalTargetDir)
  1627. {
  1628. if ($oBrandingNode)
  1629. {
  1630. // Transform file refs into files in the images folder
  1631. SetupUtils::builddir($sTempTargetDir.'/branding');
  1632. $this->CompileFiles($oBrandingNode, $sTempTargetDir.'/branding', $sFinalTargetDir.'/branding', 'branding');
  1633. $this->CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, 'main_logo', 'main-logo');
  1634. $this->CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, 'login_logo', 'login-logo');
  1635. $this->CompileLogo($oBrandingNode, $sTempTargetDir, $sFinalTargetDir, 'portal_logo', 'portal-logo');
  1636. // Cleanup the images directory (made by CompileFiles)
  1637. SetupUtils::rrmdir($sTempTargetDir.'/branding/images');
  1638. }
  1639. }
  1640. }
  1641. ?>