menunode.class.inc.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Construction and display of the application's main menu
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/utils.inc.php');
  25. require_once('../application/template.class.inc.php');
  26. /**
  27. * This class manipulates, stores and displays the navigation menu used in the application
  28. * In order to improve the modularity of the data model and to ease the update/migration
  29. * between evolving data models, the menus are no longer stored in the database, but are instead
  30. * built on the fly each time a page is loaded.
  31. * The application's menu is organized into top-level groups with, inside each group, a tree of menu items.
  32. * Top level groups do not display any content, they just expand/collapse.
  33. * Sub-items drive the actual content of the page, they are based either on templates, OQL queries or full (external?) web pages.
  34. *
  35. * Example:
  36. * Here is how to insert the following items in the application's menu:
  37. * +----------------------------------------+
  38. * | Configuration Management Group | >> Top level group
  39. * +----------------------------------------+
  40. * + Configuration Management Overview >> Template based menu item
  41. * + Contacts >> Template based menu item
  42. * + Persons >> Plain list (OQL based)
  43. * + Teams >> Plain list (OQL based)
  44. *
  45. * // Create the top-level group. fRank = 1, means it will be inserted after the group '0', which is usually 'Welcome'
  46. * $oConfigMgmtMenu = new MenuGroup('ConfigurationManagementMenu', 1);
  47. * // Create an entry, based on a custom template, for the Configuration management overview, under the top-level group
  48. * new TemplateMenuNode('ConfigurationManagementMenu', '../somedirectory/configuration_management_menu.html', $oConfigMgmtMenu->GetIndex(), 0);
  49. * // Create an entry (template based) for the overview of contacts
  50. * $oContactsMenu = new TemplateMenuNode('ContactsMenu', '../somedirectory/configuration_management_menu.html',$oConfigMgmtMenu->GetIndex(), 1);
  51. * // Plain list of persons
  52. * new OQLMenuNode('PersonsMenu', 'SELECT bizPerson', $oContactsMenu->GetIndex(), 0);
  53. *
  54. */
  55. class ApplicationMenu
  56. {
  57. static $aRootMenus = array();
  58. static $aMenusIndex = array();
  59. /**
  60. * Main function to add a menu entry into the application, can be called during the definition
  61. * of the data model objects
  62. */
  63. static public function InsertMenu(MenuNode $oMenuNode, $iParentIndex = -1, $fRank)
  64. {
  65. $index = self::GetMenuIndexById($oMenuNode->GetMenuId());
  66. if ($index == -1)
  67. {
  68. // The menu does not already exist, insert it
  69. $index = count(self::$aMenusIndex);
  70. self::$aMenusIndex[$index] = array( 'node' => $oMenuNode, 'children' => array());
  71. if ($iParentIndex == -1)
  72. {
  73. self::$aRootMenus[] = array ('rank' => $fRank, 'index' => $index);
  74. }
  75. else
  76. {
  77. self::$aMenusIndex[$iParentIndex]['children'][] = array ('rank' => $fRank, 'index' => $index);
  78. }
  79. }
  80. return $index;
  81. }
  82. /**
  83. * Entry point to display the whole menu into the web page, used by iTopWebPage
  84. */
  85. static public function DisplayMenu(iTopWebPage $oPage, $aExtraParams)
  86. {
  87. // Sort the root menu based on the rank
  88. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  89. $iAccordion = 0;
  90. $iActiveMenu = ApplicationMenu::GetActiveNodeId();
  91. foreach(self::$aRootMenus as $aMenu)
  92. {
  93. $oMenuNode = self::GetMenuNode($aMenu['index']);
  94. if (($oMenuNode->GetMenuId() == 'AdminTools') && (!UserRights::IsAdministrator())) continue; // Don't display the admin menu for non admin users
  95. $oPage->AddToMenu('<h3>'.$oMenuNode->GetTitle().'</h3>');
  96. $oPage->AddToMenu('<div>');
  97. $aChildren = self::GetChildren($aMenu['index']);
  98. if (count($aChildren) > 0)
  99. {
  100. $oPage->AddToMenu('<ul>');
  101. $bActive = self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  102. $oPage->AddToMenu('</ul>');
  103. if ($bActive)
  104. {
  105. $oPage->add_ready_script("$('#accordion').accordion('activate', $iAccordion);");
  106. $oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true});"); // Make it auto-collapsible once it has been opened properly
  107. }
  108. }
  109. $oPage->AddToMenu('</div>');
  110. $iAccordion++;
  111. }
  112. }
  113. /**
  114. * Handles the display of the sub-menus (called recursively if necessary)
  115. * @return true if the currently selected menu is one of the submenus
  116. */
  117. static protected function DisplaySubMenu($oPage, $aMenus, $aExtraParams, $iActiveMenu = -1)
  118. {
  119. // Sort the menu based on the rank
  120. $bActive = false;
  121. usort($aMenus, array('ApplicationMenu', 'CompareOnRank'));
  122. foreach($aMenus as $aMenu)
  123. {
  124. $index = $aMenu['index'];
  125. $oMenu = self::GetMenuNode($index);
  126. $aChildren = self::GetChildren($index);
  127. $sCSSClass = (count($aChildren) > 0) ? ' class="submenu"' : '';
  128. $sHyperlink = $oMenu->GetHyperlink($aExtraParams);
  129. if ($sHyperlink != '')
  130. {
  131. $oPage->AddToMenu('<li'.$sCSSClass.'><a href="'.$oMenu->GetHyperlink($aExtraParams).'">'.$oMenu->GetTitle().'</a></li>');
  132. }
  133. else
  134. {
  135. $oPage->AddToMenu('<li'.$sCSSClass.'>'.$oMenu->GetTitle().'</li>');
  136. }
  137. $aCurrentMenu = self::$aMenusIndex[$index];
  138. if ($iActiveMenu == $index)
  139. {
  140. $bActive = true;
  141. }
  142. if (count($aChildren) > 0)
  143. {
  144. $oPage->AddToMenu('<ul>');
  145. $bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  146. $oPage->AddToMenu('</ul>');
  147. }
  148. }
  149. return $bActive;
  150. }
  151. /**
  152. * Helper function to sort the menus based on their rank
  153. */
  154. static public function CompareOnRank($a, $b)
  155. {
  156. $result = 1;
  157. if ($a['rank'] == $b['rank'])
  158. {
  159. $result = 0;
  160. }
  161. if ($a['rank'] < $b['rank'])
  162. {
  163. $result = -1;
  164. }
  165. return $result;
  166. }
  167. /**
  168. * Helper function to retrieve the MenuNodeObject based on its ID
  169. */
  170. static public function GetMenuNode($index)
  171. {
  172. return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
  173. }
  174. /**
  175. * Helper function to get the list of child(ren) of a menu
  176. */
  177. static protected function GetChildren($index)
  178. {
  179. return self::$aMenusIndex[$index]['children'];
  180. }
  181. /**
  182. * Helper function to get the ID of a menu based on its name
  183. * @param string $sTitle Title of the menu (as passed when creating the menu)
  184. * @return integer ID of the menu, or -1 if not found
  185. */
  186. static public function GetMenuIndexById($sTitle)
  187. {
  188. $index = -1;
  189. foreach(self::$aMenusIndex as $aMenu)
  190. {
  191. if ($aMenu['node']->GetMenuId() == $sTitle)
  192. {
  193. $index = $aMenu['node']->GetIndex();
  194. break;
  195. }
  196. }
  197. return $index;
  198. }
  199. /**
  200. * Retrieves the currently active menu (if any, otherwise the first menu is the default)
  201. * @return MenuNode or null if there is no menu at all !
  202. */
  203. static public function GetActiveNodeId()
  204. {
  205. $iMenuIndex = utils::ReadParam('menu', -1);
  206. if ($iMenuIndex == -1)
  207. {
  208. // Make sure the root menu is sorted on 'rank'
  209. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  210. $oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
  211. $oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
  212. $iMenuIndex = $oMenuNode->GetIndex();
  213. }
  214. return $iMenuIndex;
  215. }
  216. }
  217. /**
  218. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  219. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  220. * derived from MenuNode is enough to have it inserted in the application's main menu.
  221. * The class iTopWebPage, takes care of 3 items:
  222. * +--------------------+
  223. * | Welcome |
  224. * +--------------------+
  225. * Welcome To iTop
  226. * +--------------------+
  227. * | Tools |
  228. * +--------------------+
  229. * CSV Import
  230. * +--------------------+
  231. * | Admin Tools |
  232. * +--------------------+
  233. * User Accounts
  234. * Profiles
  235. * Notifications
  236. * Run Queries
  237. * Export
  238. * Data Model
  239. * Universal Search
  240. *
  241. * All the other menu items must constructed along with the various data model modules
  242. */
  243. abstract class MenuNode
  244. {
  245. protected $sMenuId;
  246. protected $index = null;
  247. /**
  248. * Create a menu item and inserts it into the application's main menu
  249. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  250. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  251. * @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
  252. * @return MenuNode
  253. */
  254. public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0)
  255. {
  256. $this->sMenuId = $sMenuId;
  257. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  258. }
  259. public function GetMenuId()
  260. {
  261. return $this->sMenuId;
  262. }
  263. public function GetTitle()
  264. {
  265. return Dict::S("Menu:$this->sMenuId");
  266. }
  267. public function GetLabel()
  268. {
  269. return Dict::S("Menu:$this->sMenuId+");
  270. }
  271. public function GetIndex()
  272. {
  273. return $this->index;
  274. }
  275. public function GetHyperlink($aExtraParams)
  276. {
  277. $aExtraParams['menu'] = $this->GetIndex();
  278. return $this->AddParams('../pages/UI.php', $aExtraParams);
  279. }
  280. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  281. protected function AddParams($sHyperlink, $aExtraParams)
  282. {
  283. if (count($aExtraParams) > 0)
  284. {
  285. $aQuery = array();
  286. $sSeparator = '?';
  287. if (strpos($sHyperlink, '?') !== false)
  288. {
  289. $sSeparator = '&';
  290. }
  291. foreach($aExtraParams as $sName => $sValue)
  292. {
  293. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  294. }
  295. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  296. }
  297. return $sHyperlink;
  298. }
  299. }
  300. /**
  301. * This class implements a top-level menu group. A group is just a container for sub-items
  302. * it does not display a page by itself
  303. */
  304. class MenuGroup extends MenuNode
  305. {
  306. /**
  307. * Create a top-level menu group and inserts it into the application's main menu
  308. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  309. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  310. * @return MenuGroup
  311. */
  312. public function __construct($sMenuId, $fRank)
  313. {
  314. parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank);
  315. }
  316. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  317. {
  318. assert(false); // Shall never be called, groups do not display any content
  319. }
  320. }
  321. /**
  322. * This class defines a menu item which content is based on a custom template.
  323. * Note the template can be either a local file or an URL !
  324. */
  325. class TemplateMenuNode extends MenuNode
  326. {
  327. protected $sTemplateFile;
  328. /**
  329. * Create a menu item based on a custom template and inserts it into the application's main menu
  330. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  331. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  332. * @param integer $iParentIndex ID of the parent menu
  333. * @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
  334. * @return MenuNode
  335. */
  336. public function __construct($sMenuId, $sTemplateFile, $iParentIndex, $fRank = 0)
  337. {
  338. parent::__construct($sMenuId, $iParentIndex, $fRank);
  339. $this->sTemplateFile = $sTemplateFile;
  340. }
  341. public function GetHyperlink($aExtraParams)
  342. {
  343. if ($this->sTemplateFile == '') return '';
  344. return parent::GetHyperlink($aExtraParams);
  345. }
  346. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  347. {
  348. $sTemplate = @file_get_contents($this->sTemplateFile);
  349. if ($sTemplate !== false)
  350. {
  351. $oTemplate = new DisplayTemplate($sTemplate);
  352. $oTemplate->Render($oPage, $aExtraParams);
  353. }
  354. else
  355. {
  356. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  357. }
  358. }
  359. }
  360. /**
  361. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  362. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  363. */
  364. class OQLMenuNode extends MenuNode
  365. {
  366. protected $sPageTitle;
  367. protected $sOQL;
  368. /**
  369. * Create a menu item based on an OQL query and inserts it into the application's main menu
  370. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  371. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  372. * @param integer $iParentIndex ID of the parent menu
  373. * @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
  374. * @return MenuNode
  375. */
  376. public function __construct($sMenuId, $sOQL, $iParentIndex, $fRank = 0)
  377. {
  378. parent::__construct($sMenuId, $iParentIndex, $fRank);
  379. $this->sPageTitle = "Menu:$sMenuId+";
  380. $this->sOQL = $sOQL;
  381. }
  382. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  383. {
  384. try
  385. {
  386. $oSearch = DBObjectSearch::FromOQL($this->sOQL);
  387. $sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
  388. }
  389. catch(Exception $e)
  390. {
  391. $sIcon = '';
  392. }
  393. // The standard template used for all such pages: a (closed) search form at the top and a list of results at the bottom
  394. $sTemplate = <<<EOF
  395. <itopblock BlockClass="DisplayBlock" type="search" asynchronous="false" encoding="text/oql">$this->sOQL</itopblock>
  396. <p class="page-header">$sIcon<itopstring>$this->sPageTitle</itopstring></p>
  397. <itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql">$this->sOQL</itopblock>
  398. EOF;
  399. $oTemplate = new DisplayTemplate($sTemplate);
  400. $oTemplate->Render($oPage, $aExtraParams);
  401. }
  402. }
  403. /**
  404. * This class defines a menu that points to any web page. It takes only two parameters:
  405. * - The hyperlink to point to
  406. * - The name of the menu
  407. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  408. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  409. */
  410. class WebPageMenuNode extends MenuNode
  411. {
  412. protected $sHyperlink;
  413. /**
  414. * Create a menu item that points to any web page (not only UI.php)
  415. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  416. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  417. * @param integer $iParentIndex ID of the parent menu
  418. * @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
  419. * @return MenuNode
  420. */
  421. public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0)
  422. {
  423. parent::__construct($sMenuId, $iParentIndex, $fRank);
  424. $this->sHyperlink = $sHyperlink;
  425. }
  426. public function GetHyperlink($aExtraParams)
  427. {
  428. $aExtraParams['menu'] = $this->GetIndex();
  429. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  430. }
  431. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  432. {
  433. assert(false); // Shall never be called, the external web page will handle the display by itself
  434. }
  435. }
  436. ?>