compiler.class.inc.php 24 KB

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