CommonDBTM::getValueToDisplay PHP Méthode

getValueToDisplay() public méthode

display a field using standard system
public getValueToDisplay ( $field_id_or_search_options, $values, $options = [] ) : return
$field_id_or_search_options integer/string/array id of the search option field or field name or search option array
$values mixed value to display
$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
Résultat return the string to display
    function getValueToDisplay($field_id_or_search_options, $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 (isset($searchoptions['datatype'])) {
                $unit = '';
                if (isset($searchoptions['unit'])) {
                    $unit = $searchoptions['unit'];
                }
                switch ($searchoptions['datatype']) {
                    case "count":
                    case "number":
                        if (isset($searchoptions['toadd']) && isset($searchoptions['toadd'][$value])) {
                            return $searchoptions['toadd'][$value];
                        }
                        if ($options['html']) {
                            return Dropdown::getValueWithUnit(Html::formatNumber($value, false, 0), $unit);
                        }
                        return $value;
                    case "decimal":
                        if ($options['html']) {
                            return Dropdown::getValueWithUnit(Html::formatNumber($value), $unit);
                        }
                        return $value;
                    case "string":
                    case "mac":
                    case "ip":
                        return $value;
                    case "text":
                        if ($options['html']) {
                            $text = nl2br($value);
                        } else {
                            $text = $value;
                        }
                        if (isset($searchoptions['htmltext']) && $searchoptions['htmltext']) {
                            $text = Html::clean(Toolbox::unclean_cross_side_scripting_deep($text));
                        }
                        return $text;
                    case "bool":
                        return Dropdown::getYesNo($value);
                    case "date":
                    case "date_delay":
                        if (isset($options['relative_dates']) && $options['relative_dates']) {
                            $dates = Html::getGenericDateTimeSearchItems(array('with_time' => true, 'with_future' => true));
                            return $dates[$value];
                        }
                        return Html::convDate(Html::computeGenericDateTimeSearch($value, true));
                    case "datetime":
                        if (isset($options['relative_dates']) && $options['relative_dates']) {
                            $dates = Html::getGenericDateTimeSearchItems(array('with_time' => true, 'with_future' => true));
                            return $dates[$value];
                        }
                        return Html::convDateTime(Html::computeGenericDateTimeSearch($value, false));
                    case "timestamp":
                        if ($value == 0 && isset($searchoptions['emptylabel'])) {
                            return $searchoptions['emptylabel'];
                        }
                        $withseconds = false;
                        if (isset($searchoptions['withseconds'])) {
                            $withseconds = $searchoptions['withseconds'];
                        }
                        return Html::timestampToString($value, $withseconds);
                    case "email":
                        if ($options['html']) {
                            return "<a href='mailto:{$value}'>{$value}</a>";
                        }
                        return $value;
                    case "weblink":
                        $orig_link = trim($value);
                        if (!empty($orig_link)) {
                            // strip begin of link
                            $link = preg_replace('/https?:\\/\\/(www[^\\.]*\\.)?/', '', $orig_link);
                            $link = preg_replace('/\\/$/', '', $link);
                            if (Toolbox::strlen($link) > $CFG_GLPI["url_maxlength"]) {
                                $link = Toolbox::substr($link, 0, $CFG_GLPI["url_maxlength"]) . "...";
                            }
                            return "<a href=\"" . formatOutputWebLink($orig_link) . "\" target='_blank'>{$link}" . "</a>";
                        }
                        return "&nbsp;";
                    case "itemlink":
                        if ($searchoptions['table'] == $this->getTable()) {
                            break;
                        }
                    case "dropdown":
                        if (isset($searchoptions['toadd']) && isset($searchoptions['toadd'][$value])) {
                            return $searchoptions['toadd'][$value];
                        }
                        if (!is_numeric($value)) {
                            return $value;
                        }
                        if ($value == 0 && isset($searchoptions['emptylabel'])) {
                            return $searchoptions['emptylabel'];
                        }
                        if ($searchoptions['table'] == 'glpi_users') {
                            if ($param['comments']) {
                                $tmp = getUserName($value, 2);
                                return $tmp['name'] . '&nbsp;' . Html::showToolTip($tmp['comment'], array('display' => false));
                            }
                            return getUserName($value);
                        }
                        if ($param['comments']) {
                            $tmp = Dropdown::getDropdownName($searchoptions['table'], $value, 1);
                            return $tmp['name'] . '&nbsp;' . Html::showToolTip($tmp['comment'], array('display' => false));
                        }
                        return Dropdown::getDropdownName($searchoptions['table'], $value);
                    case "itemtypename":
                        if ($obj = getItemForItemtype($value)) {
                            return $obj->getTypeName(1);
                        }
                        break;
                    case "language":
                        if (isset($CFG_GLPI['languages'][$value])) {
                            return $CFG_GLPI['languages'][$value][0];
                        }
                        return __('Default value');
                }
            }
            // Get specific display if available
            $itemtype = getItemTypeForTable($searchoptions['table']);
            if ($item = getItemForItemtype($itemtype)) {
                $options['searchopt'] = $searchoptions;
                $specific = $item->getSpecificValueToDisplay($field, $values, $options);
                if (!empty($specific)) {
                    return $specific;
                }
            }
        }
        return $value;
    }

