menunode.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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(APPROOT.'/application/utils.inc.php');
  25. require_once(APPROOT.'/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. if (!$oMenuNode->IsEnabled()) continue; // Don't display a non-enabled menu
  96. $oPage->AddToMenu('<h3>'.$oMenuNode->GetTitle().'</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. $oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true});"); // Make it auto-collapsible once it has been opened properly
  108. }
  109. }
  110. $oPage->AddToMenu('</div>');
  111. $iAccordion++;
  112. }
  113. }
  114. /**
  115. * Handles the display of the sub-menus (called recursively if necessary)
  116. * @return true if the currently selected menu is one of the submenus
  117. */
  118. static protected function DisplaySubMenu($oPage, $aMenus, $aExtraParams, $iActiveMenu = -1)
  119. {
  120. // Sort the menu based on the rank
  121. $bActive = false;
  122. usort($aMenus, array('ApplicationMenu', 'CompareOnRank'));
  123. foreach($aMenus as $aMenu)
  124. {
  125. $index = $aMenu['index'];
  126. $oMenu = self::GetMenuNode($index);
  127. if ($oMenu->IsEnabled())
  128. {
  129. $aChildren = self::GetChildren($index);
  130. $sCSSClass = (count($aChildren) > 0) ? ' class="submenu"' : '';
  131. $sHyperlink = $oMenu->GetHyperlink($aExtraParams);
  132. if ($sHyperlink != '')
  133. {
  134. $oPage->AddToMenu('<li'.$sCSSClass.'><a href="'.$oMenu->GetHyperlink($aExtraParams).'">'.$oMenu->GetTitle().'</a></li>');
  135. }
  136. else
  137. {
  138. $oPage->AddToMenu('<li'.$sCSSClass.'>'.$oMenu->GetTitle().'</li>');
  139. }
  140. $aCurrentMenu = self::$aMenusIndex[$index];
  141. if ($iActiveMenu == $index)
  142. {
  143. $bActive = true;
  144. }
  145. if (count($aChildren) > 0)
  146. {
  147. $oPage->AddToMenu('<ul>');
  148. $bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  149. $oPage->AddToMenu('</ul>');
  150. }
  151. }
  152. }
  153. return $bActive;
  154. }
  155. /**
  156. * Helper function to sort the menus based on their rank
  157. */
  158. static public function CompareOnRank($a, $b)
  159. {
  160. $result = 1;
  161. if ($a['rank'] == $b['rank'])
  162. {
  163. $result = 0;
  164. }
  165. if ($a['rank'] < $b['rank'])
  166. {
  167. $result = -1;
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Helper function to retrieve the MenuNodeObject based on its ID
  173. */
  174. static public function GetMenuNode($index)
  175. {
  176. return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
  177. }
  178. /**
  179. * Helper function to get the list of child(ren) of a menu
  180. */
  181. static protected function GetChildren($index)
  182. {
  183. return self::$aMenusIndex[$index]['children'];
  184. }
  185. /**
  186. * Helper function to get the ID of a menu based on its name
  187. * @param string $sTitle Title of the menu (as passed when creating the menu)
  188. * @return integer ID of the menu, or -1 if not found
  189. */
  190. static public function GetMenuIndexById($sTitle)
  191. {
  192. $index = -1;
  193. foreach(self::$aMenusIndex as $aMenu)
  194. {
  195. if ($aMenu['node']->GetMenuId() == $sTitle)
  196. {
  197. $index = $aMenu['node']->GetIndex();
  198. break;
  199. }
  200. }
  201. return $index;
  202. }
  203. /**
  204. * Retrieves the currently active menu (if any, otherwise the first menu is the default)
  205. * @return MenuNode or null if there is no menu at all !
  206. */
  207. static public function GetActiveNodeId()
  208. {
  209. $oAppContext = new ApplicationContext();
  210. $iMenuIndex = $oAppContext->GetCurrentValue('menu', -1);
  211. if ($iMenuIndex == -1)
  212. {
  213. // Make sure the root menu is sorted on 'rank'
  214. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  215. $oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
  216. $oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
  217. $iMenuIndex = $oMenuNode->GetIndex();
  218. }
  219. return $iMenuIndex;
  220. }
  221. }
  222. /**
  223. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  224. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  225. * derived from MenuNode is enough to have it inserted in the application's main menu.
  226. * The class iTopWebPage, takes care of 3 items:
  227. * +--------------------+
  228. * | Welcome |
  229. * +--------------------+
  230. * Welcome To iTop
  231. * +--------------------+
  232. * | Tools |
  233. * +--------------------+
  234. * CSV Import
  235. * +--------------------+
  236. * | Admin Tools |
  237. * +--------------------+
  238. * User Accounts
  239. * Profiles
  240. * Notifications
  241. * Run Queries
  242. * Export
  243. * Data Model
  244. * Universal Search
  245. *
  246. * All the other menu items must constructed along with the various data model modules
  247. */
  248. abstract class MenuNode
  249. {
  250. protected $sMenuId;
  251. protected $index;
  252. /**
  253. * Class of objects to check if the menu is enabled, null if none
  254. */
  255. protected $m_sEnableClass;
  256. /**
  257. * User Rights Action code to check if the menu is enabled, null if none
  258. */
  259. protected $m_iEnableAction;
  260. /**
  261. * User Rights allowed results (actually a bitmask) to check if the menu is enabled, null if none
  262. */
  263. protected $m_iEnableActionResults;
  264. /**
  265. * Stimulus to check: if the user can 'apply' this stimulus, then she/he can see this menu
  266. */
  267. protected $m_sEnableStimulus;
  268. /**
  269. * Create a menu item, sets the condition to have it displayed and inserts it into the application's main menu
  270. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  271. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  272. * @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
  273. * @param string $sEnableClass Name of class of object
  274. * @param mixed $iActionCode UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  275. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  276. * @param string $sEnableStimulus The user can see this menu if she/he has enough rights to apply this stimulus
  277. * @return MenuNode
  278. */
  279. public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  280. {
  281. $this->sMenuId = $sMenuId;
  282. $this->m_sEnableClass = $sEnableClass;
  283. $this->m_iEnableAction = $iActionCode;
  284. $this->m_iEnableActionResults = $iAllowedResults;
  285. $this->m_sEnableStimulus = $sEnableStimulus;
  286. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  287. }
  288. public function GetMenuId()
  289. {
  290. return $this->sMenuId;
  291. }
  292. public function GetTitle()
  293. {
  294. return Dict::S("Menu:$this->sMenuId");
  295. }
  296. public function GetLabel()
  297. {
  298. return Dict::S("Menu:$this->sMenuId+");
  299. }
  300. public function GetIndex()
  301. {
  302. return $this->index;
  303. }
  304. public function GetHyperlink($aExtraParams)
  305. {
  306. $aExtraParams['c[menu]'] = $this->GetIndex();
  307. return $this->AddParams('../pages/UI.php', $aExtraParams);
  308. }
  309. /**
  310. * Tells whether the menu is enabled (i.e. displayed) for the current user
  311. * @return bool True if enabled, false otherwise
  312. */
  313. public function IsEnabled()
  314. {
  315. if ($this->m_sEnableClass != null)
  316. {
  317. if (MetaModel::IsValidClass($this->m_sEnableClass))
  318. {
  319. if ($this->m_sEnableStimulus != null)
  320. {
  321. if (!UserRights::IsStimulusAllowed($this->m_sEnableClass, $this->m_sEnableStimulus))
  322. {
  323. return false;
  324. }
  325. }
  326. if ($this->m_iEnableAction != null)
  327. {
  328. $iResult = UserRights::IsActionAllowed($this->m_sEnableClass, $this->m_iEnableAction);
  329. if (($iResult & $this->m_iEnableActionResults))
  330. {
  331. return true;
  332. }
  333. else
  334. {
  335. return false;
  336. }
  337. }
  338. return true;
  339. }
  340. return false;
  341. }
  342. return true;
  343. }
  344. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  345. protected function AddParams($sHyperlink, $aExtraParams)
  346. {
  347. if (count($aExtraParams) > 0)
  348. {
  349. $aQuery = array();
  350. $sSeparator = '?';
  351. if (strpos($sHyperlink, '?') !== false)
  352. {
  353. $sSeparator = '&';
  354. }
  355. foreach($aExtraParams as $sName => $sValue)
  356. {
  357. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  358. }
  359. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  360. }
  361. return $sHyperlink;
  362. }
  363. }
  364. /**
  365. * This class implements a top-level menu group. A group is just a container for sub-items
  366. * it does not display a page by itself
  367. */
  368. class MenuGroup extends MenuNode
  369. {
  370. /**
  371. * Create a top-level menu group and inserts it into the application's main menu
  372. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  373. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  374. * @param string $sEnableClass Name of class of object
  375. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  376. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  377. * @return MenuGroup
  378. */
  379. public function __construct($sMenuId, $fRank, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  380. {
  381. parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  382. }
  383. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  384. {
  385. assert(false); // Shall never be called, groups do not display any content
  386. }
  387. }
  388. /**
  389. * This class defines a menu item which content is based on a custom template.
  390. * Note the template can be either a local file or an URL !
  391. */
  392. class TemplateMenuNode extends MenuNode
  393. {
  394. protected $sTemplateFile;
  395. /**
  396. * Create a menu item based on a custom template and inserts it into the application's main menu
  397. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  398. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  399. * @param integer $iParentIndex ID of the parent menu
  400. * @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
  401. * @param string $sEnableClass Name of class of object
  402. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  403. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  404. * @return MenuNode
  405. */
  406. public function __construct($sMenuId, $sTemplateFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  407. {
  408. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  409. $this->sTemplateFile = $sTemplateFile;
  410. }
  411. public function GetHyperlink($aExtraParams)
  412. {
  413. if ($this->sTemplateFile == '') return '';
  414. return parent::GetHyperlink($aExtraParams);
  415. }
  416. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  417. {
  418. $sTemplate = @file_get_contents($this->sTemplateFile);
  419. if ($sTemplate !== false)
  420. {
  421. $oTemplate = new DisplayTemplate($sTemplate);
  422. $oTemplate->Render($oPage, $aExtraParams);
  423. }
  424. else
  425. {
  426. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  427. }
  428. }
  429. }
  430. /**
  431. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  432. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  433. */
  434. class OQLMenuNode extends MenuNode
  435. {
  436. protected $sPageTitle;
  437. protected $sOQL;
  438. protected $bSearch;
  439. /**
  440. * Extra parameters to be passed to the display block to fine tune its appearence
  441. */
  442. protected $m_aParams;
  443. /**
  444. * Create a menu item based on an OQL query and inserts it into the application's main menu
  445. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  446. * @param string $sOQL OQL query defining the set of objects to be displayed
  447. * @param integer $iParentIndex ID of the parent menu
  448. * @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
  449. * @param bool $bSearch Whether or not to display a (collapsed) search frame at the top of the page
  450. * @param string $sEnableClass Name of class of object
  451. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  452. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  453. * @return MenuNode
  454. */
  455. public function __construct($sMenuId, $sOQL, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  456. {
  457. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  458. $this->sPageTitle = "Menu:$sMenuId+";
  459. $this->sOQL = $sOQL;
  460. $this->bSearch = $bSearch;
  461. $this->m_aParams = array();
  462. // Enhancement: we could set as the "enable" condition that the user has enough rights to "read" the objects
  463. // of the class specified by the OQL...
  464. }
  465. /**
  466. * Set some extra parameters to be passed to the display block to fine tune its appearence
  467. * @param Hash $aParams paramCode => value. See DisplayBlock::GetDisplay for the meaning of the parameters
  468. */
  469. public function SetParameters($aParams)
  470. {
  471. $this->m_aParams = $aParams;
  472. }
  473. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  474. {
  475. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  476. try
  477. {
  478. $oSearch = DBObjectSearch::FromOQL($this->sOQL);
  479. $sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
  480. }
  481. catch(Exception $e)
  482. {
  483. $sIcon = '';
  484. }
  485. // The standard template used for all such pages: a (closed) search form at the top and a list of results at the bottom
  486. $sTemplate = '';
  487. if ($this->bSearch)
  488. {
  489. $sTemplate .= <<<EOF
  490. <itopblock BlockClass="DisplayBlock" type="search" asynchronous="false" encoding="text/oql">$this->sOQL</itopblock>
  491. EOF;
  492. }
  493. $sParams = '';
  494. if (!empty($this->m_aParams))
  495. {
  496. $sParams = 'parameters="';
  497. foreach($this->m_aParams as $sName => $sValue)
  498. {
  499. $sParams .= $sName.':'.$sValue.';';
  500. }
  501. $sParams .= '"';
  502. }
  503. $sTemplate .= <<<EOF
  504. <p class="page-header">$sIcon<itopstring>$this->sPageTitle</itopstring></p>
  505. <itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql" $sParams>$this->sOQL</itopblock>
  506. EOF;
  507. $oTemplate = new DisplayTemplate($sTemplate);
  508. $oTemplate->Render($oPage, $aExtraParams);
  509. }
  510. }
  511. /**
  512. * This class defines a menu item that displays a search form for the given class of objects
  513. */
  514. class SearchMenuNode extends MenuNode
  515. {
  516. protected $sPageTitle;
  517. protected $sClass;
  518. /**
  519. * Create a menu item based on an OQL query and inserts it into the application's main menu
  520. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  521. * @param string $sClass The class of objects to search for
  522. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  523. * @param integer $iParentIndex ID of the parent menu
  524. * @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
  525. * @param string $sEnableClass Name of class of object
  526. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  527. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  528. * @return MenuNode
  529. */
  530. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  531. {
  532. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  533. $this->sPageTitle = "Menu:$sMenuId+";
  534. $this->sClass = $sClass;
  535. }
  536. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  537. {
  538. // The standard template used for all such pages: an open search form at the top
  539. $sTemplate = <<<EOF
  540. <itopblock BlockClass="DisplayBlock" type="search" asynchronous="false" encoding="text/oql" parameters="open:true">SELECT $this->sClass</itopblock>
  541. EOF;
  542. $oTemplate = new DisplayTemplate($sTemplate);
  543. $oTemplate->Render($oPage, $aExtraParams);
  544. }
  545. }
  546. /**
  547. * This class defines a menu that points to any web page. It takes only two parameters:
  548. * - The hyperlink to point to
  549. * - The name of the menu
  550. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  551. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  552. */
  553. class WebPageMenuNode extends MenuNode
  554. {
  555. protected $sHyperlink;
  556. /**
  557. * Create a menu item that points to any web page (not only UI.php)
  558. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  559. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  560. * @param integer $iParentIndex ID of the parent menu
  561. * @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
  562. * @param string $sEnableClass Name of class of object
  563. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  564. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  565. * @return MenuNode
  566. */
  567. public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  568. {
  569. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  570. $this->sHyperlink = $sHyperlink;
  571. }
  572. public function GetHyperlink($aExtraParams)
  573. {
  574. $aExtraParams['c[menu]'] = $this->GetIndex();
  575. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  576. }
  577. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  578. {
  579. assert(false); // Shall never be called, the external web page will handle the display by itself
  580. }
  581. }
  582. /**
  583. * This class defines a menu that points to the page for creating a new object of the specified class.
  584. * It take only one parameter: the name of the class
  585. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  586. * in order to make it the active one
  587. */
  588. class NewObjectMenuNode extends MenuNode
  589. {
  590. protected $sClass;
  591. /**
  592. * 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
  593. * rights to create such an object (or an object of a child class)
  594. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  595. * @param string $sClass URL to the page to load. Use relative URL if you want to keep the application portable !
  596. * @param integer $iParentIndex ID of the parent menu
  597. * @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
  598. * @return MenuNode
  599. */
  600. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0)
  601. {
  602. parent::__construct($sMenuId, $iParentIndex, $fRank);
  603. $this->sClass = $sClass;
  604. }
  605. public function GetHyperlink($aExtraParams)
  606. {
  607. $sHyperlink = '../pages/UI.php?operation=new&class='.$this->sClass;
  608. $aExtraParams['c[menu]'] = $this->GetIndex();
  609. return $this->AddParams($sHyperlink, $aExtraParams);
  610. }
  611. /**
  612. * Overload the check of the "enable" state of this menu to take into account
  613. * derived classes of objects
  614. */
  615. public function IsEnabled()
  616. {
  617. // Enable this menu, only if the current user has enough rights to create such an object, or an object of
  618. // any child class
  619. $aSubClasses = MetaModel::EnumChildClasses($this->sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  620. $bActionIsAllowed = false;
  621. foreach($aSubClasses as $sCandidateClass)
  622. {
  623. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  624. {
  625. $bActionIsAllowed = true;
  626. break; // Enough for now
  627. }
  628. }
  629. return $bActionIsAllowed;
  630. }
  631. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  632. {
  633. assert(false); // Shall never be called, the external web page will handle the display by itself
  634. }
  635. }
  636. ?>