compiler.class.inc.php 38 KB

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