CommonDBTM::getValueToSelect PHP Method

getValueToSelect() public method

Select a field using standard system
public getValueToSelect ( $field_id_or_search_options, $name = '', $values = '', $options = [] ) : return
$field_id_or_search_options integer/string/array id of the search option field or field name or search option array
$name string name of the select (if empty use linkfield) (default '')
$values mixed default value to display (default '')
$options array of possible options: Parameters which could be used in options array : - comments : boolean / is the comments displayed near the value (default false) - any others options passed to specific display method
return return the string to display
    function getValueToSelect($field_id_or_search_options, $name = '', $values = '', $options = array())
    {
        global $CFG_GLPI;
        $param['comments'] = false;
        $param['html'] = false;
        foreach ($param as $key => $val) {
            if (!isset($options[$key])) {
                $options[$key] = $val;
            }
        }
        $searchoptions = array();
        if (is_array($field_id_or_search_options)) {
            $searchoptions = $field_id_or_search_options;
        } else {
            $searchopt = $this->getSearchOptions();
            // Get if id of search option is passed
            if (is_numeric($field_id_or_search_options)) {
                if (isset($searchopt[$field_id_or_search_options])) {
                    $searchoptions = $searchopt[$field_id_or_search_options];
                }
            } else {
                // Get if field name is passed
                $searchoptions = $this->getSearchOptionByField('field', $field_id_or_search_options, $this->getTable());
            }
        }
        if (count($searchoptions)) {
            $field = $searchoptions['field'];
            // Normalize option
            if (is_array($values)) {
                $value = $values[$field];
            } else {
                $value = $values;
                $values = array($field => $value);
            }
            if (empty($name)) {
                $name = $searchoptions['linkfield'];
            }
            // If not set : set to specific
            if (!isset($searchoptions['datatype'])) {
                $searchoptions['datatype'] = 'specific';
            }
            $options['display'] = false;
            $unit = '';
            if (isset($searchoptions['unit'])) {
                $unit = $searchoptions['unit'];
            }
            if (isset($options[$searchoptions['table'] . '.' . $searchoptions['field']])) {
                $options = array_merge($options, $options[$searchoptions['table'] . '.' . $searchoptions['field']]);
            }
            switch ($searchoptions['datatype']) {
                case "count":
                case "number":
                case "integer":
                    $copytooption = array('min', 'max', 'step', 'toadd', 'unit');
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    $options['value'] = $value;
                    return Dropdown::showNumber($name, $options);
                case "decimal":
                case "mac":
                case "ip":
                case "string":
                case "email":
                case "weblink":
                    $this->fields[$name] = $value;
                    return Html::autocompletionTextField($this, $name, $options);
                case "text":
                    $out = '';
                    if (isset($searchoptions['htmltext']) && $searchoptions['htmltext']) {
                        $out = Html::initEditorSystem($name, '', false);
                    }
                    return $out . "<textarea cols='45' rows='5' name='{$name}'>{$value}</textarea>";
                case "bool":
                    return Dropdown::showYesNo($name, $value, -1, $options);
                case "color":
                    return Html::showColorField($name, $options);
                case "date":
                case "date_delay":
                    if (isset($options['relative_dates']) && $options['relative_dates']) {
                        if (isset($searchoptions['maybefuture']) && $searchoptions['maybefuture']) {
                            $options['with_future'] = true;
                        }
                        return Html::showGenericDateTimeSearch($name, $value, $options);
                    }
                    $copytooption = array('min', 'max', 'maybeempty', 'showyear');
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    $options['value'] = $value;
                    return Html::showDateField($name, $options);
                case "datetime":
                    if (isset($options['relative_dates']) && $options['relative_dates']) {
                        if (isset($searchoptions['maybefuture']) && $searchoptions['maybefuture']) {
                            $options['with_future'] = true;
                        }
                        $options['with_time'] = true;
                        return Html::showGenericDateTimeSearch($name, $value, $options);
                    }
                    $copytooption = array('mindate', 'maxdate', 'mintime', 'maxtime', 'maybeempty', 'timestep');
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    $options['value'] = $value;
                    return Html::showDateTimeField($name, $options);
                case "timestamp":
                    $copytooption = array('addfirstminutes', 'emptylabel', 'inhours', 'max', 'min', 'step', 'toadd', 'display_emptychoice');
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    $options['value'] = $value;
                    return Dropdown::showTimeStamp($name, $options);
                case "itemlink":
                    // Do not use dropdown if wanted to select string value instead of ID
                    if (isset($options['itemlink_as_string']) && $options['itemlink_as_string']) {
                        break;
                    }
                case "dropdown":
                    $copytooption = array('condition', 'displaywith', 'emptylabel', 'right', 'toadd');
                    $options['name'] = $name;
                    $options['value'] = $value;
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    if (!isset($options['entity'])) {
                        $options['entity'] = $_SESSION['glpiactiveentities'];
                    }
                    $itemtype = getItemTypeForTable($searchoptions['table']);
                    return $itemtype::dropdown($options);
                case "right":
                    return Profile::dropdownRights(Profile::getRightsFor($searchoptions['rightclass']), $name, $value, array('multiple' => false, 'display' => false));
                case "itemtypename":
                    if (isset($searchoptions['itemtype_list'])) {
                        $options['types'] = $CFG_GLPI[$searchoptions['itemtype_list']];
                    }
                    $copytooption = array('types');
                    $options['value'] = $value;
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    if (isset($options['types'])) {
                        return Dropdown::showItemTypes($name, $options['types'], $options);
                    }
                    return false;
                case "language":
                    $copytooption = array('emptylabel', 'display_emptychoice');
                    foreach ($copytooption as $key) {
                        if (isset($searchoptions[$key]) && !isset($options[$key])) {
                            $options[$key] = $searchoptions[$key];
                        }
                    }
                    $options['value'] = $value;
                    return Dropdown::showLanguages($name, $options);
            }
            // Get specific display if available
            $itemtype = getItemTypeForTable($searchoptions['table']);
            if ($item = getItemForItemtype($itemtype)) {
                $specific = $item->getSpecificValueToSelect($searchoptions['field'], $name, $values, $options);
                if (strlen($specific)) {
                    return $specific;
                }
            }
        }
        // default case field text
        $this->fields[$name] = $value;
        return Html::autocompletionTextField($this, $name, $options);
    }

Usage Example

Exemplo n.º 1
0
 function getValueToSelect($field_id_or_search_options, $name = '', $values = '', $options = array())
 {
     switch ($field_id_or_search_options['table'] . '.' . $field_id_or_search_options['field']) {
         // For searchoption "Type"
         case $this->getTable() . '.type':
             $options['display'] = false;
             return Dropdown::showFromArray($name, self::getTypes(), $options);
         case $this->getTable() . '.itemtypes':
             $options['display'] = false;
             return Dropdown::showFromArray($name, self::getItemtypes(), $options);
     }
     return parent::getValueToSelect($field_id_or_search_options, $name, $values, $options);
 }
CommonDBTM