compiler.class.inc.php 37 KB

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