menunode.class.inc.php 37 KB

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