$search = Search::makeTextSearch($_POST['searchText']); $query .= " AND `gr`.`name` " . $search; } $query .= "ORDER BY `gr`.`name` ASC"; if ($result = $DB->query($query)) { if ($_POST['searchText'] != $CFG_GLPI["ajax_wildcard"] && $DB->numrows($result) == $NBMAX) { echo "<option value='0'>--" . __("Limited view") . "--</option>"; } else { if (!isset($_POST['display_emptychoice']) || $_POST['display_emptychoice']) { echo "<option value='0'>" . $_POST["emptylabel"] . "</option>"; } } foreach ($DB->request($query) as $data) { echo "<option value='" . $data["id"] . "'>" . $data['name']; if ($data['reference_code']) { echo " (" . $data['reference_code'] . ")"; } echo "</option>\n"; } } echo "</select>"; if (isset($_POST["comment"]) && $_POST["comment"]) { $paramscomment = array('value' => '__VALUE__', 'table' => $table); Ajax::updateItemOnSelectEvent("dropdown_" . $_POST["myname"] . $_POST["rand"], "comment_" . $_POST["myname"] . $_POST["rand"], $CFG_GLPI["root_doc"] . "/ajax/comments.php", $paramscomment); } if (isset($_POST["action"]) && $_POST["action"]) { $params = array($_POST['myname'] => '__VALUE__', 'entity_restrict' => $_POST['entity_restrict'], 'suppliers_id' => $_POST["suppliers_id"], 'rand' => $_POST['rand'], 'itemtype' => $_POST['itemtype']); Ajax::updateItemOnSelectEvent("dropdown_" . $_POST["myname"] . $_POST["rand"], $_POST['span'], $_POST['action'], $params); } Ajax::commonDropdownUpdateItem($_POST);
/** * Make a select box with all glpi users where select key = name * * @param $options array of possible options: * - name : string / name of the select (default is users_id) * - value * - right : string / limit user who have specific right : * id -> only current user (default case); * interface -> central ; * all -> all users ; * specific right like Ticket::READALL, CREATE.... (is array passed one of all passed right is needed) * - comments : boolean / is the comments displayed near the dropdown (default true) * - entity : integer or array / restrict to a defined entity or array of entities * (default -1 : no restriction) * - entity_sons : boolean / if entity restrict specified auto select its sons * only available if entity is a single value not an array(default false) * - all : Nobody or All display for none selected * all=0 (default) -> Nobody * all=1 -> All * all=-1-> nothing * - rand : integer / already computed rand value * - toupdate : array / Update a specific item on select change on dropdown * (need value_fieldname, to_update, url * (see Ajax::updateItemOnSelectEvent for information) * and may have moreparams) * - used : array / Already used items ID: not to display in dropdown (default empty) * - ldap_import * - on_change : string / value to transmit to "onChange" * - display : boolean / display or get string (default true) * - width : specific width needed (default 80%) * - specific_tags : array of HTML5 tags to add the the field * - url : url of the ajax php code which should return the json data to show in * the dropdown (default /ajax/getDropdownUsers.php) * * @return rand value if displayed / string if not **/ static function dropdown($options = array()) { global $DB, $CFG_GLPI; // Default values $p['name'] = 'users_id'; $p['value'] = ''; $p['right'] = 'id'; $p['all'] = 0; $p['on_change'] = ''; $p['comments'] = 1; $p['width'] = '80%'; $p['entity'] = -1; $p['entity_sons'] = false; $p['used'] = array(); $p['ldap_import'] = false; $p['toupdate'] = ''; $p['rand'] = mt_rand(); $p['display'] = true; $p['_user_index'] = 0; $p['specific_tags'] = array(); $p['url'] = $CFG_GLPI['root_doc'] . "/ajax/getDropdownUsers.php"; if (is_array($options) && count($options)) { foreach ($options as $key => $val) { $p[$key] = $val; } } // check default value (in case of multiple observers) if (is_array($p['value'])) { $p['value'] = $p['value'][$p['_user_index']]; } // Check default value for dropdown : need to be a numeric if (strlen($p['value']) == 0 || !is_numeric($p['value'])) { $p['value'] = 0; } $output = ''; if (!($p['entity'] < 0) && $p['entity_sons']) { if (is_array($p['entity'])) { $output .= "entity_sons options is not available with array of entity"; } else { $p['entity'] = getSonsOf('glpi_entities', $p['entity']); } } // Make a select box with all glpi users $user = getUserName($p['value'], 2); $view_users = self::canView(); if (!empty($p['value']) && $p['value'] > 0) { $default = $user["name"]; } else { if ($p['all']) { $default = __('All'); } else { $default = Dropdown::EMPTY_VALUE; } } $field_id = Html::cleanId("dropdown_" . $p['name'] . $p['rand']); $param = array('value' => $p['value'], 'valuename' => $default, 'width' => $p['width'], 'all' => $p['all'], 'right' => $p['right'], 'on_change' => $p['on_change'], 'used' => $p['used'], 'entity_restrict' => $p['entity'], 'specific_tags' => $p['specific_tags']); $output = Html::jsAjaxDropdown($p['name'], $field_id, $p['url'], $param); // Display comment if ($p['comments']) { $comment_id = Html::cleanId("comment_" . $p['name'] . $p['rand']); $link_id = Html::cleanId("comment_link_" . $p["name"] . $p['rand']); if (!$view_users) { $user["link"] = ''; } else { if (empty($user["link"])) { $user["link"] = $CFG_GLPI['root_doc'] . "/front/user.php"; } } $output .= " " . Html::showToolTip($user["comment"], array('contentid' => $comment_id, 'display' => false, 'link' => $user["link"], 'linkid' => $link_id)); $paramscomment = array('value' => '__VALUE__', 'table' => "glpi_users"); if ($view_users) { $paramscomment['withlink'] = $link_id; } $output .= Ajax::updateItemOnSelectEvent($field_id, $comment_id, $CFG_GLPI["root_doc"] . "/ajax/comments.php", $paramscomment, false); } $output .= Ajax::commonDropdownUpdateItem($p, false); if (Session::haveRight('user', self::IMPORTEXTAUTHUSERS) && $p['ldap_import'] && Entity::isEntityDirectoryConfigured($_SESSION['glpiactive_entity'])) { $output .= "<img alt='' title=\"" . __s('Import a user') . "\" src='" . $CFG_GLPI["root_doc"] . "/pics/add_dropdown.png' style='cursor:pointer; margin-left:2px;'\n onClick=\"" . Html::jsGetElementbyID('userimport' . $p['rand']) . ".dialog('open');\">"; $output .= Ajax::createIframeModalWindow('userimport' . $p['rand'], $CFG_GLPI["root_doc"] . "/front/ldap.import.php?entity=" . $_SESSION['glpiactive_entity'], array('title' => __('Import a user'), 'display' => false)); } if ($p['display']) { echo $output; return $p['rand']; } return $output; }
/** * Dropdown of values in an array * * @param $name select name * @param $elements array of elements to display * @param $options array of possible options: * - value : integer / preselected value (default 0) * - used : array / Already used items ID: not to display in dropdown (default empty) * - readonly : boolean / used as a readonly item (default false) * - on_change : string / value to transmit to "onChange" * - multiple : boolean / can select several values (default false) * - size : integer / number of rows for the select (default = 1) * - display : boolean / display or return string * - other : boolean or string if not false, then we can use an "other" value * if it is a string, then the default value will be this string * - rand : specific rand if needed (default is generated one) * - width : specific width needed (default not set) * - emptylabel : empty label if empty displayed (default self::EMPTY_VALUE) * - display_emptychoice : display empty choice (default false) * - tooltip : string / message to add as tooltip on the dropdown (default '') * - option_tooltips : array / message to add as tooltip on the dropdown options. Use the same keys as for the $elements parameter, but none is mandotary. Missing keys will just be ignored and no tooltip will be added. To add a tooltip on an option group, is the '__optgroup_label' key inside the array describing option tooltips : 'optgroupname1' => array('__optgroup_label' => 'tooltip for option group') (default empty) * * Permit to use optgroup defining items in arrays * array('optgroupname' => array('key1' => 'val1', * 'key2' => 'val2'), * 'optgroupname2' => array('key3' => 'val3', * 'key4' => 'val4')) **/ static function showFromArray($name, array $elements, $options = array()) { $param['value'] = ''; $param['values'] = array(''); $param['tooltip'] = ''; $param['option_tooltips'] = array(); $param['used'] = array(); $param['readonly'] = false; $param['on_change'] = ''; $param['width'] = ''; $param['multiple'] = false; $param['size'] = 1; $param['display'] = true; $param['other'] = false; $param['rand'] = mt_rand(); $param['emptylabel'] = self::EMPTY_VALUE; $param['display_emptychoice'] = false; if (is_array($options) && count($options)) { if (isset($options['value']) && strlen($options['value'])) { $options['values'] = array($options['value']); unset($options['value']); } foreach ($options as $key => $val) { $param[$key] = $val; } } if ($param['other'] !== false) { $other_select_option = $name . '_other_value'; $param['on_change'] .= "displayOtherSelectOptions(this, \"{$other_select_option}\");"; // If $param['other'] is a string, then we must highlight "other" option if (is_string($param['other'])) { if (!$param["multiple"]) { $param['values'] = array($other_select_option); } else { $param['values'][] = $other_select_option; } } } $param['option_tooltips'] = Html::entities_deep($param['option_tooltips']); if ($param["display_emptychoice"]) { $elements = array(0 => $param['emptylabel']) + $elements; } if ($param["multiple"]) { $field_name = $name . "[]"; } else { $field_name = $name; } $output = ''; // readonly mode $field_id = Html::cleanId("dropdown_" . $name . $param['rand']); if ($param['readonly']) { $to_display = array(); foreach ($param['values'] as $value) { $output .= "<input type='hidden' name='{$field_name}' value='{$value}'>"; if (isset($elements[$value])) { $to_display[] = $elements[$value]; } } $output .= implode('<br>', $to_display); } else { $output .= "<select name='{$field_name}' id='{$field_id}'"; if ($param['tooltip']) { $output .= ' title="' . Html::entities_deep($param['tooltip']) . '"'; } if (!empty($param["on_change"])) { $output .= " onChange='" . $param["on_change"] . "'"; } if (is_int($param["size"]) && $param["size"] > 0) { $output .= " size='" . $param["size"] . "'"; } if ($param["multiple"]) { $output .= " multiple"; } $output .= '>'; $max_option_size = 0; foreach ($elements as $key => $val) { // optgroup management if (is_array($val)) { $opt_goup = Html::entities_deep($key); if ($max_option_size < strlen($opt_goup)) { $max_option_size = strlen($opt_goup); } $output .= "<optgroup label=\"{$opt_goup}\""; $optgroup_tooltips = false; if (isset($param['option_tooltips'][$key])) { if (is_array($param['option_tooltips'][$key])) { if (isset($param['option_tooltips'][$key]['__optgroup_label'])) { $output .= ' title="' . $param['option_tooltips'][$key]['__optgroup_label'] . '"'; } $optgroup_tooltips = $param['option_tooltips'][$key]; } else { $output .= ' title="' . $param['option_tooltips'][$key] . '"'; } } $output .= ">"; foreach ($val as $key2 => $val2) { if (!isset($param['used'][$key2])) { $output .= "<option value='" . $key2 . "'"; // Do not use in_array : trouble with 0 and empty value foreach ($param['values'] as $value) { if (strcmp($key2, $value) === 0) { $output .= " selected"; break; } } if ($optgroup_tooltips && isset($optgroup_tooltips[$key2])) { $output .= ' title="' . $optgroup_tooltips[$key2] . '"'; } $output .= ">" . $val2 . "</option>"; if ($max_option_size < strlen($val2)) { $max_option_size = strlen($val2); } } } $output .= "</optgroup>"; } else { if (!isset($param['used'][$key])) { $output .= "<option value='" . $key . "'"; // Do not use in_array : trouble with 0 and empty value foreach ($param['values'] as $value) { if (strcmp($key, $value) === 0) { $output .= " selected"; break; } } if (isset($param['option_tooltips'][$key])) { $output .= ' title="' . $param['option_tooltips'][$key] . '"'; } $output .= ">" . $val . "</option>"; if ($max_option_size < strlen($val)) { $max_option_size = strlen($val); } } } } if ($param['other'] !== false) { $output .= "<option value='{$other_select_option}'"; if (is_string($param['other'])) { $output .= " selected"; } $output .= ">" . __('Other...') . "</option>"; } $output .= "</select>"; if ($param['other'] !== false) { $output .= "<input name='{$other_select_option}' id='{$other_select_option}' type='text'"; if (is_string($param['other'])) { $output .= " value=\"" . $param['other'] . "\""; } else { $output .= " style=\"display: none\""; } $output .= ">"; } } // Width set on select $output .= Html::jsAdaptDropdown($field_id, array('width' => $param["width"])); if ($param["multiple"]) { // Hack for All / None because select2 does not provide it $select = __('All'); $deselect = __('None'); $output .= "<div class='invisible' id='selectallbuttons_{$field_id}'>"; $output .= "<div class='select2-actionable-menu'>"; $output .= "<a class='vsubmit floatleft' " . "onclick=\"selectAll('{$field_id}');\$('#{$field_id}').select2('close');\">{$select}" . "</a> "; $output .= "<a class='vsubmit floatright' onclick=\"deselectAll('{$field_id}');\">{$deselect}" . "</a>"; $output .= "</div></div>"; $js = "\n var multichecksappend{$field_id} = false;\n \$('#{$field_id}').on('select2-open', function() {\n if (!multichecksappend{$field_id}) {\n \$('#select2-drop').append(\$('#selectallbuttons_{$field_id}').html());\n multichecksappend{$field_id} = true;\n }\n });"; $output .= Html::scriptBlock($js); } $output .= Ajax::commonDropdownUpdateItem($param, false); if ($param['display']) { echo $output; return $param['rand']; } return $output; }