menunode.class.inc.php 27 KB

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