compiler.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. $oNaming = $oProperties->GetUniqueElement('naming');
  354. $oNameAttributes = $oNaming->GetUniqueElement('attributes');
  355. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  356. $aNameAttCodes = array();
  357. foreach($oAttributes as $oAttribute)
  358. {
  359. $aNameAttCodes[] = $oAttribute->getAttribute('id');
  360. }
  361. if (count($aNameAttCodes) > 1)
  362. {
  363. // New style...
  364. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  365. }
  366. elseif (count($aNameAttCodes) == 1)
  367. {
  368. // New style...
  369. $sNameAttCode = "'$aNameAttCodes[0]'";
  370. }
  371. else
  372. {
  373. $sNameAttCode = "''";
  374. }
  375. $aClassParams['name_attcode'] = $sNameAttCode;
  376. $oLifecycle = $oClass->GetOptionalElement('lifecycle');
  377. if ($oLifecycle)
  378. {
  379. $sStateAttCode = $oLifecycle->GetChildText('attribute');
  380. }
  381. else
  382. {
  383. $sStateAttCode = "";
  384. }
  385. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  386. $oReconciliation = $oProperties->GetUniqueElement('reconciliation');
  387. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  388. $aReconcAttCodes = array();
  389. foreach($oReconcAttributes as $oAttribute)
  390. {
  391. $aReconcAttCodes[] = $oAttribute->getAttribute('id');
  392. }
  393. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  394. $aClassParams['reconc_keys'] = $sReconcKeys;
  395. $aClassParams['db_table'] = $this->GetPropString($oProperties, 'db_table', '');
  396. $aClassParams['db_key_field'] = $this->GetPropString($oProperties, 'db_key_field', 'id');
  397. if (array_key_exists($sClass, $this->aRootClasses))
  398. {
  399. $sDefaultFinalClass = 'finalclass';
  400. }
  401. else
  402. {
  403. $sDefaultFinalClass = '';
  404. }
  405. $aClassParams['db_finalclass_field'] = $this->GetPropString($oProperties, 'db_final_class_field', $sDefaultFinalClass);
  406. if (($sDisplayTemplate = $oProperties->GetChildText('display_template')) && (strlen($sDisplayTemplate) > 0))
  407. {
  408. $sDisplayTemplate = $sModuleRelativeDir.'/'.$sDisplayTemplate;
  409. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  410. }
  411. if (($sIcon = $oProperties->GetChildText('icon')) && (strlen($sIcon) > 0))
  412. {
  413. $sIcon = $sModuleRelativeDir.'/'.$sIcon;
  414. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  415. }
  416. $oOrder = $oProperties->GetOptionalElement('order');
  417. if ($oOrder)
  418. {
  419. $oColumnsNode = $oOrder->GetUniqueElement('columns');
  420. $oColumns = $oColumnsNode->getElementsByTagName('column');
  421. $aSortColumns = array();
  422. foreach($oColumns as $oColumn)
  423. {
  424. $aSortColumns[] = "'".$oColumn->getAttribute('id')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  425. }
  426. if (count($aSortColumns) > 0)
  427. {
  428. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  429. }
  430. }
  431. // Finalize class params declaration
  432. //
  433. $aClassParamsPHP = array();
  434. foreach($aClassParams as $sKey => $sPHPValue)
  435. {
  436. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  437. }
  438. $sClassParams = implode("\n", $aClassParamsPHP);
  439. // Comment on top of the class declaration
  440. //
  441. $sCodeComment = $oProperties->GetChildText('comment');
  442. // Fields
  443. //
  444. $sAttributes = '';
  445. foreach($this->oFactory->ListFields($oClass) as $oField)
  446. {
  447. // $oField
  448. $sAttCode = $oField->getAttribute('id');
  449. $sAttType = $oField->getAttribute('xsi:type');
  450. $aDependencies = array();
  451. $oDependencies = $oField->GetOptionalElement('dependencies');
  452. if (!is_null($oDependencies))
  453. {
  454. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  455. foreach($oDepNodes as $oDepAttribute)
  456. {
  457. $aDependencies[] = "'".$oDepAttribute->getAttribute('id')."'";
  458. }
  459. }
  460. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  461. $aParameters = array();
  462. if ($sAttType == 'AttributeLinkedSetIndirect')
  463. {
  464. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  465. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  466. $aParameters['ext_key_to_remote'] = $this->GetPropString($oField, 'ext_key_to_remote', '');
  467. // todo - utile ?
  468. $aParameters['allowed_values'] = 'null';
  469. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  470. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  471. $aParameters['duplicates'] = $this->GetPropBoolean($oField, 'duplicates', false);
  472. $aParameters['depends_on'] = $sDependencies;
  473. }
  474. elseif ($sAttType == 'AttributeLinkedSet')
  475. {
  476. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  477. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  478. // todo - utile ?
  479. $aParameters['allowed_values'] = 'null';
  480. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  481. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  482. $aParameters['depends_on'] = $sDependencies;
  483. }
  484. elseif ($sAttType == 'AttributeExternalKey')
  485. {
  486. $aParameters['targetclass'] = $this->GetPropString($oField, 'target_class', '');
  487. // deprecated: $aParameters['jointype'] = 'null';
  488. if ($sOql = $oField->GetChildText('filter'))
  489. {
  490. $sEscapedOql = addslashes($sOql);
  491. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  492. }
  493. else
  494. {
  495. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  496. }
  497. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  498. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  499. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  500. $aParameters['depends_on'] = $sDependencies;
  501. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  502. $aParameters['min_auto_complete_chars'] = $this->GetPropNumber($oField, 'min_auto_complete_chars');
  503. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  504. }
  505. elseif ($sAttType == 'AttributeHierarchicalKey')
  506. {
  507. if ($sOql = $oField->GetChildText('filter'))
  508. {
  509. $sEscapedOql = addslashes($sOql);
  510. $aParameters['allowed_values'] = "new ValueSetObjects('$sEscapedOql')"; // or "new ValueSetObjects('SELECT xxxx')"
  511. }
  512. else
  513. {
  514. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  515. }
  516. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  517. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  518. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  519. $aParameters['depends_on'] = $sDependencies;
  520. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  521. $aParameters['min_auto_complete_chars'] = $this->GetPropNumber($oField, 'min_auto_complete_chars');
  522. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  523. }
  524. elseif ($sAttType == 'AttributeExternalField')
  525. {
  526. $aParameters['allowed_values'] = 'null';
  527. $aParameters['extkey_attcode'] = $this->GetPropString($oField, 'extkey_attcode', '');
  528. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode', '');
  529. }
  530. elseif ($sAttType == 'AttributeURL')
  531. {
  532. $aParameters['target'] = $this->GetPropString($oField, 'target', '');
  533. $aParameters['allowed_values'] = 'null';
  534. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  535. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  536. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  537. $aParameters['depends_on'] = $sDependencies;
  538. }
  539. elseif ($sAttType == 'AttributeEnum')
  540. {
  541. $oValues = $oField->GetUniqueElement('values');
  542. $oValueNodes = $oValues->getElementsByTagName('value');
  543. $aValues = array();
  544. foreach($oValueNodes as $oValue)
  545. {
  546. // new style... $aValues[] = "'".addslashes($oValue->textContent)."'";
  547. $aValues[] = $oValue->textContent;
  548. }
  549. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  550. $sValues = '"'.implode(',', $aValues).'"';
  551. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  552. $aParameters['display_style'] = $this->GetPropString($oField, 'display_style', 'list');
  553. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  554. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  555. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  556. $aParameters['depends_on'] = $sDependencies;
  557. }
  558. elseif ($sAttType == 'AttributeBlob')
  559. {
  560. $aParameters['depends_on'] = $sDependencies;
  561. }
  562. else
  563. {
  564. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  565. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  566. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  567. $aParameters['is_null_allowed'] = $oField->GetChildText('is_null_allowed', false);
  568. $aParameters['depends_on'] = $sDependencies;
  569. }
  570. // Optional parameters (more for historical reasons)
  571. // Added if present...
  572. //
  573. $aParameters['validation_pattern'] = $this->GetPropString($oField, 'validation_pattern');
  574. $aParameters['width'] = $this->GetPropNumber($oField, 'width');
  575. $aParameters['height'] = $this->GetPropNumber($oField, 'height');
  576. $aParameters['digits'] = $this->GetPropNumber($oField, 'digits');
  577. $aParameters['decimals'] = $this->GetPropNumber($oField, 'decimals');
  578. $aParams = array();
  579. foreach($aParameters as $sKey => $sValue)
  580. {
  581. if (!is_null($sValue))
  582. {
  583. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  584. }
  585. }
  586. $sParams = implode(', ', $aParams);
  587. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  588. }
  589. // Lifecycle
  590. //
  591. $sLifecycle = '';
  592. if ($oLifecycle)
  593. {
  594. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  595. $sLifecycle .= "\t\t//\n";
  596. $oStimuli = $oLifecycle->GetUniqueElement('stimuli');
  597. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  598. {
  599. $sStimulus = $oStimulus->getAttribute('id');
  600. $sStimulusClass = $oStimulus->getAttribute('xsi:type');
  601. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  602. }
  603. $oStates = $oLifecycle->GetUniqueElement('states');
  604. foreach ($oStates->getElementsByTagName('state') as $oState)
  605. {
  606. $sState = $oState->getAttribute('id');
  607. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  608. $sLifecycle .= " \"".$sState."\",\n";
  609. $sLifecycle .= " array(\n";
  610. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  611. $sLifecycle .= " \"attribute_list\" => array(\n";
  612. $oFlags = $oState->GetUniqueElement('flags');
  613. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  614. {
  615. $sFlags = $this->FlagsToPHP($oAttributeNode);
  616. if (strlen($sFlags) > 0)
  617. {
  618. $sAttCode = $oAttributeNode->GetAttribute('id');
  619. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  620. }
  621. }
  622. $sLifecycle .= " ),\n";
  623. $sLifecycle .= " )\n";
  624. $sLifecycle .= " );\n";
  625. $oTransitions = $oState->GetUniqueElement('transitions');
  626. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  627. {
  628. $sStimulus = $oTransition->GetChildText('stimulus');
  629. $sTargetState = $oTransition->GetChildText('target');
  630. $oActions = $oTransition->GetUniqueElement('actions');
  631. $aVerbs = array();
  632. foreach ($oActions->getElementsByTagName('action') as $oAction)
  633. {
  634. $sVerb = $oAction->GetChildText('verb');
  635. $aVerbs[] = "'$sVerb'";
  636. }
  637. $sActions = implode(', ', $aVerbs);
  638. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  639. }
  640. }
  641. }
  642. // ZLists
  643. //
  644. $aListRef = array(
  645. 'details' => 'details',
  646. 'standard_search' => 'search',
  647. 'list' => 'list'
  648. );
  649. $oPresentation = $oClass->GetUniqueElement('presentation');
  650. $sZlists = '';
  651. foreach ($aListRef as $sListCode => $sListTag)
  652. {
  653. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  654. if ($oListNode)
  655. {
  656. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  657. $sZAttributes = var_export($aAttributes, true);
  658. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  659. }
  660. }
  661. // Methods
  662. $sMethods = "";
  663. $oMethods = $oClass->GetUniqueElement('methods');
  664. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  665. {
  666. $sMethodCode = $oMethod->GetChildText('code');
  667. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  668. {
  669. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  670. }
  671. else
  672. {
  673. $sMethods .= "\n\n".$sMethodCode."\n";
  674. }
  675. }
  676. // Let's make the whole class declaration
  677. //
  678. $sPHP = "\n\n$sCodeComment\n";
  679. if ($oProperties->GetChildText('abstract') == 'true')
  680. {
  681. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  682. }
  683. else
  684. {
  685. $sPHP .= 'class '.$oClass->getAttribute('id');
  686. }
  687. $sPHP .= " extends ".$oClass->GetChildText('parent', 'DBObject')."\n";
  688. $sPHP .=
  689. <<<EOF
  690. {
  691. public static function Init()
  692. {
  693. \$aParams = array
  694. (
  695. $sClassParams
  696. );
  697. MetaModel::Init_Params(\$aParams);
  698. MetaModel::Init_InheritAttributes();
  699. $sAttributes
  700. $sLifecycle
  701. $sZlists
  702. }
  703. $sMethods
  704. }
  705. EOF;
  706. file_put_contents($sResFile, $sPHP, FILE_APPEND);
  707. }// function CompileClass()
  708. protected function CompileMenu($oMenu, $sResFile, $sModuleRelativeDir, $oP)
  709. {
  710. $sMenuId = $oMenu->getAttribute("id");
  711. $sMenuClass = $oMenu->getAttribute("xsi:type");
  712. $sParent = $oMenu->GetChildText('parent', null);
  713. if ($sParent)
  714. {
  715. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  716. }
  717. else
  718. {
  719. $sParentSpec = '-1';
  720. }
  721. $fRank = $oMenu->GetChildText('rank');
  722. switch($sMenuClass)
  723. {
  724. case 'WebPageMenuNode':
  725. $sUrl = $oMenu->GetChildText('url');
  726. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  727. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  728. break;
  729. case 'TemplateMenuNode':
  730. $sTemplateFile = $oMenu->GetChildText('template_file');
  731. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  732. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  733. break;
  734. case 'OQLMenuNode':
  735. $sOQL = $oMenu->GetChildText('oql');
  736. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  737. $sNewMenu = "new OQLMenuNode('$sMenuId', '$sOQL', $sParentSpec, $fRank, $bSearch);";
  738. break;
  739. case 'NewObjectMenuNode':
  740. $sClass = $oMenu->GetChildText('class');
  741. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  742. break;
  743. case 'SearchMenuNode':
  744. $sClass = $oMenu->GetChildText('class');
  745. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  746. break;
  747. case 'MenuGroup':
  748. default:
  749. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  750. {
  751. $sEnableAction = $oMenu->GetChildText('enable_action');
  752. $sEnablePermission = $oMenu->GetChildText('enable_permission');
  753. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  754. if (strlen($sEnableStimulus) > 0)
  755. {
  756. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  757. }
  758. else
  759. {
  760. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  761. }
  762. //$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
  763. }
  764. else
  765. {
  766. $sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
  767. }
  768. }
  769. $sIndent = '';
  770. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  771. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  772. {
  773. $sAutoReload = addslashes($sAutoReload);
  774. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => '$sAutoReload'));";
  775. }
  776. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  777. if ($sAdminOnly && ($sAdminOnly == '1'))
  778. {
  779. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  780. $sPHP .= $sIndent."{\n";
  781. foreach($aPHPMenu as $sPHPLine)
  782. {
  783. $sPHP .= $sIndent." $sPHPLine\n";
  784. }
  785. $sPHP .= $sIndent."}\n";
  786. }
  787. else
  788. {
  789. $sPHP = '';
  790. foreach($aPHPMenu as $sPHPLine)
  791. {
  792. $sPHP .= $sIndent.$sPHPLine."\n";
  793. }
  794. }
  795. file_put_contents($sResFile, $sPHP, FILE_APPEND);
  796. }
  797. }
  798. ?>