compiler.class.inc.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. <?php
  2. // Copyright (C) 2011-2012 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 Compile($sTargetDir, $oP = null, $bUseSymbolicLinks = false)
  47. {
  48. $aAllClasses = array(); // flat list of classes
  49. // Determine the target modules for the MENUS
  50. //
  51. $aMenuNodes = array();
  52. $aMenusByModule = array();
  53. foreach ($this->oFactory->ListActiveChildNodes('menus', 'menu') as $oMenuNode)
  54. {
  55. $sMenuId = $oMenuNode->getAttribute('id');
  56. $aMenuNodes[$sMenuId] = $oMenuNode;
  57. $sModuleMenu = $oMenuNode->getAttribute('_created_in');
  58. $aMenusByModule[$sModuleMenu][] = $sMenuId;
  59. }
  60. // Determine the target module (exactly one!) for USER RIGHTS
  61. //
  62. $sUserRightsModule = '';
  63. $oUserRightsNode = $this->oFactory->GetNodes('user_rights')->item(0);
  64. if ($oUserRightsNode)
  65. {
  66. $sUserRightsModule = $oUserRightsNode->getAttribute('_created_in');
  67. }
  68. // List root classes
  69. //
  70. $this->aRootClasses = array();
  71. foreach ($this->oFactory->ListRootClasses() as $oClass)
  72. {
  73. $this->Log("Root class: ".$oClass->getAttribute('id'));
  74. $this->aRootClasses[$oClass->getAttribute('id')] = $oClass;
  75. }
  76. // Compile, module by module
  77. //
  78. $aModules = $this->oFactory->GetLoadedModules();
  79. foreach($aModules as $foo => $oModule)
  80. {
  81. $sModuleName = $oModule->GetName();
  82. $sModuleVersion = $oModule->GetVersion();
  83. $sModuleRootDir = realpath($oModule->GetRootDir());
  84. $sRelativeDir = basename($sModuleRootDir);
  85. // Push the other module files
  86. SetupUtils::copydir($sModuleRootDir, $sTargetDir.'/'.$sRelativeDir, $bUseSymbolicLinks);
  87. $sCompiledCode = '';
  88. $oClasses = $this->oFactory->ListClasses($sModuleName);
  89. $iClassCount = $oClasses->length;
  90. if ($iClassCount == 0)
  91. {
  92. $this->Log("Found module without classes declared: $sModuleName");
  93. }
  94. else
  95. {
  96. foreach($oClasses as $oClass)
  97. {
  98. $sClass = $oClass->getAttribute("id");
  99. $aAllClasses[] = $sClass;
  100. try
  101. {
  102. $sCompiledCode .= $this->CompileClass($oClass, $sRelativeDir, $oP);
  103. }
  104. catch (DOMFormatException $e)
  105. {
  106. throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
  107. }
  108. }
  109. }
  110. if (!array_key_exists($sModuleName, $aMenusByModule))
  111. {
  112. $this->Log("Found module without menus declared: $sModuleName");
  113. }
  114. else
  115. {
  116. $sCompiledCode .=
  117. <<<EOF
  118. //
  119. // Menus
  120. //
  121. EOF;
  122. // Preliminary: determine parent menus not defined within the current module
  123. $aMenusToLoad = array();
  124. $aParentMenus = array();
  125. foreach($aMenusByModule[$sModuleName] as $sMenuId)
  126. {
  127. $oMenuNode = $aMenuNodes[$sMenuId];
  128. if ($sParent = $oMenuNode->GetChildText('parent', null))
  129. {
  130. $aMenusToLoad[] = $sParent;
  131. $aParentMenus[] = $sParent;
  132. }
  133. // Note: the order matters: the parents must be defined BEFORE
  134. $aMenusToLoad[] = $sMenuId;
  135. }
  136. $aMenusToLoad = array_unique($aMenusToLoad);
  137. foreach($aMenusToLoad as $sMenuId)
  138. {
  139. $oMenuNode = $aMenuNodes[$sMenuId];
  140. if ($oMenuNode->getAttribute("xsi:type") == 'MenuGroup')
  141. {
  142. // Note: this algorithm is wrong
  143. // 1 - the module may appear empty in the current module, while children are defined in other modules
  144. // 2 - check recursively that child nodes are not empty themselves
  145. // Future algorithm:
  146. // a- browse the modules and build the menu tree
  147. // b- browse the tree and blacklist empty menus
  148. // c- before compiling, discard if blacklisted
  149. if (!in_array($oMenuNode->getAttribute("id"), $aParentMenus))
  150. {
  151. // Discard empty menu groups
  152. continue;
  153. }
  154. }
  155. try
  156. {
  157. $sCompiledCode .= $this->CompileMenu($oMenuNode, $sTargetDir, $sRelativeDir, $oP);
  158. }
  159. catch (ssDOMFormatException $e)
  160. {
  161. throw new Exception("Failed to process menu '$sMenuId', from '$sModuleRootDir': ".$e->getMessage());
  162. }
  163. }
  164. }
  165. // User rights
  166. //
  167. if ($sModuleName == $sUserRightsModule)
  168. {
  169. $sCompiledCode .= $this->CompileUserRights($oUserRightsNode);
  170. }
  171. // Create (overwrite if existing) the compiled file
  172. //
  173. if (strlen($sCompiledCode) > 0)
  174. {
  175. // We have compiled something: write the result file
  176. //
  177. $sResultFile = $sTargetDir.'/'.$sRelativeDir.'/model.'.$sModuleName.'.php';
  178. if (is_file($sResultFile))
  179. {
  180. $this->Log("Updating $sResultFile for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  181. }
  182. else
  183. {
  184. $sResultDir = dirname($sResultFile);
  185. if (!is_dir($sResultDir))
  186. {
  187. $this->Log("Creating directory $sResultDir");
  188. mkdir($sResultDir, 0777, true);
  189. }
  190. $this->Log("Creating $sResultFile for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  191. }
  192. // Compile the module into a single file
  193. //
  194. $sId = $sModuleName;
  195. $sCurrDate = date(DATE_ISO8601);
  196. $sAuthor = 'iTop compiler';
  197. $sLicence = 'http://opensource.org/licenses/AGPL-3.0';
  198. $sFileHeader =
  199. <<<EOF
  200. <?php
  201. //
  202. // File generated by ... on the $sCurrDate
  203. // Please do not edit manually
  204. //
  205. /**
  206. * Classes and menus for $sModuleName (version $sModuleVersion)
  207. *
  208. * @author $sAuthor
  209. * @license $sLicence
  210. */
  211. EOF;
  212. file_put_contents($sResultFile, $sFileHeader.$sCompiledCode);
  213. }
  214. }
  215. }
  216. /**
  217. * Helper to form a valid ZList from the array built by GetNodeAsArrayOfItems()
  218. */
  219. protected function ArrayOfItemsToZList(&$aItems)
  220. {
  221. $aTransformed = array();
  222. foreach ($aItems as $key => $value)
  223. {
  224. if (is_null($value))
  225. {
  226. $aTransformed[] = $key;
  227. }
  228. else
  229. {
  230. if (is_array($value))
  231. {
  232. $this->ArrayOfItemsToZList($value);
  233. }
  234. $aTransformed[$key] = $value;
  235. }
  236. }
  237. $aItems = $aTransformed;
  238. }
  239. /**
  240. * Helper to format the flags for an attribute, in a given state
  241. * @param object $oAttNode DOM node containing the information to build the flags
  242. * Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
  243. */
  244. protected function FlagsToPHP($oAttNode)
  245. {
  246. static $aNodeAttributeToFlag = array(
  247. 'mandatory' => 'OPT_ATT_MANDATORY',
  248. 'read_only' => 'OPT_ATT_READONLY',
  249. 'must_prompt' => 'OPT_ATT_MUSTPROMPT',
  250. 'must_change' => 'OPT_ATT_MUSTCHANGE',
  251. 'hidden' => 'OPT_ATT_HIDDEN',
  252. );
  253. $aFlags = array();
  254. foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
  255. {
  256. $bFlag = ($oAttNode->GetOptionalElement($sNodeAttribute) != null);
  257. if ($bFlag)
  258. {
  259. $aFlags[] = $sFlag;
  260. }
  261. }
  262. $sRes = implode(' | ', $aFlags);
  263. return $sRes;
  264. }
  265. /**
  266. * Helper to format the tracking level for linkset (direct or indirect attributes)
  267. * @param string $sTrackingLevel Value set from within the XML
  268. * Returns string PHP flag
  269. */
  270. protected function TrackingLevelToPHP($sTrackingLevel)
  271. {
  272. static $aXmlToPHP = array(
  273. 'none' => 'LINKSET_TRACKING_NONE',
  274. 'list' => 'LINKSET_TRACKING_LIST',
  275. 'details' => 'LINKSET_TRACKING_DETAILS',
  276. 'all' => 'LINKSET_TRACKING_ALL',
  277. );
  278. if (!array_key_exists($sTrackingLevel, $aXmlToPHP))
  279. {
  280. throw new exception("Tracking level: unknown value '$sTrackingLevel'");
  281. }
  282. return $aXmlToPHP[$sTrackingLevel];
  283. }
  284. /**
  285. * Helper to format the edit-mode for direct linkset
  286. * @param string $sEditMode Value set from within the XML
  287. * Returns string PHP flag
  288. */
  289. protected function EditModeToPHP($sEditMode)
  290. {
  291. static $aXmlToPHP = array(
  292. 'none' => 'LINKSET_EDITMODE_NONE',
  293. 'add_only' => 'LINKSET_EDITMODE_ADDONLY',
  294. 'actions' => 'LINKSET_EDITMODE_ACTIONS',
  295. 'in_place' => 'LINKSET_EDITMODE_INPLACE',
  296. );
  297. if (!array_key_exists($sEditMode, $aXmlToPHP))
  298. {
  299. throw new exception("Edit mode: unknown value '$sTrackingLevel'");
  300. }
  301. return $aXmlToPHP[$sEditMode];
  302. }
  303. /**
  304. * Format a path (file or url) as an absolute path or relative to the module or the app
  305. */
  306. protected function PathToPHP($sPath, $sModuleRelativeDir, $bIsUrl = false)
  307. {
  308. if ($sPath == '')
  309. {
  310. $sPHP = "''";
  311. }
  312. elseif (substr($sPath, 0, 2) == '$$')
  313. {
  314. // Absolute
  315. $sPHP = self::QuoteForPHP(substr($sPath, 2));
  316. }
  317. elseif (substr($sPath, 0, 1) == '$')
  318. {
  319. // Relative to the application
  320. if ($bIsUrl)
  321. {
  322. $sPHP = "utils::GetAbsoluteUrlAppRoot().".self::QuoteForPHP(substr($sPath, 1));
  323. }
  324. else
  325. {
  326. $sPHP = "APPROOT.".self::QuoteForPHP(substr($sPath, 1));
  327. }
  328. }
  329. else
  330. {
  331. // Relative to the module
  332. if ($bIsUrl)
  333. {
  334. $sPHP = "utils::GetAbsoluteUrlAppRoot().".self::QuoteForPHP($sModuleRelativeDir.''.$sPath);
  335. }
  336. else
  337. {
  338. $sPHP = "dirname(__FILE__).'/$sPath'";
  339. }
  340. }
  341. return $sPHP;
  342. }
  343. protected function GetPropString($oNode, $sTag, $sDefault = null)
  344. {
  345. $val = $oNode->GetChildText($sTag);
  346. if (is_null($val))
  347. {
  348. if (is_null($sDefault))
  349. {
  350. return null;
  351. }
  352. else
  353. {
  354. $val = $sDefault;
  355. }
  356. }
  357. return "'".$val."'";
  358. }
  359. protected function GetPropBoolean($oNode, $sTag, $bDefault = null)
  360. {
  361. $val = $oNode->GetChildText($sTag);
  362. if (is_null($val))
  363. {
  364. if (is_null($bDefault))
  365. {
  366. return null;
  367. }
  368. else
  369. {
  370. return $bDefault ? 'true' : 'false';
  371. }
  372. }
  373. return $val == 'true' ? 'true' : 'false';
  374. }
  375. protected function GetPropNumber($oNode, $sTag, $nDefault = null)
  376. {
  377. $val = $oNode->GetChildText($sTag);
  378. if (is_null($val))
  379. {
  380. if (is_null($nDefault))
  381. {
  382. return null;
  383. }
  384. else
  385. {
  386. $val = $nDefault;
  387. }
  388. }
  389. return (string)$val;
  390. }
  391. /**
  392. * Adds quotes and escape characters
  393. */
  394. protected function QuoteForPHP($sStr)
  395. {
  396. $sEscaped = str_replace(array('\\', '"', "\n"), array('\\\\', '\\"', '\\n'), $sStr);
  397. $sRet = '"'.$sEscaped.'"';
  398. return $sRet;
  399. }
  400. protected function CompileClass($oClass, $sModuleRelativeDir, $oP)
  401. {
  402. $sClass = $oClass->getAttribute('id');
  403. $oProperties = $oClass->GetUniqueElement('properties');
  404. // Class caracteristics
  405. //
  406. $aClassParams = array();
  407. $aClassParams['category'] = $this->GetPropString($oProperties, 'category', '');
  408. $aClassParams['key_type'] = "'autoincrement'";
  409. if ($oNaming = $oProperties->GetOptionalElement('naming'))
  410. {
  411. $oNameAttributes = $oNaming->GetUniqueElement('attributes');
  412. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  413. $aNameAttCodes = array();
  414. foreach($oAttributes as $oAttribute)
  415. {
  416. $aNameAttCodes[] = $oAttribute->getAttribute('id');
  417. }
  418. if (count($aNameAttCodes) > 1)
  419. {
  420. // New style...
  421. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  422. }
  423. elseif (count($aNameAttCodes) == 1)
  424. {
  425. // New style...
  426. $sNameAttCode = "'$aNameAttCodes[0]'";
  427. }
  428. else
  429. {
  430. $sNameAttCode = "''";
  431. }
  432. }
  433. else
  434. {
  435. $sNameAttCode = "''";
  436. }
  437. $aClassParams['name_attcode'] = $sNameAttCode;
  438. $oLifecycle = $oClass->GetOptionalElement('lifecycle');
  439. if ($oLifecycle)
  440. {
  441. $sStateAttCode = $oLifecycle->GetChildText('attribute');
  442. }
  443. else
  444. {
  445. $sStateAttCode = "";
  446. }
  447. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  448. if ($oReconciliation = $oProperties->GetOptionalElement('reconciliation'))
  449. {
  450. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  451. $aReconcAttCodes = array();
  452. foreach($oReconcAttributes as $oAttribute)
  453. {
  454. $aReconcAttCodes[] = $oAttribute->getAttribute('id');
  455. }
  456. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  457. }
  458. else
  459. {
  460. $sReconcKeys = "array()";
  461. }
  462. $aClassParams['reconc_keys'] = $sReconcKeys;
  463. $aClassParams['db_table'] = $this->GetPropString($oProperties, 'db_table', '');
  464. $aClassParams['db_key_field'] = $this->GetPropString($oProperties, 'db_key_field', 'id');
  465. if (array_key_exists($sClass, $this->aRootClasses))
  466. {
  467. $sDefaultFinalClass = 'finalclass';
  468. }
  469. else
  470. {
  471. $sDefaultFinalClass = '';
  472. }
  473. $aClassParams['db_finalclass_field'] = $this->GetPropString($oProperties, 'db_final_class_field', $sDefaultFinalClass);
  474. if (($sDisplayTemplate = $oProperties->GetChildText('display_template')) && (strlen($sDisplayTemplate) > 0))
  475. {
  476. $sDisplayTemplate = $sModuleRelativeDir.'/'.$sDisplayTemplate;
  477. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  478. }
  479. if (($sIcon = $oProperties->GetChildText('icon')) && (strlen($sIcon) > 0))
  480. {
  481. $sIcon = $sModuleRelativeDir.'/'.$sIcon;
  482. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  483. }
  484. $oOrder = $oProperties->GetOptionalElement('order');
  485. if ($oOrder)
  486. {
  487. $oColumnsNode = $oOrder->GetUniqueElement('columns');
  488. $oColumns = $oColumnsNode->getElementsByTagName('column');
  489. $aSortColumns = array();
  490. foreach($oColumns as $oColumn)
  491. {
  492. $aSortColumns[] = "'".$oColumn->getAttribute('id')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  493. }
  494. if (count($aSortColumns) > 0)
  495. {
  496. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  497. }
  498. }
  499. // Finalize class params declaration
  500. //
  501. $aClassParamsPHP = array();
  502. foreach($aClassParams as $sKey => $sPHPValue)
  503. {
  504. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  505. }
  506. $sClassParams = implode("\n", $aClassParamsPHP);
  507. // Comment on top of the class declaration
  508. //
  509. $sCodeComment = $oProperties->GetChildText('comment');
  510. // Fields
  511. //
  512. $sAttributes = '';
  513. foreach($this->oFactory->ListFields($oClass) as $oField)
  514. {
  515. // $oField
  516. $sAttCode = $oField->getAttribute('id');
  517. $sAttType = $oField->getAttribute('xsi:type');
  518. $aDependencies = array();
  519. $oDependencies = $oField->GetOptionalElement('dependencies');
  520. if (!is_null($oDependencies))
  521. {
  522. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  523. foreach($oDepNodes as $oDepAttribute)
  524. {
  525. $aDependencies[] = "'".$oDepAttribute->getAttribute('id')."'";
  526. }
  527. }
  528. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  529. $aParameters = array();
  530. if ($sAttType == 'AttributeLinkedSetIndirect')
  531. {
  532. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  533. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  534. $aParameters['ext_key_to_remote'] = $this->GetPropString($oField, 'ext_key_to_remote', '');
  535. $aParameters['allowed_values'] = 'null';
  536. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  537. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  538. $aParameters['duplicates'] = $this->GetPropBoolean($oField, 'duplicates', false);
  539. $sTrackingLevel = $oField->GetChildText('tracking_level');
  540. if (!is_null($sTrackingLevel))
  541. {
  542. $aParameters['tracking_level'] = $this->TrackingLevelToPHP($sTrackingLevel);
  543. }
  544. $aParameters['depends_on'] = $sDependencies;
  545. }
  546. elseif ($sAttType == 'AttributeLinkedSet')
  547. {
  548. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  549. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  550. $aParameters['allowed_values'] = 'null';
  551. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  552. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  553. $sTrackingLevel = $oField->GetChildText('tracking_level');
  554. if (!is_null($sTrackingLevel))
  555. {
  556. $aParameters['tracking_level'] = $this->TrackingLevelToPHP($sTrackingLevel);
  557. }
  558. $sEditMode = $oField->GetChildText('edit_mode');
  559. if (!is_null($sEditMode))
  560. {
  561. $aParameters['edit_mode'] = $this->EditModeToPHP($sEditMode);
  562. }
  563. $aParameters['depends_on'] = $sDependencies;
  564. }
  565. elseif ($sAttType == 'AttributeExternalKey')
  566. {
  567. $aParameters['targetclass'] = $this->GetPropString($oField, 'target_class', '');
  568. // deprecated: $aParameters['jointype'] = 'null';
  569. if ($sOql = $oField->GetChildText('filter'))
  570. {
  571. $sEscapedOql = self::QuoteForPHP($sOql);
  572. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  573. }
  574. else
  575. {
  576. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  577. }
  578. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  579. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  580. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  581. $aParameters['depends_on'] = $sDependencies;
  582. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  583. $aParameters['min_auto_complete_chars'] = $this->GetPropNumber($oField, 'min_auto_complete_chars');
  584. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  585. }
  586. elseif ($sAttType == 'AttributeHierarchicalKey')
  587. {
  588. if ($sOql = $oField->GetChildText('filter'))
  589. {
  590. $sEscapedOql = self::QuoteForPHP($sOql);
  591. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  592. }
  593. else
  594. {
  595. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  596. }
  597. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  598. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  599. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  600. $aParameters['depends_on'] = $sDependencies;
  601. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  602. $aParameters['min_auto_complete_chars'] = $this->GetPropNumber($oField, 'min_auto_complete_chars');
  603. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  604. }
  605. elseif ($sAttType == 'AttributeExternalField')
  606. {
  607. $aParameters['allowed_values'] = 'null';
  608. $aParameters['extkey_attcode'] = $this->GetPropString($oField, 'extkey_attcode', '');
  609. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode', '');
  610. }
  611. elseif ($sAttType == 'AttributeURL')
  612. {
  613. $aParameters['target'] = $this->GetPropString($oField, 'target', '');
  614. $aParameters['allowed_values'] = 'null';
  615. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  616. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  617. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  618. $aParameters['depends_on'] = $sDependencies;
  619. }
  620. elseif ($sAttType == 'AttributeEnum')
  621. {
  622. $oValues = $oField->GetUniqueElement('values');
  623. $oValueNodes = $oValues->getElementsByTagName('value');
  624. $aValues = array();
  625. foreach($oValueNodes as $oValue)
  626. {
  627. // new style... $aValues[] = self::QuoteForPHP($oValue->textContent);
  628. $aValues[] = $oValue->textContent;
  629. }
  630. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  631. $sValues = '"'.implode(',', $aValues).'"';
  632. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  633. $aParameters['display_style'] = $this->GetPropString($oField, 'display_style', 'list');
  634. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  635. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  636. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  637. $aParameters['depends_on'] = $sDependencies;
  638. }
  639. elseif ($sAttType == 'AttributeBlob')
  640. {
  641. $aParameters['depends_on'] = $sDependencies;
  642. }
  643. elseif ($sAttType == 'AttributeStopWatch')
  644. {
  645. $oStates = $oField->GetUniqueElement('states');
  646. $oStateNodes = $oStates->getElementsByTagName('state');
  647. $aStates = array();
  648. foreach($oStateNodes as $oState)
  649. {
  650. $aStates[] = '"'.$oState->GetAttribute('id').'"';
  651. }
  652. $aParameters['states'] = 'array('.implode(', ', $aStates).')';
  653. $aParameters['goal_computing'] = $this->GetPropString($oField, 'goal', 'DefaultMetricComputer'); // Optional, no deadline by default
  654. $aParameters['working_time_computing'] = $this->GetPropString($oField, 'working_time', 'DefaultWorkingTimeComputer'); // Optional, defaults to 24x7
  655. $oThresholds = $oField->GetUniqueElement('thresholds');
  656. $oThresholdNodes = $oThresholds->getElementsByTagName('threshold');
  657. $aThresholds = array();
  658. foreach($oThresholdNodes as $oThreshold)
  659. {
  660. $iPercent = $this->GetPropNumber($oThreshold, 'percent');
  661. $oActions = $oThreshold->GetUniqueElement('actions');
  662. $oActionNodes = $oActions->getElementsByTagName('action');
  663. $aActions = array();
  664. foreach($oActionNodes as $oAction)
  665. {
  666. $oParams = $oAction->GetOptionalElement('params');
  667. $aActionParams = array();
  668. if ($oParams)
  669. {
  670. $oParamNodes = $oParams->getElementsByTagName('param');
  671. foreach($oParamNodes as $oParam)
  672. {
  673. $aActionParams[] = self::QuoteForPHP($oParam->textContent);
  674. }
  675. }
  676. $sActionParams = 'array('.implode(', ', $aActionParams).')';
  677. $sVerb = $this->GetPropString($oAction, 'verb');
  678. $aActions[] = "array('verb' => $sVerb, 'params' => $sActionParams)";
  679. }
  680. $sActions = 'array('.implode(', ', $aActions).')';
  681. $aThresholds[] = $iPercent." => array('percent' => $iPercent, 'actions' => $sActions)";
  682. }
  683. $aParameters['thresholds'] = 'array('.implode(', ', $aThresholds).')';
  684. }
  685. elseif ($sAttType == 'AttributeSubItem')
  686. {
  687. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode');
  688. $aParameters['item_code'] = $this->GetPropString($oField, 'item_code');
  689. }
  690. else
  691. {
  692. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  693. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  694. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  695. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  696. $aParameters['depends_on'] = $sDependencies;
  697. }
  698. // Optional parameters (more for historical reasons)
  699. // Added if present...
  700. //
  701. $aParameters['validation_pattern'] = $this->GetPropString($oField, 'validation_pattern');
  702. $aParameters['width'] = $this->GetPropNumber($oField, 'width');
  703. $aParameters['height'] = $this->GetPropNumber($oField, 'height');
  704. $aParameters['digits'] = $this->GetPropNumber($oField, 'digits');
  705. $aParameters['decimals'] = $this->GetPropNumber($oField, 'decimals');
  706. $aParams = array();
  707. foreach($aParameters as $sKey => $sValue)
  708. {
  709. if (!is_null($sValue))
  710. {
  711. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  712. }
  713. }
  714. $sParams = implode(', ', $aParams);
  715. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  716. }
  717. // Lifecycle
  718. //
  719. $sLifecycle = '';
  720. if ($oLifecycle)
  721. {
  722. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  723. $sLifecycle .= "\t\t//\n";
  724. $oStimuli = $oLifecycle->GetUniqueElement('stimuli');
  725. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  726. {
  727. $sStimulus = $oStimulus->getAttribute('id');
  728. $sStimulusClass = $oStimulus->getAttribute('xsi:type');
  729. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  730. }
  731. $oStates = $oLifecycle->GetUniqueElement('states');
  732. foreach ($oStates->getElementsByTagName('state') as $oState)
  733. {
  734. $sState = $oState->getAttribute('id');
  735. $oInitialStatePath = $oState->GetOptionalElement('initial_state_path');
  736. if ($oInitialStatePath)
  737. {
  738. $aInitialStatePath = array();
  739. foreach ($oInitialStatePath->getElementsByTagName('state_ref') as $oIntermediateState)
  740. {
  741. $aInitialStatePath[] = "'".$oIntermediateState->GetText()."'";
  742. }
  743. $sInitialStatePath = 'Array('.implode(', ', $aInitialStatePath).')';
  744. }
  745. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  746. $sLifecycle .= " \"".$sState."\",\n";
  747. $sLifecycle .= " array(\n";
  748. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  749. $sLifecycle .= " \"attribute_list\" => array(\n";
  750. $oFlags = $oState->GetUniqueElement('flags');
  751. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  752. {
  753. $sFlags = $this->FlagsToPHP($oAttributeNode);
  754. if (strlen($sFlags) > 0)
  755. {
  756. $sAttCode = $oAttributeNode->GetAttribute('id');
  757. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  758. }
  759. }
  760. $sLifecycle .= " ),\n";
  761. if (!is_null($oInitialStatePath))
  762. {
  763. $sLifecycle .= " \"initial_state_path\" => $sInitialStatePath,\n";
  764. }
  765. $sLifecycle .= " )\n";
  766. $sLifecycle .= " );\n";
  767. $oTransitions = $oState->GetUniqueElement('transitions');
  768. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  769. {
  770. $sStimulus = $oTransition->GetChildText('stimulus');
  771. $sTargetState = $oTransition->GetChildText('target');
  772. $oActions = $oTransition->GetUniqueElement('actions');
  773. $aVerbs = array();
  774. foreach ($oActions->getElementsByTagName('action') as $oAction)
  775. {
  776. $sVerb = $oAction->GetChildText('verb');
  777. $aVerbs[] = "'$sVerb'";
  778. }
  779. $sActions = implode(', ', $aVerbs);
  780. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  781. }
  782. }
  783. }
  784. // ZLists
  785. //
  786. $aListRef = array(
  787. 'details' => 'details',
  788. 'standard_search' => 'search',
  789. 'list' => 'list'
  790. );
  791. $oPresentation = $oClass->GetUniqueElement('presentation');
  792. $sZlists = '';
  793. foreach ($aListRef as $sListCode => $sListTag)
  794. {
  795. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  796. if ($oListNode)
  797. {
  798. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  799. $this->ArrayOfItemsToZList($aAttributes);
  800. $sZAttributes = var_export($aAttributes, true);
  801. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  802. }
  803. }
  804. // Methods
  805. $sMethods = "";
  806. $oMethods = $oClass->GetUniqueElement('methods');
  807. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  808. {
  809. $sMethodCode = $oMethod->GetChildText('code');
  810. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  811. {
  812. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  813. }
  814. else
  815. {
  816. $sMethods .= "\n\n".$sMethodCode."\n";
  817. }
  818. }
  819. // Let's make the whole class declaration
  820. //
  821. $sPHP = "\n\n$sCodeComment\n";
  822. $sParentClass = $oClass->GetChildText('php_parent');
  823. $oPhpParent = $oClass->GetUniqueElement('php_parent', false);
  824. if ($oPhpParent)
  825. {
  826. $sParentClass = $oPhpParent->GetChildText('name', '');
  827. if ($sParentClass == '')
  828. {
  829. throw new Exception("Failed to process class '".$oClass->getAttribute('id')."', from '$sRelativeDir': missing required tag 'name' under 'php_parent'.");
  830. }
  831. $sIncludeFile = $oPhpParent->GetChildText('file', '');
  832. if ($sIncludeFile != '')
  833. {
  834. $sPHP .= "\nrequire_once('$sIncludeFile'); // Implementation of the class $sParentClass\n";
  835. }
  836. //TODO fix this !!!
  837. // $sFullPath = $this->sSourceDir.'/'.$sModuleRelativeDir.'/'.$sIncludeFile;
  838. // if (!file_exists($sFullPath))
  839. // {
  840. // throw new Exception("Failed to process class '".$oClass->getAttribute('id')."', from '$sModuleRelativeDir'. The required include file: '$sFullPath' does not exist.");
  841. // }
  842. }
  843. else
  844. {
  845. $sParentClass = $oClass->GetChildText('parent', 'DBObject');
  846. }
  847. if ($oProperties->GetChildText('abstract') == 'true')
  848. {
  849. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  850. }
  851. else
  852. {
  853. $sPHP .= 'class '.$oClass->getAttribute('id');
  854. }
  855. $sPHP .= " extends $sParentClass\n";
  856. $sPHP .=
  857. <<<EOF
  858. {
  859. public static function Init()
  860. {
  861. \$aParams = array
  862. (
  863. $sClassParams
  864. );
  865. MetaModel::Init_Params(\$aParams);
  866. MetaModel::Init_InheritAttributes();
  867. $sAttributes
  868. $sLifecycle
  869. $sZlists
  870. }
  871. $sMethods
  872. }
  873. EOF;
  874. return $sPHP;
  875. }// function CompileClass()
  876. protected function CompileMenu($oMenu, $sTargetDir, $sModuleRelativeDir, $oP)
  877. {
  878. $sMenuId = $oMenu->getAttribute("id");
  879. $sMenuClass = $oMenu->getAttribute("xsi:type");
  880. $sParent = $oMenu->GetChildText('parent', null);
  881. if ($sParent)
  882. {
  883. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  884. }
  885. else
  886. {
  887. $sParentSpec = '-1';
  888. }
  889. $fRank = $oMenu->GetChildText('rank');
  890. switch($sMenuClass)
  891. {
  892. case 'WebPageMenuNode':
  893. $sUrl = $oMenu->GetChildText('url');
  894. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  895. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  896. break;
  897. case 'DashboardMenuNode':
  898. $sTemplateFile = $oMenu->GetChildText('definition_file', '');
  899. if ($sTemplateFile != '')
  900. {
  901. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  902. }
  903. else
  904. {
  905. $oDashboardDefinition = $oMenu->GetOptionalElement('definition');
  906. if ($oDashboardDefinition == null)
  907. {
  908. throw(new Exception('Missing definition for Dashboard menu "'.$sMenuId.'" expecting either a tag "definition_file" or "definition".'));
  909. }
  910. $sFileName = strtolower(str_replace(array(':', '/', '\\', '*'), '_', $sMenuId)).'_dashboard_menu.xml';
  911. $sTemplateSpec = $this->PathToPHP($sFileName, $sModuleRelativeDir);
  912. $oXMLDoc = new DOMDocument('1.0', 'UTF-8');
  913. $oXMLDoc->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  914. $oXMLDoc->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  915. $oRootNode = $oXMLDoc->createElement('dashboard'); // make sure that the document is not empty
  916. $oRootNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  917. $oXMLDoc->appendChild($oRootNode);
  918. foreach($oDashboardDefinition->childNodes as $oNode)
  919. {
  920. $oDefNode = $oXMLDoc->importNode($oNode, true); // layout, cells, etc Nodes and below
  921. $oRootNode->appendChild($oDefNode);
  922. }
  923. $oXMLDoc->save($sTargetDir.'/'.$sModuleRelativeDir.'/'.$sFileName);
  924. }
  925. $sNewMenu = "new DashboardMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  926. break;
  927. case 'TemplateMenuNode':
  928. $sTemplateFile = $oMenu->GetChildText('template_file');
  929. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  930. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  931. break;
  932. case 'OQLMenuNode':
  933. $sOQL = self::QuoteForPHP($oMenu->GetChildText('oql'));
  934. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  935. $sNewMenu = "new OQLMenuNode('$sMenuId', $sOQL, $sParentSpec, $fRank, $bSearch);";
  936. break;
  937. case 'NewObjectMenuNode':
  938. $sClass = $oMenu->GetChildText('class');
  939. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  940. break;
  941. case 'SearchMenuNode':
  942. $sClass = $oMenu->GetChildText('class');
  943. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  944. break;
  945. case 'MenuGroup':
  946. default:
  947. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  948. {
  949. $sEnableAction = $oMenu->GetChildText('enable_action');
  950. $sEnablePermission = $oMenu->GetChildText('enable_permission');
  951. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  952. if (strlen($sEnableStimulus) > 0)
  953. {
  954. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  955. }
  956. else
  957. {
  958. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  959. }
  960. //$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
  961. }
  962. else
  963. {
  964. $sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
  965. }
  966. }
  967. $sIndent = '';
  968. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  969. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  970. {
  971. $sAutoReload = self::QuoteForPHP($sAutoReload);
  972. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
  973. }
  974. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  975. if ($sAdminOnly && ($sAdminOnly == '1'))
  976. {
  977. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  978. $sPHP .= $sIndent."{\n";
  979. foreach($aPHPMenu as $sPHPLine)
  980. {
  981. $sPHP .= $sIndent." $sPHPLine\n";
  982. }
  983. $sPHP .= $sIndent."}\n";
  984. }
  985. else
  986. {
  987. $sPHP = '';
  988. foreach($aPHPMenu as $sPHPLine)
  989. {
  990. $sPHP .= $sIndent.$sPHPLine."\n";
  991. }
  992. }
  993. return $sPHP;
  994. } // function CompileMenu
  995. protected function CompileUserRights($oUserRightsNode)
  996. {
  997. static $aActionsInShort = array(
  998. 'read' => 'r',
  999. 'bulk read' => 'br',
  1000. 'write' => 'w',
  1001. 'bulk write' => 'bw',
  1002. 'delete' => 'd',
  1003. 'bulk delete' => 'bd',
  1004. );
  1005. // Groups
  1006. //
  1007. $aGroupClasses = array();
  1008. $oGroups = $oUserRightsNode->GetUniqueElement('groups');
  1009. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  1010. {
  1011. $sGroupId = $oGroup->getAttribute("id");
  1012. $aClasses = array();
  1013. $oClasses = $oGroup->GetUniqueElement('classes');
  1014. foreach($oClasses->getElementsByTagName('class') as $oClass)
  1015. {
  1016. $sClass = $oClass->getAttribute("id");
  1017. $aClasses[] = $sClass;
  1018. //$bSubclasses = $this->GetPropBoolean($oClass, 'subclasses', true);
  1019. //if ($bSubclasses)...
  1020. }
  1021. $aGroupClasses[$sGroupId] = $aClasses;
  1022. }
  1023. // Profiles and grants
  1024. //
  1025. $aProfiles = array();
  1026. // Hardcode the administrator profile
  1027. $aProfiles[1] = array(
  1028. 'name' => 'Administrator',
  1029. 'description' => 'Has the rights on everything (bypassing any control)'
  1030. );
  1031. $aGrants = array();
  1032. $oProfiles = $oUserRightsNode->GetUniqueElement('profiles');
  1033. foreach($oProfiles->getElementsByTagName('profile') as $oProfile)
  1034. {
  1035. $iProfile = $oProfile->getAttribute("id");
  1036. $sName = $oProfile->GetChildText('name');
  1037. $sDescription = $oProfile->GetChildText('description');
  1038. $oGroups = $oProfile->GetUniqueElement('groups');
  1039. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  1040. {
  1041. $sGroupId = $oGroup->getAttribute("id");
  1042. $aActions = array();
  1043. $oActions = $oGroup->GetUniqueElement('actions');
  1044. foreach($oActions->getElementsByTagName('action') as $oAction)
  1045. {
  1046. $sAction = $oAction->getAttribute("id");
  1047. $sType = $oAction->getAttribute("xsi:type");
  1048. $sGrant = $oAction->GetText();
  1049. $bGrant = ($sGrant == 'allow');
  1050. if ($sGroupId == '*')
  1051. {
  1052. $aGrantClasses = array('*');
  1053. }
  1054. else
  1055. {
  1056. $aGrantClasses = $aGroupClasses[$sGroupId];
  1057. }
  1058. foreach ($aGrantClasses as $sClass)
  1059. {
  1060. if ($sType == 'stimulus')
  1061. {
  1062. $sGrantKey = $iProfile.'_'.$sClass.'_s_'.$sAction;
  1063. $sGrantKeyPlus = $iProfile.'_'.$sClass.'+_s_'.$sAction; // subclasses inherit this grant
  1064. }
  1065. else
  1066. {
  1067. $sAction = $aActionsInShort[$sType];
  1068. $sGrantKey = $iProfile.'_'.$sClass.'_'.$sAction;
  1069. $sGrantKeyPlus = $iProfile.'_'.$sClass.'+_'.$sAction; // subclasses inherit this grant
  1070. }
  1071. // The class itself
  1072. if (isset($aGrants[$sGrantKey]))
  1073. {
  1074. if (!$bGrant)
  1075. {
  1076. $aGrants[$sGrantKey] = false;
  1077. }
  1078. }
  1079. else
  1080. {
  1081. $aGrants[$sGrantKey] = $bGrant;
  1082. }
  1083. // The subclasses
  1084. if (isset($aGrants[$sGrantKeyPlus]))
  1085. {
  1086. if (!$bGrant)
  1087. {
  1088. $aGrants[$sGrantKeyPlus] = false;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. $aGrants[$sGrantKeyPlus] = $bGrant;
  1094. }
  1095. }
  1096. }
  1097. }
  1098. $aProfiles[$iProfile] = array(
  1099. 'name' => $sName,
  1100. 'description' => $sDescription
  1101. );
  1102. }
  1103. $sProfiles = var_export($aProfiles, true);
  1104. $sGrants = var_export($aGrants, true);
  1105. $sPHP =
  1106. <<<EOF
  1107. //
  1108. // List of constant profiles
  1109. // - used by the class URP_Profiles at setup (create/update/delete records)
  1110. // - used by the addon UserRightsProfile to determine user rights
  1111. //
  1112. class ProfilesConfig
  1113. {
  1114. protected static \$aPROFILES = $sProfiles;
  1115. protected static \$aGRANTS = $sGrants;
  1116. public static function GetProfileActionGrant(\$iProfileId, \$sClass, \$sAction)
  1117. {
  1118. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_'.\$sAction;
  1119. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1120. {
  1121. return self::\$aGRANTS[\$sGrantKey];
  1122. }
  1123. foreach (MetaModel::EnumParentClasses(\$sClass) as \$sParent)
  1124. {
  1125. \$sGrantKey = \$iProfileId.'_'.\$sParent.'+_'.\$sAction;
  1126. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1127. {
  1128. return self::\$aGRANTS[\$sGrantKey];
  1129. }
  1130. }
  1131. \$sGrantKey = \$iProfileId.'_*_'.\$sAction;
  1132. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1133. {
  1134. return self::\$aGRANTS[\$sGrantKey];
  1135. }
  1136. return null;
  1137. }
  1138. public static function GetProfileStimulusGrant(\$iProfileId, \$sClass, \$sStimulus)
  1139. {
  1140. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_s_'.\$sStimulus;
  1141. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1142. {
  1143. return self::\$aGRANTS[\$sGrantKey];
  1144. }
  1145. \$sGrantKey = \$iProfileId.'_*_s_'.\$sStimulus;
  1146. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1147. {
  1148. return self::\$aGRANTS[\$sGrantKey];
  1149. }
  1150. return null;
  1151. }
  1152. // returns an array of id => array of column => php value(so-called "real value")
  1153. public static function GetProfilesValues()
  1154. {
  1155. return self::\$aPROFILES;
  1156. }
  1157. }
  1158. EOF;
  1159. return $sPHP;
  1160. } // function CompileUserRights
  1161. }
  1162. ?>