compiler.class.inc.php 37 KB

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