compiler.class.inc.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. $sLifecycle .= " MetaModel::Init_DefineState(\n";
  680. $sLifecycle .= " \"".$sState."\",\n";
  681. $sLifecycle .= " array(\n";
  682. $sLifecycle .= " \"attribute_inherit\" => '',\n";
  683. $sLifecycle .= " \"attribute_list\" => array(\n";
  684. $oFlags = $oState->GetUniqueElement('flags');
  685. foreach ($oFlags->getElementsByTagName('attribute') as $oAttributeNode)
  686. {
  687. $sFlags = $this->FlagsToPHP($oAttributeNode);
  688. if (strlen($sFlags) > 0)
  689. {
  690. $sAttCode = $oAttributeNode->GetAttribute('id');
  691. $sLifecycle .= " '$sAttCode' => $sFlags,\n";
  692. }
  693. }
  694. $sLifecycle .= " ),\n";
  695. $sLifecycle .= " )\n";
  696. $sLifecycle .= " );\n";
  697. $oTransitions = $oState->GetUniqueElement('transitions');
  698. foreach ($oTransitions->getElementsByTagName('transition') as $oTransition)
  699. {
  700. $sStimulus = $oTransition->GetChildText('stimulus');
  701. $sTargetState = $oTransition->GetChildText('target');
  702. $oActions = $oTransition->GetUniqueElement('actions');
  703. $aVerbs = array();
  704. foreach ($oActions->getElementsByTagName('action') as $oAction)
  705. {
  706. $sVerb = $oAction->GetChildText('verb');
  707. $aVerbs[] = "'$sVerb'";
  708. }
  709. $sActions = implode(', ', $aVerbs);
  710. $sLifecycle .= " MetaModel::Init_DefineTransition(\"$sState\", \"$sStimulus\", array(\"target_state\"=>\"$sTargetState\", \"actions\"=>array($sActions), \"user_restriction\"=>null));\n";
  711. }
  712. }
  713. }
  714. // ZLists
  715. //
  716. $aListRef = array(
  717. 'details' => 'details',
  718. 'standard_search' => 'search',
  719. 'list' => 'list'
  720. );
  721. $oPresentation = $oClass->GetUniqueElement('presentation');
  722. $sZlists = '';
  723. foreach ($aListRef as $sListCode => $sListTag)
  724. {
  725. $oListNode = $oPresentation->GetOptionalElement($sListTag);
  726. if ($oListNode)
  727. {
  728. $aAttributes = $oListNode->GetNodeAsArrayOfItems();
  729. $sZAttributes = var_export($aAttributes, true);
  730. $sZlists .= " MetaModel::Init_SetZListItems('$sListCode', $sZAttributes);\n";
  731. }
  732. }
  733. // Methods
  734. $sMethods = "";
  735. $oMethods = $oClass->GetUniqueElement('methods');
  736. foreach($oMethods->getElementsByTagName('method') as $oMethod)
  737. {
  738. $sMethodCode = $oMethod->GetChildText('code');
  739. if ($sMethodComment = $oMethod->GetChildText('comment', null))
  740. {
  741. $sMethods .= "\n\t$sMethodComment\n".$sMethodCode."\n";
  742. }
  743. else
  744. {
  745. $sMethods .= "\n\n".$sMethodCode."\n";
  746. }
  747. }
  748. // Let's make the whole class declaration
  749. //
  750. $sPHP = "\n\n$sCodeComment\n";
  751. if ($oProperties->GetChildText('abstract') == 'true')
  752. {
  753. $sPHP .= 'abstract class '.$oClass->getAttribute('id');
  754. }
  755. else
  756. {
  757. $sPHP .= 'class '.$oClass->getAttribute('id');
  758. }
  759. $sPHP .= " extends ".$oClass->GetChildText('parent', 'DBObject')."\n";
  760. $sPHP .=
  761. <<<EOF
  762. {
  763. public static function Init()
  764. {
  765. \$aParams = array
  766. (
  767. $sClassParams
  768. );
  769. MetaModel::Init_Params(\$aParams);
  770. MetaModel::Init_InheritAttributes();
  771. $sAttributes
  772. $sLifecycle
  773. $sZlists
  774. }
  775. $sMethods
  776. }
  777. EOF;
  778. return $sPHP;
  779. }// function CompileClass()
  780. protected function CompileMenu($oMenu, $sModuleRelativeDir, $oP)
  781. {
  782. $sMenuId = $oMenu->getAttribute("id");
  783. $sMenuClass = $oMenu->getAttribute("xsi:type");
  784. $sParent = $oMenu->GetChildText('parent', null);
  785. if ($sParent)
  786. {
  787. $sParentSpec = "\$__comp_menus__['$sParent']->GetIndex()";
  788. }
  789. else
  790. {
  791. $sParentSpec = '-1';
  792. }
  793. $fRank = $oMenu->GetChildText('rank');
  794. switch($sMenuClass)
  795. {
  796. case 'WebPageMenuNode':
  797. $sUrl = $oMenu->GetChildText('url');
  798. $sUrlSpec = $this->PathToPHP($sUrl, $sModuleRelativeDir, true /* Url */);
  799. $sNewMenu = "new WebPageMenuNode('$sMenuId', $sUrlSpec, $sParentSpec, $fRank);";
  800. break;
  801. case 'DashboardMenuNode':
  802. $sTemplateFile = $oMenu->GetChildText('definition_file');
  803. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  804. $sNewMenu = "new DashboardMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  805. break;
  806. case 'TemplateMenuNode':
  807. $sTemplateFile = $oMenu->GetChildText('template_file');
  808. $sTemplateSpec = $this->PathToPHP($sTemplateFile, $sModuleRelativeDir);
  809. $sNewMenu = "new TemplateMenuNode('$sMenuId', $sTemplateSpec, $sParentSpec, $fRank);";
  810. break;
  811. case 'OQLMenuNode':
  812. $sOQL = self::QuoteForPHP($oMenu->GetChildText('oql'));
  813. $bSearch = ($oMenu->GetChildText('do_search') == '1') ? 'true' : 'false';
  814. $sNewMenu = "new OQLMenuNode('$sMenuId', $sOQL, $sParentSpec, $fRank, $bSearch);";
  815. break;
  816. case 'NewObjectMenuNode':
  817. $sClass = $oMenu->GetChildText('class');
  818. $sNewMenu = "new NewObjectMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  819. break;
  820. case 'SearchMenuNode':
  821. $sClass = $oMenu->GetChildText('class');
  822. $sNewMenu = "new SearchMenuNode('$sMenuId', '$sClass', $sParentSpec, $fRank);";
  823. break;
  824. case 'MenuGroup':
  825. default:
  826. if ($sEnableClass = $oMenu->GetChildText('enable_class'))
  827. {
  828. $sEnableAction = $oMenu->GetChildText('enable_action');
  829. $sEnablePermission = $oMenu->GetChildText('enable_permission');
  830. $sEnableStimulus = $oMenu->GetChildText('enable_stimulus');
  831. if (strlen($sEnableStimulus) > 0)
  832. {
  833. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission, '$sEnableStimulus');";
  834. }
  835. else
  836. {
  837. $sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', $sEnableAction, $sEnablePermission);";
  838. }
  839. //$sNewMenu = "new MenuGroup('$sMenuId', $fRank, '$sEnableClass', UR_ACTION_MODIFY, UR_ALLOWED_YES|UR_ALLOWED_DEPENDS);";
  840. }
  841. else
  842. {
  843. $sNewMenu = "new MenuGroup('$sMenuId', $fRank);";
  844. }
  845. }
  846. $sIndent = '';
  847. $aPHPMenu = array("\$__comp_menus__['$sMenuId'] = $sNewMenu");
  848. if ($sAutoReload = $oMenu->GetChildText('auto_reload'))
  849. {
  850. $sAutoReload = self::QuoteForPHP($sAutoReload);
  851. $aPHPMenu[] = "\$__comp_menus__['$sMenuId']->SetParameters(array('auto_reload' => $sAutoReload));";
  852. }
  853. $sAdminOnly = $oMenu->GetChildText('enable_admin_only');
  854. if ($sAdminOnly && ($sAdminOnly == '1'))
  855. {
  856. $sPHP = $sIndent."if (UserRights::IsAdministrator())\n";
  857. $sPHP .= $sIndent."{\n";
  858. foreach($aPHPMenu as $sPHPLine)
  859. {
  860. $sPHP .= $sIndent." $sPHPLine\n";
  861. }
  862. $sPHP .= $sIndent."}\n";
  863. }
  864. else
  865. {
  866. $sPHP = '';
  867. foreach($aPHPMenu as $sPHPLine)
  868. {
  869. $sPHP .= $sIndent.$sPHPLine."\n";
  870. }
  871. }
  872. return $sPHP;
  873. } // function CompileMenu
  874. protected function CompileUserRights($oUserRightsNode)
  875. {
  876. static $aActionsInShort = array(
  877. 'read' => 'r',
  878. 'bulk read' => 'br',
  879. 'write' => 'w',
  880. 'bulk write' => 'bw',
  881. 'delete' => 'd',
  882. 'bulk delete' => 'bd',
  883. );
  884. // Groups
  885. //
  886. $aGroupClasses = array();
  887. $oGroups = $oUserRightsNode->GetUniqueElement('groups');
  888. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  889. {
  890. $sGroupId = $oGroup->getAttribute("id");
  891. $aClasses = array();
  892. $oClasses = $oGroup->GetUniqueElement('classes');
  893. foreach($oClasses->getElementsByTagName('class') as $oClass)
  894. {
  895. $sClass = $oClass->getAttribute("id");
  896. $aClasses[] = $sClass;
  897. //$bSubclasses = $this->GetPropBoolean($oClass, 'subclasses', true);
  898. //if ($bSubclasses)...
  899. }
  900. $aGroupClasses[$sGroupId] = $aClasses;
  901. }
  902. // Profiles and grants
  903. //
  904. $aProfiles = array();
  905. // Hardcode the administrator profile
  906. $aProfiles[1] = array(
  907. 'name' => 'Administrator',
  908. 'description' => 'Has the rights on everything (bypassing any control)'
  909. );
  910. $aGrants = array();
  911. $oProfiles = $oUserRightsNode->GetUniqueElement('profiles');
  912. foreach($oProfiles->getElementsByTagName('profile') as $oProfile)
  913. {
  914. $iProfile = $oProfile->getAttribute("id");
  915. $sName = $oProfile->GetChildText('name');
  916. $sDescription = $oProfile->GetChildText('description');
  917. $oGroups = $oProfile->GetUniqueElement('groups');
  918. foreach($oGroups->getElementsByTagName('group') as $oGroup)
  919. {
  920. $sGroupId = $oGroup->getAttribute("id");
  921. $aActions = array();
  922. $oActions = $oGroup->GetUniqueElement('actions');
  923. foreach($oActions->getElementsByTagName('action') as $oAction)
  924. {
  925. $sAction = $oAction->getAttribute("id");
  926. $sType = $oAction->getAttribute("xsi:type");
  927. $sGrant = $oAction->GetText();
  928. $bGrant = ($sGrant == 'allow');
  929. if ($sGroupId == '*')
  930. {
  931. $aGrantClasses = array('*');
  932. }
  933. else
  934. {
  935. $aGrantClasses = $aGroupClasses[$sGroupId];
  936. }
  937. foreach ($aGrantClasses as $sClass)
  938. {
  939. if ($sType == 'stimulus')
  940. {
  941. $sGrantKey = $iProfile.'_'.$sClass.'_s_'.$sAction;
  942. $sGrantKeyPlus = $iProfile.'_'.$sClass.'+_s_'.$sAction; // subclasses inherit this grant
  943. }
  944. else
  945. {
  946. $sAction = $aActionsInShort[$sType];
  947. $sGrantKey = $iProfile.'_'.$sClass.'_'.$sAction;
  948. $sGrantKeyPlus = $iProfile.'_'.$sClass.'+_'.$sAction; // subclasses inherit this grant
  949. }
  950. // The class itself
  951. if (isset($aGrants[$sGrantKey]))
  952. {
  953. if (!$bGrant)
  954. {
  955. $aGrants[$sGrantKey] = false;
  956. }
  957. }
  958. else
  959. {
  960. $aGrants[$sGrantKey] = $bGrant;
  961. }
  962. // The subclasses
  963. if (isset($aGrants[$sGrantKeyPlus]))
  964. {
  965. if (!$bGrant)
  966. {
  967. $aGrants[$sGrantKeyPlus] = false;
  968. }
  969. }
  970. else
  971. {
  972. $aGrants[$sGrantKeyPlus] = $bGrant;
  973. }
  974. }
  975. }
  976. }
  977. $aProfiles[$iProfile] = array(
  978. 'name' => $sName,
  979. 'description' => $sDescription
  980. );
  981. }
  982. $sProfiles = var_export($aProfiles, true);
  983. $sGrants = var_export($aGrants, true);
  984. $sPHP =
  985. <<<EOF
  986. //
  987. // List of constant profiles
  988. // - used by the class URP_Profiles at setup (create/update/delete records)
  989. // - used by the addon UserRightsProfile to determine user rights
  990. //
  991. class ProfilesConfig
  992. {
  993. protected static \$aPROFILES = $sProfiles;
  994. protected static \$aGRANTS = $sGrants;
  995. public static function GetProfileActionGrant(\$iProfileId, \$sClass, \$sAction)
  996. {
  997. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_'.\$sAction;
  998. if (isset(self::\$aGRANTS[\$sGrantKey]))
  999. {
  1000. return self::\$aGRANTS[\$sGrantKey];
  1001. }
  1002. foreach (MetaModel::EnumParentClasses(\$sClass) as \$sParent)
  1003. {
  1004. \$sGrantKey = \$iProfileId.'_'.\$sParent.'+_'.\$sAction;
  1005. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1006. {
  1007. return self::\$aGRANTS[\$sGrantKey];
  1008. }
  1009. }
  1010. \$sGrantKey = \$iProfileId.'_*_'.\$sAction;
  1011. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1012. {
  1013. return self::\$aGRANTS[\$sGrantKey];
  1014. }
  1015. return null;
  1016. }
  1017. public static function GetProfileStimulusGrant(\$iProfileId, \$sClass, \$sStimulus)
  1018. {
  1019. \$sGrantKey = \$iProfileId.'_'.\$sClass.'_s_'.\$sStimulus;
  1020. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1021. {
  1022. return self::\$aGRANTS[\$sGrantKey];
  1023. }
  1024. \$sGrantKey = \$iProfileId.'_*_s_'.\$sStimulus;
  1025. if (isset(self::\$aGRANTS[\$sGrantKey]))
  1026. {
  1027. return self::\$aGRANTS[\$sGrantKey];
  1028. }
  1029. return null;
  1030. }
  1031. // returns an array of id => array of column => php value(so-called "real value")
  1032. public static function GetProfilesValues()
  1033. {
  1034. return self::\$aPROFILES;
  1035. }
  1036. }
  1037. EOF;
  1038. return $sPHP;
  1039. } // function CompileUserRights
  1040. }
  1041. ?>