compiler.class.inc.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. require_once(APPROOT.'setup/setuputils.class.inc.php');
  17. class DOMFormatException extends Exception
  18. {
  19. }
  20. /**
  21. * Compiler class
  22. */
  23. class MFCompiler
  24. {
  25. protected $oFactory;
  26. protected $sSourceDir;
  27. protected $aRootClasses;
  28. protected $aLog;
  29. public function __construct($oModelFactory, $sSourceDir)
  30. {
  31. $this->oFactory = $oModelFactory;
  32. $this->sSourceDir = $sSourceDir;
  33. $this->aLog = array();
  34. }
  35. protected function Log($sText)
  36. {
  37. $this->aLog[] = $sText;
  38. }
  39. protected function DumpLog($oPage)
  40. {
  41. foreach ($this->aLog as $sText)
  42. {
  43. $oPage->p($sText);
  44. }
  45. }
  46. public function Compile($sTargetDir, $oP = null)
  47. {
  48. $aAllClasses = array(); // flat list of classes
  49. // Determine the target modules for the MENUS
  50. //
  51. $aMenuNodes = array();
  52. $aMenusByModule = array();
  53. foreach ($this->oFactory->ListActiveChildNodes('menus', 'menu') as $oMenuNode)
  54. {
  55. $sMenuId = $oMenuNode->getAttribute('id');
  56. $aMenuNodes[$sMenuId] = $oMenuNode;
  57. $sModuleMenu = $oMenuNode->getAttribute('_created_in');
  58. $aMenusByModule[$sModuleMenu][] = $sMenuId;
  59. }
  60. // Determine the target module (exactly one!) for USER RIGHTS
  61. //
  62. $sUserRightsModule = '';
  63. $oUserRightsNode = $this->oFactory->GetNodes('user_rights')->item(0);
  64. if ($oUserRightsNode)
  65. {
  66. $sUserRightsModule = $oUserRightsNode->getAttribute('_created_in');
  67. }
  68. // List root classes
  69. //
  70. $this->aRootClasses = array();
  71. foreach ($this->oFactory->ListRootClasses() as $oClass)
  72. {
  73. $this->Log("Root class: ".$oClass->getAttribute('id'));
  74. $this->aRootClasses[$oClass->getAttribute('id')] = $oClass;
  75. }
  76. // Compile, module by module
  77. //
  78. $aModules = $this->oFactory->GetLoadedModules();
  79. foreach($aModules as $foo => $oModule)
  80. {
  81. $sModuleName = $oModule->GetName();
  82. $sModuleVersion = $oModule->GetVersion();
  83. $sModuleRootDir = realpath($oModule->GetRootDir());
  84. $sRelativeDir = substr($sModuleRootDir, strlen($this->sSourceDir) + 1);
  85. // Push the other module files
  86. SetupUtils::copydir($sModuleRootDir, $sTargetDir.'/'.$sRelativeDir);
  87. $sCompiledCode = '';
  88. $oClasses = $this->oFactory->ListClasses($sModuleName);
  89. $iClassCount = $oClasses->length;
  90. if ($iClassCount == 0)
  91. {
  92. $this->Log("Found module without classes declared: $sModuleName");
  93. }
  94. else
  95. {
  96. foreach($oClasses as $oClass)
  97. {
  98. $sClass = $oClass->getAttribute("id");
  99. $aAllClasses[] = $sClass;
  100. try
  101. {
  102. $sCompiledCode .= $this->CompileClass($oClass, $sRelativeDir, $oP);
  103. }
  104. catch (ssDOMFormatException $e)
  105. {
  106. throw new Exception("Failed to process class '$sClass', from '$sModuleRootDir': ".$e->getMessage());
  107. }
  108. }
  109. }
  110. if (!array_key_exists($sModuleName, $aMenusByModule))
  111. {
  112. $this->Log("Found module without menus declared: $sModuleName");
  113. }
  114. else
  115. {
  116. $sCompiledCode .=
  117. <<<EOF
  118. //
  119. // Menus
  120. //
  121. EOF;
  122. // Preliminary: determine parent menus not defined within the current module
  123. $aMenusToLoad = array();
  124. $aParentMenus = array();
  125. foreach($aMenusByModule[$sModuleName] as $sMenuId)
  126. {
  127. $oMenuNode = $aMenuNodes[$sMenuId];
  128. if ($sParent = $oMenuNode->GetChildText('parent', null))
  129. {
  130. $aMenusToLoad[] = $sParent;
  131. $aParentMenus[] = $sParent;
  132. }
  133. // Note: the order matters: the parents must be defined BEFORE
  134. $aMenusToLoad[] = $sMenuId;
  135. }
  136. $aMenusToLoad = array_unique($aMenusToLoad);
  137. foreach($aMenusToLoad as $sMenuId)
  138. {
  139. $oMenuNode = $aMenuNodes[$sMenuId];
  140. if ($oMenuNode->getAttribute("xsi:type") == 'MenuGroup')
  141. {
  142. // Note: this algorithm is wrong
  143. // 1 - the module may appear empty in the current module, while children are defined in other modules
  144. // 2 - check recursively that child nodes are not empty themselves
  145. // Future algorithm:
  146. // a- browse the modules and build the menu tree
  147. // b- browse the tree and blacklist empty menus
  148. // c- before compiling, discard if blacklisted
  149. if (!in_array($oMenuNode->getAttribute("id"), $aParentMenus))
  150. {
  151. // Discard empty menu groups
  152. continue;
  153. }
  154. }
  155. try
  156. {
  157. $sCompiledCode .= $this->CompileMenu($oMenuNode, $sRelativeDir, $oP);
  158. }
  159. catch (ssDOMFormatException $e)
  160. {
  161. throw new Exception("Failed to process menu '$sMenuId', from '$sModuleRootDir': ".$e->getMessage());
  162. }
  163. }
  164. }
  165. // User rights
  166. //
  167. if ($sModuleName == $sUserRightsModule)
  168. {
  169. $sCompiledCode .= $this->CompileUserRights($oUserRightsNode);
  170. }
  171. // Create (overwrite if existing) the compiled file
  172. //
  173. if (strlen($sCompiledCode) > 0)
  174. {
  175. // We have compiled something: write the result file
  176. //
  177. $sResultFile = $sTargetDir.'/'.$sRelativeDir.'/model.'.$sModuleName.'.php';
  178. if (is_file($sResultFile))
  179. {
  180. $this->Log("Updating $sResultFile for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  181. }
  182. else
  183. {
  184. $sResultDir = dirname($sResultFile);
  185. if (!is_dir($sResultDir))
  186. {
  187. $this->Log("Creating directory $sResultDir");
  188. mkdir($sResultDir, 0777, true);
  189. }
  190. $this->Log("Creating $sResultFile for module $sModuleName in version $sModuleVersion ($iClassCount classes)");
  191. }
  192. // Compile the module into a single file
  193. //
  194. $sId = $sModuleName;
  195. $sCurrDate = date(DATE_ISO8601);
  196. $sAuthor = 'Combodo compiler';
  197. $sLicence = 'http://www.opensource.org/licenses/gpl-3.0.html LGPL';
  198. $sFileHeader =
  199. <<<EOF
  200. <?php
  201. //
  202. // File generated by ... on the $sCurrDate
  203. // Please do not edit manually
  204. //
  205. //
  206. // Copyright (C) 2010 Combodo SARL
  207. //
  208. // ben on met quoi ici ?
  209. // Signé: Romain
  210. //
  211. // This program is free software; you can redistribute it and/or modify
  212. // it under the terms of the GNU General Public License as published by
  213. // the Free Software Foundation; version 3 of the License.
  214. //
  215. // This program is distributed in the hope that it will be useful,
  216. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  217. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  218. // GNU General Public License for more details.
  219. //
  220. // You should have received a copy of the GNU General Public License
  221. // along with this program; if not, write to the Free Software
  222. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  223. /**
  224. * Classes and menus for $sModuleName (version $sModuleVersion)
  225. *
  226. * @author $sAuthor
  227. * @license $sLicence
  228. */
  229. EOF;
  230. file_put_contents($sResultFile, $sFileHeader.$sCompiledCode);
  231. }
  232. }
  233. }
  234. /**
  235. * Helper to format the flags for an attribute, in a given state
  236. * @param object $oAttNode DOM node containing the information to build the flags
  237. * Returns string PHP flags, based on the OPT_ATT_ constants, or empty (meaning 0, can be omitted)
  238. */
  239. protected function FlagsToPHP($oAttNode)
  240. {
  241. static $aNodeAttributeToFlag = array(
  242. 'mandatory' => 'OPT_ATT_MANDATORY',
  243. 'read_only' => 'OPT_ATT_READONLY',
  244. 'must_prompt' => 'OPT_ATT_MUSTPROMPT',
  245. 'must_change' => 'OPT_ATT_MUSTCHANGE',
  246. 'hidden' => 'OPT_ATT_HIDDEN',
  247. );
  248. $aFlags = array();
  249. foreach ($aNodeAttributeToFlag as $sNodeAttribute => $sFlag)
  250. {
  251. $bFlag = ($oAttNode->GetOptionalElement($sNodeAttribute) != null);
  252. if ($bFlag)
  253. {
  254. $aFlags[] = $sFlag;
  255. }
  256. }
  257. $sRes = implode(' | ', $aFlags);
  258. return $sRes;
  259. }
  260. /**
  261. * Format a path (file or url) as an absolute path or relative to the module or the app
  262. */
  263. protected function PathToPHP($sPath, $sModuleRelativeDir, $bIsUrl = false)
  264. {
  265. if ($sPath == '')
  266. {
  267. $sPHP = "''";
  268. }
  269. elseif (substr($sPath, 0, 2) == '$$')
  270. {
  271. // Absolute
  272. $sPHP = self::QuoteForPHP(substr($sPath, 2));
  273. }
  274. elseif (substr($sPath, 0, 1) == '$')
  275. {
  276. // Relative to the application
  277. if ($bIsUrl)
  278. {
  279. $sPHP = "utils::GetAbsoluteUrlAppRoot().".self::QuoteForPHP(substr($sPath, 1));
  280. }
  281. else
  282. {
  283. $sPHP = "APPROOT.".self::QuoteForPHP(substr($sPath, 1));
  284. }
  285. }
  286. else
  287. {
  288. // Relative to the module
  289. if ($bIsUrl)
  290. {
  291. $sPHP = "utils::GetAbsoluteUrlAppRoot().".self::QuoteForPHP($sModuleRelativeDir.''.$sPath);
  292. }
  293. else
  294. {
  295. $sPHP = "dirname(__FILE__).'/$sPath'";
  296. }
  297. }
  298. return $sPHP;
  299. }
  300. protected function GetPropString($oNode, $sTag, $sDefault = null)
  301. {
  302. $val = $oNode->GetChildText($sTag);
  303. if (is_null($val))
  304. {
  305. if (is_null($sDefault))
  306. {
  307. return null;
  308. }
  309. else
  310. {
  311. $val = $sDefault;
  312. }
  313. }
  314. return "'".$val."'";
  315. }
  316. protected function GetPropBoolean($oNode, $sTag, $bDefault = null)
  317. {
  318. $val = $oNode->GetChildText($sTag);
  319. if (is_null($val))
  320. {
  321. if (is_null($bDefault))
  322. {
  323. return null;
  324. }
  325. else
  326. {
  327. return $bDefault ? 'true' : 'false';
  328. }
  329. }
  330. return $val == 'true' ? 'true' : 'false';
  331. }
  332. protected function GetPropNumber($oNode, $sTag, $nDefault = null)
  333. {
  334. $val = $oNode->GetChildText($sTag);
  335. if (is_null($val))
  336. {
  337. if (is_null($nDefault))
  338. {
  339. return null;
  340. }
  341. else
  342. {
  343. $val = $nDefault;
  344. }
  345. }
  346. return (string)$val;
  347. }
  348. /**
  349. * Adds quotes and escape characters
  350. */
  351. protected function QuoteForPHP($sStr)
  352. {
  353. $sEscaped = str_replace(array('\\', '"', "\n"), array('\\\\', '\\"', '\\n'), $sStr);
  354. $sRet = '"'.$sEscaped.'"';
  355. return $sRet;
  356. }
  357. protected function CompileClass($oClass, $sModuleRelativeDir, $oP)
  358. {
  359. $sClass = $oClass->getAttribute('id');
  360. $oProperties = $oClass->GetUniqueElement('properties');
  361. // Class caracteristics
  362. //
  363. $aClassParams = array();
  364. $aClassParams['category'] = $this->GetPropString($oProperties, 'category', '');
  365. $aClassParams['key_type'] = "'autoincrement'";
  366. if ($oNaming = $oProperties->GetOptionalElement('naming'))
  367. {
  368. $oNameAttributes = $oNaming->GetUniqueElement('attributes');
  369. $oAttributes = $oNameAttributes->getElementsByTagName('attribute');
  370. $aNameAttCodes = array();
  371. foreach($oAttributes as $oAttribute)
  372. {
  373. $aNameAttCodes[] = $oAttribute->getAttribute('id');
  374. }
  375. if (count($aNameAttCodes) > 1)
  376. {
  377. // New style...
  378. $sNameAttCode = "array('".implode("', '", $aNameAttCodes)."')";
  379. }
  380. elseif (count($aNameAttCodes) == 1)
  381. {
  382. // New style...
  383. $sNameAttCode = "'$aNameAttCodes[0]'";
  384. }
  385. else
  386. {
  387. $sNameAttCode = "''";
  388. }
  389. }
  390. else
  391. {
  392. $sNameAttCode = "''";
  393. }
  394. $aClassParams['name_attcode'] = $sNameAttCode;
  395. $oLifecycle = $oClass->GetOptionalElement('lifecycle');
  396. if ($oLifecycle)
  397. {
  398. $sStateAttCode = $oLifecycle->GetChildText('attribute');
  399. }
  400. else
  401. {
  402. $sStateAttCode = "";
  403. }
  404. $aClassParams['state_attcode'] = "'$sStateAttCode'";
  405. if ($oReconciliation = $oProperties->GetOptionalElement('reconciliation'))
  406. {
  407. $oReconcAttributes = $oReconciliation->getElementsByTagName('attribute');
  408. $aReconcAttCodes = array();
  409. foreach($oReconcAttributes as $oAttribute)
  410. {
  411. $aReconcAttCodes[] = $oAttribute->getAttribute('id');
  412. }
  413. $sReconcKeys = "array('".implode("', '", $aReconcAttCodes)."')";
  414. }
  415. else
  416. {
  417. $sReconcKeys = "array()";
  418. }
  419. $aClassParams['reconc_keys'] = $sReconcKeys;
  420. $aClassParams['db_table'] = $this->GetPropString($oProperties, 'db_table', '');
  421. $aClassParams['db_key_field'] = $this->GetPropString($oProperties, 'db_key_field', 'id');
  422. if (array_key_exists($sClass, $this->aRootClasses))
  423. {
  424. $sDefaultFinalClass = 'finalclass';
  425. }
  426. else
  427. {
  428. $sDefaultFinalClass = '';
  429. }
  430. $aClassParams['db_finalclass_field'] = $this->GetPropString($oProperties, 'db_final_class_field', $sDefaultFinalClass);
  431. if (($sDisplayTemplate = $oProperties->GetChildText('display_template')) && (strlen($sDisplayTemplate) > 0))
  432. {
  433. $sDisplayTemplate = $sModuleRelativeDir.'/'.$sDisplayTemplate;
  434. $aClassParams['display_template'] = "utils::GetAbsoluteUrlModulesRoot().'$sDisplayTemplate'";
  435. }
  436. if (($sIcon = $oProperties->GetChildText('icon')) && (strlen($sIcon) > 0))
  437. {
  438. $sIcon = $sModuleRelativeDir.'/'.$sIcon;
  439. $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'";
  440. }
  441. $oOrder = $oProperties->GetOptionalElement('order');
  442. if ($oOrder)
  443. {
  444. $oColumnsNode = $oOrder->GetUniqueElement('columns');
  445. $oColumns = $oColumnsNode->getElementsByTagName('column');
  446. $aSortColumns = array();
  447. foreach($oColumns as $oColumn)
  448. {
  449. $aSortColumns[] = "'".$oColumn->getAttribute('id')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false');
  450. }
  451. if (count($aSortColumns) > 0)
  452. {
  453. $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")";
  454. }
  455. }
  456. // Finalize class params declaration
  457. //
  458. $aClassParamsPHP = array();
  459. foreach($aClassParams as $sKey => $sPHPValue)
  460. {
  461. $aClassParamsPHP[] = " '$sKey' => $sPHPValue,";
  462. }
  463. $sClassParams = implode("\n", $aClassParamsPHP);
  464. // Comment on top of the class declaration
  465. //
  466. $sCodeComment = $oProperties->GetChildText('comment');
  467. // Fields
  468. //
  469. $sAttributes = '';
  470. foreach($this->oFactory->ListFields($oClass) as $oField)
  471. {
  472. // $oField
  473. $sAttCode = $oField->getAttribute('id');
  474. $sAttType = $oField->getAttribute('xsi:type');
  475. $aDependencies = array();
  476. $oDependencies = $oField->GetOptionalElement('dependencies');
  477. if (!is_null($oDependencies))
  478. {
  479. $oDepNodes = $oDependencies->getElementsByTagName('attribute');
  480. foreach($oDepNodes as $oDepAttribute)
  481. {
  482. $aDependencies[] = "'".$oDepAttribute->getAttribute('id')."'";
  483. }
  484. }
  485. $sDependencies = 'array('.implode(', ', $aDependencies).')';
  486. $aParameters = array();
  487. if ($sAttType == 'AttributeLinkedSetIndirect')
  488. {
  489. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  490. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  491. $aParameters['ext_key_to_remote'] = $this->GetPropString($oField, 'ext_key_to_remote', '');
  492. // todo - utile ?
  493. $aParameters['allowed_values'] = 'null';
  494. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  495. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  496. $aParameters['duplicates'] = $this->GetPropBoolean($oField, 'duplicates', false);
  497. $aParameters['depends_on'] = $sDependencies;
  498. }
  499. elseif ($sAttType == 'AttributeLinkedSet')
  500. {
  501. $aParameters['linked_class'] = $this->GetPropString($oField, 'linked_class', '');
  502. $aParameters['ext_key_to_me'] = $this->GetPropString($oField, 'ext_key_to_me', '');
  503. // todo - utile ?
  504. $aParameters['allowed_values'] = 'null';
  505. $aParameters['count_min'] = $this->GetPropNumber($oField, 'count_min', 0);
  506. $aParameters['count_max'] = $this->GetPropNumber($oField, 'count_max', 0);
  507. $aParameters['depends_on'] = $sDependencies;
  508. }
  509. elseif ($sAttType == 'AttributeExternalKey')
  510. {
  511. $aParameters['targetclass'] = $this->GetPropString($oField, 'target_class', '');
  512. // deprecated: $aParameters['jointype'] = 'null';
  513. if ($sOql = $oField->GetChildText('filter'))
  514. {
  515. $sEscapedOql = self::QuoteForPHP($sOql);
  516. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  517. }
  518. else
  519. {
  520. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  521. }
  522. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  523. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  524. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  525. $aParameters['depends_on'] = $sDependencies;
  526. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  527. $aParameters['min_auto_complete_chars'] = $this->GetPropNumber($oField, 'min_auto_complete_chars');
  528. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  529. }
  530. elseif ($sAttType == 'AttributeHierarchicalKey')
  531. {
  532. if ($sOql = $oField->GetChildText('filter'))
  533. {
  534. $sEscapedOql = self::QuoteForPHP($sOql);
  535. $aParameters['allowed_values'] = "new ValueSetObjects($sEscapedOql)"; // or "new ValueSetObjects('SELECT xxxx')"
  536. }
  537. else
  538. {
  539. $aParameters['allowed_values'] = 'null'; // or "new ValueSetObjects('SELECT xxxx')"
  540. }
  541. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  542. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  543. $aParameters['on_target_delete'] = $oField->GetChildText('on_target_delete');
  544. $aParameters['depends_on'] = $sDependencies;
  545. $aParameters['max_combo_length'] = $this->GetPropNumber($oField, 'max_combo_length');
  546. $aParameters['min_auto_complete_chars'] = $this->GetPropNumber($oField, 'min_auto_complete_chars');
  547. $aParameters['allow_target_creation'] = $this->GetPropBoolean($oField, 'allow_target_creation');
  548. }
  549. elseif ($sAttType == 'AttributeExternalField')
  550. {
  551. $aParameters['allowed_values'] = 'null';
  552. $aParameters['extkey_attcode'] = $this->GetPropString($oField, 'extkey_attcode', '');
  553. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode', '');
  554. }
  555. elseif ($sAttType == 'AttributeURL')
  556. {
  557. $aParameters['target'] = $this->GetPropString($oField, 'target', '');
  558. $aParameters['allowed_values'] = 'null';
  559. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  560. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  561. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  562. $aParameters['depends_on'] = $sDependencies;
  563. }
  564. elseif ($sAttType == 'AttributeEnum')
  565. {
  566. $oValues = $oField->GetUniqueElement('values');
  567. $oValueNodes = $oValues->getElementsByTagName('value');
  568. $aValues = array();
  569. foreach($oValueNodes as $oValue)
  570. {
  571. // new style... $aValues[] = self::QuoteForPHP($oValue->textContent);
  572. $aValues[] = $oValue->textContent;
  573. }
  574. // new style... $sValues = 'array('.implode(', ', $aValues).')';
  575. $sValues = '"'.implode(',', $aValues).'"';
  576. $aParameters['allowed_values'] = "new ValueSetEnum($sValues)";
  577. $aParameters['display_style'] = $this->GetPropString($oField, 'display_style', 'list');
  578. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  579. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  580. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  581. $aParameters['depends_on'] = $sDependencies;
  582. }
  583. elseif ($sAttType == 'AttributeBlob')
  584. {
  585. $aParameters['depends_on'] = $sDependencies;
  586. }
  587. elseif ($sAttType == 'AttributeStopWatch')
  588. {
  589. $oStates = $oField->GetUniqueElement('states');
  590. $oStateNodes = $oStates->getElementsByTagName('state');
  591. $aStates = array();
  592. foreach($oStateNodes as $oState)
  593. {
  594. $aStates[] = '"'.$oState->GetAttribute('id').'"';
  595. }
  596. $aParameters['states'] = 'array('.implode(', ', $aStates).')';
  597. $aParameters['goal_computing'] = $this->GetPropString($oField, 'goal', 'DefaultMetricComputer'); // Optional, no deadline by default
  598. $aParameters['working_time_computing'] = $this->GetPropString($oField, 'working_time', 'DefaultWorkingTimeComputer'); // Optional, defaults to 24x7
  599. $oThresholds = $oField->GetUniqueElement('thresholds');
  600. $oThresholdNodes = $oThresholds->getElementsByTagName('threshold');
  601. $aThresholds = array();
  602. foreach($oThresholdNodes as $oThreshold)
  603. {
  604. $iPercent = $this->GetPropNumber($oThreshold, 'percent');
  605. $oActions = $oThreshold->GetUniqueElement('actions');
  606. $oActionNodes = $oActions->getElementsByTagName('action');
  607. $aActions = array();
  608. foreach($oActionNodes as $oAction)
  609. {
  610. $oParams = $oAction->GetOptionalElement('params');
  611. $aActionParams = array();
  612. if ($oParams)
  613. {
  614. $oParamNodes = $oParams->getElementsByTagName('param');
  615. foreach($oParamNodes as $oParam)
  616. {
  617. $aActionParams[] = self::QuoteForPHP($oParam->textContent);
  618. }
  619. }
  620. $sActionParams = 'array('.implode(', ', $aActionParams).')';
  621. $sVerb = $this->GetPropString($oAction, 'verb');
  622. $aActions[] = "array('verb' => $sVerb, 'params' => $sActionParams)";
  623. }
  624. $sActions = 'array('.implode(', ', $aActions).')';
  625. $aThresholds[] = $iPercent." => array('percent' => $iPercent, 'actions' => $sActions)";
  626. }
  627. $aParameters['thresholds'] = 'array('.implode(', ', $aThresholds).')';
  628. }
  629. elseif ($sAttType == 'AttributeSubItem')
  630. {
  631. $aParameters['target_attcode'] = $this->GetPropString($oField, 'target_attcode');
  632. $aParameters['item_code'] = $this->GetPropString($oField, 'item_code');
  633. }
  634. else
  635. {
  636. $aParameters['allowed_values'] = 'null'; // or "new ValueSetEnum('SELECT xxxx')"
  637. $aParameters['sql'] = $this->GetPropString($oField, 'sql', '');
  638. $aParameters['default_value'] = $this->GetPropString($oField, 'default_value', '');
  639. $aParameters['is_null_allowed'] = $this->GetPropBoolean($oField, 'is_null_allowed', false);
  640. $aParameters['depends_on'] = $sDependencies;
  641. }
  642. // Optional parameters (more for historical reasons)
  643. // Added if present...
  644. //
  645. $aParameters['validation_pattern'] = $this->GetPropString($oField, 'validation_pattern');
  646. $aParameters['width'] = $this->GetPropNumber($oField, 'width');
  647. $aParameters['height'] = $this->GetPropNumber($oField, 'height');
  648. $aParameters['digits'] = $this->GetPropNumber($oField, 'digits');
  649. $aParameters['decimals'] = $this->GetPropNumber($oField, 'decimals');
  650. $aParams = array();
  651. foreach($aParameters as $sKey => $sValue)
  652. {
  653. if (!is_null($sValue))
  654. {
  655. $aParams[] = '"'.$sKey.'"=>'.$sValue;
  656. }
  657. }
  658. $sParams = implode(', ', $aParams);
  659. $sAttributes .= " MetaModel::Init_AddAttribute(new $sAttType(\"$sAttCode\", array($sParams)));\n";
  660. }
  661. // Lifecycle
  662. //
  663. $sLifecycle = '';
  664. if ($oLifecycle)
  665. {
  666. $sLifecycle .= "\t\t// Lifecycle (status attribute: $sStateAttCode)\n";
  667. $sLifecycle .= "\t\t//\n";
  668. $oStimuli = $oLifecycle->GetUniqueElement('stimuli');
  669. foreach ($oStimuli->getElementsByTagName('stimulus') as $oStimulus)
  670. {
  671. $sStimulus = $oStimulus->getAttribute('id');
  672. $sStimulusClass = $oStimulus->getAttribute('xsi:type');
  673. $sLifecycle .= " MetaModel::Init_DefineStimulus(new ".$sStimulusClass."(\"".$sStimulus."\", array()));\n";
  674. }
  675. $oStates = $oLifecycle->GetUniqueElement('states');
  676. foreach ($oStates->getElementsByTagName('state') as $oState)
  677. {
  678. $sState = $oState->getAttribute('id');
  679. $oInitialStatePath = $oState->GetOptionalElement('initial_state_path');
  680. if ($oInitialStatePath)
  681. {
  682. $aInitialStatePath = array();
  683. foreach ($oInitialStatePath->getElementsByTagName('state_ref') as $oIntermediateState)
  684. {
  685. $aInitialStatePath[] = "'".$oIntermediateState->GetText()."'";
  686. }
  687. $sInitialStatePath = 'Array('.implode(', ', $aInitialStatePath).')';
  688. }
  689. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  690. $sLifecycle .= " \"".$sState."\",\n";
  691. $sLifecycle .= " array(\n";
  692. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  693. $sLifecycle .= " \"attribute_list\" => array(\n";
  694. $oFlags = $oState->GetUniqueElement('flags');
  695. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  696. {
  697. $sFlags = $this->FlagsToPHP($oAttributeNode);
  698. if (strlen($sFlags) > 0)
  699. {
  700. $sAttCode = $oAttributeNode->GetAttribute('id');
  701. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  702. }
  703. }
  704. $sLifecycle .= " ),\n";
  705. if (!is_null($oInitialStatePath))
  706. {
  707. $sLifecycle .= " \"initial_state_path\" => $sInitialStatePath,\n";
  708. }
  709. $sLifecycle .= " )\n";
  710. $sLifecycle .= " );\n";
  711. $oTransitions = $oState->GetUniqueElement('transitions');
  712. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  713. {
  714. $sStimulus = $oTransition->GetChildText('stimulus');
  715. $sTargetState = $oTransition->GetChildText('target');
  716. $oActions = $oTransition->GetUniqueElement('actions');
  717. $aVerbs = array();
  718. foreach ($oActions->getElementsByTagName('action') as $oAction)
  719. {
  720. $sVerb = $oAction->GetChildText('verb');
  721. $aVerbs[] = "'$sVerb'";
  722. }
  723. $sActions = implode(', ', $aVerbs);
  724. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  725. }
  726. }
  727. }
  728. // ZLists
  729. //
  730. $aListRef = array(
  731. 'details' => 'details',
  732. 'standard_search' => 'search',
  733. 'list' => 'list'
  734. );
  735. $oPresentation = $oClass->GetUniqueElement('presentation');
  736. $sZlists = '';
  737. foreach ($aListRef as $sListCode => $sListTag)
  738. {
  739. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  740. if ($oListNode)
  741. {
  742. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  743. $sZAttributes = var_export($aAttributes, true);
  744. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  745. }
  746. }
  747. // Methods
  748. $sMethods = "";
  749. $oMethods = $oClass->GetUniqueElement('methods');
  750. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  751. {
  752. $sMethodCode = $oMethod->GetChildText('code');
  753. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  754. {
  755. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  756. }
  757. else
  758. {
  759. $sMethods .= "\n\n".$sMethodCode."\n";
  760. }
  761. }
  762. // Let's make the whole class declaration
  763. //
  764. $sPHP = "\n\n$sCodeComment\n";
  765. if ($oProperties->GetChildText('abstract') == 'true')
  766. {
  767. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  768. }
  769. else
  770. {
  771. $sPHP .= 'class '.$oClass->getAttribute('id');
  772. }
  773. $sPHP .= " extends ".$oClass->GetChildText('parent', 'DBObject')."\n";
  774. $sPHP .=
  775. <<<EOF
  776. {
  777. public static function Init()
  778. {
  779. \$aParams = array
  780. (
  781. $sClassParams
  782. );
  783. MetaModel::Init_Params(\$aParams);
  784. MetaModel::Init_InheritAttributes();
  785. $sAttributes
  786. $sLifecycle
  787. $sZlists
  788. }
  789. $sMethods
  790. }
  791. EOF;
  792. return $sPHP;
  793. }// function CompileClass()
  794. protected function CompileMenu($oMenu, $sModuleRelativeDir, $oP)
  795. {
  796. $sMenuId = $oMenu->getAttribute("id");
  797. $sMenuClass = $oMenu->getAttribute("xsi:type");
  798. $sParent = $oMenu->GetChildText('parent', null);
  799. if ($sParent)
  800. {
  801. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  802. }
  803. else
  804. {
  805. $sParentSpec = '-1';
  806. }
  807. $fRank = $oMenu->GetChildText('rank');
  808. switch($sMenuClass)
  809. {
  810. case 'WebPageMenuNode':
  811. $sUrl = $oMenu->GetChildText('url');
  812. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  813. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  814. break;
  815. case 'DashboardMenuNode':
  816. $sTemplateFile = $oMenu->GetChildText('definition_file');
  817. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  818. $sNewMenu = "new DashboardMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  819. break;
  820. case 'TemplateMenuNode':
  821. $sTemplateFile = $oMenu->GetChildText('template_file');
  822. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  823. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  824. break;
  825. case 'OQLMenuNode':
  826. $sOQL = self::QuoteForPHP($oMenu->GetChildText('oql'));
  827. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  828. $sNewMenu = "new OQLMenuNode('$sMenuId', $sOQL, $sParentSpec, $fRank, $bSearch);";
  829. break;
  830. case 'NewObjectMenuNode':
  831. $sClass = $oMenu->GetChildText('class');
  832. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  833. break;
  834. case 'SearchMenuNode':
  835. $sClass = $oMenu->GetChildText('class');
  836. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  837. break;
  838. case 'MenuGroup':
  839. default:
  840. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  841. {
  842. $sEnableAction = $oMenu->GetChildText('enable_action');
  843. $sEnablePermission = $oMenu->GetChildText('enable_permission');
  844. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  845. if (strlen($sEnableStimulus) > 0)
  846. {
  847. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  848. }
  849. else
  850. {
  851. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  852. }
  853. //$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
  854. }
  855. else
  856. {
  857. $sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
  858. }
  859. }
  860. $sIndent = '';
  861. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  862. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  863. {
  864. $sAutoReload = self::QuoteForPHP($sAutoReload);
  865. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
  866. }
  867. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  868. if ($sAdminOnly && ($sAdminOnly == '1'))
  869. {
  870. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  871. $sPHP .= $sIndent."{\n";
  872. foreach($aPHPMenu as $sPHPLine)
  873. {
  874. $sPHP .= $sIndent." $sPHPLine\n";
  875. }
  876. $sPHP .= $sIndent."}\n";
  877. }
  878. else
  879. {
  880. $sPHP = '';
  881. foreach($aPHPMenu as $sPHPLine)
  882. {
  883. $sPHP .= $sIndent.$sPHPLine."\n";
  884. }
  885. }
  886. return $sPHP;
  887. } // function CompileMenu
  888. protected function CompileUserRights($oUserRightsNode)
  889. {
  890. static $aActionsInShort = array(
  891. 'read' => 'r',
  892. 'bulk read' => 'br',
  893. 'write' => 'w',
  894. 'bulk write' => 'bw',
  895. 'delete' => 'd',
  896. 'bulk delete' => 'bd',
  897. );
  898. // Groups
  899. //
  900. $aGroupClasses = array();
  901. $oGroups = $oUserRightsNode->GetUniqueElement('groups');
  902. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  903. {
  904. $sGroupId = $oGroup->getAttribute("id");
  905. $aClasses = array();
  906. $oClasses = $oGroup->GetUniqueElement('classes');
  907. foreach($oClasses->getElementsByTagName('class') as $oClass)
  908. {
  909. $sClass = $oClass->getAttribute("id");
  910. $aClasses[] = $sClass;
  911. //$bSubclasses = $this->GetPropBoolean($oClass, 'subclasses', true);
  912. //if ($bSubclasses)...
  913. }
  914. $aGroupClasses[$sGroupId] = $aClasses;
  915. }
  916. // Profiles and grants
  917. //
  918. $aProfiles = array();
  919. // Hardcode the administrator profile
  920. $aProfiles[1] = array(
  921. 'name' => 'Administrator',
  922. 'description' => 'Has the rights on everything (bypassing any control)'
  923. );
  924. $aGrants = array();
  925. $oProfiles = $oUserRightsNode->GetUniqueElement('profiles');
  926. foreach($oProfiles->getElementsByTagName('profile') as $oProfile)
  927. {
  928. $iProfile = $oProfile->getAttribute("id");
  929. $sName = $oProfile->GetChildText('name');
  930. $sDescription = $oProfile->GetChildText('description');
  931. $oGroups = $oProfile->GetUniqueElement('groups');
  932. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  933. {
  934. $sGroupId = $oGroup->getAttribute("id");
  935. $aActions = array();
  936. $oActions = $oGroup->GetUniqueElement('actions');
  937. foreach($oActions->getElementsByTagName('action') as $oAction)
  938. {
  939. $sAction = $oAction->getAttribute("id");
  940. $sType = $oAction->getAttribute("xsi:type");
  941. $sGrant = $oAction->GetText();
  942. $bGrant = ($sGrant == 'allow');
  943. if ($sGroupId == '*')
  944. {
  945. $aGrantClasses = array('*');
  946. }
  947. else
  948. {
  949. $aGrantClasses = $aGroupClasses[$sGroupId];
  950. }
  951. foreach ($aGrantClasses as $sClass)
  952. {
  953. if ($sType == 'stimulus')
  954. {
  955. $sGrantKey = $iProfile.'_'.$sClass.'_s_'.$sAction;
  956. $sGrantKeyPlus = $iProfile.'_'.$sClass.'+_s_'.$sAction; // subclasses inherit this grant
  957. }
  958. else
  959. {
  960. $sAction = $aActionsInShort[$sType];
  961. $sGrantKey = $iProfile.'_'.$sClass.'_'.$sAction;
  962. $sGrantKeyPlus = $iProfile.'_'.$sClass.'+_'.$sAction; // subclasses inherit this grant
  963. }
  964. // The class itself
  965. if (isset($aGrants[$sGrantKey]))
  966. {
  967. if (!$bGrant)
  968. {
  969. $aGrants[$sGrantKey] = false;
  970. }
  971. }
  972. else
  973. {
  974. $aGrants[$sGrantKey] = $bGrant;
  975. }
  976. // The subclasses
  977. if (isset($aGrants[$sGrantKeyPlus]))
  978. {
  979. if (!$bGrant)
  980. {
  981. $aGrants[$sGrantKeyPlus] = false;
  982. }
  983. }
  984. else
  985. {
  986. $aGrants[$sGrantKeyPlus] = $bGrant;
  987. }
  988. }
  989. }
  990. }
  991. $aProfiles[$iProfile] = array(
  992. 'name' => $sName,
  993. 'description' => $sDescription
  994. );
  995. }
  996. $sProfiles = var_export($aProfiles, true);
  997. $sGrants = var_export($aGrants, true);
  998. $sPHP =
  999. <<<EOF
  1000. //
  1001. // List of constant profiles
  1002. // - used by the class URP_Profiles at setup (create/update/delete records)
  1003. // - used by the addon UserRightsProfile to determine user rights
  1004. //
  1005. class ProfilesConfig
  1006. {
  1007. protected static \$aPROFILES = $sProfiles;
  1008. protected static \$aGRANTS = $sGrants;
  1009. public static function GetProfileActionGrant(\$iProfileId, \$sClass, \$sAction)
  1010. {
  1011. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_'.\$sAction;
  1012. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1013. {
  1014. return self::\$aGRANTS[\$sGrantKey];
  1015. }
  1016. foreach (MetaModel::EnumParentClasses(\$sClass) as \$sParent)
  1017. {
  1018. \$sGrantKey = \$iProfileId.'_'.\$sParent.'+_'.\$sAction;
  1019. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1020. {
  1021. return self::\$aGRANTS[\$sGrantKey];
  1022. }
  1023. }
  1024. \$sGrantKey = \$iProfileId.'_*_'.\$sAction;
  1025. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1026. {
  1027. return self::\$aGRANTS[\$sGrantKey];
  1028. }
  1029. return null;
  1030. }
  1031. public static function GetProfileStimulusGrant(\$iProfileId, \$sClass, \$sStimulus)
  1032. {
  1033. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_s_'.\$sStimulus;
  1034. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1035. {
  1036. return self::\$aGRANTS[\$sGrantKey];
  1037. }
  1038. \$sGrantKey = \$iProfileId.'_*_s_'.\$sStimulus;
  1039. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1040. {
  1041. return self::\$aGRANTS[\$sGrantKey];
  1042. }
  1043. return null;
  1044. }
  1045. // returns an array of id => array of column => php value(so-called "real value")
  1046. public static function GetProfilesValues()
  1047. {
  1048. return self::\$aPROFILES;
  1049. }
  1050. }
  1051. EOF;
  1052. return $sPHP;
  1053. } // function CompileUserRights
  1054. }
  1055. ?>