compiler.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. $sClass = $oClass->getAttribute("name");
  127. try
  128. {
  129. $this->CompileClass($oClass, $sResultFile, $sRelativeDir, $oP);
  130. }
  131. catch (ssDOMFormatException $e)
  132. {
  133. $sClass = $oClass->getAttribute("name");
  134. throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
  135. }
  136. }
  137. }
  138. $oMenus = $this->oFactory->ListMenus($sModuleName);
  139. $iMenuCount = $oMenus->length;
  140. if ($iMenuCount == 0)
  141. {
  142. $oP->p("Found module without menus declared: $sModuleName");
  143. }
  144. else
  145. {
  146. $sMenusHeader =
  147. <<<EOF
  148. //
  149. // Menus
  150. //
  151. EOF;
  152. file_put_contents($sResultFile, $sMenusHeader, FILE_APPEND);
  153. foreach($oMenus as $oMenu)
  154. {
  155. try
  156. {
  157. $this->CompileMenu($oMenu, $sResultFile, $sRelativeDir, $oP);
  158. }
  159. catch (ssDOMFormatException $e)
  160. {
  161. $sClass = $oClass->getAttribute("name");
  162. throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
  163. }
  164. }
  165. }
  166. }
  167. if (count($aResultFiles))
  168. {
  169. $oP->add('<h2>Files</h2>');
  170. foreach ($aResultFiles as $sModuleName => $sFile)
  171. {
  172. $oP->add('<h3>'.$sFile.'</h3>');
  173. $oP->add('<a name="'.$sModuleName.'"/><div style="border:1;">');
  174. $oP->add(highlight_file($sFile, true));
  175. $oP->add('</div style="">');
  176. }
  177. }
  178. $oP->output();
  179. //$this->oFactory->Dump();
  180. }
  181. /**
  182. * Helper to copy the module files to the exploitation environment
  183. * Returns true if successfull
  184. */
  185. protected function CopyDirectory($sSource, $sDest)
  186. {
  187. if (is_dir($sSource))
  188. {
  189. if (!is_dir($sDest))
  190. {
  191. mkdir($sDest);
  192. }
  193. $aFiles = scandir($sSource);
  194. if(sizeof($aFiles) > 0 )
  195. {
  196. foreach($aFiles as $sFile)
  197. {
  198. if ($sFile == '.' || $sFile == '..' || $sFile == '.svn')
  199. {
  200. // Skip
  201. continue;
  202. }
  203. if (is_dir($sSource.'/'.$sFile))
  204. {
  205. $this->CopyDirectory($sSource.'/'.$sFile, $sDest.'/'.$sFile);
  206. }
  207. else
  208. {
  209. copy($sSource.'/'.$sFile, $sDest.'/'.$sFile);
  210. }
  211. }
  212. }
  213. return true;
  214. }
  215. elseif (is_file($sSource))
  216. {
  217. return copy($sSource, $sDest);
  218. }
  219. else
  220. {
  221. return false;
  222. }
  223. }
  224. /**
  225. * Helper to browse the DOM -could be factorized in ModelFactory
  226. * Returns the node directly under the given node, and that is supposed to be always present and unique
  227. */
  228. protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
  229. {
  230. $oNode = null;
  231. if ($oDOMNode->hasChildNodes())
  232. {
  233. foreach($oDOMNode->childNodes as $oChildNode)
  234. {
  235. if ($oChildNode->nodeName == $sTagName)
  236. {
  237. $oNode = $oChildNode;
  238. break;
  239. }
  240. }
  241. }
  242. if ($bMustExist && is_null($oNode))
  243. {
  244. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  245. }
  246. return $oNode;
  247. }
  248. /**
  249. * Helper to browse the DOM -could be factorized in ModelFactory
  250. * Returns the node directly under the given node, or null is missing
  251. */
  252. protected function GetOptionalElement($oDOMNode, $sTagName)
  253. {
  254. return $this->GetUniqueElement($oDOMNode, $sTagName, false);
  255. }
  256. /**
  257. * Helper to browse the DOM -could be factorized in ModelFactory
  258. * Returns the TEXT of the given node (possibly from several subnodes)
  259. */
  260. protected function GetNodeText($oNode)
  261. {
  262. $sText = '';
  263. if ($oNode->hasChildNodes())
  264. {
  265. foreach($oNode->childNodes as $oChildNode)
  266. {
  267. if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
  268. {
  269. $sText .= $oChildNode->wholeText;
  270. }
  271. }
  272. }
  273. return $sText;
  274. }
  275. /**
  276. * Helper to browse the DOM -could be factorized in ModelFactory
  277. * Assumes the given node to be either a text or
  278. * <items>
  279. * <item [key]="..."]>value<item>
  280. * <item [key]="..."]>value<item>
  281. * </items>
  282. * where value can be the either a text or an array of items... recursively
  283. * Returns a PHP array
  284. */
  285. protected function GetNodeAsArrayOfItems($oNode)
  286. {
  287. $oItems = $this->GetOptionalElement($oNode, 'items');
  288. if ($oItems)
  289. {
  290. $res = array();
  291. if ($oItems->hasChildNodes())
  292. {
  293. foreach($oItems->childNodes as $oItem)
  294. {
  295. // When an attribute is msising
  296. if ($oItem->hasAttributes() && $oItem->hasAttribute('key'))
  297. {
  298. $key = $oItem->getAttribute('key');
  299. $res[$key] = $this->GetNodeAsArrayOfItems($oItem);
  300. }
  301. else
  302. {
  303. $res[] = $this->GetNodeAsArrayOfItems($oItem);
  304. }
  305. }
  306. }
  307. }
  308. else
  309. {
  310. $res = $this->GetNodeText($oNode);
  311. }
  312. return $res;
  313. }
  314. /**
  315. * Helper to format the flags for an attribute, in a given state
  316. * @param object $oAttNode DOM node containing the information to build the flags
  317. * Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
  318. */
  319. protected function FlagsToPHP($oAttNode)
  320. {
  321. static $aNodeAttributeToFlag = array(
  322. 'mandatory' => 'OPT_ATT_MANDATORY',
  323. 'read_only' => 'OPT_ATT_READONLY',
  324. 'must_prompt' => 'OPT_ATT_MUSTPROMPT',
  325. 'must_change' => 'OPT_ATT_MUSTCHANGE',
  326. 'hidden' => 'OPT_ATT_HIDDEN',
  327. );
  328. $aFlags = array();
  329. foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
  330. {
  331. $bFlag = ($oAttNode->GetAttribute($sNodeAttribute) == '1');
  332. if ($bFlag)
  333. {
  334. $aFlags[] = $sFlag;
  335. }
  336. }
  337. $sRes = implode(' | ', $aFlags);
  338. return $sRes;
  339. }
  340. /**
  341. * Format a path (file or url) as an absolute path or relative to the module or the app
  342. */
  343. protected function PathToPHP($sPath, $sModuleRelativeDir, $bIsUrl = false)
  344. {
  345. if (substr($sPath, 0, 2) == '$$')
  346. {
  347. // Absolute
  348. $sPHP = "'".addslashes(substr($sPath, 2))."'";
  349. }
  350. elseif (substr($sPath, 0, 1) == '$')
  351. {
  352. // Relative to the application
  353. if ($bIsUrl)
  354. {
  355. $sPHP = "utils::GetAbsoluteUrlAppRoot().'".addslashes(substr($sPath, 1))."'";
  356. }
  357. else
  358. {
  359. $sPHP = "APPROOT.'".addslashes(substr($sPath, 1))."'";
  360. }
  361. }
  362. else
  363. {
  364. // Relative to the module
  365. if ($bIsUrl)
  366. {
  367. $sPHP = "utils::GetAbsoluteUrlAppRoot().'".addslashes($sModuleRelativeDir.''.$sPath)."'";
  368. }
  369. else
  370. {
  371. $sPHP = "dirname(__FILE__).'/$sPath'";
  372. }
  373. }
  374. return $sPHP;
  375. }
  376. protected function CompileClass($oClass, $sResFile, $sModuleRelativeDir, $oP)
  377. {
  378. $sClass = $oClass->getAttribute('name');
  379. $oProperties = $this->GetUniqueElement($oClass, 'properties');
  380. // Class caracteristics
  381. //
  382. $aClassParams = array();
  383. $aClassParams['category'] = "'".$oClass->getAttribute('category')."'";
  384. $aClassParams['key_type'] = "'autoincrement'";
  385. $oNaming = $this->GetUniqueElement($oProperties, 'naming');
  386. $oNameAttributes = $this->GetUniqueElement($oNaming, 'attributes');
  387. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  388. $aNameAttCodes = array();
  389. foreach($oAttributes as $oAttribute)
  390. {
  391. $aNameAttCodes[] = $oAttribute->getAttribute('name');
  392. }
  393. if (count($aNameAttCodes) > 1)
  394. {
  395. // New style...
  396. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  397. }
  398. elseif (count($aNameAttCodes) == 1)
  399. {
  400. // New style...
  401. $sNameAttCode = "'$aNameAttCodes[0]'";
  402. }
  403. else
  404. {
  405. $sNameAttCode = "''";
  406. }
  407. $aClassParams['name_attcode'] = $sNameAttCode;
  408. $oLifecycle = $this->GetOptionalElement($oClass, 'lifecycle');
  409. if ($oLifecycle)
  410. {
  411. $sStateAttCode = $oLifecycle->getAttribute('attribute');
  412. }
  413. else
  414. {
  415. $sStateAttCode = "";
  416. }
  417. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  418. $oReconciliation = $this->GetUniqueElement($oProperties, 'reconciliation');
  419. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  420. $aReconcAttCodes = array();
  421. foreach($oReconcAttributes as $oAttribute)
  422. {
  423. $aReconcAttCodes[] = $oAttribute->getAttribute('name');
  424. }
  425. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  426. $aClassParams['reconc_keys'] = $sReconcKeys;
  427. $aClassParams['db_table'] = "'".$oClass->getAttribute('db_table')."'";
  428. $aClassParams['db_key_field'] = "'".$oClass->getAttribute('db_key_field')."'";
  429. $aClassParams['db_finalclass_field'] = "'".$oClass->getAttribute('db_final_class_field')."'";
  430. $oDisplayTemplate = $this->GetOptionalElement($oProperties, 'display_template');
  431. if ($oDisplayTemplate && (strlen($oDisplayTemplate->textContent) > 0))
  432. {
  433. $sDisplayTemplate = $sModuleRelativeDir.'/'.$oDisplayTemplate->textContent;
  434. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  435. }
  436. $oIcon = $this->GetOptionalElement($oProperties, 'icon');
  437. if ($oIcon && (strlen($oIcon->textContent) > 0))
  438. {
  439. $sIcon = $sModuleRelativeDir.'/'.$oIcon->textContent;
  440. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  441. }
  442. $oOrder = $this->GetOptionalElement($oProperties, 'order');
  443. if ($oOrder)
  444. {
  445. $oColumnsNode = $this->GetUniqueElement($oOrder, 'columns');
  446. $oColumns = $oColumnsNode->getElementsByTagName('column');
  447. $aSortColumns = array();
  448. foreach($oColumns as $oColumn)
  449. {
  450. $aSortColumns[] = "'".$oColumn->getAttribute('name')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  451. }
  452. if (count($aSortColumns) > 0)
  453. {
  454. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  455. }
  456. }
  457. // Finalize class params declaration
  458. //
  459. $aClassParamsPHP = array();
  460. foreach($aClassParams as $sKey => $sPHPValue)
  461. {
  462. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  463. }
  464. $sClassParams = implode("\n", $aClassParamsPHP);
  465. // Comment on top of the class declaration
  466. //
  467. $oComment = $this->GetOptionalElement($oProperties, 'comment');
  468. if ($oComment)
  469. {
  470. $sCodeComment = $oComment->textContent;
  471. }
  472. else
  473. {
  474. $sCodeComment = '';
  475. }
  476. // Fields
  477. //
  478. $sAttributes = '';
  479. foreach($this->oFactory->ListFields($oClass) as $oField)
  480. {
  481. // $oField
  482. $sAttCode = $oField->getAttribute('name');
  483. $sAttType = 'Attribute'.$oField->getAttribute('type');
  484. $aDependencies = array();
  485. $oDependencies = $this->GetOptionalElement($oField, 'dependencies');
  486. if (!is_null($oDependencies))
  487. {
  488. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  489. foreach($oDepNodes as $oDepAttribute)
  490. {
  491. $aDependencies[] = "'".$oDepAttribute->getAttribute('name')."'";
  492. }
  493. }
  494. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  495. $aParameters = array();
  496. if ($sAttType == 'AttributeLinkedSetIndirect')
  497. {
  498. $aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
  499. $aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
  500. $aParameters['ext_key_to_remote'] = "'".$oField->getAttribute('ext_key_to_remote')."'";
  501. // todo - utile ?
  502. $aParameters['allowed_values'] = 'null';
  503. $aParameters['count_min'] = $oField->getAttribute('count_min');
  504. $aParameters['count_max'] = $oField->getAttribute('count_max');
  505. $aParameters['depends_on'] = $sDependencies;
  506. }
  507. elseif ($sAttType == 'AttributeLinkedSet')
  508. {
  509. $aParameters['linked_class'] = "'".$oField->getAttribute('linked_class')."'";
  510. $aParameters['ext_key_to_me'] = "'".$oField->getAttribute('ext_key_to_me')."'";
  511. // todo - utile ?
  512. $aParameters['allowed_values'] = 'null';
  513. $aParameters['count_min'] = $oField->getAttribute('count_min');
  514. $aParameters['count_max'] = $oField->getAttribute('count_max');
  515. $aParameters['depends_on'] = $sDependencies;
  516. }
  517. elseif ($sAttType == 'AttributeExternalKey')
  518. {
  519. $aParameters['targetclass'] = "'".$oField->getAttribute('target_class')."'";
  520. // todo = v�rifier l'utilit�
  521. $aParameters['jointype'] = 'null';
  522. if (($sOql = $oField->getAttribute('filter')) != '')
  523. {
  524. $sEscapedOql = addslashes($sOql);
  525. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  526. }
  527. else
  528. {
  529. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  530. }
  531. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  532. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  533. $aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
  534. $aParameters['depends_on'] = $sDependencies;
  535. }
  536. elseif ($sAttType == 'AttributeHierarchicalKey')
  537. {
  538. if (($sOql = $oField->getAttribute('filter')) != '')
  539. {
  540. $sEscapedOql = addslashes($sOql);
  541. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  542. }
  543. else
  544. {
  545. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  546. }
  547. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  548. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  549. $aParameters['on_target_delete'] = $oField->getAttribute('on_target_delete');
  550. $aParameters['depends_on'] = $sDependencies;
  551. }
  552. elseif ($sAttType == 'AttributeExternalField')
  553. {
  554. $aParameters['allowed_values'] = 'null';
  555. $aParameters['extkey_attcode'] = "'".$oField->getAttribute('extkey_attcode')."'";
  556. $aParameters['target_attcode'] = "'".$oField->getAttribute('target_attcode')."'";
  557. }
  558. elseif ($sAttType == 'AttributeURL')
  559. {
  560. $aParameters['target'] = "'".$oField->getAttribute('target')."'";
  561. $aParameters['allowed_values'] = 'null';
  562. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  563. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  564. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  565. $aParameters['depends_on'] = $sDependencies;
  566. }
  567. elseif ($sAttType == 'AttributeEnum')
  568. {
  569. $oValues = $this->GetUniqueElement($oField, 'values');
  570. $oValueNodes = $oValues->getElementsByTagName('value');
  571. $aValues = array();
  572. foreach($oValueNodes as $oValue)
  573. {
  574. // new style... $aValues[] = "'".addslashes($oValue->textContent)."'";
  575. $aValues[] = $oValue->textContent;
  576. }
  577. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  578. $sValues = '"'.implode(',', $aValues).'"';
  579. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  580. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  581. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  582. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  583. $aParameters['depends_on'] = $sDependencies;
  584. }
  585. elseif ($sAttType == 'AttributeBlob')
  586. {
  587. $aParameters['depends_on'] = $sDependencies;
  588. }
  589. else
  590. {
  591. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  592. $aParameters['sql'] = "'".$oField->getAttribute('sql')."'";
  593. $aParameters['default_value'] = "'".$oField->getAttribute('default_value')."'";
  594. $aParameters['is_null_allowed'] = $oField->getAttribute('is_null_allowed') == 'true' ? 'true' : 'false';
  595. $aParameters['depends_on'] = $sDependencies;
  596. if ($sValidationPattern = $oField->getAttribute('validation_pattern'))
  597. {
  598. $aParameters['validation_pattern'] = '"'.addslashes($sValidationPattern).'"';
  599. }
  600. }
  601. $aParams = array();
  602. foreach($aParameters as $sKey => $sValue)
  603. {
  604. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  605. }
  606. $sParams = implode(', ', $aParams);
  607. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  608. }
  609. // Lifecycle
  610. //
  611. $sLifecycle = '';
  612. if ($oLifecycle)
  613. {
  614. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  615. $sLifecycle .= "\t\t//\n";
  616. $oStimuli = $this->GetUniqueElement($oLifecycle, 'stimuli');
  617. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  618. {
  619. $sStimulus = $oStimulus->getAttribute('name');
  620. $sStimulusClass = $oStimulus->getAttribute('type');
  621. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  622. }
  623. $oStates = $this->GetUniqueElement($oLifecycle, 'states');
  624. foreach ($oStates->getElementsByTagName('state') as $oState)
  625. {
  626. $sState = $oState->getAttribute('name');
  627. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  628. $sLifecycle .= " \"".$sState."\",\n";
  629. $sLifecycle .= " array(\n";
  630. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  631. $sLifecycle .= " \"attribute_list\" => array(\n";
  632. $oFlags = $this->GetUniqueElement($oState, 'flags');
  633. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  634. {
  635. $sFlags = $this->FlagsToPHP($oAttributeNode);
  636. if (strlen($sFlags) > 0)
  637. {
  638. $sAttCode = $oAttributeNode->GetAttribute('name');
  639. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  640. }
  641. }
  642. $sLifecycle .= " ),\n";
  643. $sLifecycle .= " )\n";
  644. $sLifecycle .= " );\n";
  645. $oTransitions = $this->GetUniqueElement($oState, 'transitions');
  646. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  647. {
  648. $sStimulus = $oTransition->getAttribute('stimulus');
  649. $sTargetState = $oTransition->getAttribute('target');
  650. $oActions = $this->GetUniqueElement($oTransition, 'actions');
  651. $aVerbs = array();
  652. foreach ($oActions->getElementsByTagName('action') as $oAction)
  653. {
  654. $sVerb = $oAction->getAttribute('verb');
  655. $aVerbs[] = "'$sVerb'";
  656. }
  657. $sActions = implode(', ', $aVerbs);
  658. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  659. }
  660. }
  661. }
  662. // ZLists
  663. //
  664. $aListRef = array(
  665. 'details' => 'details',
  666. 'standard_search' => 'search',
  667. 'list' => 'list'
  668. );
  669. $oPresentation = $this->GetUniqueElement($oClass, 'presentation');
  670. $sZlists = '';
  671. foreach ($aListRef as $sListCode => $sListTag)
  672. {
  673. $oListNode = $this->GetOptionalElement($oPresentation, $sListTag);
  674. if ($oListNode)
  675. {
  676. $aAttributes = $this->GetNodeAsArrayOfItems($oListNode);
  677. $sZAttributes = var_export($aAttributes, true);
  678. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  679. }
  680. }
  681. // Methods
  682. $sMethods = "";
  683. $oMethods = $this->GetUniqueElement($oClass, 'methods');
  684. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  685. {
  686. $sMethodCode = $this->GetNodeText($oMethod);
  687. $oMethodComment = $this->GetOptionalElement($oMethod, 'comment');
  688. if ($oMethodComment)
  689. {
  690. $sMethods .= "\n\t".$oMethodComment->textContent."\n".$sMethodCode."\n";
  691. }
  692. else
  693. {
  694. $sMethods .= "\n\n".$sMethodCode."\n";
  695. }
  696. }
  697. // Let's make the whole class declaration
  698. //
  699. $sPHP = "\n\n$sCodeComment\n";
  700. if ($oClass->getAttribute('abstract') == 'true')
  701. {
  702. $sPHP .= 'abstract class '.$oClass->getAttribute('name');
  703. }
  704. else
  705. {
  706. $sPHP .= 'class '.$oClass->getAttribute('name');
  707. }
  708. $sPHP .= " extends ".$oClass->getAttribute('parent')."\n";
  709. $sPHP .=
  710. <<<EOF
  711. {
  712. public static function Init()
  713. {
  714. \$aParams = array
  715. (
  716. $sClassParams
  717. );
  718. MetaModel::Init_Params(\$aParams);
  719. MetaModel::Init_InheritAttributes();
  720. $sAttributes
  721. $sLifecycle
  722. $sZlists
  723. }
  724. $sMethods
  725. }
  726. EOF;
  727. file_put_contents($sResFile, $sPHP, FILE_APPEND);
  728. }// function CompileClass()
  729. protected function CompileMenu($oMenu, $sResFile, $sModuleRelativeDir, $oP)
  730. {
  731. $sMenuId = $oMenu->getAttribute("id");
  732. $sMenuClass = $oMenu->getAttribute("type");
  733. $oParent = $this->GetOptionalElement($oMenu, 'parent');
  734. if ($oParent)
  735. {
  736. $sParent = $oParent->GetAttribute('value');
  737. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  738. }
  739. else
  740. {
  741. $sParentSpec = '-1';
  742. }
  743. $fRank = $this->GetUniqueElement($oMenu, 'rank')->GetAttribute('value');
  744. switch($sMenuClass)
  745. {
  746. case 'WebPageMenuNode':
  747. $sUrl = $this->GetUniqueElement($oMenu, 'url')->GetAttribute('value');
  748. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  749. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  750. break;
  751. case 'TemplateMenuNode':
  752. $sTemplateFile = $this->GetUniqueElement($oMenu, 'template_file')->GetAttribute('value');
  753. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  754. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  755. break;
  756. case 'OQLMenuNode':
  757. $sOQL = $this->GetUniqueElement($oMenu, 'oql')->GetAttribute('value');
  758. $bSearch = ($this->GetUniqueElement($oMenu, 'do_search')->GetAttribute('value') == '1') ? 'true' : 'false';
  759. $sNewMenu = "new OQLMenuNode('$sMenuId', '$sOQL', $sParentSpec, $fRank, $bSearch);";
  760. break;
  761. case 'NewObjectMenuNode':
  762. $sClass = $this->GetUniqueElement($oMenu, 'class')->GetAttribute('value');
  763. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  764. break;
  765. case 'SearchMenuNode':
  766. $sClass = $this->GetUniqueElement($oMenu, 'class')->GetAttribute('value');
  767. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  768. break;
  769. case 'MenuGroup':
  770. default:
  771. if ($oEnableClass = $this->GetOptionalElement($oMenu, 'enable_class'))
  772. {
  773. $sEnableClass = $oEnableClass->GetAttribute('value');
  774. $sEnableAction = $this->GetUniqueElement($oMenu, 'enable_action')->GetAttribute('value');
  775. $sEnablePermission = $this->GetUniqueElement($oMenu, 'enable_permission')->GetAttribute('value');
  776. $sEnableStimulus = $this->GetUniqueElement($oMenu, 'enable_stimulus')->GetAttribute('value');
  777. if (strlen($sEnableStimulus) > 0)
  778. {
  779. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  780. }
  781. else
  782. {
  783. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  784. }
  785. //$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
  786. }
  787. else
  788. {
  789. $sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
  790. }
  791. }
  792. $sIndent = ' ';
  793. $sPHPMenu = "\$__comp_menus__['$sMenuId'] = $sNewMenu";
  794. $oAdminOnly = $this->GetOptionalElement($oMenu, 'enable_admin_only');
  795. if ($oAdminOnly && $oAdminOnly->GetAttribute('value') == '1')
  796. {
  797. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  798. $sPHP .= $sIndent."{\n";
  799. $sPHP .= $sIndent." $sPHPMenu\n";
  800. $sPHP .= $sIndent."}\n";
  801. }
  802. else
  803. {
  804. $sPHP = $sIndent."$sPHPMenu\n";
  805. }
  806. file_put_contents($sResFile, $sPHP, FILE_APPEND);
  807. }
  808. }
  809. ?>