. * --------------------------------------------------------------------- */ 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 "
$val
"; self::displayStandardTab($item, $key, $withtemplate, $options); } } } return true; case -2 : $item->showDebugInfo(); return true; default : $data = explode('$', $tab); $itemtype = $data[0]; // Default set $tabnum = 1; if (isset($data[1])) { $tabnum = $data[1]; } $options['withtemplate'] = $withtemplate; if ($tabnum == 'main') { Plugin::doHook('pre_show_item', ['item' => $item, 'options' => &$options]); $ret = $item->showForm($item->getID(), $options); Plugin::doHook('post_show_item', ['item' => $item, 'options' => $options]); return $ret; } if (!is_integer($itemtype) && ($itemtype != 'empty') && ($obj = getItemForItemtype($itemtype))) { $options['tabnum'] = $tabnum; $options['itemtype'] = $itemtype; Plugin::doHook('pre_show_tab', [ 'item' => $item, 'options' => &$options]); $ret = $obj->displayTabContentForItem($item, $tabnum, $withtemplate); Plugin::doHook('post_show_tab', ['item' => $item, 'options' => $options]); return $ret; } break; } return false; } /** * create tab text entry * * @param string $text text to display * @param integer $nb number of items (default 0) * * @return array array containing the onglets **/ static function createTabEntry($text, $nb = 0) { if ($nb) { //TRANS: %1$s is the name of the tab, $2$d is number of items in the tab between () $text = sprintf(__('%1$s %2$s'), $text, "$nb"); } return $text; } /** * Redirect to the list page from which the item was selected * Default to the search engine for the type * * @return void **/ function redirectToList() { global $CFG_GLPI; if (isset($_GET['withtemplate']) && !empty($_GET['withtemplate'])) { Html::redirect($CFG_GLPI["root_doc"]."/front/setup.templates.php?add=0&itemtype=". $this->getType()); } else if (isset($_SESSION['glpilisturl'][$this->getType()]) && !empty($_SESSION['glpilisturl'][$this->getType()])) { Html::redirect($_SESSION['glpilisturl'][$this->getType()]); } else { Html::redirect($this->getSearchURL()); } } /** * is the current object a new one - Always false here (virtual Objet) * * @since 0.83 * * @return boolean **/ function isNewItem() { return false; } /** * is the current object a new one - Always true here (virtual Objet) * * @since 0.84 * * @param integer $ID Id to check * * @return boolean **/ static function isNewID($ID) { return true; } /** * Get the search page URL for the current classe * * @param boolean $full path or relative one (true by default) * * @return string **/ static function getTabsURL($full = true) { return Toolbox::getItemTypeTabsURL(get_called_class(), $full); } /** * Get the search page URL for the current class * * @param boolean $full path or relative one (true by default) * * @return string **/ static function getSearchURL($full = true) { return Toolbox::getItemTypeSearchURL(get_called_class(), $full); } /** * Get the form page URL for the current class * * @param boolean $full path or relative one (true by default) * * @return string **/ static function getFormURL($full = true) { return Toolbox::getItemTypeFormURL(get_called_class(), $full); } /** * Get the form page URL for the current class and point to a specific ID * * @since 0.90 * * @param integer $id Id (default 0) * @param boolean $full Full path or relative one (true by default) * * @return string **/ static function getFormURLWithID($id = 0, $full = true) { $itemtype = get_called_class(); $link = $itemtype::getFormURL($full); $link .= (strpos($link, '?') ? '&':'?').'id=' . $id; return $link; } /** * Show primary form * * @since 0.90 * * @param array $options Options * * @return boolean **/ function showPrimaryForm($options = []) { if (!method_exists($this, "showForm")) { return false; } $ong = $this->defineAllTabs(); $class = "main_form"; if (count($ong) == 0) { $class .= " no_tab"; } if (!isset($_GET['id']) || (($_GET['id'] <= 0) && !$this instanceof Entity )) { $class .= " create_form"; } else { $class .= " modify_form"; } echo "
"; echo "
"; Plugin::doHook('pre_show_item', ['item' => $this, 'options' => &$options]); $this->showForm($options['id'], $options); Plugin::doHook('post_show_item', ['item' => $this, 'options' => $options]); echo "
"; echo "
"; } /** * Show tabs content * * @since 0.85 * * @param array $options parameters to add to URLs and ajax * - withtemplate is a template view ? * * @return void **/ function showTabsContent($options = []) { // for objects not in table like central if (isset($this->fields['id'])) { $ID = $this->fields['id']; } else { if (isset($options['id'])) { $ID = $options['id']; } else { $ID = 0; } } $target = $_SERVER['PHP_SELF']; $extraparamhtml = ""; $withtemplate = ""; if (is_array($options) && count($options)) { if (isset($options['withtemplate'])) { $withtemplate = $options['withtemplate']; } $cleaned_options = $options; if (isset($cleaned_options['id'])) { unset($cleaned_options['id']); } if (isset($cleaned_options['stock_image'])) { unset($cleaned_options['stock_image']); } if ($this->getType() == 'Ticket') { $this->input = $cleaned_options; $this->saveInput(); // $extraparamhtml can be tool long in case of ticket with content // (passed in GET in ajax request) unset($cleaned_options['content']); } // prevent double sanitize, because the includes.php sanitize all data $cleaned_options = Toolbox::stripslashes_deep($cleaned_options); $extraparamhtml = "&".Toolbox::append_params($cleaned_options, '&'); } echo "
"; echo "
"; $onglets = $this->defineAllTabs($options); $display_all = true; if (isset($onglets['no_all_tab'])) { $display_all = false; unset($onglets['no_all_tab']); } if (count($onglets)) { $tabpage = $this->getTabsURL(); $tabs = []; foreach ($onglets as $key => $val) { $tabs[$key] = ['title' => $val, 'url' => $tabpage, 'params' => "_target=$target&_itemtype=".$this->getType(). "&_glpi_tab=$key&id=$ID$extraparamhtml"]; } // Not all tab for templates and if only 1 tab if ($display_all && empty($withtemplate) && (count($tabs) > 1)) { $tabs[-1] = ['title' => __('All'), 'url' => $tabpage, 'params' => "_target=$target&_itemtype=".$this->getType(). "&_glpi_tab=-1&id=$ID$extraparamhtml"]; } Ajax::createTabs('tabspanel', 'tabcontent', $tabs, $this->getType(), $ID, $this->taborientation, $options); } echo "
"; } //sophal Gestion des accès impression function get_ticket_category($id){ global $DB; $query = "SELECT itilcategories_id as category FROM `glpi_tickets` WHERE id = ?"; $stmt = $DB->prepare($query); $stmt->bind_param("i" , $id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); } return $row['category']; } //sophal /** * Show tabs * * @param array $options parameters to add to URLs and ajax * - withtemplate is a template view ? * * @return void **/ function showNavigationHeader($options = []) { global $CFG_GLPI; // for objects not in table like central if (isset($this->fields['id'])) { $ID = $this->fields['id']; } else { if (isset($options['id'])) { $ID = $options['id']; } else { $ID = 0; } } $target = $_SERVER['PHP_SELF']; $extraparamhtml = ""; $withtemplate = ""; if (is_array($options) && count($options)) { $cleanoptions = $options; if (isset($options['withtemplate'])) { $withtemplate = $options['withtemplate']; unset($cleanoptions['withtemplate']); } foreach (array_keys($cleanoptions) as $key) { // Do not include id options if (($key[0] == '_') || ($key == 'id')) { unset($cleanoptions[$key]); } } $extraparamhtml = "&".Toolbox::append_params($cleanoptions, '&'); } if (empty($withtemplate) && !$this->isNewID($ID) && $this->getType() && $this->displaylist) { $glpilistitems =& $_SESSION['glpilistitems'][$this->getType()]; $glpilisttitle =& $_SESSION['glpilisttitle'][$this->getType()]; $glpilisturl =& $_SESSION['glpilisturl'][$this->getType()]; if (empty($glpilisturl)) { $glpilisturl = $this->getSearchURL(); } // echo "