menunode.class.inc.php 16 KB

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