menunode.class.inc.php 28 KB

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