compiler.class.inc.php 38 KB

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