compiler.class.inc.php 23 KB

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