compiler.class.inc.php 26 KB

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