Dropdown::showSelectItemFromItemtypes PHP Method

showSelectItemFromItemtypes() static public method

Make a select box for all items
static public showSelectItemFromItemtypes ( array $options = [] ) : randomized
$options array array: - itemtype_name : the name of the field containing the itemtype (default 'itemtype') - items_id_name : the name of the field containing the id of the selected item (default 'items_id') - itemtypes : all possible types to search for (default: $CFG_GLPI["state_types"]) - default_itemtype : the default itemtype to select (don't define if you don't need a default) (defaut 0) - entity_restrict : restrict entity in searching items (default -1) - onlyglobal : don't match item that don't have `is_global` == 1 (false by default) - checkright : check to see if we can "view" the itemtype (false by default) - showItemSpecificity : given an item, the AJAX file to open if there is special treatment. For instance, select a Item_Device* for CommonDevice - emptylabel : Empty choice's label (default self::EMPTY_VALUE) - used : array / Already used items ID: not to display in dropdown (default empty)
return randomized value used to generate HTML IDs
    static function showSelectItemFromItemtypes(array $options = array())
    {
        global $CFG_GLPI;
        $params = array();
        $params['itemtype_name'] = 'itemtype';
        $params['items_id_name'] = 'items_id';
        $params['itemtypes'] = '';
        $params['default_itemtype'] = 0;
        $params['entity_restrict'] = -1;
        $params['onlyglobal'] = false;
        $params['checkright'] = false;
        $params['showItemSpecificity'] = '';
        $params['emptylabel'] = self::EMPTY_VALUE;
        $params['used'] = array();
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $params[$key] = $val;
            }
        }
        $rand = self::showItemType($params['itemtypes'], array('checkright' => $params['checkright'], 'name' => $params['itemtype_name'], 'emptylabel' => $params['emptylabel']));
        if ($rand) {
            $p = array('idtable' => '__VALUE__', 'name' => $params['items_id_name'], 'entity_restrict' => $params['entity_restrict'], 'showItemSpecificity' => $params['showItemSpecificity']);
            // manage condition
            if ($params['onlyglobal']) {
                $p['condition'] = static::addNewCondition("`is_global` = 1");
            }
            if ($params['used']) {
                $p['used'] = $params['used'];
            }
            $field_id = Html::cleanId("dropdown_" . $params['itemtype_name'] . $rand);
            $show_id = Html::cleanId("show_" . $params['items_id_name'] . $rand);
            Ajax::updateItemOnSelectEvent($field_id, $show_id, $CFG_GLPI["root_doc"] . "/ajax/dropdownAllItems.php", $p);
            echo "<br><span id='{$show_id}'>&nbsp;</span>\n";
            // We check $options as the caller will set $options['default_itemtype'] only if it needs a
            // default itemtype and the default value can be '' thus empty won't be valid !
            if (array_key_exists('default_itemtype', $options)) {
                echo "<script type='text/javascript' >\n";
                echo Html::jsSetDropdownValue($field_id, $params['default_itemtype']);
                echo "</script>\n";
                $p["idtable"] = $params['default_itemtype'];
                Ajax::updateItem($show_id, $CFG_GLPI["root_doc"] . "/ajax/dropdownAllItems.php", $p);
            }
        }
        return $rand;
    }

