menunode.class.inc.php 33 KB

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