compiler.class.inc.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. }
  980. else
  981. {
  982. $sAction = $aActionsInShort[$sType];
  983. $sGrantKey = $iProfile.'_'.$sClass.'_'.$sAction;
  984. }
  985. if (isset($aGrants[$sGrantKey]))
  986. {
  987. if (!$bGrant)
  988. {
  989. $aGrants[$sGrantKey] = false;
  990. }
  991. }
  992. else
  993. {
  994. $aGrants[$sGrantKey] = $bGrant;
  995. }
  996. }
  997. }
  998. }
  999. $aProfiles[$iProfile] = array(
  1000. 'name' => $sName,
  1001. 'description' => $sDescription
  1002. );
  1003. }
  1004. $sProfiles = var_export($aProfiles, true);
  1005. $sGrants = var_export($aGrants, true);
  1006. $sPHP =
  1007. <<<EOF
  1008. //
  1009. // List of constant profiles
  1010. // - used by the class URP_Profiles at setup (create/update/delete records)
  1011. // - used by the addon UserRightsProfile to determine user rights
  1012. //
  1013. class ProfilesConfig
  1014. {
  1015. protected static \$aPROFILES = $sProfiles;
  1016. protected static \$aGRANTS = $sGrants;
  1017. public static function GetProfileActionGrant(\$iProfileId, \$sClass, \$sAction)
  1018. {
  1019. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_'.\$sAction;
  1020. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1021. {
  1022. return self::\$aGRANTS[\$sGrantKey];
  1023. }
  1024. \$sGrantKey = \$iProfileId.'_*_'.\$sAction;
  1025. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1026. {
  1027. return self::\$aGRANTS[\$sGrantKey];
  1028. }
  1029. return null;
  1030. }
  1031. public static function GetProfileStimulusGrant(\$iProfileId, \$sClass, \$sStimulus)
  1032. {
  1033. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_s_'.\$sStimulus;
  1034. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1035. {
  1036. return self::\$aGRANTS[\$sGrantKey];
  1037. }
  1038. \$sGrantKey = \$iProfileId.'_*_s_'.\$sStimulus;
  1039. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1040. {
  1041. return self::\$aGRANTS[\$sGrantKey];
  1042. }
  1043. return null;
  1044. }
  1045. // returns an array of id => array of column => php value(so-called "real value")
  1046. public static function GetProfilesValues()
  1047. {
  1048. return self::\$aPROFILES;
  1049. }
  1050. }
  1051. EOF;
  1052. return $sPHP;
  1053. } // function CompileUserRights
  1054. }
  1055. ?>