compiler.class.inc.php 34 KB

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