menunode.class.inc.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Construction and display of the application's main menu
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'/application/utils.inc.php');
  25. require_once(APPROOT.'/application/template.class.inc.php');
  26. require_once(APPROOT."/application/user.dashboard.class.inc.php");
  27. /**
  28. * This class manipulates, stores and displays the navigation menu used in the application
  29. * In order to improve the modularity of the data model and to ease the update/migration
  30. * between evolving data models, the menus are no longer stored in the database, but are instead
  31. * built on the fly each time a page is loaded.
  32. * The application's menu is organized into top-level groups with, inside each group, a tree of menu items.
  33. * Top level groups do not display any content, they just expand/collapse.
  34. * Sub-items drive the actual content of the page, they are based either on templates, OQL queries or full (external?) web pages.
  35. *
  36. * Example:
  37. * Here is how to insert the following items in the application's menu:
  38. * +----------------------------------------+
  39. * | Configuration Management Group | >> Top level group
  40. * +----------------------------------------+
  41. * + Configuration Management Overview >> Template based menu item
  42. * + Contacts >> Template based menu item
  43. * + Persons >> Plain list (OQL based)
  44. * + Teams >> Plain list (OQL based)
  45. *
  46. * // Create the top-level group. fRank = 1, means it will be inserted after the group '0', which is usually 'Welcome'
  47. * $oConfigMgmtMenu = new MenuGroup('ConfigurationManagementMenu', 1);
  48. * // Create an entry, based on a custom template, for the Configuration management overview, under the top-level group
  49. * new TemplateMenuNode('ConfigurationManagementMenu', '../somedirectory/configuration_management_menu.html', $oConfigMgmtMenu->GetIndex(), 0);
  50. * // Create an entry (template based) for the overview of contacts
  51. * $oContactsMenu = new TemplateMenuNode('ContactsMenu', '../somedirectory/configuration_management_menu.html',$oConfigMgmtMenu->GetIndex(), 1);
  52. * // Plain list of persons
  53. * new OQLMenuNode('PersonsMenu', 'SELECT bizPerson', $oContactsMenu->GetIndex(), 0);
  54. *
  55. */
  56. class ApplicationMenu
  57. {
  58. static $bAdditionalMenusLoaded = false;
  59. static $aRootMenus = array();
  60. static $aMenusIndex = array();
  61. static $sFavoriteSiloQuery = 'SELECT Organization';
  62. static public function LoadAdditionalMenus()
  63. {
  64. if (!self::$bAdditionalMenusLoaded)
  65. {
  66. // Build menus from module handlers
  67. //
  68. foreach(get_declared_classes() as $sPHPClass)
  69. {
  70. if (is_subclass_of($sPHPClass, 'ModuleHandlerAPI'))
  71. {
  72. $aCallSpec = array($sPHPClass, 'OnMenuCreation');
  73. call_user_func($aCallSpec);
  74. }
  75. }
  76. // Build menus from the menus themselves (e.g. the ShortcutContainerMenuNode will do that)
  77. //
  78. foreach(self::$aRootMenus as $aMenu)
  79. {
  80. $oMenuNode = self::GetMenuNode($aMenu['index']);
  81. $oMenuNode->PopulateChildMenus();
  82. }
  83. self::$bAdditionalMenusLoaded = true;
  84. }
  85. }
  86. /**
  87. * Set the query used to limit the list of displayed organizations in the drop-down menu
  88. * @param $sOQL string The OQL query returning a list of Organization objects
  89. * @return none
  90. */
  91. static public function SetFavoriteSiloQuery($sOQL)
  92. {
  93. self::$sFavoriteSiloQuery = $sOQL;
  94. }
  95. /**
  96. * Get the query used to limit the list of displayed organizations in the drop-down menu
  97. * @return string The OQL query returning a list of Organization objects
  98. */
  99. static public function GetFavoriteSiloQuery()
  100. {
  101. return self::$sFavoriteSiloQuery;
  102. }
  103. /**
  104. * Main function to add a menu entry into the application, can be called during the definition
  105. * of the data model objects
  106. */
  107. static public function InsertMenu(MenuNode $oMenuNode, $iParentIndex, $fRank)
  108. {
  109. $index = self::GetMenuIndexById($oMenuNode->GetMenuId());
  110. if ($index == -1)
  111. {
  112. // The menu does not already exist, insert it
  113. $index = count(self::$aMenusIndex);
  114. if ($iParentIndex == -1)
  115. {
  116. $sParentId = '';
  117. self::$aRootMenus[] = array ('rank' => $fRank, 'index' => $index);
  118. }
  119. else
  120. {
  121. $sParentId = self::$aMenusIndex[$iParentIndex]['node']->GetMenuId();
  122. self::$aMenusIndex[$iParentIndex]['children'][] = array ('rank' => $fRank, 'index' => $index);
  123. }
  124. // Note: At the time when 'parent', 'rank' and 'source_file' have been added for the reflection API,
  125. // they were not used to display the menus (redundant or unused)
  126. //
  127. $aBacktrace = debug_backtrace();
  128. $sFile = isset($aBacktrace[2]["file"]) ? $aBacktrace[2]["file"] : $aBacktrace[1]["file"];
  129. self::$aMenusIndex[$index] = array('node' => $oMenuNode, 'children' => array(), 'parent' => $sParentId, 'rank' => $fRank, 'source_file' => $sFile);
  130. }
  131. else
  132. {
  133. // the menu already exists, let's combine the conditions that make it visible
  134. self::$aMenusIndex[$index]['node']->AddCondition($oMenuNode);
  135. }
  136. return $index;
  137. }
  138. /**
  139. * Reflection API - Get menu entries
  140. */
  141. static public function ReflectionMenuNodes()
  142. {
  143. self::LoadAdditionalMenus();
  144. return self::$aMenusIndex;
  145. }
  146. /**
  147. * Entry point to display the whole menu into the web page, used by iTopWebPage
  148. */
  149. static public function DisplayMenu($oPage, $aExtraParams)
  150. {
  151. self::LoadAdditionalMenus();
  152. // Sort the root menu based on the rank
  153. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  154. $iAccordion = 0;
  155. $iActiveMenu = self::GetMenuIndexById(self::GetActiveNodeId());
  156. foreach(self::$aRootMenus as $aMenu)
  157. {
  158. $oMenuNode = self::GetMenuNode($aMenu['index']);
  159. if (!$oMenuNode->IsEnabled()) continue; // Don't display a non-enabled menu
  160. $oPage->AddToMenu('<h3>'.$oMenuNode->GetTitle().'</h3>');
  161. $oPage->AddToMenu('<div>');
  162. $aChildren = self::GetChildren($aMenu['index']);
  163. if (count($aChildren) > 0)
  164. {
  165. $oPage->AddToMenu('<ul>');
  166. $bActive = self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  167. $oPage->AddToMenu('</ul>');
  168. if ($bActive)
  169. {
  170. //$oPage->add_ready_script("$('#accordion').accordion('activate', $iAccordion);");
  171. // $oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true});"); // Make it auto-collapsible once it has been opened properly
  172. $oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true, active: $iAccordion});"); // Make it auto-collapsible once it has been opened properly
  173. }
  174. }
  175. $oPage->AddToMenu('</div>');
  176. $iAccordion++;
  177. }
  178. }
  179. /**
  180. * Handles the display of the sub-menus (called recursively if necessary)
  181. * @return true if the currently selected menu is one of the submenus
  182. */
  183. static protected function DisplaySubMenu($oPage, $aMenus, $aExtraParams, $iActiveMenu = -1)
  184. {
  185. // Sort the menu based on the rank
  186. $bActive = false;
  187. usort($aMenus, array('ApplicationMenu', 'CompareOnRank'));
  188. foreach($aMenus as $aMenu)
  189. {
  190. $index = $aMenu['index'];
  191. $oMenu = self::GetMenuNode($index);
  192. if ($oMenu->IsEnabled())
  193. {
  194. $aChildren = self::GetChildren($index);
  195. $sCSSClass = (count($aChildren) > 0) ? ' class="submenu"' : '';
  196. $sHyperlink = $oMenu->GetHyperlink($aExtraParams);
  197. if ($sHyperlink != '')
  198. {
  199. $oPage->AddToMenu('<li'.$sCSSClass.'><a href="'.$oMenu->GetHyperlink($aExtraParams).'">'.$oMenu->GetTitle().'</a></li>');
  200. }
  201. else
  202. {
  203. $oPage->AddToMenu('<li'.$sCSSClass.'>'.$oMenu->GetTitle().'</li>');
  204. }
  205. $aCurrentMenu = self::$aMenusIndex[$index];
  206. if ($iActiveMenu == $index)
  207. {
  208. $bActive = true;
  209. }
  210. if (count($aChildren) > 0)
  211. {
  212. $oPage->AddToMenu('<ul>');
  213. $bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  214. $oPage->AddToMenu('</ul>');
  215. }
  216. }
  217. }
  218. return $bActive;
  219. }
  220. /**
  221. * Helper function to sort the menus based on their rank
  222. */
  223. static public function CompareOnRank($a, $b)
  224. {
  225. $result = 1;
  226. if ($a['rank'] == $b['rank'])
  227. {
  228. $result = 0;
  229. }
  230. if ($a['rank'] < $b['rank'])
  231. {
  232. $result = -1;
  233. }
  234. return $result;
  235. }
  236. /**
  237. * Helper function to retrieve the MenuNodeObject based on its ID
  238. */
  239. static public function GetMenuNode($index)
  240. {
  241. return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
  242. }
  243. /**
  244. * Helper function to get the list of child(ren) of a menu
  245. */
  246. static public function GetChildren($index)
  247. {
  248. return self::$aMenusIndex[$index]['children'];
  249. }
  250. /**
  251. * Helper function to get the ID of a menu based on its name
  252. * @param string $sTitle Title of the menu (as passed when creating the menu)
  253. * @return integer ID of the menu, or -1 if not found
  254. */
  255. static public function GetMenuIndexById($sTitle)
  256. {
  257. $index = -1;
  258. foreach(self::$aMenusIndex as $aMenu)
  259. {
  260. if ($aMenu['node']->GetMenuId() == $sTitle)
  261. {
  262. $index = $aMenu['node']->GetIndex();
  263. break;
  264. }
  265. }
  266. return $index;
  267. }
  268. /**
  269. * Retrieves the currently active menu (if any, otherwise the first menu is the default)
  270. * @return string The Id of the currently active menu
  271. */
  272. static public function GetActiveNodeId()
  273. {
  274. $oAppContext = new ApplicationContext();
  275. $sMenuId = $oAppContext->GetCurrentValue('menu', null);
  276. if ($sMenuId === null)
  277. {
  278. // Make sure the root menu is sorted on 'rank'
  279. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  280. $oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
  281. $oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
  282. $sMenuId = $oMenuNode->GetMenuId();
  283. }
  284. return $sMenuId;
  285. }
  286. }
  287. /**
  288. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  289. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  290. * derived from MenuNode is enough to have it inserted in the application's main menu.
  291. * The class iTopWebPage, takes care of 3 items:
  292. * +--------------------+
  293. * | Welcome |
  294. * +--------------------+
  295. * Welcome To iTop
  296. * +--------------------+
  297. * | Tools |
  298. * +--------------------+
  299. * CSV Import
  300. * +--------------------+
  301. * | Admin Tools |
  302. * +--------------------+
  303. * User Accounts
  304. * Profiles
  305. * Notifications
  306. * Run Queries
  307. * Export
  308. * Data Model
  309. * Universal Search
  310. *
  311. * All the other menu items must constructed along with the various data model modules
  312. */
  313. abstract class MenuNode
  314. {
  315. protected $sMenuId;
  316. protected $index;
  317. /**
  318. * Properties reflecting how the node has been declared
  319. */
  320. protected $aReflectionProperties;
  321. /**
  322. * Class of objects to check if the menu is enabled, null if none
  323. */
  324. protected $m_aEnableClasses;
  325. /**
  326. * User Rights Action code to check if the menu is enabled, null if none
  327. */
  328. protected $m_aEnableActions;
  329. /**
  330. * User Rights allowed results (actually a bitmask) to check if the menu is enabled, null if none
  331. */
  332. protected $m_aEnableActionResults;
  333. /**
  334. * Stimulus to check: if the user can 'apply' this stimulus, then she/he can see this menu
  335. */
  336. protected $m_aEnableStimuli;
  337. /**
  338. * Create a menu item, sets the condition to have it displayed and inserts it into the application's main menu
  339. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  340. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  341. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  342. * @param string $sEnableClass Name of class of object
  343. * @param mixed $iActionCode UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  344. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  345. * @param string $sEnableStimulus The user can see this menu if she/he has enough rights to apply this stimulus
  346. * @return MenuNode
  347. */
  348. public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  349. {
  350. $this->sMenuId = $sMenuId;
  351. $this->aReflectionProperties = array();
  352. if (strlen($sEnableClass) > 0)
  353. {
  354. $this->aReflectionProperties['enable_class'] = $sEnableClass;
  355. $this->aReflectionProperties['enable_action'] = $iActionCode;
  356. $this->aReflectionProperties['enable_permission'] = $iAllowedResults;
  357. $this->aReflectionProperties['enable_stimulus'] = $sEnableStimulus;
  358. }
  359. $this->m_aEnableClasses = array($sEnableClass);
  360. $this->m_aEnableActions = array($iActionCode);
  361. $this->m_aEnableActionResults = array($iAllowedResults);
  362. $this->m_aEnableStimuli = array($sEnableStimulus);
  363. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  364. }
  365. public function ReflectionProperties()
  366. {
  367. return $this->aReflectionProperties;
  368. }
  369. public function GetMenuId()
  370. {
  371. return $this->sMenuId;
  372. }
  373. public function GetTitle()
  374. {
  375. return Dict::S("Menu:$this->sMenuId", str_replace('_', ' ', $this->sMenuId));
  376. }
  377. public function GetLabel()
  378. {
  379. return Dict::S("Menu:$this->sMenuId+", "");
  380. }
  381. public function GetIndex()
  382. {
  383. return $this->index;
  384. }
  385. public function PopulateChildMenus()
  386. {
  387. foreach (ApplicationMenu::GetChildren($this->GetIndex()) as $aMenu)
  388. {
  389. $index = $aMenu['index'];
  390. $oMenu = ApplicationMenu::GetMenuNode($index);
  391. $oMenu->PopulateChildMenus();
  392. }
  393. }
  394. public function GetHyperlink($aExtraParams)
  395. {
  396. $aExtraParams['c[menu]'] = $this->GetMenuId();
  397. return $this->AddParams(utils::GetAbsoluteUrlAppRoot().'pages/UI.php', $aExtraParams);
  398. }
  399. /**
  400. * Add a limiting display condition for the same menu node. The conditions will be combined with a AND
  401. * @param $oMenuNode MenuNode Another definition of the same menu node, with potentially different access restriction
  402. * @return void
  403. */
  404. public function AddCondition(MenuNode $oMenuNode)
  405. {
  406. foreach($oMenuNode->m_aEnableClasses as $index => $sClass )
  407. {
  408. $this->m_aEnableClasses[] = $sClass;
  409. $this->m_aEnableActions[] = $oMenuNode->m_aEnableActions[$index];
  410. $this->m_aEnableActionResults[] = $oMenuNode->m_aEnableActionResults[$index];
  411. $this->m_aEnableStimuli[] = $oMenuNode->m_aEnableStimuli[$index];
  412. }
  413. }
  414. /**
  415. * Tells whether the menu is enabled (i.e. displayed) for the current user
  416. * @return bool True if enabled, false otherwise
  417. */
  418. public function IsEnabled()
  419. {
  420. foreach($this->m_aEnableClasses as $index => $sClass)
  421. {
  422. if ($sClass != null)
  423. {
  424. if (MetaModel::IsValidClass($sClass))
  425. {
  426. if ($this->m_aEnableStimuli[$index] != null)
  427. {
  428. if (!UserRights::IsStimulusAllowed($sClass, $this->m_aEnableStimuli[$index]))
  429. {
  430. return false;
  431. }
  432. }
  433. if ($this->m_aEnableActions[$index] != null)
  434. {
  435. $iResult = UserRights::IsActionAllowed($sClass, $this->m_aEnableActions[$index]);
  436. if (!($iResult & $this->m_aEnableActionResults[$index]))
  437. {
  438. return false;
  439. }
  440. }
  441. }
  442. else
  443. {
  444. return false;
  445. }
  446. }
  447. }
  448. return true;
  449. }
  450. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  451. protected function AddParams($sHyperlink, $aExtraParams)
  452. {
  453. if (count($aExtraParams) > 0)
  454. {
  455. $aQuery = array();
  456. $sSeparator = '?';
  457. if (strpos($sHyperlink, '?') !== false)
  458. {
  459. $sSeparator = '&';
  460. }
  461. foreach($aExtraParams as $sName => $sValue)
  462. {
  463. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  464. }
  465. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  466. }
  467. return $sHyperlink;
  468. }
  469. }
  470. /**
  471. * This class implements a top-level menu group. A group is just a container for sub-items
  472. * it does not display a page by itself
  473. */
  474. class MenuGroup extends MenuNode
  475. {
  476. /**
  477. * Create a top-level menu group and inserts it into the application's main menu
  478. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  479. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  480. * @param string $sEnableClass Name of class of object
  481. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  482. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  483. * @return MenuGroup
  484. */
  485. public function __construct($sMenuId, $fRank, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  486. {
  487. parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  488. }
  489. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  490. {
  491. assert(false); // Shall never be called, groups do not display any content
  492. }
  493. }
  494. /**
  495. * This class defines a menu item which content is based on a custom template.
  496. * Note the template can be either a local file or an URL !
  497. */
  498. class TemplateMenuNode extends MenuNode
  499. {
  500. protected $sTemplateFile;
  501. /**
  502. * Create a menu item based on a custom template and inserts it into the application's main menu
  503. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  504. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  505. * @param integer $iParentIndex ID of the parent menu
  506. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  507. * @param string $sEnableClass Name of class of object
  508. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  509. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  510. * @return MenuNode
  511. */
  512. public function __construct($sMenuId, $sTemplateFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  513. {
  514. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  515. $this->sTemplateFile = $sTemplateFile;
  516. $this->aReflectionProperties['template_file'] = $sTemplateFile;
  517. }
  518. public function GetHyperlink($aExtraParams)
  519. {
  520. if ($this->sTemplateFile == '') return '';
  521. return parent::GetHyperlink($aExtraParams);
  522. }
  523. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  524. {
  525. $sTemplate = @file_get_contents($this->sTemplateFile);
  526. if ($sTemplate !== false)
  527. {
  528. $aExtraParams['table_id'] = 'Menu_'.$this->GetMenuId();
  529. $oTemplate = new DisplayTemplate($sTemplate);
  530. $oTemplate->Render($oPage, $aExtraParams);
  531. }
  532. else
  533. {
  534. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  535. }
  536. }
  537. }
  538. /**
  539. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  540. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  541. */
  542. class OQLMenuNode extends MenuNode
  543. {
  544. protected $sPageTitle;
  545. protected $sOQL;
  546. protected $bSearch;
  547. /**
  548. * Extra parameters to be passed to the display block to fine tune its appearence
  549. */
  550. protected $m_aParams;
  551. /**
  552. * Create a menu item based on an OQL query and inserts it into the application's main menu
  553. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  554. * @param string $sOQL OQL query defining the set of objects to be displayed
  555. * @param integer $iParentIndex ID of the parent menu
  556. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  557. * @param bool $bSearch Whether or not to display a (collapsed) search frame at the top of the page
  558. * @param string $sEnableClass Name of class of object
  559. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  560. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  561. * @return MenuNode
  562. */
  563. public function __construct($sMenuId, $sOQL, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  564. {
  565. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  566. $this->sPageTitle = "Menu:$sMenuId+";
  567. $this->sOQL = $sOQL;
  568. $this->bSearch = $bSearch;
  569. $this->m_aParams = array();
  570. $this->aReflectionProperties['oql'] = $sOQL;
  571. $this->aReflectionProperties['do_search'] = $bSearch;
  572. // Enhancement: we could set as the "enable" condition that the user has enough rights to "read" the objects
  573. // of the class specified by the OQL...
  574. }
  575. /**
  576. * Set some extra parameters to be passed to the display block to fine tune its appearence
  577. * @param Hash $aParams paramCode => value. See DisplayBlock::GetDisplay for the meaning of the parameters
  578. */
  579. public function SetParameters($aParams)
  580. {
  581. $this->m_aParams = $aParams;
  582. foreach($aParams as $sKey => $value)
  583. {
  584. $this->aReflectionProperties[$sKey] = $value;
  585. }
  586. }
  587. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  588. {
  589. OQLMenuNode::RenderOQLSearch
  590. (
  591. $this->sOQL,
  592. Dict::S($this->sPageTitle),
  593. 'Menu_'.$this->GetMenuId(),
  594. $this->bSearch, // Search pane
  595. true, // Search open
  596. $oPage,
  597. array_merge($this->m_aParams, $aExtraParams)
  598. );
  599. }
  600. public static function RenderOQLSearch($sOql, $sTitle, $sUsageId, $bSearchPane, $bSearchOpen, WebPage $oPage, $aExtraParams = array())
  601. {
  602. $sUsageId = utils::GetSafeId($sUsageId);
  603. $oSearch = DBObjectSearch::FromOQL($sOql);
  604. $sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
  605. if ($bSearchPane)
  606. {
  607. $aParams = array_merge(array('open' => $bSearchOpen, 'table_id' => $sUsageId), $aExtraParams);
  608. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  609. $oBlock->Display($oPage, 0);
  610. }
  611. $oPage->add("<p class=\"page-header\">$sIcon ".Dict::S($sTitle)."</p>");
  612. $aParams = array_merge(array('table_id' => $sUsageId), $aExtraParams);
  613. $oBlock = new DisplayBlock($oSearch, 'list', false /* Asynchronous */, $aParams);
  614. $oBlock->Display($oPage, $sUsageId);
  615. }
  616. }
  617. /**
  618. * This class defines a menu item that displays a search form for the given class of objects
  619. */
  620. class SearchMenuNode extends MenuNode
  621. {
  622. protected $sPageTitle;
  623. protected $sClass;
  624. /**
  625. * Create a menu item based on an OQL query and inserts it into the application's main menu
  626. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  627. * @param string $sClass The class of objects to search for
  628. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  629. * @param integer $iParentIndex ID of the parent menu
  630. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  631. * @param string $sEnableClass Name of class of object
  632. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  633. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  634. * @return MenuNode
  635. */
  636. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  637. {
  638. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  639. $this->sPageTitle = "Menu:$sMenuId+";
  640. $this->sClass = $sClass;
  641. $this->aReflectionProperties['class'] = $sClass;
  642. }
  643. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  644. {
  645. $oSearch = new DBObjectSearch($this->sClass);
  646. $aParams = array_merge(array('open' => true, 'table_id' => 'Menu_'.utils::GetSafeId($this->GetMenuId())), $aExtraParams);
  647. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  648. $oBlock->Display($oPage, 0);
  649. }
  650. }
  651. /**
  652. * This class defines a menu that points to any web page. It takes only two parameters:
  653. * - The hyperlink to point to
  654. * - The name of the menu
  655. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  656. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  657. */
  658. class WebPageMenuNode extends MenuNode
  659. {
  660. protected $sHyperlink;
  661. /**
  662. * Create a menu item that points to any web page (not only UI.php)
  663. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  664. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  665. * @param integer $iParentIndex ID of the parent menu
  666. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  667. * @param string $sEnableClass Name of class of object
  668. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  669. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  670. * @return MenuNode
  671. */
  672. public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  673. {
  674. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  675. $this->sHyperlink = $sHyperlink;
  676. $this->aReflectionProperties['url'] = $sHyperlink;
  677. }
  678. public function GetHyperlink($aExtraParams)
  679. {
  680. $aExtraParams['c[menu]'] = $this->GetMenuId();
  681. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  682. }
  683. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  684. {
  685. assert(false); // Shall never be called, the external web page will handle the display by itself
  686. }
  687. }
  688. /**
  689. * This class defines a menu that points to the page for creating a new object of the specified class.
  690. * It take only one parameter: the name of the class
  691. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  692. * in order to make it the active one
  693. */
  694. class NewObjectMenuNode extends MenuNode
  695. {
  696. protected $sClass;
  697. /**
  698. * Create a menu item that points to the URL for creating a new object, the menu will be added only if the current user has enough
  699. * rights to create such an object (or an object of a child class)
  700. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  701. * @param string $sClass URL to the page to load. Use relative URL if you want to keep the application portable !
  702. * @param integer $iParentIndex ID of the parent menu
  703. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  704. * @return MenuNode
  705. */
  706. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0)
  707. {
  708. parent::__construct($sMenuId, $iParentIndex, $fRank);
  709. $this->sClass = $sClass;
  710. $this->aReflectionProperties['class'] = $sClass;
  711. }
  712. public function GetHyperlink($aExtraParams)
  713. {
  714. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=new&class='.$this->sClass;
  715. $aExtraParams['c[menu]'] = $this->GetMenuId();
  716. return $this->AddParams($sHyperlink, $aExtraParams);
  717. }
  718. /**
  719. * Overload the check of the "enable" state of this menu to take into account
  720. * derived classes of objects
  721. */
  722. public function IsEnabled()
  723. {
  724. // Enable this menu, only if the current user has enough rights to create such an object, or an object of
  725. // any child class
  726. $aSubClasses = MetaModel::EnumChildClasses($this->sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  727. $bActionIsAllowed = false;
  728. foreach($aSubClasses as $sCandidateClass)
  729. {
  730. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  731. {
  732. $bActionIsAllowed = true;
  733. break; // Enough for now
  734. }
  735. }
  736. return $bActionIsAllowed;
  737. }
  738. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  739. {
  740. assert(false); // Shall never be called, the external web page will handle the display by itself
  741. }
  742. }
  743. require_once(APPROOT.'application/dashboard.class.inc.php');
  744. /**
  745. * This class defines a menu item which content is based on XML dashboard.
  746. */
  747. class DashboardMenuNode extends MenuNode
  748. {
  749. protected $sDashboardFile;
  750. /**
  751. * Create a menu item based on a custom template and inserts it into the application's main menu
  752. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  753. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  754. * @param integer $iParentIndex ID of the parent menu
  755. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  756. * @param string $sEnableClass Name of class of object
  757. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  758. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  759. * @return MenuNode
  760. */
  761. public function __construct($sMenuId, $sDashboardFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  762. {
  763. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  764. $this->sDashboardFile = $sDashboardFile;
  765. $this->aReflectionProperties['definition_file'] = $sDashboardFile;
  766. }
  767. public function GetHyperlink($aExtraParams)
  768. {
  769. if ($this->sDashboardFile == '') return '';
  770. return parent::GetHyperlink($aExtraParams);
  771. }
  772. public function GetDashboard()
  773. {
  774. $sDashboardDefinition = @file_get_contents($this->sDashboardFile);
  775. if ($sDashboardDefinition !== false)
  776. {
  777. $bCustomized = false;
  778. // Search for an eventual user defined dashboard, overloading the existing one
  779. $oUDSearch = new DBObjectSearch('UserDashboard');
  780. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  781. $oUDSearch->AddCondition('menu_code', $this->sMenuId, '=');
  782. $oUDSet = new DBObjectSet($oUDSearch);
  783. if ($oUDSet->Count() > 0)
  784. {
  785. // Assuming there is at most one couple {user, menu}!
  786. $oUserDashboard = $oUDSet->Fetch();
  787. $sDashboardDefinition = $oUserDashboard->Get('contents');
  788. $bCustomized = true;
  789. }
  790. $oDashboard = new RuntimeDashboard($this->sMenuId);
  791. $oDashboard->FromXml($sDashboardDefinition);
  792. $oDashboard->SetCustomFlag($bCustomized);
  793. }
  794. else
  795. {
  796. $oDashboard = null;
  797. }
  798. return $oDashboard;
  799. }
  800. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  801. {
  802. $oDashboard = $this->GetDashboard();
  803. if ($oDashboard != null)
  804. {
  805. $oDashboard->Render($oPage, false, $aExtraParams);
  806. $bEdit = utils::ReadParam('edit', false);
  807. if ($bEdit)
  808. {
  809. $sId = addslashes($this->sMenuId);
  810. $oPage->add_ready_script("EditDashboard('$sId');");
  811. }
  812. }
  813. else
  814. {
  815. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  816. }
  817. }
  818. public function RenderEditor(WebPage $oPage)
  819. {
  820. $oDashboard = $this->GetDashboard();
  821. if ($oDashboard != null)
  822. {
  823. $oDashboard->RenderEditor($oPage);
  824. }
  825. else
  826. {
  827. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  828. }
  829. }
  830. public function AddDashlet($oDashlet)
  831. {
  832. $oDashboard = $this->GetDashboard();
  833. if ($oDashboard != null)
  834. {
  835. $oDashboard->AddDashlet($oDashlet);
  836. $oDashboard->Save();
  837. }
  838. else
  839. {
  840. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  841. }
  842. }
  843. }
  844. /**
  845. * A shortcut container is the preferred destination of newly created shortcuts
  846. */
  847. class ShortcutContainerMenuNode extends MenuNode
  848. {
  849. public function GetHyperlink($aExtraParams)
  850. {
  851. return '';
  852. }
  853. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  854. {
  855. }
  856. public function PopulateChildMenus()
  857. {
  858. // Load user shortcuts in DB
  859. //
  860. $oBMSearch = new DBObjectSearch('Shortcut');
  861. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  862. $oBMSet = new DBObjectSet($oBMSearch, array('friendlyname' => true)); // ascending on friendlyname
  863. $fRank = 1;
  864. while ($oShortcut = $oBMSet->Fetch())
  865. {
  866. $sName = $this->GetMenuId().'_'.$oShortcut->GetKey();
  867. $oShortcutMenu = new ShortcutMenuNode($sName, $oShortcut, $this->GetIndex(), $fRank++);
  868. }
  869. // Complete the tree
  870. //
  871. parent::PopulateChildMenus();
  872. }
  873. }
  874. require_once(APPROOT.'application/shortcut.class.inc.php');
  875. /**
  876. * This class defines a menu item which content is a shortcut.
  877. */
  878. class ShortcutMenuNode extends MenuNode
  879. {
  880. protected $oShortcut;
  881. /**
  882. * Create a menu item based on a custom template and inserts it into the application's main menu
  883. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  884. * @param object $oShortcut Shortcut object
  885. * @param integer $iParentIndex ID of the parent menu
  886. * @param float $fRank Number used to order the list, any number will do, but for a given level (i.e same parent) all menus are sorted based on this value
  887. * @param string $sEnableClass Name of class of object
  888. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  889. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  890. * @return MenuNode
  891. */
  892. public function __construct($sMenuId, $oShortcut, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  893. {
  894. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  895. $this->oShortcut = $oShortcut;
  896. $this->aReflectionProperties['shortcut'] = $oShortcut->GetKey();
  897. }
  898. public function GetHyperlink($aExtraParams)
  899. {
  900. $sContext = $this->oShortcut->Get('context');
  901. $aContext = unserialize($sContext);
  902. if (isset($aContext['menu']))
  903. {
  904. unset($aContext['menu']);
  905. }
  906. foreach ($aContext as $sArgName => $sArgValue)
  907. {
  908. $aExtraParams[$sArgName] = $sArgValue;
  909. }
  910. return parent::GetHyperlink($aExtraParams);
  911. }
  912. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  913. {
  914. $this->oShortcut->RenderContent($oPage, $aExtraParams);
  915. }
  916. public function GetTitle()
  917. {
  918. return $this->oShortcut->Get('name');
  919. }
  920. public function GetLabel()
  921. {
  922. return $this->oShortcut->Get('name');
  923. }
  924. }