menunode.class.inc.php 32 KB

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