menunode.class.inc.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. 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. self::$bAdditionalMenusLoaded = true;
  77. }
  78. }
  79. /**
  80. * Set the query used to limit the list of displayed organizations in the drop-down menu
  81. * @param $sOQL string The OQL query returning a list of Organization objects
  82. * @return none
  83. */
  84. static public function SetFavoriteSiloQuery($sOQL)
  85. {
  86. self::$sFavoriteSiloQuery = $sOQL;
  87. }
  88. /**
  89. * Get the query used to limit the list of displayed organizations in the drop-down menu
  90. * @return string The OQL query returning a list of Organization objects
  91. */
  92. static public function GetFavoriteSiloQuery()
  93. {
  94. return self::$sFavoriteSiloQuery;
  95. }
  96. /**
  97. * Main function to add a menu entry into the application, can be called during the definition
  98. * of the data model objects
  99. */
  100. static public function InsertMenu(MenuNode $oMenuNode, $iParentIndex, $fRank)
  101. {
  102. $index = self::GetMenuIndexById($oMenuNode->GetMenuId());
  103. if ($index == -1)
  104. {
  105. // The menu does not already exist, insert it
  106. $index = count(self::$aMenusIndex);
  107. if ($iParentIndex == -1)
  108. {
  109. $sParentId = '';
  110. self::$aRootMenus[] = array ('rank' => $fRank, 'index' => $index);
  111. }
  112. else
  113. {
  114. $sParentId = self::$aMenusIndex[$iParentIndex]['node']->GetMenuId();
  115. self::$aMenusIndex[$iParentIndex]['children'][] = array ('rank' => $fRank, 'index' => $index);
  116. }
  117. // Note: At the time when 'parent', 'rank' and 'source_file' have been added for the reflection API,
  118. // they were not used to display the menus (redundant or unused)
  119. //
  120. $aBacktrace = debug_backtrace();
  121. $sFile = $aBacktrace[2]["file"];
  122. self::$aMenusIndex[$index] = array('node' => $oMenuNode, 'children' => array(), 'parent' => $sParentId, 'rank' => $fRank, 'source_file' => $sFile);
  123. }
  124. else
  125. {
  126. // the menu already exists, let's combine the conditions that make it visible
  127. self::$aMenusIndex[$index]['node']->AddCondition($oMenuNode);
  128. }
  129. return $index;
  130. }
  131. /**
  132. * Reflection API - Get menu entries
  133. */
  134. static public function ReflectionMenuNodes()
  135. {
  136. self::LoadAdditionalMenus();
  137. return self::$aMenusIndex;
  138. }
  139. /**
  140. * Entry point to display the whole menu into the web page, used by iTopWebPage
  141. */
  142. static public function DisplayMenu(iTopWebPage $oPage, $aExtraParams)
  143. {
  144. self::LoadAdditionalMenus();
  145. // Sort the root menu based on the rank
  146. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  147. $iAccordion = 0;
  148. $iActiveMenu = self::GetMenuIndexById(self::GetActiveNodeId());
  149. foreach(self::$aRootMenus as $aMenu)
  150. {
  151. $oMenuNode = self::GetMenuNode($aMenu['index']);
  152. if (!$oMenuNode->IsEnabled()) continue; // Don't display a non-enabled menu
  153. $oPage->AddToMenu('<h3>'.$oMenuNode->GetTitle().'</h3>');
  154. $oPage->AddToMenu('<div>');
  155. $aChildren = self::GetChildren($aMenu['index']);
  156. if (count($aChildren) > 0)
  157. {
  158. $oPage->AddToMenu('<ul>');
  159. $bActive = self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  160. $oPage->AddToMenu('</ul>');
  161. if ($bActive)
  162. {
  163. $oPage->add_ready_script("$('#accordion').accordion('activate', $iAccordion);");
  164. $oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true});"); // Make it auto-collapsible once it has been opened properly
  165. }
  166. }
  167. $oPage->AddToMenu('</div>');
  168. $iAccordion++;
  169. }
  170. }
  171. /**
  172. * Handles the display of the sub-menus (called recursively if necessary)
  173. * @return true if the currently selected menu is one of the submenus
  174. */
  175. static protected function DisplaySubMenu($oPage, $aMenus, $aExtraParams, $iActiveMenu = -1)
  176. {
  177. // Sort the menu based on the rank
  178. $bActive = false;
  179. usort($aMenus, array('ApplicationMenu', 'CompareOnRank'));
  180. foreach($aMenus as $aMenu)
  181. {
  182. $index = $aMenu['index'];
  183. $oMenu = self::GetMenuNode($index);
  184. if ($oMenu->IsEnabled())
  185. {
  186. $aChildren = self::GetChildren($index);
  187. $sCSSClass = (count($aChildren) > 0) ? ' class="submenu"' : '';
  188. $sHyperlink = $oMenu->GetHyperlink($aExtraParams);
  189. if ($sHyperlink != '')
  190. {
  191. $oPage->AddToMenu('<li'.$sCSSClass.'><a href="'.$oMenu->GetHyperlink($aExtraParams).'">'.$oMenu->GetTitle().'</a></li>');
  192. }
  193. else
  194. {
  195. $oPage->AddToMenu('<li'.$sCSSClass.'>'.$oMenu->GetTitle().'</li>');
  196. }
  197. $aCurrentMenu = self::$aMenusIndex[$index];
  198. if ($iActiveMenu == $index)
  199. {
  200. $bActive = true;
  201. }
  202. if (count($aChildren) > 0)
  203. {
  204. $oPage->AddToMenu('<ul>');
  205. $bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
  206. $oPage->AddToMenu('</ul>');
  207. }
  208. }
  209. }
  210. return $bActive;
  211. }
  212. /**
  213. * Helper function to sort the menus based on their rank
  214. */
  215. static public function CompareOnRank($a, $b)
  216. {
  217. $result = 1;
  218. if ($a['rank'] == $b['rank'])
  219. {
  220. $result = 0;
  221. }
  222. if ($a['rank'] < $b['rank'])
  223. {
  224. $result = -1;
  225. }
  226. return $result;
  227. }
  228. /**
  229. * Helper function to retrieve the MenuNodeObject based on its ID
  230. */
  231. static public function GetMenuNode($index)
  232. {
  233. return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
  234. }
  235. /**
  236. * Helper function to get the list of child(ren) of a menu
  237. */
  238. static protected function GetChildren($index)
  239. {
  240. return self::$aMenusIndex[$index]['children'];
  241. }
  242. /**
  243. * Helper function to get the ID of a menu based on its name
  244. * @param string $sTitle Title of the menu (as passed when creating the menu)
  245. * @return integer ID of the menu, or -1 if not found
  246. */
  247. static public function GetMenuIndexById($sTitle)
  248. {
  249. $index = -1;
  250. foreach(self::$aMenusIndex as $aMenu)
  251. {
  252. if ($aMenu['node']->GetMenuId() == $sTitle)
  253. {
  254. $index = $aMenu['node']->GetIndex();
  255. break;
  256. }
  257. }
  258. return $index;
  259. }
  260. /**
  261. * Retrieves the currently active menu (if any, otherwise the first menu is the default)
  262. * @return string The Id of the currently active menu
  263. */
  264. static public function GetActiveNodeId()
  265. {
  266. $oAppContext = new ApplicationContext();
  267. $sMenuId = $oAppContext->GetCurrentValue('menu', null);
  268. if ($sMenuId === null)
  269. {
  270. // Make sure the root menu is sorted on 'rank'
  271. usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
  272. $oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
  273. $oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
  274. $sMenuId = $oMenuNode->GetMenuId();
  275. }
  276. return $sMenuId;
  277. }
  278. }
  279. /**
  280. * Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
  281. * MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
  282. * derived from MenuNode is enough to have it inserted in the application's main menu.
  283. * The class iTopWebPage, takes care of 3 items:
  284. * +--------------------+
  285. * | Welcome |
  286. * +--------------------+
  287. * Welcome To iTop
  288. * +--------------------+
  289. * | Tools |
  290. * +--------------------+
  291. * CSV Import
  292. * +--------------------+
  293. * | Admin Tools |
  294. * +--------------------+
  295. * User Accounts
  296. * Profiles
  297. * Notifications
  298. * Run Queries
  299. * Export
  300. * Data Model
  301. * Universal Search
  302. *
  303. * All the other menu items must constructed along with the various data model modules
  304. */
  305. abstract class MenuNode
  306. {
  307. protected $sMenuId;
  308. protected $index;
  309. /**
  310. * Properties reflecting how the node has been declared
  311. */
  312. protected $aReflectionProperties;
  313. /**
  314. * Class of objects to check if the menu is enabled, null if none
  315. */
  316. protected $m_aEnableClasses;
  317. /**
  318. * User Rights Action code to check if the menu is enabled, null if none
  319. */
  320. protected $m_aEnableActions;
  321. /**
  322. * User Rights allowed results (actually a bitmask) to check if the menu is enabled, null if none
  323. */
  324. protected $m_aEnableActionResults;
  325. /**
  326. * Stimulus to check: if the user can 'apply' this stimulus, then she/he can see this menu
  327. */
  328. protected $m_aEnableStimuli;
  329. /**
  330. * Create a menu item, sets the condition to have it displayed and inserts it into the application's main menu
  331. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  332. * @param integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
  333. * @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
  334. * @param string $sEnableClass Name of class of object
  335. * @param mixed $iActionCode UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  336. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  337. * @param string $sEnableStimulus The user can see this menu if she/he has enough rights to apply this stimulus
  338. * @return MenuNode
  339. */
  340. public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  341. {
  342. $this->sMenuId = $sMenuId;
  343. $this->aReflectionProperties = array();
  344. if (strlen($sEnableClass) > 0)
  345. {
  346. $this->aReflectionProperties['enable_class'] = $sEnableClass;
  347. $this->aReflectionProperties['enable_action'] = $iActionCode;
  348. $this->aReflectionProperties['enable_permission'] = $iAllowedResults;
  349. $this->aReflectionProperties['enable_stimulus'] = $sEnableStimulus;
  350. }
  351. $this->m_aEnableClasses = array($sEnableClass);
  352. $this->m_aEnableActions = array($iActionCode);
  353. $this->m_aEnableActionResults = array($iAllowedResults);
  354. $this->m_aEnableStimuli = array($sEnableStimulus);
  355. $this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
  356. }
  357. public function ReflectionProperties()
  358. {
  359. return $this->aReflectionProperties;
  360. }
  361. public function GetMenuId()
  362. {
  363. return $this->sMenuId;
  364. }
  365. public function GetTitle()
  366. {
  367. return Dict::S("Menu:$this->sMenuId", str_replace('_', ' ', $this->sMenuId));
  368. }
  369. public function GetLabel()
  370. {
  371. return Dict::S("Menu:$this->sMenuId+", "");
  372. }
  373. public function GetIndex()
  374. {
  375. return $this->index;
  376. }
  377. public function GetHyperlink($aExtraParams)
  378. {
  379. $aExtraParams['c[menu]'] = $this->GetMenuId();
  380. return $this->AddParams(utils::GetAbsoluteUrlAppRoot().'pages/UI.php', $aExtraParams);
  381. }
  382. /**
  383. * Add a limiting display condition for the same menu node. The conditions will be combined with a AND
  384. * @param $oMenuNode MenuNode Another definition of the same menu node, with potentially different access restriction
  385. * @return void
  386. */
  387. public function AddCondition(MenuNode $oMenuNode)
  388. {
  389. foreach($oMenuNode->m_aEnableClasses as $index => $sClass )
  390. {
  391. $this->m_aEnableClasses[] = $sClass;
  392. $this->m_aEnableActions[] = $oMenuNode->m_aEnableActions[$index];
  393. $this->m_aEnableActionResults[] = $oMenuNode->m_aEnableActionResults[$index];
  394. $this->m_aEnableStimuli[] = $oMenuNode->m_aEnableStimuli[$index];
  395. }
  396. }
  397. /**
  398. * Tells whether the menu is enabled (i.e. displayed) for the current user
  399. * @return bool True if enabled, false otherwise
  400. */
  401. public function IsEnabled()
  402. {
  403. foreach($this->m_aEnableClasses as $index => $sClass)
  404. {
  405. if ($sClass != null)
  406. {
  407. if (MetaModel::IsValidClass($sClass))
  408. {
  409. if ($this->m_aEnableStimuli[$index] != null)
  410. {
  411. if (!UserRights::IsStimulusAllowed($sClass, $this->m_aEnableStimuli[$index]))
  412. {
  413. return false;
  414. }
  415. }
  416. if ($this->m_aEnableActions[$index] != null)
  417. {
  418. $iResult = UserRights::IsActionAllowed($sClass, $this->m_aEnableActions[$index]);
  419. if (!($iResult & $this->m_aEnableActionResults[$index]))
  420. {
  421. return false;
  422. }
  423. }
  424. }
  425. else
  426. {
  427. return false;
  428. }
  429. }
  430. }
  431. return true;
  432. }
  433. public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
  434. protected function AddParams($sHyperlink, $aExtraParams)
  435. {
  436. if (count($aExtraParams) > 0)
  437. {
  438. $aQuery = array();
  439. $sSeparator = '?';
  440. if (strpos($sHyperlink, '?') !== false)
  441. {
  442. $sSeparator = '&';
  443. }
  444. foreach($aExtraParams as $sName => $sValue)
  445. {
  446. $aQuery[] = urlencode($sName).'='.urlencode($sValue);
  447. }
  448. $sHyperlink .= $sSeparator.implode('&', $aQuery);
  449. }
  450. return $sHyperlink;
  451. }
  452. }
  453. /**
  454. * This class implements a top-level menu group. A group is just a container for sub-items
  455. * it does not display a page by itself
  456. */
  457. class MenuGroup extends MenuNode
  458. {
  459. /**
  460. * Create a top-level menu group and inserts it into the application's main menu
  461. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  462. * @param float $fRank Number used to order the list, the groups are sorted based on this value
  463. * @param string $sEnableClass Name of class of object
  464. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  465. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  466. * @return MenuGroup
  467. */
  468. public function __construct($sMenuId, $fRank, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  469. {
  470. parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  471. }
  472. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  473. {
  474. assert(false); // Shall never be called, groups do not display any content
  475. }
  476. }
  477. /**
  478. * This class defines a menu item which content is based on a custom template.
  479. * Note the template can be either a local file or an URL !
  480. */
  481. class TemplateMenuNode extends MenuNode
  482. {
  483. protected $sTemplateFile;
  484. /**
  485. * Create a menu item based on a custom template and inserts it into the application's main menu
  486. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  487. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  488. * @param integer $iParentIndex ID of the parent menu
  489. * @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
  490. * @param string $sEnableClass Name of class of object
  491. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  492. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  493. * @return MenuNode
  494. */
  495. public function __construct($sMenuId, $sTemplateFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  496. {
  497. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  498. $this->sTemplateFile = $sTemplateFile;
  499. $this->aReflectionProperties['template_file'] = $sTemplateFile;
  500. }
  501. public function GetHyperlink($aExtraParams)
  502. {
  503. if ($this->sTemplateFile == '') return '';
  504. return parent::GetHyperlink($aExtraParams);
  505. }
  506. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  507. {
  508. $sTemplate = @file_get_contents($this->sTemplateFile);
  509. if ($sTemplate !== false)
  510. {
  511. $aExtraParams['table_id'] = 'Menu_'.$this->GetMenuId();
  512. $oTemplate = new DisplayTemplate($sTemplate);
  513. $oTemplate->Render($oPage, $aExtraParams);
  514. }
  515. else
  516. {
  517. $oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
  518. }
  519. }
  520. }
  521. /**
  522. * This class defines a menu item that uses a standard template to display a list of items therefore it allows
  523. * only two parameters: the page's title and the OQL expression defining the list of items to be displayed
  524. */
  525. class OQLMenuNode extends MenuNode
  526. {
  527. protected $sPageTitle;
  528. protected $sOQL;
  529. protected $bSearch;
  530. /**
  531. * Extra parameters to be passed to the display block to fine tune its appearence
  532. */
  533. protected $m_aParams;
  534. /**
  535. * Create a menu item based on an OQL query and inserts it into the application's main menu
  536. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  537. * @param string $sOQL OQL query defining the set of objects to be displayed
  538. * @param integer $iParentIndex ID of the parent menu
  539. * @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
  540. * @param bool $bSearch Whether or not to display a (collapsed) search frame at the top of the page
  541. * @param string $sEnableClass Name of class of object
  542. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  543. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  544. * @return MenuNode
  545. */
  546. public function __construct($sMenuId, $sOQL, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  547. {
  548. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  549. $this->sPageTitle = "Menu:$sMenuId+";
  550. $this->sOQL = $sOQL;
  551. $this->bSearch = $bSearch;
  552. $this->m_aParams = array();
  553. $this->aReflectionProperties['oql'] = $sOQL;
  554. $this->aReflectionProperties['do_search'] = $bSearch;
  555. // Enhancement: we could set as the "enable" condition that the user has enough rights to "read" the objects
  556. // of the class specified by the OQL...
  557. }
  558. /**
  559. * Set some extra parameters to be passed to the display block to fine tune its appearence
  560. * @param Hash $aParams paramCode => value. See DisplayBlock::GetDisplay for the meaning of the parameters
  561. */
  562. public function SetParameters($aParams)
  563. {
  564. $this->m_aParams = $aParams;
  565. foreach($aParams as $sKey => $value)
  566. {
  567. $this->aReflectionProperties[$sKey] = $value;
  568. }
  569. }
  570. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  571. {
  572. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  573. try
  574. {
  575. $oSearch = DBObjectSearch::FromOQL($this->sOQL);
  576. $sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
  577. }
  578. catch(Exception $e)
  579. {
  580. $sIcon = '';
  581. }
  582. if ($this->bSearch)
  583. {
  584. $aParams = array_merge(array('open' => true, 'table_id' => 'Menu_'.$this->GetMenuId()), $aExtraParams);
  585. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  586. $oBlock->Display($oPage, 0);
  587. }
  588. $oPage->add("<p class=\"page-header\">$sIcon ".Dict::S($this->sPageTitle)."</p>");
  589. $aParams = array_merge(array('table_id' => 'Menu_'.$this->GetMenuId()), $aExtraParams);
  590. $oBlock = new DisplayBlock($oSearch, 'list', false /* Asynchronous */, $aParams);
  591. $oBlock->Display($oPage, 1);
  592. }
  593. }
  594. /**
  595. * This class defines a menu item that displays a search form for the given class of objects
  596. */
  597. class SearchMenuNode extends MenuNode
  598. {
  599. protected $sPageTitle;
  600. protected $sClass;
  601. /**
  602. * Create a menu item based on an OQL query and inserts it into the application's main menu
  603. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  604. * @param string $sClass The class of objects to search for
  605. * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation)
  606. * @param integer $iParentIndex ID of the parent menu
  607. * @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
  608. * @param string $sEnableClass Name of class of object
  609. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  610. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  611. * @return MenuNode
  612. */
  613. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  614. {
  615. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  616. $this->sPageTitle = "Menu:$sMenuId+";
  617. $this->sClass = $sClass;
  618. $this->aReflectionProperties['class'] = $sClass;
  619. }
  620. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  621. {
  622. $oSearch = new DBObjectSearch($this->sClass);
  623. $aParams = array_merge(array('open' => true, 'table_id' => 'Menu_'.$this->GetMenuId()), $aExtraParams);
  624. $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams);
  625. $oBlock->Display($oPage, 0);
  626. }
  627. }
  628. /**
  629. * This class defines a menu that points to any web page. It takes only two parameters:
  630. * - The hyperlink to point to
  631. * - The name of the menu
  632. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  633. * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu
  634. */
  635. class WebPageMenuNode extends MenuNode
  636. {
  637. protected $sHyperlink;
  638. /**
  639. * Create a menu item that points to any web page (not only UI.php)
  640. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  641. * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable !
  642. * @param integer $iParentIndex ID of the parent menu
  643. * @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
  644. * @param string $sEnableClass Name of class of object
  645. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  646. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  647. * @return MenuNode
  648. */
  649. public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  650. {
  651. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  652. $this->sHyperlink = $sHyperlink;
  653. $this->aReflectionProperties['url'] = $sHyperlink;
  654. }
  655. public function GetHyperlink($aExtraParams)
  656. {
  657. $aExtraParams['c[menu]'] = $this->GetMenuId();
  658. return $this->AddParams( $this->sHyperlink, $aExtraParams);
  659. }
  660. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  661. {
  662. assert(false); // Shall never be called, the external web page will handle the display by itself
  663. }
  664. }
  665. /**
  666. * This class defines a menu that points to the page for creating a new object of the specified class.
  667. * It take only one parameter: the name of the class
  668. * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink
  669. * in order to make it the active one
  670. */
  671. class NewObjectMenuNode extends MenuNode
  672. {
  673. protected $sClass;
  674. /**
  675. * 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
  676. * rights to create such an object (or an object of a child class)
  677. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  678. * @param string $sClass URL to the page to load. Use relative URL if you want to keep the application portable !
  679. * @param integer $iParentIndex ID of the parent menu
  680. * @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
  681. * @return MenuNode
  682. */
  683. public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0)
  684. {
  685. parent::__construct($sMenuId, $iParentIndex, $fRank);
  686. $this->sClass = $sClass;
  687. $this->aReflectionProperties['class'] = $sClass;
  688. }
  689. public function GetHyperlink($aExtraParams)
  690. {
  691. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=new&class='.$this->sClass;
  692. $aExtraParams['c[menu]'] = $this->GetMenuId();
  693. return $this->AddParams($sHyperlink, $aExtraParams);
  694. }
  695. /**
  696. * Overload the check of the "enable" state of this menu to take into account
  697. * derived classes of objects
  698. */
  699. public function IsEnabled()
  700. {
  701. // Enable this menu, only if the current user has enough rights to create such an object, or an object of
  702. // any child class
  703. $aSubClasses = MetaModel::EnumChildClasses($this->sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself
  704. $bActionIsAllowed = false;
  705. foreach($aSubClasses as $sCandidateClass)
  706. {
  707. if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  708. {
  709. $bActionIsAllowed = true;
  710. break; // Enough for now
  711. }
  712. }
  713. return $bActionIsAllowed;
  714. }
  715. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  716. {
  717. assert(false); // Shall never be called, the external web page will handle the display by itself
  718. }
  719. }
  720. require_once(APPROOT.'application/dashboard.class.inc.php');
  721. /**
  722. * This class defines a menu item which content is based on XML dashboard.
  723. */
  724. class DashboardMenuNode extends MenuNode
  725. {
  726. protected $sDashboardFile;
  727. /**
  728. * Create a menu item based on a custom template and inserts it into the application's main menu
  729. * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary)
  730. * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content
  731. * @param integer $iParentIndex ID of the parent menu
  732. * @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
  733. * @param string $sEnableClass Name of class of object
  734. * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE
  735. * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them...
  736. * @return MenuNode
  737. */
  738. public function __construct($sMenuId, $sDashboardFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
  739. {
  740. parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
  741. $this->sDashboardFile = $sDashboardFile;
  742. $this->aReflectionProperties['definition_file'] = $sDashboardFile;
  743. }
  744. public function GetHyperlink($aExtraParams)
  745. {
  746. if ($this->sDashboardFile == '') return '';
  747. return parent::GetHyperlink($aExtraParams);
  748. }
  749. public function GetDashboard()
  750. {
  751. $sDashboardDefinition = @file_get_contents($this->sDashboardFile);
  752. if ($sDashboardDefinition !== false)
  753. {
  754. $bCustomized = false;
  755. // Search for an eventual user defined dashboard, overloading the existing one
  756. $oUDSearch = new DBObjectSearch('UserDashboard');
  757. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  758. $oUDSearch->AddCondition('menu_code', $this->sMenuId, '=');
  759. $oUDSet = new DBObjectSet($oUDSearch);
  760. if ($oUDSet->Count() > 0)
  761. {
  762. // Assuming there is at most one couple {user, menu}!
  763. $oUserDashboard = $oUDSet->Fetch();
  764. $sDashboardDefinition = $oUserDashboard->Get('contents');
  765. $bCustomized = true;
  766. }
  767. $oDashboard = new RuntimeDashboard($this->sMenuId);
  768. $oDashboard->FromXml($sDashboardDefinition);
  769. $oDashboard->SetCustomFlag($bCustomized);
  770. }
  771. else
  772. {
  773. $oDashboard = null;
  774. }
  775. return $oDashboard;
  776. }
  777. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  778. {
  779. $oDashboard = $this->GetDashboard();
  780. if ($oDashboard != null)
  781. {
  782. $oDashboard->Render($oPage, false, $aExtraParams);
  783. $bEdit = utils::ReadParam('edit', false);
  784. if ($bEdit)
  785. {
  786. $sId = addslashes($this->sMenuId);
  787. $oPage->add_ready_script("EditDashboard('$sId');");
  788. }
  789. }
  790. else
  791. {
  792. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  793. }
  794. }
  795. public function RenderEditor(WebPage $oPage)
  796. {
  797. $oDashboard = $this->GetDashboard();
  798. if ($oDashboard != null)
  799. {
  800. $oDashboard->RenderEditor($oPage);
  801. }
  802. else
  803. {
  804. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  805. }
  806. }
  807. public function AddDashlet($oDashlet)
  808. {
  809. $oDashboard = $this->GetDashboard();
  810. if ($oDashboard != null)
  811. {
  812. $oDashboard->AddDashlet($oDashlet);
  813. $oDashboard->Save();
  814. }
  815. else
  816. {
  817. $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
  818. }
  819. }
  820. }