> 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
*/
class ApplicationMenu
{
/**
* @var bool
*/
public static $bAdditionalMenusLoaded = false;
/**
* @var array
*/
public static $aRootMenus = [];
/**
* @var array
*/
public static $aMenusIndex = [];
/**
* @var array
*/
public static $aMenusById = [];
/**
* @var string
*/
public static $sFavoriteSiloQuery = 'SELECT Organization';
/**
* @return void
*/
public static function LoadAdditionalMenus()
{
if (!self::$bAdditionalMenusLoaded) {
// Build menus from module handlers
//
/** @var \ModuleHandlerApiInterface $oPHPClass */
foreach (MetaModel::EnumPlugins('ModuleHandlerApiInterface') as $oPHPClass) {
$oPHPClass::OnMenuCreation();
}
// Build menus from the menus themselves (e.g. the ShortcutContainerMenuNode will do that)
//
foreach (self::$aRootMenus as $aMenu) {
$oMenuNode = self::GetMenuNode($aMenu['index']);
$oMenuNode->PopulateChildMenus();
}
self::$bAdditionalMenusLoaded = true;
}
}
/**
* Set the query used to limit the list of displayed organizations in the drop-down menu
* @param string $sOQL The OQL query returning a list of Organization objects
* @return void
*/
public static 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
*/
public static function GetFavoriteSiloQuery()
{
return self::$sFavoriteSiloQuery;
}
/**
* Check whether a menu Id is enabled or not
*
* @param string $sMenuId
*
* @throws \Exception
*/
public static function CheckMenuIdEnabled($sMenuId)
{
if (self::IsMenuIdEnabled($sMenuId) === false) {
require_once(APPROOT.'/setup/setuppage.class.inc.php');
$oP = new ErrorPage(Dict::S('UI:PageTitle:FatalError'));
$oP->add("
");
} else {
$oPage->add("
");
}
$aExtraParams['panel_class'] = $sClass;
$aExtraParams['panel_title'] = $sTitle;
$aExtraParams['panel_icon'] = $sIcon;
$aParams = array_merge(['table_id' => $sUsageId], $aExtraParams);
$oBlock = new DisplayBlock($oSearch, 'list', false /* Asynchronous */, $aParams);
$oBlock->Display($oPage, $sUsageId);
$oPage->add("
");
if ($bEnableBreadcrumb && ($oPage instanceof iTopWebPage)) {
// Breadcrumb
//$iCount = $oBlock->GetDisplayedCount();
$sPageId = "ui-search-".$oSearch->GetClass();
$sLabel = MetaModel::GetName($oSearch->GetClass());
$oPage->SetBreadCrumbEntry($sPageId, $sLabel, $sTitle, '', 'fas fa-list', iTopWebPage::ENUM_BREADCRUMB_ENTRY_ICON_TYPE_CSS_CLASSES);
}
}
public function HasCount()
{
return true;
}
public function GetEntriesCount()
{
return $this->GetEntriesCountFromOQL($this->sOQL);
}
}
/**
* This class defines a menu item that displays a search form for the given class of objects
*/
class SearchMenuNode extends MenuNode
{
/**
* @var string
*/
protected $sPageTitle;
/**
* @var string
*/
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 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 (not used)
* @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...
* @param string $sEnableStimulus
*/
public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0.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;
}
/**
* @inheritDoc
* @throws \DictExceptionMissingString
* @throws \Exception
*/
public function RenderContent(WebPage $oPage, $aExtraParams = [])
{
ApplicationMenu::CheckMenuIdEnabled($this->GetMenuId());
$oPage->SetBreadCrumbEntry("menu-".$this->sMenuId, $this->GetTitle(), '', '', 'fas fa-search', iTopWebPage::ENUM_BREADCRUMB_ENTRY_ICON_TYPE_CSS_CLASSES);
$oSearch = new DBObjectSearch($this->sClass);
$sUsageId = 'Menu_'.utils::GetSafeId($this->GetMenuId());
$aParams = array_merge(['table_id' => $sUsageId], $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
{
/**
* @var string
*/
protected $sHyperlink;
/** @var bool */
protected $bIsLinkInNewWindow;
/**
* 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...
* @param string $sEnableStimulus
* @param bool $bIsLinkInNewWindow for the {@link WebPageMenuNode::IsHyperLinkInNewWindow} method
*/
public function __construct(
$sMenuId,
$sHyperlink,
$iParentIndex,
$fRank = 0.0,
$sEnableClass = null,
$iActionCode = null,
$iAllowedResults = UR_ALLOWED_YES,
$sEnableStimulus = null,
$bIsLinkInNewWindow = false
) {
parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
$this->sHyperlink = $sHyperlink;
$this->aReflectionProperties['url'] = $sHyperlink;
$this->bIsLinkInNewWindow = $bIsLinkInNewWindow;
}
/**
* @inheritDoc
*/
public function GetHyperlink($aExtraParams)
{
$aExtraParams['c[menu]'] = $this->GetMenuId();
return $this->AddParams($this->sHyperlink, $aExtraParams);
}
/**
* @inheritDoc
*/
public function IsHyperLinkInNewWindow()
{
return $this->bIsLinkInNewWindow;
}
/**
* @inheritDoc
*/
public function RenderContent(WebPage $oPage, $aExtraParams = [])
{
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
{
/**
* @var string
*/
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
* @param string $sEnableClass
* @param int|null $iActionCode
* @param int $iAllowedResults
* @param string $sEnableStimulus
*/
public function __construct($sMenuId, $sClass, $iParentIndex, $fRank = 0.0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
{
parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
$this->sClass = $sClass;
$this->aReflectionProperties['class'] = $sClass;
}
/**
* @inheritDoc
*/
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
* @throws CoreException
*/
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;
}
/**
* @inheritDoc
*/
public function RenderContent(WebPage $oPage, $aExtraParams = [])
{
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
{
/**
* @var string
*/
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 $sDashboardFile
* @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
* @param string $sEnableStimulus
*/
public function __construct($sMenuId, $sDashboardFile, $iParentIndex, $fRank = 0.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;
}
/**
* @inheritDoc
*/
public function GetHyperlink($aExtraParams)
{
if ($this->sDashboardFile == '') {
return '';
}
return parent::GetHyperlink($aExtraParams);
}
/**
* @return null|RuntimeDashboard
* @throws CoreException
* @throws Exception
*/
public function GetDashboard()
{
return RuntimeDashboard::GetDashboard($this->sDashboardFile, $this->sMenuId);
}
/**
* @inheritDoc
* @throws \Exception
*/
public function RenderContent(WebPage $oPage, $aExtraParams = [])
{
ApplicationMenu::CheckMenuIdEnabled($this->GetMenuId());
$oDashboard = $this->GetDashboard();
if ($oDashboard != null) {
WebResourcesHelper::EnableC3JSToWebPage($oPage);
$sDivId = utils::Sanitize($this->sMenuId, '', 'element_identifier');
$oPage->add('
');
$aExtraParams['dashboard_div_id'] = $sDivId;
$aExtraParams['from_dashboard_page'] = true;
$oDashboard->SetReloadURL($this->GetHyperlink($aExtraParams));
$oDashboard->Render($oPage, false, $aExtraParams);
$oPage->add('
');
$bEdit = utils::ReadParam('edit', false);
if ($bEdit) {
$sId = addslashes($this->sMenuId);
$oPage->add_ready_script("EditDashboard('$sId');");
} else {
$oParentMenu = ApplicationMenu::GetMenuNode($this->iParentIndex);
$sParentTitle = $oParentMenu->GetTitle();
$sThisTitle = $this->GetTitle();
if ($sParentTitle != $sThisTitle) {
$sDescription = $sParentTitle.' / '.$sThisTitle;
} else {
$sDescription = $sThisTitle;
}
if ($this->sMenuId == ApplicationMenu::GetDefaultMenuId()) {
$sIcon = 'fas fa-home';
} else {
$sIcon = 'fas fa-chart-pie';
}
$oPage->SetBreadCrumbEntry("ui-dashboard-".$this->sMenuId, $this->GetTitle(), $sDescription, '', $sIcon, iTopWebPage::ENUM_BREADCRUMB_ENTRY_ICON_TYPE_CSS_CLASSES);
}
} else {
$oPage->p("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
}
}
/**
* @param WebPage $oPage
* @throws CoreException
* @throws Exception
*/
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}'");
}
}
/**
* @param $oDashlet
* @throws Exception
*/
public function AddDashlet($oDashlet)
{
$oDashboard = $this->GetDashboard();
if ($oDashboard != null) {
$oDashboard->AddDashlet($oDashlet);
$oDashboard->Save();
} else {
throw new Exception("Error: failed to load dashboard file: '{$this->sDashboardFile}'");
}
}
}
/**
* A shortcut container is the preferred destination of newly created shortcuts
*/
class ShortcutContainerMenuNode extends MenuNode
{
/**
* @inheritDoc
*/
public function GetHyperlink($aExtraParams)
{
return '';
}
/**
* @inheritDoc
*/
public function RenderContent(WebPage $oPage, $aExtraParams = [])
{
}
/**
* @inheritDoc
* @throws CoreException
* @throws Exception
*/
public function PopulateChildMenus()
{
// Load user shortcuts in DB
//
$oBMSearch = new DBObjectSearch('Shortcut');
$oBMSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
$oBMSet = new DBObjectSet($oBMSearch, ['friendlyname' => true]); // ascending on friendlyname
$fRank = 1;
while ($oShortcut = $oBMSet->Fetch()) {
$sName = $this->GetMenuId().'_'.$oShortcut->GetKey();
new ShortcutMenuNode($sName, $oShortcut, $this->GetIndex(), $fRank++);
}
// Complete the tree
//
parent::PopulateChildMenus();
}
}
require_once(APPROOT.'application/shortcut.class.inc.php');
/**
* This class defines a menu item which content is a shortcut.
*/
class ShortcutMenuNode extends MenuNode
{
/**
* @var Shortcut
*/
protected $oShortcut;
/**
* 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 object $oShortcut Shortcut object
* @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...
* @param string $sEnableStimulus
*/
public function __construct($sMenuId, $oShortcut, $iParentIndex, $fRank = 0.0, $sEnableClass = null, $iActionCode = null, $iAllowedResults = UR_ALLOWED_YES, $sEnableStimulus = null)
{
parent::__construct($sMenuId, $iParentIndex, $fRank, $sEnableClass, $iActionCode, $iAllowedResults, $sEnableStimulus);
$this->oShortcut = $oShortcut;
$this->aReflectionProperties['shortcut'] = $oShortcut->GetKey();
}
/**
* @inheritDoc
*/
public function GetHyperlink($aExtraParams)
{
$sContext = $this->oShortcut->Get('context');
$aContext = unserialize($sContext);
if (isset($aContext['menu'])) {
unset($aContext['menu']);
}
foreach ($aContext as $sArgName => $sArgValue) {
$aExtraParams[$sArgName] = $sArgValue;
}
return parent::GetHyperlink($aExtraParams);
}
/**
* @inheritDoc
* @throws \Exception
*/
public function RenderContent(WebPage $oPage, $aExtraParams = [])
{
ApplicationMenu::CheckMenuIdEnabled($this->GetMenuId());
$this->oShortcut->RenderContent($oPage, $aExtraParams);
}
/**
* @inheritDoc
*
* @throws \Exception
*/
public function GetTitle()
{
return $this->oShortcut->Get('name');
}
/**
* @inheritDoc
*
* @throws \Exception
*/
public function GetLabel()
{
return $this->oShortcut->Get('name');
}
/**
* Indicates if the page corresponding to this menu node is countable
*
* @return bool true if corresponding page is countable
* @since 3.0.0
*/
public function HasCount()
{
return true;
}
public function GetEntriesCount()
{
return $this->GetEntriesCountFromOQL($this->oShortcut->Get('oql'));
}
}