Contract::dropdown PHP Method

dropdown() static public method

Print a select named $name with contracts options and selected value $value
static public dropdown ( $options = [] ) : Nothing
$options array of possible options: - name : string / name of the select (default is contracts_id) - value : integer / preselected value (default 0) - entity : integer or array / restrict to a defined entity or array of entities (default -1 : no restriction) - rand : (defauolt mt_rand) - entity_sons : boolean / if entity restrict specified auto select its sons only available if entity is a single value not an array (default false) - used : array / Already used items ID: not to display in dropdown (default empty) - nochecklimit : boolean / disable limit for nomber of device (for supplier, default false) - on_change : string / value to transmit to "onChange" - display : boolean / display or return string (default true) - expired : boolean / display expired contract (default false)
return Nothing (display)
    static function dropdown($options = array())
    {
        global $DB;
        //$name,$entity_restrict=-1,$alreadyused=array(),$nochecklimit=false
        $p['name'] = 'contracts_id';
        $p['value'] = '';
        $p['entity'] = '';
        $p['rand'] = mt_rand();
        $p['entity_sons'] = false;
        $p['used'] = array();
        $p['nochecklimit'] = false;
        $p['on_change'] = '';
        $p['display'] = true;
        $p['expired'] = false;
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $p[$key] = $val;
            }
        }
        if (!($p['entity'] < 0) && $p['entity_sons']) {
            if (is_array($p['entity'])) {
                // no translation needed (only for dev)
                echo "entity_sons options is not available with array of entity";
            } else {
                $p['entity'] = getSonsOf('glpi_entities', $p['entity']);
            }
        }
        $entrest = "";
        $idrest = "";
        $expired = "";
        if ($p['entity'] >= 0) {
            $entrest = getEntitiesRestrictRequest("AND", "glpi_contracts", "entities_id", $p['entity'], true);
        }
        if (count($p['used'])) {
            $idrest = " AND `glpi_contracts`.`id` NOT IN (" . implode(",", $p['used']) . ") ";
        }
        if (!$p['expired']) {
            $expired = " AND (DATEDIFF(ADDDATE(`glpi_contracts`.`begin_date`, INTERVAL\n                                               `glpi_contracts`.`duration` MONTH), CURDATE()) > '0'\n                           OR `glpi_contracts`.`begin_date` IS NULL\n                           OR (`glpi_contracts`.`duration` = 0\n                               AND DATEDIFF(`glpi_contracts`.`begin_date`, CURDATE() ) < '0' ))";
        }
        $query = "SELECT `glpi_contracts`.*\n                FROM `glpi_contracts`\n                LEFT JOIN `glpi_entities` ON (`glpi_contracts`.`entities_id` = `glpi_entities`.`id`)\n                WHERE `glpi_contracts`.`is_deleted` = '0' {$entrest} {$idrest} {$expired}\n                ORDER BY `glpi_entities`.`completename`,\n                         `glpi_contracts`.`name` ASC,\n                         `glpi_contracts`.`begin_date` DESC";
        $result = $DB->query($query);
        $group = '';
        $prev = -1;
        $values = array();
        while ($data = $DB->fetch_assoc($result)) {
            if ($p['nochecklimit'] || $data["max_links_allowed"] == 0 || $data["max_links_allowed"] > countElementsInTable('glpi_contracts_items', ['contracts_id' => $data['id']])) {
                if ($data["entities_id"] != $prev) {
                    $group = Dropdown::getDropdownName("glpi_entities", $data["entities_id"]);
                    $prev = $data["entities_id"];
                }
                $name = $data["name"];
                if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) {
                    $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
                }
                $tmp = sprintf(__('%1$s - %2$s'), $name, $data["num"]);
                $tmp = sprintf(__('%1$s - %2$s'), $tmp, Html::convDateTime($data["begin_date"]));
                $values[$group][$data['id']] = $tmp;
            }
        }
        return Dropdown::showFromArray($p['name'], $values, array('value' => $p['value'], 'on_change' => $p['on_change'], 'display' => $p['display'], 'display_emptychoice' => true));
    }

