/** * Construction and display of the application's main menu * * @copyright Copyright (C) 2010-2012 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ require_once(APPROOT.'/application/utils.inc.php'); require_once(APPROOT.'/application/template.class.inc.php'); require_once(APPROOT."/application/user.dashboard.class.inc.php"); /** * This class manipulates, stores and displays the navigation menu used in the application * In order to improve the modularity of the data model and to ease the update/migration * between evolving data models, the menus are no longer stored in the database, but are instead * built on the fly each time a page is loaded. * The application's menu is organized into top-level groups with, inside each group, a tree of menu items. * Top level groups do not display any content, they just expand/collapse. * Sub-items drive the actual content of the page, they are based either on templates, OQL queries or full (external?) web pages. * * Example: * Here is how to insert the following items in the application's menu: * +----------------------------------------+ * | Configuration Management Group | >> Top level group * +----------------------------------------+ * + Configuration Management Overview >> Template based menu item * + Contacts >> Template based menu item * + Persons >> Plain list (OQL based) * + Teams >> Plain list (OQL based) * * // Create the top-level group. fRank = 1, means it will be inserted after the group '0', which is usually 'Welcome' * $oConfigMgmtMenu = new MenuGroup('ConfigurationManagementMenu', 1); * // Create an entry, based on a custom template, for the Configuration management overview, under the top-level group * new TemplateMenuNode('ConfigurationManagementMenu', '../somedirectory/configuration_management_menu.html', $oConfigMgmtMenu->GetIndex(), 0); * // Create an entry (template based) for the overview of contacts * $oContactsMenu = new TemplateMenuNode('ContactsMenu', '../somedirectory/configuration_management_menu.html',$oConfigMgmtMenu->GetIndex(), 1); * // Plain list of persons * new OQLMenuNode('PersonsMenu', 'SELECT bizPerson', $oContactsMenu->GetIndex(), 0); * */ class ApplicationMenu { static $bAdditionalMenusLoaded = false; static $aRootMenus = array(); static $aMenusIndex = array(); static $sFavoriteSiloQuery = 'SELECT Organization'; static public function LoadAdditionalMenus() { if (!self::$bAdditionalMenusLoaded) { // Build menus from module handlers // foreach(get_declared_classes() as $sPHPClass) { if (is_subclass_of($sPHPClass, 'ModuleHandlerAPI')) { $aCallSpec = array($sPHPClass, 'OnMenuCreation'); call_user_func($aCallSpec); } } self::$bAdditionalMenusLoaded = true; } } /** * Set the query used to limit the list of displayed organizations in the drop-down menu * @param $sOQL string The OQL query returning a list of Organization objects * @return none */ static public function SetFavoriteSiloQuery($sOQL) { self::$sFavoriteSiloQuery = $sOQL; } /** * Get the query used to limit the list of displayed organizations in the drop-down menu * @return string The OQL query returning a list of Organization objects */ static public function GetFavoriteSiloQuery() { return self::$sFavoriteSiloQuery; } /** * Main function to add a menu entry into the application, can be called during the definition * of the data model objects */ static public function InsertMenu(MenuNode $oMenuNode, $iParentIndex, $fRank) { $index = self::GetMenuIndexById($oMenuNode->GetMenuId()); if ($index == -1) { // The menu does not already exist, insert it $index = count(self::$aMenusIndex); if ($iParentIndex == -1) { $sParentId = ''; self::$aRootMenus[] = array ('rank' => $fRank, 'index' => $index); } else { $sParentId = self::$aMenusIndex[$iParentIndex]['node']->GetMenuId(); self::$aMenusIndex[$iParentIndex]['children'][] = array ('rank' => $fRank, 'index' => $index); } // Note: At the time when 'parent', 'rank' and 'source_file' have been added for the reflection API, // they were not used to display the menus (redundant or unused) // $aBacktrace = debug_backtrace(); $sFile = $aBacktrace[2]["file"]; self::$aMenusIndex[$index] = array('node' => $oMenuNode, 'children' => array(), 'parent' => $sParentId, 'rank' => $fRank, 'source_file' => $sFile); } else { // the menu already exists, let's combine the conditions that make it visible self::$aMenusIndex[$index]['node']->AddCondition($oMenuNode); } return $index; } /** * Reflection API - Get menu entries */ static public function ReflectionMenuNodes() { self::LoadAdditionalMenus(); return self::$aMenusIndex; } /** * Entry point to display the whole menu into the web page, used by iTopWebPage */ static public function DisplayMenu(iTopWebPage $oPage, $aExtraParams) { self::LoadAdditionalMenus(); // Sort the root menu based on the rank usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank')); $iAccordion = 0; $iActiveMenu = self::GetMenuIndexById(self::GetActiveNodeId()); foreach(self::$aRootMenus as $aMenu) { $oMenuNode = self::GetMenuNode($aMenu['index']); if (!$oMenuNode->IsEnabled()) continue; // Don't display a non-enabled menu $oPage->AddToMenu('
$sIcon ".Dict::S($this->sPageTitle)."
"); $aParams = array_merge(array('table_id' => 'Menu_'.$this->GetMenuId()), $aExtraParams); $oBlock = new DisplayBlock($oSearch, 'list', false /* Asynchronous */, $aParams); $oBlock->Display($oPage, 1); } } /** * This class defines a menu item that displays a search form for the given class of objects */ class SearchMenuNode extends MenuNode { protected $sPageTitle; protected $sClass; /** * Create a menu item based on an OQL query and inserts it into the application's main menu * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary) * @param string $sClass The class of objects to search for * @param string $sPageTitle Title displayed into the page's content (will be looked-up in the dictionnary for translation) * @param integer $iParentIndex ID of the parent menu * @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 * @param string $sEnableClass Name of class of object * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them... * @return MenuNode */ public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0, $bSearch = false, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null) { parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus); $this->sPageTitle = "Menu:$sMenuId+"; $this->sClass = $sClass; $this->aReflectionProperties['class'] = $sClass; } public function RenderContent(WebPage $oPage, $aExtraParams = array()) { $oSearch = new DBObjectSearch($this->sClass); $aParams = array_merge(array('open' => true, 'table_id' => 'Menu_'.$this->GetMenuId()), $aExtraParams); $oBlock = new DisplayBlock($oSearch, 'search', false /* Asynchronous */, $aParams); $oBlock->Display($oPage, 0); } } /** * This class defines a menu that points to any web page. It takes only two parameters: * - The hyperlink to point to * - The name of the menu * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink * in order to make it the active one, if the target page is based on iTopWebPage and therefore displays the menu */ class WebPageMenuNode extends MenuNode { protected $sHyperlink; /** * Create a menu item that points to any web page (not only UI.php) * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary) * @param string $sHyperlink URL to the page to load. Use relative URL if you want to keep the application portable ! * @param integer $iParentIndex ID of the parent menu * @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 * @param string $sEnableClass Name of class of object * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them... * @return MenuNode */ public function __construct($sMenuId, $sHyperlink, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null) { parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus); $this->sHyperlink = $sHyperlink; $this->aReflectionProperties['url'] = $sHyperlink; } public function GetHyperlink($aExtraParams) { $aExtraParams['c[menu]'] = $this->GetMenuId(); return $this->AddParams( $this->sHyperlink, $aExtraParams); } public function RenderContent(WebPage $oPage, $aExtraParams = array()) { assert(false); // Shall never be called, the external web page will handle the display by itself } } /** * This class defines a menu that points to the page for creating a new object of the specified class. * It take only one parameter: the name of the class * Note: the parameter menu=xxx (where xxx is the id of the menu itself) will be added to the hyperlink * in order to make it the active one */ class NewObjectMenuNode extends MenuNode { protected $sClass; /** * 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 * rights to create such an object (or an object of a child class) * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary) * @param string $sClass URL to the page to load. Use relative URL if you want to keep the application portable ! * @param integer $iParentIndex ID of the parent menu * @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 * @return MenuNode */ public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0) { parent::__construct($sMenuId, $iParentIndex, $fRank); $this->sClass = $sClass; $this->aReflectionProperties['class'] = $sClass; } public function GetHyperlink($aExtraParams) { $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=new&class='.$this->sClass; $aExtraParams['c[menu]'] = $this->GetMenuId(); return $this->AddParams($sHyperlink, $aExtraParams); } /** * Overload the check of the "enable" state of this menu to take into account * derived classes of objects */ public function IsEnabled() { // Enable this menu, only if the current user has enough rights to create such an object, or an object of // any child class $aSubClasses = MetaModel::EnumChildClasses($this->sClass, ENUM_CHILD_CLASSES_ALL); // Including the specified class itself $bActionIsAllowed = false; foreach($aSubClasses as $sCandidateClass) { if (!MetaModel::IsAbstract($sCandidateClass) && (UserRights::IsActionAllowed($sCandidateClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES)) { $bActionIsAllowed = true; break; // Enough for now } } return $bActionIsAllowed; } public function RenderContent(WebPage $oPage, $aExtraParams = array()) { assert(false); // Shall never be called, the external web page will handle the display by itself } } require_once(APPROOT.'application/dashboard.class.inc.php'); /** * This class defines a menu item which content is based on XML dashboard. */ class DashboardMenuNode extends MenuNode { protected $sDashboardFile; /** * Create a menu item based on a custom template and inserts it into the application's main menu * @param string $sMenuId Unique identifier of the menu (used to identify the menu for bookmarking, and for getting the labels from the dictionary) * @param string $sTemplateFile Path (or URL) to the file that will be used as a template for displaying the page's content * @param integer $iParentIndex ID of the parent menu * @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 * @param string $sEnableClass Name of class of object * @param integer $iActionCode Either UR_ACTION_READ, UR_ACTION_MODIFY, UR_ACTION_DELETE, UR_ACTION_BULKREAD, UR_ACTION_BULKMODIFY or UR_ACTION_BULKDELETE * @param integer $iAllowedResults Expected "rights" for the action: either UR_ALLOWED_YES, UR_ALLOWED_NO, UR_ALLOWED_DEPENDS or a mix of them... * @return MenuNode */ public function __construct($sMenuId, $sDashboardFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null) { parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus); $this->sDashboardFile = $sDashboardFile; $this->aReflectionProperties['definition_file'] = $sDashboardFile; } public function GetHyperlink($aExtraParams) { if ($this->sDashboardFile == '') return ''; return parent::GetHyperlink($aExtraParams); } public function GetDashboard() { $sDashboardDefinition = @file_get_contents($this->sDashboardFile); if ($sDashboardDefinition !== false) { $bCustomized = false; // Search for an eventual user defined dashboard, overloading the existing one $oUDSearch = new DBObjectSearch('UserDashboard'); $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '='); $oUDSearch->AddCondition('menu_code', $this->sMenuId, '='); $oUDSet = new DBObjectSet($oUDSearch); if ($oUDSet->Count() > 0) { // Assuming there is at most one couple {user, menu}! $oUserDashboard = $oUDSet->Fetch(); $sDashboardDefinition = $oUserDashboard->Get('contents'); $bCustomized = true; } $oDashboard = new RuntimeDashboard($this->sMenuId); $oDashboard->FromXml($sDashboardDefinition); $oDashboard->SetCustomFlag($bCustomized); } else { $oDashboard = null; } return $oDashboard; } public function RenderContent(WebPage $oPage, $aExtraParams = array()) { $oDashboard = $this->GetDashboard(); if ($oDashboard != null) { $oDashboard->Render($oPage, false, $aExtraParams); $bEdit = utils::ReadParam('edit', false); if ($bEdit) { $sId = addslashes($this->sMenuId); $oPage->add_ready_script("EditDashboard('$sId');"); } } else { $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'"); } } public function RenderEditor(WebPage $oPage) { $oDashboard = $this->GetDashboard(); if ($oDashboard != null) { $oDashboard->RenderEditor($oPage); } else { $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'"); } } public function AddDashlet($oDashlet) { $oDashboard = $this->GetDashboard(); if ($oDashboard != null) { $oDashboard->AddDashlet($oDashlet); $oDashboard->Save(); } else { $oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'"); } } }