compiler.class.inc.php 29 KB

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