menunode.class.inc.php 16 KB

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