Usage Example

Exemple #1
0
 /**
  * Retrieve last history Data for an item
  *
  * @param $item                     CommonDBTM object
  * @param $start        integer     first line to retrieve (default 0)
  * @param $limit        integer     max number of line to retrive (0 for all) (default 0)
  * @param $sqlfilter    string      to add an SQL filter (default '')
  *
  * @return array of localized log entry (TEXT only, no HTML)
  **/
 static function getHistoryData(CommonDBTM $item, $start = 0, $limit = 0, $sqlfilter = '')
 {
     global $DB;
     $itemtype = $item->getType();
     $items_id = $item->getField('id');
     $itemtable = $item->getTable();
     $SEARCHOPTION = Search::getOptions($itemtype);
     $query = "SELECT *\n                FROM `glpi_logs`\n                WHERE `items_id` = '{$items_id}'\n                      AND `itemtype` = '{$itemtype}' ";
     if ($sqlfilter) {
         $query .= "AND ({$sqlfilter}) ";
     }
     $query .= "ORDER BY `id` DESC";
     if ($limit) {
         $query .= " LIMIT " . intval($start) . "," . intval($limit);
     }
     $changes = array();
     foreach ($DB->request($query) as $data) {
         $tmp = array();
         $tmp['display_history'] = true;
         $tmp['id'] = $data["id"];
         $tmp['date_mod'] = Html::convDateTime($data["date_mod"]);
         $tmp['user_name'] = $data["user_name"];
         $tmp['field'] = "";
         $tmp['change'] = "";
         $tmp['datatype'] = "";
         // This is an internal device ?
         if ($data["linked_action"]) {
             // Yes it is an internal device
             switch ($data["linked_action"]) {
                 case self::HISTORY_CREATE_ITEM:
                     $tmp['change'] = __('Add the item');
                     break;
                 case self::HISTORY_DELETE_ITEM:
                     $tmp['change'] = __('Delete the item');
                     break;
                 case self::HISTORY_LOCK_ITEM:
                     $tmp['change'] = __('Lock the item');
                     break;
                 case self::HISTORY_UNLOCK_ITEM:
                     $tmp['change'] = __('Unlock the item');
                     break;
                 case self::HISTORY_RESTORE_ITEM:
                     $tmp['change'] = __('Restore the item');
                     break;
                 case self::HISTORY_ADD_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     //TRANS: %s is the component name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Add the component'), $data["new_value"]);
                     break;
                 case self::HISTORY_UPDATE_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     $change = '';
                     $linktype_field = explode('#', $data["itemtype_link"]);
                     $linktype = $linktype_field[0];
                     $field = $linktype_field[1];
                     $devicetype = $linktype::getDeviceType();
                     $tmp['field'] = $devicetype;
                     $specif_fields = $linktype::getSpecificities();
                     if (isset($specif_fields[$field]['short name'])) {
                         $tmp['field'] = $devicetype;
                         $tmp['field'] .= " (" . $specif_fields[$field]['short name'] . ")";
                     }
                     //TRANS: %1$s is the old_value, %2$s is the new_value
                     $tmp['change'] = sprintf(__('Change the component %1$s: %2$s'), $tmp['field'], sprintf(__('%1$s by %2$s'), $data["old_value"], $data["new_value"]));
                     break;
                 case self::HISTORY_DELETE_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     //TRANS: %s is the component name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Delete the component'), $data["old_value"]);
                     break;
                 case self::HISTORY_LOCK_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     //TRANS: %s is the component name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Lock the component'), $data["old_value"]);
                     break;
                 case self::HISTORY_UNLOCK_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     //TRANS: %s is the component name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Unlock the component'), $data["new_value"]);
                     break;
                 case self::HISTORY_INSTALL_SOFTWARE:
                     $tmp['field'] = _n('Software', 'Software', 1);
                     //TRANS: %s is the software name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Install the software'), $data["new_value"]);
                     break;
                 case self::HISTORY_UNINSTALL_SOFTWARE:
                     $tmp['field'] = _n('Software', 'Software', 1);
                     //TRANS: %s is the software name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Uninstall the software'), $data["old_value"]);
                     break;
                 case self::HISTORY_DISCONNECT_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     //TRANS: %s is the item name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Disconnect the item'), $data["old_value"]);
                     break;
                 case self::HISTORY_CONNECT_DEVICE:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     //TRANS: %s is the item name
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Connect the item'), $data["new_value"]);
                     break;
                 case self::HISTORY_LOG_SIMPLE_MESSAGE:
                     $tmp['field'] = "";
                     $tmp['change'] = $data["new_value"];
                     break;
                 case self::HISTORY_ADD_RELATION:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Add a link with an item'), $data["new_value"]);
                     break;
                 case self::HISTORY_DEL_RELATION:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Delete a link with an item'), $data["old_value"]);
                     break;
                 case self::HISTORY_LOCK_RELATION:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Lock a link with an item'), $data["old_value"]);
                     break;
                 case self::HISTORY_UNLOCK_RELATION:
                     $tmp['field'] = NOT_AVAILABLE;
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Unlock a link with an item'), $data["new_value"]);
                     break;
                 case self::HISTORY_ADD_SUBITEM:
                     $tmp['field'] = '';
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Add the item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["new_value"]));
                     break;
                 case self::HISTORY_UPDATE_SUBITEM:
                     $tmp['field'] = '';
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Update the item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["new_value"]));
                     break;
                 case self::HISTORY_DELETE_SUBITEM:
                     $tmp['field'] = '';
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Delete the item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["old_value"]));
                     break;
                 case self::HISTORY_LOCK_SUBITEM:
                     $tmp['field'] = '';
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Lock an item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["old_value"]));
                     break;
                 case self::HISTORY_UNLOCK_SUBITEM:
                     $tmp['field'] = '';
                     if ($item2 = getItemForItemtype($data["itemtype_link"])) {
                         $tmp['field'] = $item2->getTypeName(1);
                     }
                     $tmp['change'] = sprintf(__('%1$s: %2$s'), __('Unlock an item'), sprintf(__('%1$s (%2$s)'), $tmp['field'], $data["new_value"]));
                     break;
                 default:
                     $fct = array($data['itemtype_link'], 'getHistoryEntry');
                     if ($data['linked_action'] >= self::HISTORY_PLUGIN && $data['itemtype_link'] && is_callable($fct)) {
                         $tmp['field'] = $data['itemtype_link']::getTypeName(1);
                         $tmp['change'] = call_user_func($fct, $data);
                     }
                     $tmp['display_history'] = !empty($tmp['change']);
             }
         } else {
             $fieldname = "";
             $searchopt = array();
             $tablename = '';
             // It's not an internal device
             foreach ($SEARCHOPTION as $key2 => $val2) {
                 if ($key2 == $data["id_search_option"]) {
                     $tmp['field'] = $val2["name"];
                     $tablename = $val2["table"];
                     $fieldname = $val2["field"];
                     $searchopt = $val2;
                     if (isset($val2['datatype'])) {
                         $tmp['datatype'] = $val2["datatype"];
                     }
                     break;
                 }
             }
             if ($itemtable == $tablename) {
                 switch ($tmp['datatype']) {
                     // specific case for text field
                     case 'text':
                         $tmp['change'] = __('Update of the field');
                         break;
                     default:
                         $data["old_value"] = $item->getValueToDisplay($searchopt, $data["old_value"]);
                         $data["new_value"] = $item->getValueToDisplay($searchopt, $data["new_value"]);
                         break;
                 }
             }
             if (empty($tmp['change'])) {
                 $tmp['change'] = sprintf(__('Change %1$s by %2$s'), $data["old_value"], $data["new_value"]);
             }
         }
         $changes[] = $tmp;
     }
     return $changes;
 }
CommonDBTM