. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Common GLPI object **/ class CommonGLPI { /// GLPI Item type cache : set dynamically calling getType protected $type = -1; /// Display list on Navigation Header protected $displaylist = true; /// Show Debug public $showdebug = false; /** * Tab orientation : horizontal or vertical. * * @var string */ public $taborientation = 'horizontal'; /** * Rightname used to check rights to do actions on item. * * @var string */ static $rightname = ''; /** * Need to get item to show tab * * @var boolean */ public $get_item_to_display_tab = false; static protected $othertabs = []; /** * Return the localized name of the current Type * Should be overloaded in each new class * * @param integer $nb Number of items * * @return string **/ static function getTypeName($nb = 0) { return __('General'); } /** * Return the simplified localized label of the current Type in the context of a form. * Avoid to recall the type in the label (Computer status -> Status) * * Should be overloaded in each new class * * @return string **/ static function getFieldLabel() { return static::getTypeName(); } /** * Return the type of the object : class name * * @return string **/ static function getType() { return get_called_class(); } /** * Check rights on CommonGLPI Object (without corresponding table) * Same signature as CommonDBTM::can but in case of this class, we don't check instance rights * so, id and input parameters are unused. * * @param integer $ID ID of the item (-1 if new item) * @param mixed $right Right to check : r / w / recursive / READ / UPDATE / DELETE * @param array $input array of input data (used for adding item) (default NULL) * * @return boolean **/ function can($ID, $right, array &$input = null) { switch ($right) { case READ : return static::canView(); case UPDATE : return static::canUpdate(); case DELETE : return static::canDelete(); case PURGE : return static::canPurge(); case CREATE : return static::canCreate(); } return false; } /** * Have I the global right to "create" the Object * May be overloaded if needed (ex KnowbaseItem) * * @return boolean **/ static function canCreate() { if (static::$rightname) { return Session::haveRight(static::$rightname, CREATE); } return false; } /** * Have I the global right to "view" the Object * * Default is true and check entity if the objet is entity assign * * May be overloaded if needed * * @return boolean **/ static function canView() { if (static::$rightname) { return Session::haveRight(static::$rightname, READ); } return false; } /** * Have I the global right to "update" the Object * * Default is calling canCreate * May be overloaded if needed * * @return boolean **/ static function canUpdate() { if (static::$rightname) { return Session::haveRight(static::$rightname, UPDATE); } } /** * Have I the global right to "delete" the Object * * May be overloaded if needed * * @return boolean **/ static function canDelete() { if (static::$rightname) { return Session::haveRight(static::$rightname, DELETE); } return false; } /** * Have I the global right to "purge" the Object * * May be overloaded if needed * * @return boolean **/ static function canPurge() { if (static::$rightname) { return Session::haveRight(static::$rightname, PURGE); } return false; } /** * Register tab on an objet * * @since 0.83 * * @param string $typeform object class name to add tab on form * @param string $typetab object class name which manage the tab * * @return void **/ static function registerStandardTab($typeform, $typetab) { if (isset(self::$othertabs[$typeform])) { self::$othertabs[$typeform][] = $typetab; } else { self::$othertabs[$typeform] = [$typetab]; } } /** * Get the array of Tab managed by other types * Getter for plugin (ex PDF) to access protected property * * @since 0.83 * * @param string $typeform object class name to add tab on form * * @return array array of types **/ static function getOtherTabs($typeform) { if (isset(self::$othertabs[$typeform])) { return self::$othertabs[$typeform]; } return []; } /** * Define tabs to display * * NB : Only called for existing object * * @param array $options Options * - withtemplate is a template view ? * * @return array array containing the tabs **/ function defineTabs($options = []) { $ong = []; $this->addDefaultFormTab($ong); $this->addImpactTab($ong, $options); return $ong; } /** * return all the tabs for current object * * @since 0.83 * * @param array $options Options * - withtemplate is a template view ? * * @return array array containing the tabs **/ final function defineAllTabs($options = []) { global $CFG_GLPI; $onglets = []; // Tabs known by the object if ($this->isNewItem()) { $this->addDefaultFormTab($onglets); } else { $onglets = $this->defineTabs($options); } // Object with class with 'addtabon' attribute if (isset(self::$othertabs[$this->getType()]) && !$this->isNewItem()) { foreach (self::$othertabs[$this->getType()] as $typetab) { $this->addStandardTab($typetab, $onglets, $options); } } $class = $this->getType(); if (($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) && (!$this->isNewItem() || $this->showdebug) && (method_exists($class, 'showDebug') || Infocom::canApplyOn($class) || in_array($class, $CFG_GLPI["reservation_types"]))) { $onglets[-2] = __('Debug'); } return $onglets; } /** * Add standard define tab * * @param string $itemtype itemtype link to the tab * @param array $ong defined tabs * @param array $options options (for withtemplate) * * @return CommonGLPI **/ function addStandardTab($itemtype, array &$ong, array $options) { $withtemplate = 0; if (isset($options['withtemplate'])) { $withtemplate = $options['withtemplate']; } switch ($itemtype) { default : if (!is_integer($itemtype) && ($obj = getItemForItemtype($itemtype))) { $titles = $obj->getTabNameForItem($this, $withtemplate); if (!is_array($titles)) { $titles = [1 => $titles]; } foreach ($titles as $key => $val) { if (!empty($val)) { $ong[$itemtype.'$'.$key] = $val; } } } break; } return $this; } /** * Add the impact tab if enabled for this item type * * @param array $ong defined tabs * @param array $options options (for withtemplate) * * @return CommonGLPI **/ function addImpactTab(array &$ong, array $options) { global $CFG_GLPI; // Check if impact analysis is enabled for this item type if (Impact::isEnabled(static::class)) { $this->addStandardTab('Impact', $ong, $options); } return $this; } /** * Add default tab for form * * @since 0.85 * * @param array $ong Tabs * * @return CommonGLPI **/ function addDefaultFormTab(array &$ong) { if (self::isLayoutExcludedPage() || !self::isLayoutWithMain() || !method_exists($this, "showForm")) { $ong[$this->getType().'$main'] = $this->getTypeName(1); } return $this; } /** * get menu content * * @since 0.85 * * @return array array for menu **/ static function getMenuContent() { $menu = []; $type = static::getType(); $item = new $type(); $forbidden = $type::getForbiddenActionsForMenu(); if ($item instanceof CommonDBTM) { if ($type::canView()) { $menu['title'] = static::getMenuName(); $menu['shortcut'] = static::getMenuShorcut(); $menu['page'] = static::getSearchURL(false); $menu['links']['search'] = static::getSearchURL(false); $menu['icon'] = static::getIcon(); if (!in_array('add', $forbidden) && $type::canCreate()) { if ($item->maybeTemplate()) { $menu['links']['add'] = '/front/setup.templates.php?'.'itemtype='.$type. '&add=1'; if (!in_array('template', $forbidden)) { $menu['links']['template'] = '/front/setup.templates.php?'.'itemtype='.$type. '&add=0'; } } else { $menu['links']['add'] = static::getFormURL(false); } } $extra_links = static::getAdditionalMenuLinks(); if (is_array($extra_links) && count($extra_links)) { $menu['links'] += $extra_links; } } } else { if (!method_exists($type, 'canView') || $item->canView()) { $menu['title'] = static::getMenuName(); $menu['shortcut'] = static::getMenuShorcut(); $menu['page'] = static::getSearchURL(false); $menu['links']['search'] = static::getSearchURL(false); if (method_exists($item, 'getIcon')) { $menu['icon'] = static::getIcon(); } } } if ($data = static::getAdditionalMenuOptions()) { $menu['options'] = $data; } if ($data = static::getAdditionalMenuContent()) { $newmenu = [ strtolower($type) => $menu, ]; // Force overwrite existing menu foreach ($data as $key => $val) { $newmenu[$key] = $val; } $newmenu['is_multi_entries'] = true; $menu = $newmenu; } if (count($menu)) { return $menu; } return false; } /** * get additional menu content * * @since 0.85 * * @return array array for menu **/ static function getAdditionalMenuContent() { return false; } /** * Get forbidden actions for menu : may be add / template * * @since 0.85 * * @return array array of forbidden actions **/ static function getForbiddenActionsForMenu() { return []; } /** * Get additional menu options * * @since 0.85 * * @return array array of additional options **/ static function getAdditionalMenuOptions() { return false; } /** * Get additional menu links * * @since 0.85 * * @return array array of additional options **/ static function getAdditionalMenuLinks() { return false; } /** * Get menu shortcut * * @since 0.85 * * @return string character menu shortcut key **/ static function getMenuShorcut() { return ''; } /** * Get menu name * * @since 0.85 * * @return string character menu shortcut key **/ static function getMenuName() { return static::getTypeName(Session::getPluralNumber()); } /** * Get Tab Name used for itemtype * * NB : Only called for existing object * Must check right on what will be displayed + template * * @since 0.83 * * @param CommonGLPI $item Item on which the tab need to be displayed * @param boolean $withtemplate is a template object ? (default 0) * * @return string tab name **/ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { return ''; } /** * show Tab content * * @since 0.83 * * @param CommonGLPI $item Item on which the tab need to be displayed * @param integer $tabnum tab number (default 1) * @param boolean $withtemplate is a template object ? (default 0) * * @return boolean **/ static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { return false; } /** * display standard tab contents * * @param CommonGLPI $item Item on which the tab need to be displayed * @param string $tab tab name * @param boolean $withtemplate is a template object ? (default 0) * @param array $options additional options to pass * * @return boolean true **/ static function displayStandardTab(CommonGLPI $item, $tab, $withtemplate = 0, $options = []) { switch ($tab) { // All tab case -1 : // get tabs and loop over $ong = $item->defineAllTabs(['withtemplate' => $withtemplate]); if (!self::isLayoutExcludedPage() && self::isLayoutWithMain()) { //on classical and vertical split; the main tab is always displayed array_shift($ong); } if (count($ong)) { foreach ($ong as $key => $val) { if ($key != 'empty') { echo "