Dropdown::getDropdownName PHP Method

getDropdownName() static public method

Returns the value of the dropdown from $table with ID $id.
static public getDropdownName ( $table, $id, $withcomment, $translate = true, $tooltip = true ) : string
$table the dropdown table from witch we want values on the select
$id id of the element to get
$withcomment give array with name and comment (default 0)
$translate (true by default)
$tooltip boolean (true by default) returns a tooltip, else returns only 'comment'
return string the value of the dropdown or   if not exists
    static function getDropdownName($table, $id, $withcomment = 0, $translate = true, $tooltip = true)
    {
        global $DB, $CFG_GLPI;
        $item = getItemForItemtype(getItemTypeForTable($table));
        if ($item instanceof CommonTreeDropdown) {
            return getTreeValueCompleteName($table, $id, $withcomment, $translate, $tooltip);
        }
        $name = "";
        $comment = "";
        if ($id) {
            $SELECTNAME = "'' AS transname";
            $SELECTCOMMENT = "'' AS transcomment";
            $JOIN = '';
            if ($translate) {
                if (Session::haveTranslations(getItemTypeForTable($table), 'name')) {
                    $SELECTNAME = "`namet`.`value` AS transname";
                    $JOIN .= " LEFT JOIN `glpi_dropdowntranslations` AS namet\n                                 ON (`namet`.`itemtype` = '" . getItemTypeForTable($table) . "'\n                                     AND `namet`.`items_id` = `{$table}`.`id`\n                                     AND `namet`.`language` = '" . $_SESSION['glpilanguage'] . "'\n                                     AND `namet`.`field` = 'name')";
                }
                if (Session::haveTranslations(getItemTypeForTable($table), 'comment')) {
                    $SELECTCOMMENT = "`namec`.`value` AS transcomment";
                    $JOIN .= " LEFT JOIN `glpi_dropdowntranslations` AS namec\n                                    ON (`namec`.`itemtype` = '" . getItemTypeForTable($table) . "'\n                                        AND `namec`.`items_id` = `{$table}`.`id`\n                                        AND `namec`.`language` = '" . $_SESSION['glpilanguage'] . "'\n                                              AND `namec`.`field` = 'comment')";
                }
            }
            $query = "SELECT `{$table}`.*, {$SELECTNAME}, {$SELECTCOMMENT}\n                   FROM `{$table}`\n                   {$JOIN}\n                   WHERE `{$table}`.`id` = '{$id}'";
            /// TODO review comment management...
            /// TODO getDropdownName need to return only name
            /// When needed to use comment use class instead : getComments function
            /// GetName of class already give Name !!
            /// TODO CommonDBTM : review getComments to be recursive and add informations from class hierarchy
            /// getUserName have the same system : clean it too
            /// Need to study the problem
            if ($result = $DB->query($query)) {
                if ($DB->numrows($result) != 0) {
                    $data = $DB->fetch_assoc($result);
                    if ($translate && !empty($data['transname'])) {
                        $name = $data['transname'];
                    } else {
                        $name = $data["name"];
                    }
                    if (isset($data["comment"])) {
                        if ($translate && !empty($data['transcomment'])) {
                            $comment = $data['transcomment'];
                        } else {
                            $comment = $data["comment"];
                        }
                    }
                    switch ($table) {
                        case "glpi_computers":
                            if (empty($name)) {
                                $name = "({$id})";
                            }
                            break;
                        case "glpi_contacts":
                            //TRANS: %1$s is the name, %2$s is the firstname
                            $name = sprintf(__('%1$s %2$s'), $name, $data["firstname"]);
                            if ($tooltip) {
                                if (!empty($data["phone"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Phone'), "</span>" . $data['phone']);
                                }
                                if (!empty($data["phone2"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Phone 2'), "</span>" . $data['phone2']);
                                }
                                if (!empty($data["mobile"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Mobile phone'), "</span>" . $data['mobile']);
                                }
                                if (!empty($data["fax"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Fax'), "</span>" . $data['fax']);
                                }
                                if (!empty($data["email"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Email'), "</span>" . $data['email']);
                                }
                            }
                            break;
                        case "glpi_suppliers":
                            if ($tooltip) {
                                if (!empty($data["phonenumber"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Phone'), "</span>" . $data['phonenumber']);
                                }
                                if (!empty($data["fax"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Fax'), "</span>" . $data['fax']);
                                }
                                if (!empty($data["email"])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Email'), "</span>" . $data['email']);
                                }
                            }
                            break;
                        case "glpi_netpoints":
                            $name = sprintf(__('%1$s (%2$s)'), $name, self::getDropdownName("glpi_locations", $data["locations_id"], false, $translate));
                            break;
                        case "glpi_budgets":
                            if ($tooltip) {
                                if (!empty($data['locations_id'])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Location') . "</span>", self::getDropdownName("glpi_locations", $data["locations_id"], false, $translate));
                                }
                                if (!empty($data['budgettypes_id'])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Type') . "</span>", self::getDropdownName("glpi_budgettypes", $data["budgettypes_id"], false, $translate));
                                }
                                if (!empty($data['begin_date'])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('Start date') . "</span>", Html::convDateTime($data["begin_date"]));
                                }
                                if (!empty($data['end_date'])) {
                                    $comment .= "<br>" . sprintf(__('%1$s: %2$s'), "<span class='b'>" . __('End date') . "</span>", Html::convDateTime($data["end_date"]));
                                }
                            }
                    }
                }
            }
        }
        if (empty($name)) {
            $name = "&nbsp;";
        }
        /*
              if (!$item instanceof CommonTreeDropdown) {
                 $search  = array("/\&lt;/","/\&gt;/");
                 $replace = array("<",">");
                 $name    = preg_replace($search, $replace, $name);
              }*/
        if ($withcomment) {
            return array('name' => $name, 'comment' => $comment);
        }
        return $name;
    }

Usage Example

 /**
  * Get all data needed for template processing
  **/
 function getDatasForTemplate($event, $options = array())
 {
     global $LANG, $CFG_GLPI;
     $this->datas['##contract.entity##'] = Dropdown::getDropdownName('glpi_entities', $options['entities_id']);
     $events = $this->getEvents();
     $this->datas['##contract.action##'] = $LANG['mailing'][39] . " - " . $events[$event];
     foreach ($options['contracts'] as $id => $contract) {
         $tmp = array();
         $tmp['##contract.name##'] = $contract['name'];
         $tmp['##contract.number##'] = $contract['num'];
         if ($contract['contracttypes_id']) {
             $tmp['##contract.type##'] = Dropdown::getDropdownName('glpi_contracttypes', $contract['contracttypes_id']);
         } else {
             $tmp['##contract.type##'] = "";
         }
         $tmp['##contract.time##'] = getWarrantyExpir($contract["begin_date"], $contract["duration"], $contract["notice"]);
         $tmp['##contract.url##'] = urldecode($CFG_GLPI["url_base"] . "/index.php?redirect=contract_" . $id);
         $this->datas['contracts'][] = $tmp;
     }
     $this->datas['##lang.contract.time##'] = $event == Alert::END ? $LANG['contract'][0] : $LANG['contract'][1];
     $this->getTags();
     foreach ($this->tag_descriptions[NotificationTarget::TAG_LANGUAGE] as $tag => $values) {
         if (!isset($this->datas[$tag])) {
             $this->datas[$tag] = $values['label'];
         }
     }
 }
All Usage Examples Of Dropdown::getDropdownName