Dropdown::showItemType PHP Method

showItemType() static public method

show a dropdown to selec a type
static public showItemType ( $types = '', $options = [] ) : integer
$types Types used (default "state_types") (default '')
$options Array of optional options name, value, rand, emptylabel, display_emptychoice, on_change, plural, checkright - toupdate : array / Update a specific item on select change on dropdown (need value_fieldname, to_update, url (see Ajax::updateItemOnSelectEvent for information) and may have moreparams)
return integer rand for select id
    static function showItemType($types = '', $options = array())
    {
        global $CFG_GLPI;
        $params['name'] = 'itemtype';
        $params['value'] = '';
        $params['rand'] = mt_rand();
        $params['on_change'] = '';
        $params['plural'] = false;
        //Parameters about choice 0
        //Empty choice's label
        $params['emptylabel'] = self::EMPTY_VALUE;
        //Display emptychoice ?
        $params['display_emptychoice'] = true;
        $params['checkright'] = false;
        $params['toupdate'] = '';
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $params[$key] = $val;
            }
        }
        if (!is_array($types)) {
            $types = $CFG_GLPI["state_types"];
        }
        $options = array();
        foreach ($types as $type) {
            if ($item = getItemForItemtype($type)) {
                if ($params['checkright'] && !$item->canView()) {
                    continue;
                }
                $options[$type] = $item->getTypeName($params['plural'] ? 2 : 1);
            }
        }
        asort($options);
        if (count($options)) {
            return Dropdown::showFromArray($params['name'], $options, array('value' => $params['value'], 'on_change' => $params['on_change'], 'toupdate' => $params['toupdate'], 'display_emptychoice' => $params['display_emptychoice'], 'emptylabel' => $params['emptylabel']));
        }
        return 0;
    }

Usage Example

 function preaddRule($componentscatalogs_id)
 {
     global $CFG_GLPI, $DB;
     $networkport_types = $CFG_GLPI['networkport_types'];
     $networkport_types[] = "PluginMonitoringNetworkport";
     $a_usedItemtypes = array();
     $query = "SELECT * FROM `" . $this->getTable() . "`\n         WHERE `plugin_monitoring_componentscalalog_id`='" . $componentscatalogs_id . "'";
     $result = $DB->query($query);
     while ($data = $DB->fetch_array($result)) {
         $key = array_search($data['itemtype'], $networkport_types);
         if (isset($key)) {
             unset($networkport_types[$key]);
         }
     }
     if (count($a_usedItemtypes) == count($networkport_types)) {
         return;
     }
     $this->getEmpty();
     $this->showFormHeader();
     echo "<tr class='tab_bg_1'>";
     echo "<td>";
     echo __('Name') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     echo "<input type='text' name='name' value=''/>";
     echo "</td>";
     echo "<td>";
     echo __('Item type') . "&nbsp;:";
     echo "</td>";
     echo "<td>";
     Dropdown::showItemType($networkport_types);
     echo "<input type='hidden' name='plugin_monitoring_componentscalalog_id' value='" . $componentscatalogs_id . "' >";
     echo "</td>";
     echo "</tr>";
     $this->showFormButtons();
 }
All Usage Examples Of Dropdown::showItemType