Usage Example

 /**
  * Print an HTML array with contracts associated to the enterprise
  *
  * @since version 0.84
  *
  * @param $supplier   Supplier object
  *
  * @return Nothing (display)
  **/
 static function showForSupplier(Supplier $supplier)
 {
     global $DB, $CFG_GLPI;
     $ID = $supplier->fields['id'];
     if (!Session::haveRight("contract", "r") || !$supplier->can($ID, 'r')) {
         return false;
     }
     $canedit = $supplier->can($ID, 'w');
     $rand = mt_rand();
     $query = "SELECT `glpi_contracts`.*,\n                       `glpi_contracts_suppliers`.`id` AS assocID,\n                       `glpi_entities`.`id` AS entity\n                FROM `glpi_contracts_suppliers`, `glpi_contracts`\n                LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id`=`glpi_contracts`.`entities_id`)\n                WHERE `glpi_contracts_suppliers`.`suppliers_id` = '{$ID}'\n                      AND `glpi_contracts_suppliers`.`contracts_id`=`glpi_contracts`.`id`" . getEntitiesRestrictRequest(" AND", "glpi_contracts", '', '', true) . "\n                ORDER BY `glpi_entities`.`completename`,\n                         `glpi_contracts`.`name`";
     $result = $DB->query($query);
     $contracts = array();
     $used = array();
     if ($number = $DB->numrows($result)) {
         while ($data = $DB->fetch_assoc($result)) {
             $contracts[$data['assocID']] = $data;
             $used[$data['id']] = $data['id'];
         }
     }
     if ($canedit) {
         echo "<div class='firstbloc'>";
         echo "<form name='contractsupplier_form{$rand}' id='contractsupplier_form{$rand}' method='post'\n                action='" . Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
         echo "<input type='hidden' name='suppliers_id' value='{$ID}'>";
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr class='tab_bg_2'><th colspan='2'>" . __('Add a contract') . "</th></tr>";
         echo "<tr class='tab_bg_1'><td class='right'>";
         Contract::dropdown(array('used' => $used, 'entity' => $supplier->fields["entities_id"], 'entity_sons' => $supplier->fields["is_recursive"], 'nochecklimit' => true));
         echo "</td><td class='center'>";
         echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
         echo "</td></tr>";
         echo "</table>";
         Html::closeForm();
         echo "</div>";
     }
     echo "<div class='spaced'>";
     if ($canedit && $number) {
         Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
         $massiveactionparams = array('num_displayed' => $number);
         Html::showMassiveActions(__CLASS__, $massiveactionparams);
     }
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr>";
     if ($canedit && $number) {
         echo "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
     }
     echo "<th>" . __('Name') . "</th>";
     echo "<th>" . __('Entity') . "</th>";
     echo "<th>" . _x('phone', 'Number') . "</th>";
     echo "<th>" . __('Contract type') . "</th>";
     echo "<th>" . __('Start date') . "</th>";
     echo "<th>" . __('Initial contract period') . "</th>";
     echo "</tr>";
     $used = array();
     foreach ($contracts as $data) {
         $cID = $data["id"];
         $used[$cID] = $cID;
         $assocID = $data["assocID"];
         echo "<tr class='tab_bg_1" . ($data["is_deleted"] ? "_2" : "") . "'>";
         if ($canedit) {
             echo "<td>";
             Html::showMassiveActionCheckBox(__CLASS__, $data["assocID"]);
             echo "</td>";
         }
         $name = $data["name"];
         if ($_SESSION["glpiis_ids_visible"] || empty($data["name"])) {
             $name = sprintf(__('%1$s (%2$s)'), $name, $data["id"]);
         }
         echo "<td class='center b'>\n               <a href='" . $CFG_GLPI["root_doc"] . "/front/contract.form.php?id={$cID}'>" . $name . "</a>";
         echo "</td>";
         echo "<td class='center'>" . Dropdown::getDropdownName("glpi_entities", $data["entity"]);
         echo "</td><td class='center'>" . $data["num"] . "</td>";
         echo "<td class='center'>" . Dropdown::getDropdownName("glpi_contracttypes", $data["contracttypes_id"]) . "</td>";
         echo "<td class='center'>" . Html::convDate($data["begin_date"]) . "</td>";
         echo "<td class='center'>";
         sprintf(_n('%d month', '%d months', $data["duration"]), $data["duration"]);
         if ($data["begin_date"] != '' && !empty($data["begin_date"])) {
             echo " -> " . Infocom::getWarrantyExpir($data["begin_date"], $data["duration"], 0, true);
         }
         echo "</td>";
         echo "</tr>";
     }
     echo "</table>";
     if ($canedit && $number) {
         $paramsma['ontop'] = false;
         Html::showMassiveActions(__CLASS__, $paramsma);
         Html::closeForm();
     }
     echo "</div>";
 }
All Usage Examples Of Contract::dropdown