Html::autocompletionTextField PHP Method

autocompletionTextField() static public method

Show div with auto completion
static public autocompletionTextField ( CommonDBTM $item, $field, $options = [] ) : nothing
$item CommonDBTM item object used for create dropdown
$field field to search for autocompletion
$options array of possible options: - name : string / name of the select (default is field parameter) - value : integer / preselected value (default value of the item object) - size : integer / size of the text field - entity : integer / restrict to a defined entity (default entity of the object if define) set to -1 not to take into account - user : integer / restrict to a defined user (default -1 : no restriction) - option : string / options to add to text field - display : boolean / if false get string
return nothing (print out an HTML div)
    static function autocompletionTextField(CommonDBTM $item, $field, $options = array())
    {
        global $CFG_GLPI;
        $params['name'] = $field;
        $params['value'] = '';
        if (array_key_exists($field, $item->fields)) {
            $params['value'] = $item->fields[$field];
        }
        $params['entity'] = -1;
        if (array_key_exists('entities_id', $item->fields)) {
            $params['entity'] = $item->fields['entities_id'];
        }
        $params['user'] = -1;
        $params['option'] = '';
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $params[$key] = $val;
            }
        }
        $output = '';
        if ($CFG_GLPI["use_ajax_autocompletion"]) {
            $rand = mt_rand();
            $name = "field_" . $params['name'] . $rand;
            $output .= "<input " . $params['option'] . " id='text{$name}' type='text' name='" . $params['name'] . "' value=\"" . self::cleanInputText($params['value']) . "\"\n                       class='autocompletion-text-field'>";
            $parameters['itemtype'] = $item->getType();
            $parameters['field'] = $field;
            if ($params['entity'] >= 0) {
                $parameters['entity_restrict'] = $params['entity'];
            }
            if ($params['user'] >= 0) {
                $parameters['user_restrict'] = $params['user'];
            }
            $js = "  \$( '#text{$name}' ).autocomplete({\n                        source: '" . $CFG_GLPI["root_doc"] . "/ajax/autocompletion.php?" . Toolbox::append_params($parameters, '&') . "',\n                        minLength: 3,\n                        });";
            $output .= Html::scriptBlock($js);
        } else {
            $output .= "<input " . $params['option'] . " type='text' name='" . $params['name'] . "'\n                value=\"" . self::cleanInputText($params['value']) . "\">\n";
        }
        if (!isset($options['display']) || $options['display']) {
            echo $output;
        } else {
            return $output;
        }
    }

Usage Example

Example #1
0
 /**
  * @see Rule::showNewRuleForm()
  **/
 function showNewRuleForm($ID)
 {
     echo "<form method='post' action='" . Toolbox::getItemTypeFormURL('Entity') . "'>";
     echo "<table  class='tab_cadre_fixe'>";
     echo "<tr><th colspan='7'>" . __('Authorizations assignment rules') . "</th></tr>\n";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('Name') . "</td><td>";
     Html::autocompletionTextField($this, "name", array('value' => '', 'size' => 33));
     echo '</td><td>' . __('Description') . "</td><td>";
     Html::autocompletionTextField($this, "description", array('value' => '', 'size' => 33));
     echo "</td><td>" . __('Logical operator') . "</td><td>";
     $this->dropdownRulesMatch();
     echo "</td><td rowspan='2' class='tab_bg_2 center middle'>";
     echo "<input type=hidden name='sub_type' value='" . get_class($this) . "'>";
     echo "<input type=hidden name='entities_id' value='-1'>";
     echo "<input type=hidden name='affectentity' value='{$ID}'>";
     echo "<input type=hidden name='_method' value='AddRule'>";
     echo "<input type='submit' name='execute' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
     echo "</td></tr>\n";
     echo "<tr class='tab_bg_1'>";
     echo "<td class='center'>" . _n('Profile', 'Profiles', 1) . "</td><td>";
     Profile::dropdown();
     echo "</td><td><span class='small_space'>" . __('Recursive') . "</span></td><td colspan='3'>";
     Dropdown::showYesNo("is_recursive", 0);
     echo "</td></tr>\n";
     echo "</table>";
     Html::closeForm();
 }
All Usage Examples Of Html::autocompletionTextField
Html