menunode.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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('UI:ConfigurationManagementMenu', 1);
  47. * // Create an entry, based on a custom template, for the Configuration management overview, under the top-level group
  48. * new TemplateMenuNode('UI:ConfigurationManagementMenu', '../business/templates/configuration_management_menu.html', $oConfigMgmtMenu->GetIndex(), 0);
  49. * // Create an entry (template based) for the overview of contacts
  50. * $oContactsMenu = new TemplateMenuNode('UI:ContactsMenu', '../business/templates/configuration_management_menu.html',$oConfigMgmtMenu->GetIndex(), 1);
  51. * // Plain list of persons
  52. * new OQLMenuNode('UI:PersonsMenu', 'UI:PersonsMenu:Title', 'SELECT bizPerson', $oContactsMenu->GetIndex(), 0);
  53. * // Plain list of teams
  54. * new OQLMenuNode('UI:TeamsMenu', 'UI:TeamsMenu:Title', 'SELECT bizTeam', $oContactsMenu->GetIndex(), 1);
  55. *
  56. */
  57. class ApplicationMenu
  58. {
  59. static $aRootMenus = array();
  60. static $aMenusIndex = array();
  61. /**
  62. * Main function to add a menu entry into the application, can be called during the definition
  63. * of the data model objects
  64. */
  65. static public function InsertMenu(MenuNode $oMenuNode, $iParentIndex = -1, $fRank)
  66. {
  67. $index = count(self::$aMenusIndex);
  68. self::$aMenusIndex[$index] = array( 'node' => $oMenuNode, 'children' => array());
  69. if ($iParentIndex == -1)
  70. {
  71. self::$aRootMenus[] = array ('rank' => $fRank, 'index' => $index);
  72. }
  73. else
  74. {
  75. self::$aMenusIndex[$iParentIndex]['children'][] = array ('rank' => $fRank, 'index' => $index);
  76. }
  77. return $index;
  78. }
  79. /**
  80. * Entry point to display the whole menu into the web page, used by iTopWebPage
  81. */
  82. static public function DisplayMenu(iTopWebPage $oPage, $aExtraParams)
  83. {
  84. // Sort the root menu based on the rank
  85. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  86. $iAccordion = 0;
  87. $iActiveMenu = ApplicationMenu::GetActiveNodeId();
  88. foreach(self::$aRootMenus as $aMenu)
  89. {
  90. $oMenuNode = self::GetMenuNode($aMenu['index']);
  91. $oPage->AddToMenu('<h3><a href="'.$oMenuNode->GetHyperlink($aExtraParams).'">'.$oMenuNode->GetTitle().'</a></h3>');
  92. $oPage->AddToMenu('<div>');
  93. $aChildren = self::GetChildren($aMenu['index']);
  94. if (count($aChildren) > 0)
  95. {
  96. $oPage->AddToMenu('<ul>');
  97. $bActive = self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  98. $oPage->AddToMenu('</ul>');
  99. if ($bActive)
  100. {
  101. $oPage->add_ready_script("$('#accordion').accordion('activate', $iAccordion);");
  102. }
  103. }
  104. $oPage->AddToMenu('</div>');
  105. $iAccordion++;
  106. }
  107. }
  108. /**
  109. * Handles the display of the sub-menus (called recursively if necessary)
  110. * @return true if the currently selected menu is one of the submenus
  111. */
  112. static protected function DisplaySubMenu($oPage, $aMenus, $aExtraParams, $iActiveMenu = -1)
  113. {
  114. // Sort the menu based on the rank
  115. $bActive = false;
  116. usort($aMenus, array('ApplicationMenu', 'CompareOnRank'));
  117. foreach($aMenus as $aMenu)
  118. {
  119. $index = $aMenu['index'];
  120. $oMenu = self::GetMenuNode($index);
  121. $oPage->AddToMenu('<li><a href="'.$oMenu->GetHyperlink($aExtraParams).'">'.$oMenu->GetTitle().'</a></li>');
  122. $aCurrentMenu = self::$aMenusIndex[$index];
  123. $aChildren = self::GetChildren($index);
  124. if ($iActiveMenu == $index)
  125. {
  126. $bActive = true;
  127. }
  128. if (count($aChildren) > 0)
  129. {
  130. $oPage->AddToMenu('<ul>');
  131. $bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  132. $oPage->AddToMenu('</ul>');
  133. }
  134. }
  135. return $bActive;
  136. }
  137. /**
  138. * Helper function to sort the menus based on their rank
  139. */
  140. static public function CompareOnRank($a, $b)
  141. {
  142. $result = 1;
  143. if ($a['rank'] == $b['rank'])
  144. {
  145. $result = 0;
  146. }
  147. if ($a['rank'] < $b['rank'])
  148. {
  149. $result = -1;
  150. }
  151. return $result;
  152. }
  153. /**
  154. * Helper function to retrieve the MenuNodeObject based on its ID
  155. */
  156. static public function GetMenuNode($index)
  157. {
  158. return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
  159. }
  160. /**
  161. * Helper function to get the list of child(ren) of a menu
  162. */
  163. static protected function GetChildren($index)
  164. {
  165. return self::$aMenusIndex[$index]['children'];
  166. }
  167. /**
  168. * Helper function to get the ID of a menu based on its name
  169. * @param string $sTitle Title of the menu (as passed when creating the menu)
  170. * @return integer ID of the menu, or -1 if not found
  171. */
  172. static public function GetMenuIndexByTitle($sTitle)
  173. {
  174. $index = -1;
  175. foreach(self::$aMenusIndex as $aMenu)
  176. {
  177. if ($aMenu['node']->GetTitle() == $sTitle)
  178. {
  179. $id = $aMenu['node']->GetIndex();
  180. break;
  181. }
  182. }
  183. return $index;
  184. }
  185. /**
  186. * Retrieves the currently active menu (if any, otherwise the first menu is the default)
  187. * @return MenuNode or null if there is no menu at all !
  188. */
  189. static public function GetActiveNodeId()
  190. {
  191. $iMenuIndex = utils::ReadParam('menu', -1);
  192. if ($iMenuIndex == -1)
  193. {
  194. // Make sure the root menu is sorted on 'rank'
  195. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  196. $oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
  197. $oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
  198. $iMenuIndex = $oMenuNode->GetIndex();
  199. }
  200. return $iMenuIndex;
  201. }
  202. }
  203. /**
  204. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  205. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  206. * derived from MenuNode is enough to have it inserted in the application's main menu.
  207. * The class iTopWebPage, takes care of 3 items:
  208. * +--------------------+
  209. * | Welcome |
  210. * +--------------------+
  211. * Welcome To iTop
  212. * +--------------------+
  213. * | Tools |
  214. * +--------------------+
  215. * CSV Import
  216. * +--------------------+
  217. * | Admin Tools |
  218. * +--------------------+
  219. * User Accounts
  220. * Profiles
  221. * Notifications
  222. * Run Queries
  223. * Export
  224. * Data Model
  225. * Universal Search
  226. *
  227. * All the other menu items must constructed along with the various data model modules
  228. */
  229. abstract class MenuNode
  230. {
  231. protected $sTitle;
  232. protected $index = null;
  233. /**
  234. * Create a menu item and inserts it into the application's main menu
  235. * @param string $sTitle Title of the menu (will be looked-up in the dictionnary, for translation)
  236. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  237. * @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
  238. * @return MenuNode
  239. */
  240. public function __construct($sTitle, $iParentIndex = -1, $fRank = 0)
  241. {
  242. $this->sTitle = $sTitle;
  243. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  244. }
  245. public function GetTitle()
  246. {
  247. return Dict::S($this->sTitle);
  248. }
  249. public function GetLabel()
  250. {
  251. return Dict::S($this->sTitle);
  252. }
  253. public function GetIndex()
  254. {
  255. return $this->index;
  256. }
  257. public function GetHyperlink($aExtraParams)
  258. {
  259. $aExtraParams['menu'] = $this->GetIndex();
  260. return $this->AddParams('../pages/UI.php', $aExtraParams);
  261. }
  262. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  263. protected function AddParams($sHyperlink, $aExtraParams)
  264. {
  265. if (count($aExtraParams) > 0)
  266. {
  267. $aQuery = array();
  268. $sSeparator = '?';
  269. if (strpos($sHyperlink, '?') !== false)
  270. {
  271. $sSeparator = '&';
  272. }
  273. foreach($aExtraParams as $sName => $sValue)
  274. {
  275. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  276. }
  277. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  278. }
  279. return $sHyperlink;
  280. }
  281. }
  282. /**
  283. * This class implements a top-level menu group. A group is just a container for sub-items
  284. * it does not display a page by itself
  285. */
  286. class MenuGroup extends MenuNode
  287. {
  288. /**
  289. * Create a top-level menu group and inserts it into the application's main menu
  290. * @param string $sTitle Title of the menu (will be looked-up in the dictionnary for translation)
  291. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  292. * @return MenuGroup
  293. */
  294. public function __construct($sTitle, $fRank)
  295. {
  296. parent::__construct($sTitle, -1 /* no parent, groups are at root level */, $fRank);
  297. }
  298. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  299. {
  300. assert(false); // Shall never be called, groups do not display any content
  301. }
  302. }
  303. /**
  304. * This class defines a menu item which content is based on a custom template.
  305. * Note the template can be either a local file or an URL !
  306. */
  307. class TemplateMenuNode extends MenuNode
  308. {
  309. protected $sTemplateFile;
  310. /**
  311. * Create a menu item based on a custom template and inserts it into the application's main menu
  312. * @param string $sTitle Title of the menu (will be looked-up in the dictionnary for translation)
  313. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  314. * @param integer $iParentIndex ID of the parent menu
  315. * @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
  316. * @return MenuNode
  317. */
  318. public function __construct($sTitle, $sTemplateFile, $iParentIndex, $fRank = 0)
  319. {
  320. parent::__construct($sTitle, $iParentIndex, $fRank);
  321. $this->sTemplateFile = $sTemplateFile;
  322. }
  323. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  324. {
  325. $sTemplate = @file_get_contents($this->sTemplateFile);
  326. if ($sTemplate !== false)
  327. {
  328. $oTemplate = new DisplayTemplate($sTemplate);
  329. $oTemplate->Render($oPage, $aExtraParams);
  330. }
  331. else
  332. {
  333. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  334. }
  335. }
  336. }
  337. /**
  338. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  339. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  340. */
  341. class OQLMenuNode extends MenuNode
  342. {
  343. protected $sPageTitle;
  344. protected $sOQL;
  345. /**
  346. * Create a menu item based on an OQL query and inserts it into the application's main menu
  347. * @param string $sTitle Title of the menu (will be looked-up in the dictionnary for translation)
  348. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  349. * @param integer $iParentIndex ID of the parent menu
  350. * @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
  351. * @return MenuNode
  352. */
  353. public function __construct($sTitle, $sPageTitle, $sOQL, $iParentIndex, $fRank = 0)
  354. {
  355. parent::__construct($sTitle, $iParentIndex, $fRank);
  356. $this->sPageTitle = $sPageTitle;
  357. $this->sOQL = $sOQL;
  358. }
  359. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  360. {
  361. // The standard template used for all such pages: a (closed) search form at the top and a list of results at the bottom
  362. $sTemplate = <<<EOF
  363. <itopblock BlockClass="DisplayBlock" type="search" asynchronous="false" encoding="text/oql">$this->sOQL</itopblock>
  364. <p class="page-header"><itopstring>$this->sPageTitle</itopstring></p>
  365. <itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql">$this->sOQL</itopblock>
  366. EOF;
  367. $oTemplate = new DisplayTemplate($sTemplate);
  368. $oTemplate->Render($oPage, $aExtraParams);
  369. }
  370. }
  371. /**
  372. * This class defines a menu that points to any web page. It takes only two parameters:
  373. * - The hyperlink to point to
  374. * - The name of the menu
  375. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  376. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  377. */
  378. class WebPageMenuNode extends MenuNode
  379. {
  380. protected $sHyperlink;
  381. /**
  382. * Create a menu item that points to any web page (not only UI.php)
  383. * @param string $sTitle Title of the menu (will be looked-up in the dictionnary for translation)
  384. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  385. * @param integer $iParentIndex ID of the parent menu
  386. * @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
  387. * @return MenuNode
  388. */
  389. public function __construct($sTitle, $sHyperlink, $iParentIndex, $fRank = 0)
  390. {
  391. parent::__construct($sTitle, $iParentIndex, $fRank);
  392. $this->sHyperlink = $sHyperlink;
  393. }
  394. public function GetHyperlink($aExtraParams)
  395. {
  396. $aExtraParams['menu'] = $this->GetIndex();
  397. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  398. }
  399. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  400. {
  401. assert(false); // Shall never be called, the external web page will handle the display by itself
  402. }
  403. }
  404. ?>