Usage Example

 /**
  * Print the HTML array for Items linked to a problem
  *
  * @param $problem Problem object
  *
  * @return Nothing (display)
  **/
 static function showForProblem(Problem $problem)
 {
     global $DB, $CFG_GLPI;
     $instID = $problem->fields['id'];
     if (!$problem->can($instID, READ)) {
         return false;
     }
     $canedit = $problem->canEdit($instID);
     $rand = mt_rand();
     $query = "SELECT DISTINCT `itemtype`\n                FROM `glpi_items_problems`\n                WHERE `glpi_items_problems`.`problems_id` = '{$instID}'\n                ORDER BY `itemtype`";
     $result = $DB->query($query);
     $number = $DB->numrows($result);
     if ($canedit) {
         echo "<div class='firstbloc'>";
         echo "<form name='problemitem_form{$rand}' id='problemitem_form{$rand}' method='post'\n                action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr class='tab_bg_2'><th colspan='2'>" . __('Add an item') . "</th></tr>";
         echo "<tr class='tab_bg_1'><td>";
         $types = array();
         foreach ($problem->getAllTypesForHelpdesk() as $key => $val) {
             $types[] = $key;
         }
         Dropdown::showSelectItemFromItemtypes(array('itemtypes' => $types, 'entity_restrict' => $problem->fields['is_recursive'] ? getSonsOf('glpi_entities', $problem->fields['entities_id']) : $problem->fields['entities_id']));
         echo "</td><td class='center' width='30%'>";
         echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
         echo "<input type='hidden' name='problems_id' value='{$instID}'>";
         echo "</td></tr>";
         echo "</table>";
         Html::closeForm();
         echo "</div>";
     }
     echo "<div class='spaced'>";
     if ($canedit && $number) {
         Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
         $massiveactionparams = array('container' => 'mass' . __CLASS__ . $rand);
         Html::showMassiveActions($massiveactionparams);
     }
     echo "<table class='tab_cadre_fixehov'>";
     $header_begin = "<tr>";
     $header_top = '';
     $header_bottom = '';
     $header_end = '';
     if ($canedit && $number) {
         $header_top .= "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
         $header_top .= "</th>";
         $header_bottom .= "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
         $header_bottom .= "</th>";
     }
     $header_end .= "<th>" . __('Type') . "</th>";
     $header_end .= "<th>" . __('Entity') . "</th>";
     $header_end .= "<th>" . __('Name') . "</th>";
     $header_end .= "<th>" . __('Serial number') . "</th>";
     $header_end .= "<th>" . __('Inventory number') . "</th></tr>";
     echo $header_begin . $header_top . $header_end;
     $totalnb = 0;
     for ($i = 0; $i < $number; $i++) {
         $itemtype = $DB->result($result, $i, "itemtype");
         if (!($item = getItemForItemtype($itemtype))) {
             continue;
         }
         if ($item->canView()) {
             $itemtable = getTableForItemType($itemtype);
             $query = "SELECT `{$itemtable}`.*,\n                             `glpi_items_problems`.`id` AS IDD,\n                             `glpi_entities`.`id` AS entity\n                      FROM `glpi_items_problems`,\n                           `{$itemtable}`";
             if ($itemtype != 'Entity') {
                 $query .= " LEFT JOIN `glpi_entities`\n                                 ON (`{$itemtable}`.`entities_id`=`glpi_entities`.`id`) ";
             }
             $query .= " WHERE `{$itemtable}`.`id` = `glpi_items_problems`.`items_id`\n                              AND `glpi_items_problems`.`itemtype` = '{$itemtype}'\n                              AND `glpi_items_problems`.`problems_id` = '{$instID}'";
             if ($item->maybeTemplate()) {
                 $query .= " AND `{$itemtable}`.`is_template` = '0'";
             }
             $query .= getEntitiesRestrictRequest(" AND", $itemtable, '', '', $item->maybeRecursive()) . "\n                      ORDER BY `glpi_entities`.`completename`, `{$itemtable}`.`name`";
             $result_linked = $DB->query($query);
             $nb = $DB->numrows($result_linked);
             for ($prem = true; $data = $DB->fetch_assoc($result_linked); $prem = false) {
                 $name = $data["name"];
                 if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) {
                     $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
                 }
                 $link = $itemtype::getFormURLWithID($data['id']);
                 $namelink = "<a href=\"" . $link . "\">" . $name . "</a>";
                 echo "<tr class='tab_bg_1'>";
                 if ($canedit) {
                     echo "<td width='10'>";
                     Html::showMassiveActionCheckBox(__CLASS__, $data["IDD"]);
                     echo "</td>";
                 }
                 if ($prem) {
                     $typename = $item->getTypeName($nb);
                     echo "<td class='center top' rowspan='{$nb}'>" . ($nb > 1 ? sprintf(__('%1$s: %2$s'), $typename, $nb) : $typename) . "</td>";
                 }
                 echo "<td class='center'>";
                 echo Dropdown::getDropdownName("glpi_entities", $data['entity']) . "</td>";
                 echo "<td class='center" . (isset($data['is_deleted']) && $data['is_deleted'] ? " tab_bg_2_2'" : "'");
                 echo ">" . $namelink . "</td>";
                 echo "<td class='center'>" . (isset($data["serial"]) ? "" . $data["serial"] . "" : "-") . "</td>";
                 echo "<td class='center'>" . (isset($data["otherserial"]) ? "" . $data["otherserial"] . "" : "-") . "</td>";
                 echo "</tr>";
             }
             $totalnb += $nb;
         }
     }
     if ($number) {
         echo $header_begin . $header_bottom . $header_end;
     }
     echo "</table>";
     if ($canedit && $number) {
         $massiveactionparams['ontop'] = false;
         Html::showMassiveActions($massiveactionparams);
         Html::closeForm();
     }
     echo "</div>";
 }
All Usage Examples Of Dropdown::showSelectItemFromItemtypes