* @author Romain Quetiez '.$oMenuNode->GetTitle().'
');
$oPage->AddToMenu('');
$bActive = self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
$oPage->AddToMenu('
');
if ($bActive)
{
$oPage->add_ready_script("$('#accordion').accordion('activate', $iAccordion);");
$oPage->add_ready_script("$('#accordion').accordion('option', {collapsible: true});"); // Make it auto-collapsible once it has been opened properly
}
}
$oPage->AddToMenu('');
$bActive |= self::DisplaySubMenu($oPage, $aChildren, $aExtraParams, $iActiveMenu);
$oPage->AddToMenu('
');
}
}
}
return $bActive;
}
/**
* Helper function to sort the menus based on their rank
*/
static public function CompareOnRank($a, $b)
{
$result = 1;
if ($a['rank'] == $b['rank'])
{
$result = 0;
}
if ($a['rank'] < $b['rank'])
{
$result = -1;
}
return $result;
}
/**
* Helper function to retrieve the MenuNodeObject based on its ID
*/
static public function GetMenuNode($index)
{
return isset(self::$aMenusIndex[$index]) ? self::$aMenusIndex[$index]['node'] : null;
}
/**
* Helper function to get the list of child(ren) of a menu
*/
static protected function GetChildren($index)
{
return self::$aMenusIndex[$index]['children'];
}
/**
* Helper function to get the ID of a menu based on its name
* @param string $sTitle Title of the menu (as passed when creating the menu)
* @return integer ID of the menu, or -1 if not found
*/
static public function GetMenuIndexById($sTitle)
{
$index = -1;
foreach(self::$aMenusIndex as $aMenu)
{
if ($aMenu['node']->GetMenuId() == $sTitle)
{
$index = $aMenu['node']->GetIndex();
break;
}
}
return $index;
}
/**
* Retrieves the currently active menu (if any, otherwise the first menu is the default)
* @return MenuNode or null if there is no menu at all !
*/
static public function GetActiveNodeId()
{
$oAppContext = new ApplicationContext();
$iMenuIndex = $oAppContext->GetCurrentValue('menu', -1);
if ($iMenuIndex == -1)
{
// Make sure the root menu is sorted on 'rank'
usort(self::$aRootMenus, array('ApplicationMenu', 'CompareOnRank'));
$oFirstGroup = self::GetMenuNode(self::$aRootMenus[0]['index']);
$oMenuNode = self::GetMenuNode(self::$aMenusIndex[$oFirstGroup->GetIndex()]['children'][0]['index']);
$iMenuIndex = $oMenuNode->GetIndex();
}
return $iMenuIndex;
}
}
/**
* Root class for all the kind of node in the menu tree, data model providers are responsible for instantiating
* MenuNodes (i.e instances from derived classes) in order to populate the application's menu. Creating an objet
* derived from MenuNode is enough to have it inserted in the application's main menu.
* The class iTopWebPage, takes care of 3 items:
* +--------------------+
* | Welcome |
* +--------------------+
* Welcome To iTop
* +--------------------+
* | Tools |
* +--------------------+
* CSV Import
* +--------------------+
* | Admin Tools |
* +--------------------+
* User Accounts
* Profiles
* Notifications
* Run Queries
* Export
* Data Model
* Universal Search
*
* All the other menu items must constructed along with the various data model modules
*/
abstract class MenuNode
{
protected $sMenuId;
protected $index;
/**
* Class of objects to check if the menu is enabled, null if none
*/
protected $m_sEnableClass;
/**
* User Rights Action code to check if the menu is enabled, null if none
*/
protected $m_iEnableAction;
/**
* User Rights allowed results (actually a bitmask) to check if the menu is enabled, null if none
*/
protected $m_iEnableActionResults;
/**
* Stimulus to check: if the user can 'apply' this stimulus, then she/he can see this menu
*/
protected $m_sEnableStimulus;
/**
* Create a menu item, sets the condition to have it displayed 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 integer $iParentIndex ID of the parent menu, pass -1 for top level (group) items
* @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 mixed $iActionCode 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...
* @param string $sEnableStimulus The user can see this menu if she/he has enough rights to apply this stimulus
* @return MenuNode
*/
public function __construct($sMenuId, $iParentIndex = -1, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
{
$this->sMenuId = $sMenuId;
$this->m_sEnableClass = $sEnableClass;
$this->m_iEnableAction = $iActionCode;
$this->m_iEnableActionResults = $iAllowedResults;
$this->m_sEnableStimulus = $sEnableStimulus;
$this->index = ApplicationMenu::InsertMenu($this, $iParentIndex, $fRank);
}
public function GetMenuId()
{
return $this->sMenuId;
}
public function GetTitle()
{
return Dict::S("Menu:$this->sMenuId");
}
public function GetLabel()
{
return Dict::S("Menu:$this->sMenuId+");
}
public function GetIndex()
{
return $this->index;
}
public function GetHyperlink($aExtraParams)
{
$aExtraParams['c[menu]'] = $this->GetIndex();
return $this->AddParams('../pages/UI.php', $aExtraParams);
}
/**
* Tells whether the menu is enabled (i.e. displayed) for the current user
* @return bool True if enabled, false otherwise
*/
public function IsEnabled()
{
if ($this->m_sEnableClass != null)
{
if (MetaModel::IsValidClass($this->m_sEnableClass))
{
if ($this->m_sEnableStimulus != null)
{
if (!UserRights::IsStimulusAllowed($this->m_sEnableClass, $this->m_sEnableStimulus))
{
return false;
}
}
if ($this->m_iEnableAction != null)
{
$iResult = UserRights::IsActionAllowed($this->m_sEnableClass, $this->m_iEnableAction);
if (($iResult & $this->m_iEnableActionResults))
{
return true;
}
else
{
return false;
}
}
return true;
}
return false;
}
return true;
}
public abstract function RenderContent(WebPage $oPage, $aExtraParams = array());
protected function AddParams($sHyperlink, $aExtraParams)
{
if (count($aExtraParams) > 0)
{
$aQuery = array();
$sSeparator = '?';
if (strpos($sHyperlink, '?') !== false)
{
$sSeparator = '&';
}
foreach($aExtraParams as $sName => $sValue)
{
$aQuery[] = urlencode($sName).'='.urlencode($sValue);
}
$sHyperlink .= $sSeparator.implode('&', $aQuery);
}
return $sHyperlink;
}
}
/**
* This class implements a top-level menu group. A group is just a container for sub-items
* it does not display a page by itself
*/
class MenuGroup extends MenuNode
{
/**
* Create a top-level menu group 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 float $fRank Number used to order the list, the groups 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 MenuGroup
*/
public function __construct($sMenuId, $fRank, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
{
parent::__construct($sMenuId, -1 /* no parent, groups are at root level */, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
}
public function RenderContent(WebPage $oPage, $aExtraParams = array())
{
assert(false); // Shall never be called, groups do not display any content
}
}
/**
* This class defines a menu item which content is based on a custom template.
* Note the template can be either a local file or an URL !
*/
class TemplateMenuNode extends MenuNode
{
protected $sTemplateFile;
/**
* 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, $sTemplateFile, $iParentIndex, $fRank = 0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
{
parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
$this->sTemplateFile = $sTemplateFile;
}
public function GetHyperlink($aExtraParams)
{
if ($this->sTemplateFile == '') return '';
return parent::GetHyperlink($aExtraParams);
}
public function RenderContent(WebPage $oPage, $aExtraParams = array())
{
$sTemplate = @file_get_contents($this->sTemplateFile);
if ($sTemplate !== false)
{
$oTemplate = new DisplayTemplate($sTemplate);
$oTemplate->Render($oPage, $aExtraParams);
}
else
{
$oPage->p("Error: failed to load template file: '{$this->sTemplateFile}'"); // No need to translate ?
}
}
}
/**
* This class defines a menu item that uses a standard template to display a list of items therefore it allows
* only two parameters: the page's title and the OQL expression defining the list of items to be displayed
*/
class OQLMenuNode extends MenuNode
{
protected $sPageTitle;
protected $sOQL;
protected $bSearch;
/**
* Extra parameters to be passed to the display block to fine tune its appearence
*/
protected $m_aParams;
/**
* 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 $sOQL OQL query defining the set of objects to be displayed
* @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 bool $bSearch Whether or not to display a (collapsed) search frame at the top of the page
* @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, $sOQL, $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->sOQL = $sOQL;
$this->bSearch = $bSearch;
$this->m_aParams = array();
// Enhancement: we could set as the "enable" condition that the user has enough rights to "read" the objects
// of the class specified by the OQL...
}
/**
* Set some extra parameters to be passed to the display block to fine tune its appearence
* @param Hash $aParams paramCode => value. See DisplayBlock::GetDisplay for the meaning of the parameters
*/
public function SetParameters($aParams)
{
$this->m_aParams = $aParams;
}
public function RenderContent(WebPage $oPage, $aExtraParams = array())
{
$aExtraParams = array_merge($aExtraParams, $this->m_aParams);
try
{
$oSearch = DBObjectSearch::FromOQL($this->sOQL);
$sIcon = MetaModel::GetClassIcon($oSearch->GetClass());
}
catch(Exception $e)
{
$sIcon = '';
}
// The standard template used for all such pages: a (closed) search form at the top and a list of results at the bottom
$sTemplate = '';
if ($this->bSearch)
{
$sTemplate .= <<