compiler.class.inc.php 34 KB

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