menunode.class.inc.php 26 KB

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