compiler.class.inc.php 38 KB

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