menunode.class.inc.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. <?php
  2. // Copyright (C) 2010-2013 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. $aChildren = self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'];
  282. usort($aChildren, array('ApplicationMenu', 'CompareOnRank'));
  283. $oMenuNode = self::GetMenuNode($aChildren[0]['index']);
  284. $sMenuId = $oMenuNode->GetMenuId();
  285. }
  286. return $sMenuId;
  287. }
  288. }
  289. /**
  290. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  291. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  292. * derived from MenuNode is enough to have it inserted in the application's main menu.
  293. * The class iTopWebPage, takes care of 3 items:
  294. * +--------------------+
  295. * | Welcome |
  296. * +--------------------+
  297. * Welcome To iTop
  298. * +--------------------+
  299. * | Tools |
  300. * +--------------------+
  301. * CSV Import
  302. * +--------------------+
  303. * | Admin Tools |
  304. * +--------------------+
  305. * User Accounts
  306. * Profiles
  307. * Notifications
  308. * Run Queries
  309. * Export
  310. * Data Model
  311. * Universal Search
  312. *
  313. * All the other menu items must constructed along with the various data model modules
  314. */
  315. abstract class MenuNode
  316. {
  317. protected $sMenuId;
  318. protected $index;
  319. /**
  320. * Properties reflecting how the node has been declared
  321. */
  322. protected $aReflectionProperties;
  323. /**
  324. * Class of objects to check if the menu is enabled, null if none
  325. */
  326. protected $m_aEnableClasses;
  327. /**
  328. * User Rights Action code to check if the menu is enabled, null if none
  329. */
  330. protected $m_aEnableActions;
  331. /**
  332. * User Rights allowed results (actually a bitmask) to check if the menu is enabled, null if none
  333. */
  334. protected $m_aEnableActionResults;
  335. /**
  336. * Stimulus to check: if the user can 'apply' this stimulus, then she/he can see this menu
  337. */
  338. protected $m_aEnableStimuli;
  339. /**
  340. * Create a menu item, sets the condition to have it displayed and inserts it into the application's main menu
  341. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  342. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  343. * @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
  344. * @param string $sEnableClass Name of class of object
  345. * @param mixed $iActionCode UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  346. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  347. * @param string $sEnableStimulus The user can see this menu if she/he has enough rights to apply this stimulus
  348. * @return MenuNode
  349. */
  350. public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  351. {
  352. $this->sMenuId = $sMenuId;
  353. $this->aReflectionProperties = array();
  354. if (strlen($sEnableClass) > 0)
  355. {
  356. $this->aReflectionProperties['enable_class'] = $sEnableClass;
  357. $this->aReflectionProperties['enable_action'] = $iActionCode;
  358. $this->aReflectionProperties['enable_permission'] = $iAllowedResults;
  359. $this->aReflectionProperties['enable_stimulus'] = $sEnableStimulus;
  360. }
  361. $this->m_aEnableClasses = array($sEnableClass);
  362. $this->m_aEnableActions = array($iActionCode);
  363. $this->m_aEnableActionResults = array($iAllowedResults);
  364. $this->m_aEnableStimuli = array($sEnableStimulus);
  365. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  366. }
  367. public function ReflectionProperties()
  368. {
  369. return $this->aReflectionProperties;
  370. }
  371. public function GetMenuId()
  372. {
  373. return $this->sMenuId;
  374. }
  375. public function GetTitle()
  376. {
  377. return Dict::S("Menu:$this->sMenuId", str_replace('_', ' ', $this->sMenuId));
  378. }
  379. public function GetLabel()
  380. {
  381. return Dict::S("Menu:$this->sMenuId+", "");
  382. }
  383. public function GetIndex()
  384. {
  385. return $this->index;
  386. }
  387. public function PopulateChildMenus()
  388. {
  389. foreach (ApplicationMenu::GetChildren($this->GetIndex()) as $aMenu)
  390. {
  391. $index = $aMenu['index'];
  392. $oMenu = ApplicationMenu::GetMenuNode($index);
  393. $oMenu->PopulateChildMenus();
  394. }
  395. }
  396. public function GetHyperlink($aExtraParams)
  397. {
  398. $aExtraParams['c[menu]'] = $this->GetMenuId();
  399. return $this->AddParams(utils::GetAbsoluteUrlAppRoot().'pages/UI.php', $aExtraParams);
  400. }
  401. /**
  402. * Add a limiting display condition for the same menu node. The conditions will be combined with a AND
  403. * @param $oMenuNode MenuNode Another definition of the same menu node, with potentially different access restriction
  404. * @return void
  405. */
  406. public function AddCondition(MenuNode $oMenuNode)
  407. {
  408. foreach($oMenuNode->m_aEnableClasses as $index => $sClass )
  409. {
  410. $this->m_aEnableClasses[] = $sClass;
  411. $this->m_aEnableActions[] = $oMenuNode->m_aEnableActions[$index];
  412. $this->m_aEnableActionResults[] = $oMenuNode->m_aEnableActionResults[$index];
  413. $this->m_aEnableStimuli[] = $oMenuNode->m_aEnableStimuli[$index];
  414. }
  415. }
  416. /**
  417. * Tells whether the menu is enabled (i.e. displayed) for the current user
  418. * @return bool True if enabled, false otherwise
  419. */
  420. public function IsEnabled()
  421. {
  422. foreach($this->m_aEnableClasses as $index => $sClass)
  423. {
  424. if ($sClass != null)
  425. {
  426. if (MetaModel::IsValidClass($sClass))
  427. {
  428. if ($this->m_aEnableStimuli[$index] != null)
  429. {
  430. if (!UserRights::IsStimulusAllowed($sClass, $this->m_aEnableStimuli[$index]))
  431. {
  432. return false;
  433. }
  434. }
  435. if ($this->m_aEnableActions[$index] != null)
  436. {
  437. $iResult = UserRights::IsActionAllowed($sClass, $this->m_aEnableActions[$index]);
  438. if (!($iResult & $this->m_aEnableActionResults[$index]))
  439. {
  440. return false;
  441. }
  442. }
  443. }
  444. else
  445. {
  446. return false;
  447. }
  448. }
  449. }
  450. return true;
  451. }
  452. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  453. protected function AddParams($sHyperlink, $aExtraParams)
  454. {
  455. if (count($aExtraParams) > 0)
  456. {
  457. $aQuery = array();
  458. $sSeparator = '?';
  459. if (strpos($sHyperlink, '?') !== false)
  460. {
  461. $sSeparator = '&';
  462. }
  463. foreach($aExtraParams as $sName => $sValue)
  464. {
  465. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  466. }
  467. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  468. }
  469. return $sHyperlink;
  470. }
  471. }
  472. /**
  473. * This class implements a top-level menu group. A group is just a container for sub-items
  474. * it does not display a page by itself
  475. */
  476. class MenuGroup extends MenuNode
  477. {
  478. /**
  479. * Create a top-level menu group and inserts it into the application's main menu
  480. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  481. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  482. * @param string $sEnableClass Name of class of object
  483. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  484. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  485. * @return MenuGroup
  486. */
  487. public function __construct($sMenuId, $fRank, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  488. {
  489. parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  490. }
  491. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  492. {
  493. assert(false); // Shall never be called, groups do not display any content
  494. }
  495. }
  496. /**
  497. * This class defines a menu item which content is based on a custom template.
  498. * Note the template can be either a local file or an URL !
  499. */
  500. class TemplateMenuNode extends MenuNode
  501. {
  502. protected $sTemplateFile;
  503. /**
  504. * Create a menu item based on a custom template and inserts it into the application's main menu
  505. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  506. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  507. * @param integer $iParentIndex ID of the parent menu
  508. * @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
  509. * @param string $sEnableClass Name of class of object
  510. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  511. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  512. * @return MenuNode
  513. */
  514. public function __construct($sMenuId, $sTemplateFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  515. {
  516. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  517. $this->sTemplateFile = $sTemplateFile;
  518. $this->aReflectionProperties['template_file'] = $sTemplateFile;
  519. }
  520. public function GetHyperlink($aExtraParams)
  521. {
  522. if ($this->sTemplateFile == '') return '';
  523. return parent::GetHyperlink($aExtraParams);
  524. }
  525. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  526. {
  527. $sTemplate = @file_get_contents($this->sTemplateFile);
  528. if ($sTemplate !== false)
  529. {
  530. $aExtraParams['table_id'] = 'Menu_'.$this->GetMenuId();
  531. $oTemplate = new DisplayTemplate($sTemplate);
  532. $oTemplate->Render($oPage, $aExtraParams);
  533. }
  534. else
  535. {
  536. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  537. }
  538. }
  539. }
  540. /**
  541. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  542. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  543. */
  544. class OQLMenuNode extends MenuNode
  545. {
  546. protected $sPageTitle;
  547. protected $sOQL;
  548. protected $bSearch;
  549. /**
  550. * Extra parameters to be passed to the display block to fine tune its appearence
  551. */
  552. protected $m_aParams;
  553. /**
  554. * Create a menu item based on an OQL query and inserts it into the application's main menu
  555. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  556. * @param string $sOQL OQL query defining the set of objects to be displayed
  557. * @param integer $iParentIndex ID of the parent menu
  558. * @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
  559. * @param bool $bSearch Whether or not to display a (collapsed) search frame at the top of the page
  560. * @param string $sEnableClass Name of class of object
  561. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  562. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  563. * @return MenuNode
  564. */
  565. public function __construct($sMenuId, $sOQL, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  566. {
  567. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  568. $this->sPageTitle = "Menu:$sMenuId+";
  569. $this->sOQL = $sOQL;
  570. $this->bSearch = $bSearch;
  571. $this->m_aParams = array();
  572. $this->aReflectionProperties['oql'] = $sOQL;
  573. $this->aReflectionProperties['do_search'] = $bSearch;
  574. // Enhancement: we could set as the "enable" condition that the user has enough rights to "read" the objects
  575. // of the class specified by the OQL...
  576. }
  577. /**
  578. * Set some extra parameters to be passed to the display block to fine tune its appearence
  579. * @param Hash $aParams paramCode => value. See DisplayBlock::GetDisplay for the meaning of the parameters
  580. */
  581. public function SetParameters($aParams)
  582. {
  583. $this->m_aParams = $aParams;
  584. foreach($aParams as $sKey => $value)
  585. {
  586. $this->aReflectionProperties[$sKey] = $value;
  587. }
  588. }
  589. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  590. {
  591. OQLMenuNode::RenderOQLSearch
  592. (
  593. $this->sOQL,
  594. Dict::S($this->sPageTitle),
  595. 'Menu_'.$this->GetMenuId(),
  596. $this->bSearch, // Search pane
  597. true, // Search open
  598. $oPage,
  599. array_merge($this->m_aParams, $aExtraParams)
  600. );
  601. }
  602. public static function RenderOQLSearch($sOql, $sTitle, $sUsageId, $bSearchPane, $bSearchOpen, WebPage $oPage, $aExtraParams = array())
  603. {
  604. $sUsageId = utils::GetSafeId($sUsageId);
  605. $oSearch = DBObjectSearch::FromOQL($sOql);
  606. $sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
  607. if ($bSearchPane)
  608. {
  609. $aParams = array_merge(array('open' => $bSearchOpen, 'table_id' => $sUsageId), $aExtraParams);
  610. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  611. $oBlock->Display($oPage, 0);
  612. }
  613. $oPage->add("<p class=\"page-header\">$sIcon ".Dict::S($sTitle)."</p>");
  614. $aParams = array_merge(array('table_id' => $sUsageId), $aExtraParams);
  615. $oBlock = new DisplayBlock($oSearch, 'list', false /* Asynchronous */, $aParams);
  616. $oBlock->Display($oPage, $sUsageId);
  617. }
  618. }
  619. /**
  620. * This class defines a menu item that displays a search form for the given class of objects
  621. */
  622. class SearchMenuNode extends MenuNode
  623. {
  624. protected $sPageTitle;
  625. protected $sClass;
  626. /**
  627. * Create a menu item based on an OQL query and inserts it into the application's main menu
  628. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  629. * @param string $sClass The class of objects to search for
  630. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  631. * @param integer $iParentIndex ID of the parent menu
  632. * @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
  633. * @param string $sEnableClass Name of class of object
  634. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  635. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  636. * @return MenuNode
  637. */
  638. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  639. {
  640. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  641. $this->sPageTitle = "Menu:$sMenuId+";
  642. $this->sClass = $sClass;
  643. $this->aReflectionProperties['class'] = $sClass;
  644. }
  645. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  646. {
  647. $oSearch = new DBObjectSearch($this->sClass);
  648. $aParams = array_merge(array('open' => true, 'table_id' => 'Menu_'.utils::GetSafeId($this->GetMenuId())), $aExtraParams);
  649. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  650. $oBlock->Display($oPage, 0);
  651. }
  652. }
  653. /**
  654. * This class defines a menu that points to any web page. It takes only two parameters:
  655. * - The hyperlink to point to
  656. * - The name of the menu
  657. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  658. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  659. */
  660. class WebPageMenuNode extends MenuNode
  661. {
  662. protected $sHyperlink;
  663. /**
  664. * Create a menu item that points to any web page (not only UI.php)
  665. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  666. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  667. * @param integer $iParentIndex ID of the parent menu
  668. * @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
  669. * @param string $sEnableClass Name of class of object
  670. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  671. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  672. * @return MenuNode
  673. */
  674. public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  675. {
  676. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  677. $this->sHyperlink = $sHyperlink;
  678. $this->aReflectionProperties['url'] = $sHyperlink;
  679. }
  680. public function GetHyperlink($aExtraParams)
  681. {
  682. $aExtraParams['c[menu]'] = $this->GetMenuId();
  683. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  684. }
  685. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  686. {
  687. assert(false); // Shall never be called, the external web page will handle the display by itself
  688. }
  689. }
  690. /**
  691. * This class defines a menu that points to the page for creating a new object of the specified class.
  692. * It take only one parameter: the name of the class
  693. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  694. * in order to make it the active one
  695. */
  696. class NewObjectMenuNode extends MenuNode
  697. {
  698. protected $sClass;
  699. /**
  700. * 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
  701. * rights to create such an object (or an object of a child class)
  702. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  703. * @param string $sClass URL to the page to load. Use relative URL if you want to keep the application portable !
  704. * @param integer $iParentIndex ID of the parent menu
  705. * @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
  706. * @return MenuNode
  707. */
  708. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0)
  709. {
  710. parent::__construct($sMenuId, $iParentIndex, $fRank);
  711. $this->sClass = $sClass;
  712. $this->aReflectionProperties['class'] = $sClass;
  713. }
  714. public function GetHyperlink($aExtraParams)
  715. {
  716. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=new&class='.$this->sClass;
  717. $aExtraParams['c[menu]'] = $this->GetMenuId();
  718. return $this->AddParams($sHyperlink, $aExtraParams);
  719. }
  720. /**
  721. * Overload the check of the "enable" state of this menu to take into account
  722. * derived classes of objects
  723. */
  724. public function IsEnabled()
  725. {
  726. // Enable this menu, only if the current user has enough rights to create such an object, or an object of
  727. // any child class
  728. $aSubClasses = MetaModel::EnumChildClasses($this->sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  729. $bActionIsAllowed = false;
  730. foreach($aSubClasses as $sCandidateClass)
  731. {
  732. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  733. {
  734. $bActionIsAllowed = true;
  735. break; // Enough for now
  736. }
  737. }
  738. return $bActionIsAllowed;
  739. }
  740. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  741. {
  742. assert(false); // Shall never be called, the external web page will handle the display by itself
  743. }
  744. }
  745. require_once(APPROOT.'application/dashboard.class.inc.php');
  746. /**
  747. * This class defines a menu item which content is based on XML dashboard.
  748. */
  749. class DashboardMenuNode extends MenuNode
  750. {
  751. protected $sDashboardFile;
  752. /**
  753. * Create a menu item based on a custom template and inserts it into the application's main menu
  754. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  755. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  756. * @param integer $iParentIndex ID of the parent menu
  757. * @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
  758. * @param string $sEnableClass Name of class of object
  759. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  760. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  761. * @return MenuNode
  762. */
  763. public function __construct($sMenuId, $sDashboardFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  764. {
  765. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  766. $this->sDashboardFile = $sDashboardFile;
  767. $this->aReflectionProperties['definition_file'] = $sDashboardFile;
  768. }
  769. public function GetHyperlink($aExtraParams)
  770. {
  771. if ($this->sDashboardFile == '') return '';
  772. return parent::GetHyperlink($aExtraParams);
  773. }
  774. public function GetDashboard()
  775. {
  776. $sDashboardDefinition = @file_get_contents($this->sDashboardFile);
  777. if ($sDashboardDefinition !== false)
  778. {
  779. $bCustomized = false;
  780. // Search for an eventual user defined dashboard, overloading the existing one
  781. $oUDSearch = new DBObjectSearch('UserDashboard');
  782. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  783. $oUDSearch->AddCondition('menu_code', $this->sMenuId, '=');
  784. $oUDSet = new DBObjectSet($oUDSearch);
  785. if ($oUDSet->Count() > 0)
  786. {
  787. // Assuming there is at most one couple {user, menu}!
  788. $oUserDashboard = $oUDSet->Fetch();
  789. $sDashboardDefinition = $oUserDashboard->Get('contents');
  790. $bCustomized = true;
  791. }
  792. $oDashboard = new RuntimeDashboard($this->sMenuId);
  793. $oDashboard->FromXml($sDashboardDefinition);
  794. $oDashboard->SetCustomFlag($bCustomized);
  795. }
  796. else
  797. {
  798. $oDashboard = null;
  799. }
  800. return $oDashboard;
  801. }
  802. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  803. {
  804. $oDashboard = $this->GetDashboard();
  805. if ($oDashboard != null)
  806. {
  807. $sDivId = preg_replace('/[^a-zA-Z0-9_]/', '', $this->sMenuId);
  808. $oPage->add('<div class="dashboard_contents" id="'.$sDivId.'">');
  809. $oDashboard->Render($oPage, false, $aExtraParams);
  810. $oPage->add('</div>');
  811. $oDashboard->RenderEditionTools($oPage);
  812. if ($oDashboard->GetAutoReload())
  813. {
  814. $sId = $this->sMenuId;
  815. $sExtraParams = json_encode($aExtraParams);
  816. $iReloadInterval = 1000 * $oDashboard->GetAutoReloadInterval();
  817. $oPage->add_script(
  818. <<<EOF
  819. setInterval("ReloadDashboard('$sDivId');", $iReloadInterval);
  820. function ReloadDashboard(sDivId)
  821. {
  822. var oExtraParams = $sExtraParams;
  823. // Do not reload when a dialog box is active
  824. if (!($('.ui-dialog:visible').length > 0))
  825. {
  826. $('.dashboard_contents#'+sDivId).block();
  827. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php',
  828. { operation: 'reload_dashboard', dashboard_id: '$sId', extra_params: oExtraParams},
  829. function(data){
  830. $('.dashboard_contents#'+sDivId).html(data);
  831. $('.dashboard_contents#'+sDivId).unblock();
  832. }
  833. );
  834. }
  835. }
  836. EOF
  837. );
  838. }
  839. $bEdit = utils::ReadParam('edit', false);
  840. if ($bEdit)
  841. {
  842. $sId = addslashes($this->sMenuId);
  843. $oPage->add_ready_script("EditDashboard('$sId');");
  844. }
  845. }
  846. else
  847. {
  848. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  849. }
  850. }
  851. public function RenderEditor(WebPage $oPage)
  852. {
  853. $oDashboard = $this->GetDashboard();
  854. if ($oDashboard != null)
  855. {
  856. $oDashboard->RenderEditor($oPage);
  857. }
  858. else
  859. {
  860. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  861. }
  862. }
  863. public function AddDashlet($oDashlet)
  864. {
  865. $oDashboard = $this->GetDashboard();
  866. if ($oDashboard != null)
  867. {
  868. $oDashboard->AddDashlet($oDashlet);
  869. $oDashboard->Save();
  870. }
  871. else
  872. {
  873. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  874. }
  875. }
  876. }
  877. /**
  878. * A shortcut container is the preferred destination of newly created shortcuts
  879. */
  880. class ShortcutContainerMenuNode extends MenuNode
  881. {
  882. public function GetHyperlink($aExtraParams)
  883. {
  884. return '';
  885. }
  886. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  887. {
  888. }
  889. public function PopulateChildMenus()
  890. {
  891. // Load user shortcuts in DB
  892. //
  893. $oBMSearch = new DBObjectSearch('Shortcut');
  894. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  895. $oBMSet = new DBObjectSet($oBMSearch, array('friendlyname' => true)); // ascending on friendlyname
  896. $fRank = 1;
  897. while ($oShortcut = $oBMSet->Fetch())
  898. {
  899. $sName = $this->GetMenuId().'_'.$oShortcut->GetKey();
  900. $oShortcutMenu = new ShortcutMenuNode($sName, $oShortcut, $this->GetIndex(), $fRank++);
  901. }
  902. // Complete the tree
  903. //
  904. parent::PopulateChildMenus();
  905. }
  906. }
  907. require_once(APPROOT.'application/shortcut.class.inc.php');
  908. /**
  909. * This class defines a menu item which content is a shortcut.
  910. */
  911. class ShortcutMenuNode extends MenuNode
  912. {
  913. protected $oShortcut;
  914. /**
  915. * Create a menu item based on a custom template and inserts it into the application's main menu
  916. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  917. * @param object $oShortcut Shortcut object
  918. * @param integer $iParentIndex ID of the parent menu
  919. * @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
  920. * @param string $sEnableClass Name of class of object
  921. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  922. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  923. * @return MenuNode
  924. */
  925. public function __construct($sMenuId, $oShortcut, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  926. {
  927. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  928. $this->oShortcut = $oShortcut;
  929. $this->aReflectionProperties['shortcut'] = $oShortcut->GetKey();
  930. }
  931. public function GetHyperlink($aExtraParams)
  932. {
  933. $sContext = $this->oShortcut->Get('context');
  934. $aContext = unserialize($sContext);
  935. if (isset($aContext['menu']))
  936. {
  937. unset($aContext['menu']);
  938. }
  939. foreach ($aContext as $sArgName => $sArgValue)
  940. {
  941. $aExtraParams[$sArgName] = $sArgValue;
  942. }
  943. return parent::GetHyperlink($aExtraParams);
  944. }
  945. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  946. {
  947. $this->oShortcut->RenderContent($oPage, $aExtraParams);
  948. }
  949. public function GetTitle()
  950. {
  951. return $this->oShortcut->Get('name');
  952. }
  953. public function GetLabel()
  954. {
  955. return $this->oShortcut->Get('name');
  956. }
  957. }