compiler.class.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. else
  608. {
  609. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  610. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  611. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  612. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  613. $aParameters['depends_on'] = $sDependencies;
  614. }
  615. // Optional parameters (more for historical reasons)
  616. // Added if present...
  617. //
  618. $aParameters['validation_pattern'] = $this->GetPropString($oField, 'validation_pattern');
  619. $aParameters['width'] = $this->GetPropNumber($oField, 'width');
  620. $aParameters['height'] = $this->GetPropNumber($oField, 'height');
  621. $aParameters['digits'] = $this->GetPropNumber($oField, 'digits');
  622. $aParameters['decimals'] = $this->GetPropNumber($oField, 'decimals');
  623. $aParams = array();
  624. foreach($aParameters as $sKey => $sValue)
  625. {
  626. if (!is_null($sValue))
  627. {
  628. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  629. }
  630. }
  631. $sParams = implode(', ', $aParams);
  632. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  633. }
  634. // Lifecycle
  635. //
  636. $sLifecycle = '';
  637. if ($oLifecycle)
  638. {
  639. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  640. $sLifecycle .= "\t\t//\n";
  641. $oStimuli = $oLifecycle->GetUniqueElement('stimuli');
  642. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  643. {
  644. $sStimulus = $oStimulus->getAttribute('id');
  645. $sStimulusClass = $oStimulus->getAttribute('xsi:type');
  646. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  647. }
  648. $oStates = $oLifecycle->GetUniqueElement('states');
  649. foreach ($oStates->getElementsByTagName('state') as $oState)
  650. {
  651. $sState = $oState->getAttribute('id');
  652. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  653. $sLifecycle .= " \"".$sState."\",\n";
  654. $sLifecycle .= " array(\n";
  655. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  656. $sLifecycle .= " \"attribute_list\" => array(\n";
  657. $oFlags = $oState->GetUniqueElement('flags');
  658. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  659. {
  660. $sFlags = $this->FlagsToPHP($oAttributeNode);
  661. if (strlen($sFlags) > 0)
  662. {
  663. $sAttCode = $oAttributeNode->GetAttribute('id');
  664. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  665. }
  666. }
  667. $sLifecycle .= " ),\n";
  668. $sLifecycle .= " )\n";
  669. $sLifecycle .= " );\n";
  670. $oTransitions = $oState->GetUniqueElement('transitions');
  671. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  672. {
  673. $sStimulus = $oTransition->GetChildText('stimulus');
  674. $sTargetState = $oTransition->GetChildText('target');
  675. $oActions = $oTransition->GetUniqueElement('actions');
  676. $aVerbs = array();
  677. foreach ($oActions->getElementsByTagName('action') as $oAction)
  678. {
  679. $sVerb = $oAction->GetChildText('verb');
  680. $aVerbs[] = "'$sVerb'";
  681. }
  682. $sActions = implode(', ', $aVerbs);
  683. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  684. }
  685. }
  686. }
  687. // ZLists
  688. //
  689. $aListRef = array(
  690. 'details' => 'details',
  691. 'standard_search' => 'search',
  692. 'list' => 'list'
  693. );
  694. $oPresentation = $oClass->GetUniqueElement('presentation');
  695. $sZlists = '';
  696. foreach ($aListRef as $sListCode => $sListTag)
  697. {
  698. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  699. if ($oListNode)
  700. {
  701. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  702. $sZAttributes = var_export($aAttributes, true);
  703. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  704. }
  705. }
  706. // Methods
  707. $sMethods = "";
  708. $oMethods = $oClass->GetUniqueElement('methods');
  709. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  710. {
  711. $sMethodCode = $oMethod->GetChildText('code');
  712. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  713. {
  714. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  715. }
  716. else
  717. {
  718. $sMethods .= "\n\n".$sMethodCode."\n";
  719. }
  720. }
  721. // Let's make the whole class declaration
  722. //
  723. $sPHP = "\n\n$sCodeComment\n";
  724. if ($oProperties->GetChildText('abstract') == 'true')
  725. {
  726. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  727. }
  728. else
  729. {
  730. $sPHP .= 'class '.$oClass->getAttribute('id');
  731. }
  732. $sPHP .= " extends ".$oClass->GetChildText('parent', 'DBObject')."\n";
  733. $sPHP .=
  734. <<<EOF
  735. {
  736. public static function Init()
  737. {
  738. \$aParams = array
  739. (
  740. $sClassParams
  741. );
  742. MetaModel::Init_Params(\$aParams);
  743. MetaModel::Init_InheritAttributes();
  744. $sAttributes
  745. $sLifecycle
  746. $sZlists
  747. }
  748. $sMethods
  749. }
  750. EOF;
  751. return $sPHP;
  752. }// function CompileClass()
  753. protected function CompileMenu($oMenu, $sModuleRelativeDir, $oP)
  754. {
  755. $sMenuId = $oMenu->getAttribute("id");
  756. $sMenuClass = $oMenu->getAttribute("xsi:type");
  757. $sParent = $oMenu->GetChildText('parent', null);
  758. if ($sParent)
  759. {
  760. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  761. }
  762. else
  763. {
  764. $sParentSpec = '-1';
  765. }
  766. $fRank = $oMenu->GetChildText('rank');
  767. switch($sMenuClass)
  768. {
  769. case 'WebPageMenuNode':
  770. $sUrl = $oMenu->GetChildText('url');
  771. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  772. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  773. break;
  774. case 'DashboardMenuNode':
  775. $sTemplateFile = $oMenu->GetChildText('definition_file');
  776. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  777. $sNewMenu = "new DashboardMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  778. break;
  779. case 'TemplateMenuNode':
  780. $sTemplateFile = $oMenu->GetChildText('template_file');
  781. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  782. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  783. break;
  784. case 'OQLMenuNode':
  785. $sOQL = self::QuoteForPHP($oMenu->GetChildText('oql'));
  786. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  787. $sNewMenu = "new OQLMenuNode('$sMenuId', $sOQL, $sParentSpec, $fRank, $bSearch);";
  788. break;
  789. case 'NewObjectMenuNode':
  790. $sClass = $oMenu->GetChildText('class');
  791. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  792. break;
  793. case 'SearchMenuNode':
  794. $sClass = $oMenu->GetChildText('class');
  795. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  796. break;
  797. case 'MenuGroup':
  798. default:
  799. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  800. {
  801. $sEnableAction = $oMenu->GetChildText('enable_action');
  802. $sEnablePermission = $oMenu->GetChildText('enable_permission');
  803. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  804. if (strlen($sEnableStimulus) > 0)
  805. {
  806. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  807. }
  808. else
  809. {
  810. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  811. }
  812. //$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
  813. }
  814. else
  815. {
  816. $sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
  817. }
  818. }
  819. $sIndent = '';
  820. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  821. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  822. {
  823. $sAutoReload = self::QuoteForPHP($sAutoReload);
  824. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
  825. }
  826. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  827. if ($sAdminOnly && ($sAdminOnly == '1'))
  828. {
  829. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  830. $sPHP .= $sIndent."{\n";
  831. foreach($aPHPMenu as $sPHPLine)
  832. {
  833. $sPHP .= $sIndent." $sPHPLine\n";
  834. }
  835. $sPHP .= $sIndent."}\n";
  836. }
  837. else
  838. {
  839. $sPHP = '';
  840. foreach($aPHPMenu as $sPHPLine)
  841. {
  842. $sPHP .= $sIndent.$sPHPLine."\n";
  843. }
  844. }
  845. return $sPHP;
  846. }
  847. }
  848. ?>