compiler.class.inc.php 26 KB

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