compiler.class.inc.php 26 KB

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