menunode.class.inc.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Construction and display of the application's main menu
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'/application/utils.inc.php');
  25. require_once(APPROOT.'/application/template.class.inc.php');
  26. require_once(APPROOT."/application/user.dashboard.class.inc.php");
  27. /**
  28. * This class manipulates, stores and displays the navigation menu used in the application
  29. * In order to improve the modularity of the data model and to ease the update/migration
  30. * between evolving data models, the menus are no longer stored in the database, but are instead
  31. * built on the fly each time a page is loaded.
  32. * The application's menu is organized into top-level groups with, inside each group, a tree of menu items.
  33. * Top level groups do not display any content, they just expand/collapse.
  34. * Sub-items drive the actual content of the page, they are based either on templates, OQL queries or full (external?) web pages.
  35. *
  36. * Example:
  37. * Here is how to insert the following items in the application's menu:
  38. * +----------------------------------------+
  39. * | Configuration Management Group | >> Top level group
  40. * +----------------------------------------+
  41. * + Configuration Management Overview >> Template based menu item
  42. * + Contacts >> Template based menu item
  43. * + Persons >> Plain list (OQL based)
  44. * + Teams >> Plain list (OQL based)
  45. *
  46. * // Create the top-level group. fRank = 1, means it will be inserted after the group '0', which is usually 'Welcome'
  47. * $oConfigMgmtMenu = new MenuGroup('ConfigurationManagementMenu', 1);
  48. * // Create an entry, based on a custom template, for the Configuration management overview, under the top-level group
  49. * new TemplateMenuNode('ConfigurationManagementMenu', '../somedirectory/configuration_management_menu.html', $oConfigMgmtMenu->GetIndex(), 0);
  50. * // Create an entry (template based) for the overview of contacts
  51. * $oContactsMenu = new TemplateMenuNode('ContactsMenu', '../somedirectory/configuration_management_menu.html',$oConfigMgmtMenu->GetIndex(), 1);
  52. * // Plain list of persons
  53. * new OQLMenuNode('PersonsMenu', 'SELECT bizPerson', $oContactsMenu->GetIndex(), 0);
  54. *
  55. */
  56. class ApplicationMenu
  57. {
  58. static $bAdditionalMenusLoaded = false;
  59. static $aRootMenus = array();
  60. static $aMenusIndex = array();
  61. static $sFavoriteSiloQuery = 'SELECT Organization';
  62. static public function LoadAdditionalMenus()
  63. {
  64. if (!self::$bAdditionalMenusLoaded)
  65. {
  66. // Build menus from module handlers
  67. //
  68. foreach(get_declared_classes() as $sPHPClass)
  69. {
  70. if (is_subclass_of($sPHPClass, 'ModuleHandlerAPI'))
  71. {
  72. $aCallSpec = array($sPHPClass, 'OnMenuCreation');
  73. call_user_func($aCallSpec);
  74. }
  75. }
  76. // Build menus from the menus themselves (e.g. the ShortcutContainerMenuNode will do that)
  77. //
  78. foreach(self::$aRootMenus as $aMenu)
  79. {
  80. $oMenuNode = self::GetMenuNode($aMenu['index']);
  81. $oMenuNode->PopulateChildMenus();
  82. }
  83. self::$bAdditionalMenusLoaded = true;
  84. }
  85. }
  86. /**
  87. * Set the query used to limit the list of displayed organizations in the drop-down menu
  88. * @param $sOQL string The OQL query returning a list of Organization objects
  89. * @return none
  90. */
  91. static public function SetFavoriteSiloQuery($sOQL)
  92. {
  93. self::$sFavoriteSiloQuery = $sOQL;
  94. }
  95. /**
  96. * Get the query used to limit the list of displayed organizations in the drop-down menu
  97. * @return string The OQL query returning a list of Organization objects
  98. */
  99. static public function GetFavoriteSiloQuery()
  100. {
  101. return self::$sFavoriteSiloQuery;
  102. }
  103. /**
  104. * Main function to add a menu entry into the application, can be called during the definition
  105. * of the data model objects
  106. */
  107. static public function InsertMenu(MenuNode $oMenuNode, $iParentIndex, $fRank)
  108. {
  109. $index = self::GetMenuIndexById($oMenuNode->GetMenuId());
  110. if ($index == -1)
  111. {
  112. // The menu does not already exist, insert it
  113. $index = count(self::$aMenusIndex);
  114. if ($iParentIndex == -1)
  115. {
  116. $sParentId = '';
  117. self::$aRootMenus[] = array ('rank' => $fRank, 'index' => $index);
  118. }
  119. else
  120. {
  121. $sParentId = self::$aMenusIndex[$iParentIndex]['node']->GetMenuId();
  122. self::$aMenusIndex[$iParentIndex]['children'][] = array ('rank' => $fRank, 'index' => $index);
  123. }
  124. // Note: At the time when 'parent', 'rank' and 'source_file' have been added for the reflection API,
  125. // they were not used to display the menus (redundant or unused)
  126. //
  127. $aBacktrace = debug_backtrace();
  128. $sFile = isset($aBacktrace[2]["file"]) ? $aBacktrace[2]["file"] : $aBacktrace[1]["file"];
  129. self::$aMenusIndex[$index] = array('node' => $oMenuNode, 'children' => array(), 'parent' => $sParentId, 'rank' => $fRank, 'source_file' => $sFile);
  130. }
  131. else
  132. {
  133. // the menu already exists, let's combine the conditions that make it visible
  134. self::$aMenusIndex[$index]['node']->AddCondition($oMenuNode);
  135. }
  136. return $index;
  137. }
  138. /**
  139. * Reflection API - Get menu entries
  140. */
  141. static public function ReflectionMenuNodes()
  142. {
  143. self::LoadAdditionalMenus();
  144. return self::$aMenusIndex;
  145. }
  146. /**
  147. * Entry point to display the whole menu into the web page, used by iTopWebPage
  148. */
  149. static public function DisplayMenu($oPage, $aExtraParams)
  150. {
  151. self::LoadAdditionalMenus();
  152. // Sort the root menu based on the rank
  153. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  154. $iAccordion = 0;
  155. $iActiveMenu = self::GetMenuIndexById(self::GetActiveNodeId());
  156. foreach(self::$aRootMenus as $aMenu)
  157. {
  158. $oMenuNode = self::GetMenuNode($aMenu['index']);
  159. if (!$oMenuNode->IsEnabled()) continue; // Don't display a non-enabled menu
  160. $oPage->AddToMenu('<h3>'.$oMenuNode->GetTitle().'</h3>');
  161. $oPage->AddToMenu('<div>');
  162. $aChildren = self::GetChildren($aMenu['index']);
  163. if (count($aChildren) > 0)
  164. {
  165. $oPage->AddToMenu('<ul>');
  166. $bActive = self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  167. $oPage->AddToMenu('</ul>');
  168. if ($bActive)
  169. {
  170. $oPage->add_ready_script("$('#accordion').accordion('activate', $iAccordion);");
  171. $oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true});"); // Make it auto-collapsible once it has been opened properly
  172. }
  173. }
  174. $oPage->AddToMenu('</div>');
  175. $iAccordion++;
  176. }
  177. }
  178. /**
  179. * Handles the display of the sub-menus (called recursively if necessary)
  180. * @return true if the currently selected menu is one of the submenus
  181. */
  182. static protected function DisplaySubMenu($oPage, $aMenus, $aExtraParams, $iActiveMenu = -1)
  183. {
  184. // Sort the menu based on the rank
  185. $bActive = false;
  186. usort($aMenus, array('ApplicationMenu', 'CompareOnRank'));
  187. foreach($aMenus as $aMenu)
  188. {
  189. $index = $aMenu['index'];
  190. $oMenu = self::GetMenuNode($index);
  191. if ($oMenu->IsEnabled())
  192. {
  193. $aChildren = self::GetChildren($index);
  194. $sCSSClass = (count($aChildren) > 0) ? ' class="submenu"' : '';
  195. $sHyperlink = $oMenu->GetHyperlink($aExtraParams);
  196. if ($sHyperlink != '')
  197. {
  198. $oPage->AddToMenu('<li'.$sCSSClass.'><a href="'.$oMenu->GetHyperlink($aExtraParams).'">'.$oMenu->GetTitle().'</a></li>');
  199. }
  200. else
  201. {
  202. $oPage->AddToMenu('<li'.$sCSSClass.'>'.$oMenu->GetTitle().'</li>');
  203. }
  204. $aCurrentMenu = self::$aMenusIndex[$index];
  205. if ($iActiveMenu == $index)
  206. {
  207. $bActive = true;
  208. }
  209. if (count($aChildren) > 0)
  210. {
  211. $oPage->AddToMenu('<ul>');
  212. $bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  213. $oPage->AddToMenu('</ul>');
  214. }
  215. }
  216. }
  217. return $bActive;
  218. }
  219. /**
  220. * Helper function to sort the menus based on their rank
  221. */
  222. static public function CompareOnRank($a, $b)
  223. {
  224. $result = 1;
  225. if ($a['rank'] == $b['rank'])
  226. {
  227. $result = 0;
  228. }
  229. if ($a['rank'] < $b['rank'])
  230. {
  231. $result = -1;
  232. }
  233. return $result;
  234. }
  235. /**
  236. * Helper function to retrieve the MenuNodeObject based on its ID
  237. */
  238. static public function GetMenuNode($index)
  239. {
  240. return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
  241. }
  242. /**
  243. * Helper function to get the list of child(ren) of a menu
  244. */
  245. static public function GetChildren($index)
  246. {
  247. return self::$aMenusIndex[$index]['children'];
  248. }
  249. /**
  250. * Helper function to get the ID of a menu based on its name
  251. * @param string $sTitle Title of the menu (as passed when creating the menu)
  252. * @return integer ID of the menu, or -1 if not found
  253. */
  254. static public function GetMenuIndexById($sTitle)
  255. {
  256. $index = -1;
  257. foreach(self::$aMenusIndex as $aMenu)
  258. {
  259. if ($aMenu['node']->GetMenuId() == $sTitle)
  260. {
  261. $index = $aMenu['node']->GetIndex();
  262. break;
  263. }
  264. }
  265. return $index;
  266. }
  267. /**
  268. * Retrieves the currently active menu (if any, otherwise the first menu is the default)
  269. * @return string The Id of the currently active menu
  270. */
  271. static public function GetActiveNodeId()
  272. {
  273. $oAppContext = new ApplicationContext();
  274. $sMenuId = $oAppContext->GetCurrentValue('menu', null);
  275. if ($sMenuId === null)
  276. {
  277. // Make sure the root menu is sorted on 'rank'
  278. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  279. $oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
  280. $oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
  281. $sMenuId = $oMenuNode->GetMenuId();
  282. }
  283. return $sMenuId;
  284. }
  285. }
  286. /**
  287. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  288. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  289. * derived from MenuNode is enough to have it inserted in the application's main menu.
  290. * The class iTopWebPage, takes care of 3 items:
  291. * +--------------------+
  292. * | Welcome |
  293. * +--------------------+
  294. * Welcome To iTop
  295. * +--------------------+
  296. * | Tools |
  297. * +--------------------+
  298. * CSV Import
  299. * +--------------------+
  300. * | Admin Tools |
  301. * +--------------------+
  302. * User Accounts
  303. * Profiles
  304. * Notifications
  305. * Run Queries
  306. * Export
  307. * Data Model
  308. * Universal Search
  309. *
  310. * All the other menu items must constructed along with the various data model modules
  311. */
  312. abstract class MenuNode
  313. {
  314. protected $sMenuId;
  315. protected $index;
  316. /**
  317. * Properties reflecting how the node has been declared
  318. */
  319. protected $aReflectionProperties;
  320. /**
  321. * Class of objects to check if the menu is enabled, null if none
  322. */
  323. protected $m_aEnableClasses;
  324. /**
  325. * User Rights Action code to check if the menu is enabled, null if none
  326. */
  327. protected $m_aEnableActions;
  328. /**
  329. * User Rights allowed results (actually a bitmask) to check if the menu is enabled, null if none
  330. */
  331. protected $m_aEnableActionResults;
  332. /**
  333. * Stimulus to check: if the user can 'apply' this stimulus, then she/he can see this menu
  334. */
  335. protected $m_aEnableStimuli;
  336. /**
  337. * Create a menu item, sets the condition to have it displayed and inserts it into the application's main menu
  338. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  339. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  340. * @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
  341. * @param string $sEnableClass Name of class of object
  342. * @param mixed $iActionCode UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  343. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  344. * @param string $sEnableStimulus The user can see this menu if she/he has enough rights to apply this stimulus
  345. * @return MenuNode
  346. */
  347. public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  348. {
  349. $this->sMenuId = $sMenuId;
  350. $this->aReflectionProperties = array();
  351. if (strlen($sEnableClass) > 0)
  352. {
  353. $this->aReflectionProperties['enable_class'] = $sEnableClass;
  354. $this->aReflectionProperties['enable_action'] = $iActionCode;
  355. $this->aReflectionProperties['enable_permission'] = $iAllowedResults;
  356. $this->aReflectionProperties['enable_stimulus'] = $sEnableStimulus;
  357. }
  358. $this->m_aEnableClasses = array($sEnableClass);
  359. $this->m_aEnableActions = array($iActionCode);
  360. $this->m_aEnableActionResults = array($iAllowedResults);
  361. $this->m_aEnableStimuli = array($sEnableStimulus);
  362. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  363. }
  364. public function ReflectionProperties()
  365. {
  366. return $this->aReflectionProperties;
  367. }
  368. public function GetMenuId()
  369. {
  370. return $this->sMenuId;
  371. }
  372. public function GetTitle()
  373. {
  374. return Dict::S("Menu:$this->sMenuId", str_replace('_', ' ', $this->sMenuId));
  375. }
  376. public function GetLabel()
  377. {
  378. return Dict::S("Menu:$this->sMenuId+", "");
  379. }
  380. public function GetIndex()
  381. {
  382. return $this->index;
  383. }
  384. public function PopulateChildMenus()
  385. {
  386. foreach (ApplicationMenu::GetChildren($this->GetIndex()) as $aMenu)
  387. {
  388. $index = $aMenu['index'];
  389. $oMenu = ApplicationMenu::GetMenuNode($index);
  390. $oMenu->PopulateChildMenus();
  391. }
  392. }
  393. public function GetHyperlink($aExtraParams)
  394. {
  395. $aExtraParams['c[menu]'] = $this->GetMenuId();
  396. return $this->AddParams(utils::GetAbsoluteUrlAppRoot().'pages/UI.php', $aExtraParams);
  397. }
  398. /**
  399. * Add a limiting display condition for the same menu node. The conditions will be combined with a AND
  400. * @param $oMenuNode MenuNode Another definition of the same menu node, with potentially different access restriction
  401. * @return void
  402. */
  403. public function AddCondition(MenuNode $oMenuNode)
  404. {
  405. foreach($oMenuNode->m_aEnableClasses as $index => $sClass )
  406. {
  407. $this->m_aEnableClasses[] = $sClass;
  408. $this->m_aEnableActions[] = $oMenuNode->m_aEnableActions[$index];
  409. $this->m_aEnableActionResults[] = $oMenuNode->m_aEnableActionResults[$index];
  410. $this->m_aEnableStimuli[] = $oMenuNode->m_aEnableStimuli[$index];
  411. }
  412. }
  413. /**
  414. * Tells whether the menu is enabled (i.e. displayed) for the current user
  415. * @return bool True if enabled, false otherwise
  416. */
  417. public function IsEnabled()
  418. {
  419. foreach($this->m_aEnableClasses as $index => $sClass)
  420. {
  421. if ($sClass != null)
  422. {
  423. if (MetaModel::IsValidClass($sClass))
  424. {
  425. if ($this->m_aEnableStimuli[$index] != null)
  426. {
  427. if (!UserRights::IsStimulusAllowed($sClass, $this->m_aEnableStimuli[$index]))
  428. {
  429. return false;
  430. }
  431. }
  432. if ($this->m_aEnableActions[$index] != null)
  433. {
  434. $iResult = UserRights::IsActionAllowed($sClass, $this->m_aEnableActions[$index]);
  435. if (!($iResult & $this->m_aEnableActionResults[$index]))
  436. {
  437. return false;
  438. }
  439. }
  440. }
  441. else
  442. {
  443. return false;
  444. }
  445. }
  446. }
  447. return true;
  448. }
  449. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  450. protected function AddParams($sHyperlink, $aExtraParams)
  451. {
  452. if (count($aExtraParams) > 0)
  453. {
  454. $aQuery = array();
  455. $sSeparator = '?';
  456. if (strpos($sHyperlink, '?') !== false)
  457. {
  458. $sSeparator = '&';
  459. }
  460. foreach($aExtraParams as $sName => $sValue)
  461. {
  462. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  463. }
  464. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  465. }
  466. return $sHyperlink;
  467. }
  468. }
  469. /**
  470. * This class implements a top-level menu group. A group is just a container for sub-items
  471. * it does not display a page by itself
  472. */
  473. class MenuGroup extends MenuNode
  474. {
  475. /**
  476. * Create a top-level menu group and inserts it into the application's main menu
  477. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  478. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  479. * @param string $sEnableClass Name of class of object
  480. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  481. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  482. * @return MenuGroup
  483. */
  484. public function __construct($sMenuId, $fRank, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  485. {
  486. parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  487. }
  488. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  489. {
  490. assert(false); // Shall never be called, groups do not display any content
  491. }
  492. }
  493. /**
  494. * This class defines a menu item which content is based on a custom template.
  495. * Note the template can be either a local file or an URL !
  496. */
  497. class TemplateMenuNode extends MenuNode
  498. {
  499. protected $sTemplateFile;
  500. /**
  501. * Create a menu item based on a custom template and inserts it into the application's main menu
  502. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  503. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  504. * @param integer $iParentIndex ID of the parent menu
  505. * @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
  506. * @param string $sEnableClass Name of class of object
  507. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  508. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  509. * @return MenuNode
  510. */
  511. public function __construct($sMenuId, $sTemplateFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  512. {
  513. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  514. $this->sTemplateFile = $sTemplateFile;
  515. $this->aReflectionProperties['template_file'] = $sTemplateFile;
  516. }
  517. public function GetHyperlink($aExtraParams)
  518. {
  519. if ($this->sTemplateFile == '') return '';
  520. return parent::GetHyperlink($aExtraParams);
  521. }
  522. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  523. {
  524. $sTemplate = @file_get_contents($this->sTemplateFile);
  525. if ($sTemplate !== false)
  526. {
  527. $aExtraParams['table_id'] = 'Menu_'.$this->GetMenuId();
  528. $oTemplate = new DisplayTemplate($sTemplate);
  529. $oTemplate->Render($oPage, $aExtraParams);
  530. }
  531. else
  532. {
  533. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  534. }
  535. }
  536. }
  537. /**
  538. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  539. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  540. */
  541. class OQLMenuNode extends MenuNode
  542. {
  543. protected $sPageTitle;
  544. protected $sOQL;
  545. protected $bSearch;
  546. /**
  547. * Extra parameters to be passed to the display block to fine tune its appearence
  548. */
  549. protected $m_aParams;
  550. /**
  551. * Create a menu item based on an OQL query and inserts it into the application's main menu
  552. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  553. * @param string $sOQL OQL query defining the set of objects to be displayed
  554. * @param integer $iParentIndex ID of the parent menu
  555. * @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
  556. * @param bool $bSearch Whether or not to display a (collapsed) search frame at the top of the page
  557. * @param string $sEnableClass Name of class of object
  558. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  559. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  560. * @return MenuNode
  561. */
  562. public function __construct($sMenuId, $sOQL, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  563. {
  564. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  565. $this->sPageTitle = "Menu:$sMenuId+";
  566. $this->sOQL = $sOQL;
  567. $this->bSearch = $bSearch;
  568. $this->m_aParams = array();
  569. $this->aReflectionProperties['oql'] = $sOQL;
  570. $this->aReflectionProperties['do_search'] = $bSearch;
  571. // Enhancement: we could set as the "enable" condition that the user has enough rights to "read" the objects
  572. // of the class specified by the OQL...
  573. }
  574. /**
  575. * Set some extra parameters to be passed to the display block to fine tune its appearence
  576. * @param Hash $aParams paramCode => value. See DisplayBlock::GetDisplay for the meaning of the parameters
  577. */
  578. public function SetParameters($aParams)
  579. {
  580. $this->m_aParams = $aParams;
  581. foreach($aParams as $sKey => $value)
  582. {
  583. $this->aReflectionProperties[$sKey] = $value;
  584. }
  585. }
  586. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  587. {
  588. OQLMenuNode::RenderOQLSearch
  589. (
  590. $this->sOQL,
  591. Dict::S($this->sPageTitle),
  592. 'Menu_'.$this->GetMenuId(),
  593. $this->bSearch, // Search pane
  594. true, // Search open
  595. $oPage,
  596. $aExtraParams
  597. );
  598. }
  599. public static function RenderOQLSearch($sOql, $sTitle, $sUsageId, $bSearchPane, $bSearchOpen, WebPage $oPage, $aExtraParams = array())
  600. {
  601. $oSearch = DBObjectSearch::FromOQL($sOql);
  602. $sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
  603. if ($bSearchPane)
  604. {
  605. $aParams = array_merge(array('open' => $bSearchOpen, 'table_id' => $sUsageId), $aExtraParams);
  606. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  607. $oBlock->Display($oPage, 0);
  608. }
  609. $oPage->add("<p class=\"page-header\">$sIcon ".Dict::S($sTitle)."</p>");
  610. $aParams = array_merge(array('table_id' => $sUsageId), $aExtraParams);
  611. $oBlock = new DisplayBlock($oSearch, 'list', false /* Asynchronous */, $aParams);
  612. $oBlock->Display($oPage, $sUsageId);
  613. }
  614. }
  615. /**
  616. * This class defines a menu item that displays a search form for the given class of objects
  617. */
  618. class SearchMenuNode extends MenuNode
  619. {
  620. protected $sPageTitle;
  621. protected $sClass;
  622. /**
  623. * Create a menu item based on an OQL query and inserts it into the application's main menu
  624. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  625. * @param string $sClass The class of objects to search for
  626. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  627. * @param integer $iParentIndex ID of the parent menu
  628. * @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
  629. * @param string $sEnableClass Name of class of object
  630. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  631. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  632. * @return MenuNode
  633. */
  634. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  635. {
  636. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  637. $this->sPageTitle = "Menu:$sMenuId+";
  638. $this->sClass = $sClass;
  639. $this->aReflectionProperties['class'] = $sClass;
  640. }
  641. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  642. {
  643. $oSearch = new DBObjectSearch($this->sClass);
  644. $aParams = array_merge(array('open' => true, 'table_id' => 'Menu_'.$this->GetMenuId()), $aExtraParams);
  645. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  646. $oBlock->Display($oPage, 0);
  647. }
  648. }
  649. /**
  650. * This class defines a menu that points to any web page. It takes only two parameters:
  651. * - The hyperlink to point to
  652. * - The name of the menu
  653. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  654. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  655. */
  656. class WebPageMenuNode extends MenuNode
  657. {
  658. protected $sHyperlink;
  659. /**
  660. * Create a menu item that points to any web page (not only UI.php)
  661. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  662. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  663. * @param integer $iParentIndex ID of the parent menu
  664. * @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
  665. * @param string $sEnableClass Name of class of object
  666. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  667. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  668. * @return MenuNode
  669. */
  670. public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  671. {
  672. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  673. $this->sHyperlink = $sHyperlink;
  674. $this->aReflectionProperties['url'] = $sHyperlink;
  675. }
  676. public function GetHyperlink($aExtraParams)
  677. {
  678. $aExtraParams['c[menu]'] = $this->GetMenuId();
  679. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  680. }
  681. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  682. {
  683. assert(false); // Shall never be called, the external web page will handle the display by itself
  684. }
  685. }
  686. /**
  687. * This class defines a menu that points to the page for creating a new object of the specified class.
  688. * It take only one parameter: the name of the class
  689. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  690. * in order to make it the active one
  691. */
  692. class NewObjectMenuNode extends MenuNode
  693. {
  694. protected $sClass;
  695. /**
  696. * 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
  697. * rights to create such an object (or an object of a child class)
  698. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  699. * @param string $sClass URL to the page to load. Use relative URL if you want to keep the application portable !
  700. * @param integer $iParentIndex ID of the parent menu
  701. * @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
  702. * @return MenuNode
  703. */
  704. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0)
  705. {
  706. parent::__construct($sMenuId, $iParentIndex, $fRank);
  707. $this->sClass = $sClass;
  708. $this->aReflectionProperties['class'] = $sClass;
  709. }
  710. public function GetHyperlink($aExtraParams)
  711. {
  712. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=new&class='.$this->sClass;
  713. $aExtraParams['c[menu]'] = $this->GetMenuId();
  714. return $this->AddParams($sHyperlink, $aExtraParams);
  715. }
  716. /**
  717. * Overload the check of the "enable" state of this menu to take into account
  718. * derived classes of objects
  719. */
  720. public function IsEnabled()
  721. {
  722. // Enable this menu, only if the current user has enough rights to create such an object, or an object of
  723. // any child class
  724. $aSubClasses = MetaModel::EnumChildClasses($this->sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  725. $bActionIsAllowed = false;
  726. foreach($aSubClasses as $sCandidateClass)
  727. {
  728. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  729. {
  730. $bActionIsAllowed = true;
  731. break; // Enough for now
  732. }
  733. }
  734. return $bActionIsAllowed;
  735. }
  736. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  737. {
  738. assert(false); // Shall never be called, the external web page will handle the display by itself
  739. }
  740. }
  741. require_once(APPROOT.'application/dashboard.class.inc.php');
  742. /**
  743. * This class defines a menu item which content is based on XML dashboard.
  744. */
  745. class DashboardMenuNode extends MenuNode
  746. {
  747. protected $sDashboardFile;
  748. /**
  749. * Create a menu item based on a custom template and inserts it into the application's main menu
  750. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  751. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  752. * @param integer $iParentIndex ID of the parent menu
  753. * @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
  754. * @param string $sEnableClass Name of class of object
  755. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  756. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  757. * @return MenuNode
  758. */
  759. public function __construct($sMenuId, $sDashboardFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  760. {
  761. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  762. $this->sDashboardFile = $sDashboardFile;
  763. $this->aReflectionProperties['definition_file'] = $sDashboardFile;
  764. }
  765. public function GetHyperlink($aExtraParams)
  766. {
  767. if ($this->sDashboardFile == '') return '';
  768. return parent::GetHyperlink($aExtraParams);
  769. }
  770. public function GetDashboard()
  771. {
  772. $sDashboardDefinition = @file_get_contents($this->sDashboardFile);
  773. if ($sDashboardDefinition !== false)
  774. {
  775. $bCustomized = false;
  776. // Search for an eventual user defined dashboard, overloading the existing one
  777. $oUDSearch = new DBObjectSearch('UserDashboard');
  778. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  779. $oUDSearch->AddCondition('menu_code', $this->sMenuId, '=');
  780. $oUDSet = new DBObjectSet($oUDSearch);
  781. if ($oUDSet->Count() > 0)
  782. {
  783. // Assuming there is at most one couple {user, menu}!
  784. $oUserDashboard = $oUDSet->Fetch();
  785. $sDashboardDefinition = $oUserDashboard->Get('contents');
  786. $bCustomized = true;
  787. }
  788. $oDashboard = new RuntimeDashboard($this->sMenuId);
  789. $oDashboard->FromXml($sDashboardDefinition);
  790. $oDashboard->SetCustomFlag($bCustomized);
  791. }
  792. else
  793. {
  794. $oDashboard = null;
  795. }
  796. return $oDashboard;
  797. }
  798. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  799. {
  800. $oDashboard = $this->GetDashboard();
  801. if ($oDashboard != null)
  802. {
  803. $oDashboard->Render($oPage, false, $aExtraParams);
  804. $bEdit = utils::ReadParam('edit', false);
  805. if ($bEdit)
  806. {
  807. $sId = addslashes($this->sMenuId);
  808. $oPage->add_ready_script("EditDashboard('$sId');");
  809. }
  810. }
  811. else
  812. {
  813. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  814. }
  815. }
  816. public function RenderEditor(WebPage $oPage)
  817. {
  818. $oDashboard = $this->GetDashboard();
  819. if ($oDashboard != null)
  820. {
  821. $oDashboard->RenderEditor($oPage);
  822. }
  823. else
  824. {
  825. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  826. }
  827. }
  828. public function AddDashlet($oDashlet)
  829. {
  830. $oDashboard = $this->GetDashboard();
  831. if ($oDashboard != null)
  832. {
  833. $oDashboard->AddDashlet($oDashlet);
  834. $oDashboard->Save();
  835. }
  836. else
  837. {
  838. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  839. }
  840. }
  841. }
  842. /**
  843. * A shortcut container is the preferred destination of newly created shortcuts
  844. */
  845. class ShortcutContainerMenuNode extends MenuNode
  846. {
  847. public function GetHyperlink($aExtraParams)
  848. {
  849. return '';
  850. }
  851. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  852. {
  853. }
  854. public function PopulateChildMenus()
  855. {
  856. // Load user shortcuts in DB
  857. //
  858. $oBMSearch = new DBObjectSearch('Shortcut');
  859. $oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  860. $oBMSet = new DBObjectSet($oBMSearch, array('friendlyname' => true)); // ascending on friendlyname
  861. $fRank = 1;
  862. while ($oShortcut = $oBMSet->Fetch())
  863. {
  864. $sName = $this->GetMenuId().'_'.$oShortcut->GetKey();
  865. $oShortcutMenu = new ShortcutMenuNode($sName, $oShortcut, $this->GetIndex(), $fRank++);
  866. }
  867. // Complete the tree
  868. //
  869. parent::PopulateChildMenus();
  870. }
  871. }
  872. require_once(APPROOT.'application/shortcut.class.inc.php');
  873. /**
  874. * This class defines a menu item which content is a shortcut.
  875. */
  876. class ShortcutMenuNode extends MenuNode
  877. {
  878. protected $oShortcut;
  879. /**
  880. * Create a menu item based on a custom template and inserts it into the application's main menu
  881. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  882. * @param object $oShortcut Shortcut object
  883. * @param integer $iParentIndex ID of the parent menu
  884. * @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
  885. * @param string $sEnableClass Name of class of object
  886. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  887. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  888. * @return MenuNode
  889. */
  890. public function __construct($sMenuId, $oShortcut, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  891. {
  892. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  893. $this->oShortcut = $oShortcut;
  894. $this->aReflectionProperties['shortcut'] = $oShortcut->GetKey();
  895. }
  896. public function GetHyperlink($aExtraParams)
  897. {
  898. $sContext = $this->oShortcut->Get('context');
  899. $aContext = unserialize($sContext);
  900. if (isset($aContext['menu']))
  901. {
  902. unset($aContext['menu']);
  903. }
  904. foreach ($aContext as $sArgName => $sArgValue)
  905. {
  906. $aExtraParams[$sArgName] = $sArgValue;
  907. }
  908. return parent::GetHyperlink($aExtraParams);
  909. }
  910. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  911. {
  912. $this->oShortcut->RenderContent($oPage, $aExtraParams);
  913. }
  914. public function GetTitle()
  915. {
  916. return $this->oShortcut->Get('name');
  917. }
  918. public function GetLabel()
  919. {
  920. return $this->oShortcut->Get('name');
  921. }
  922. }