Html::showGenericDateTimeSearch PHP Method

showGenericDateTimeSearch() static public method

Show generic date search
static public showGenericDateTimeSearch ( $element, $value = '', $options = [] ) : rand
$element name of the html element
$value default value (default '')
$options array of possible options: - with_time display with time selection ? (default false) - with_future display with future date selection ? (default false) - with_days display specific days selection TODAY, BEGINMONTH, LASTMONDAY... ? (default true)
return rand value of dropdown
    static function showGenericDateTimeSearch($element, $value = '', $options = array())
    {
        global $CFG_GLPI;
        $p['with_time'] = false;
        $p['with_future'] = false;
        $p['with_days'] = true;
        $p['with_specific_date'] = true;
        $p['display'] = true;
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $p[$key] = $val;
            }
        }
        $rand = mt_rand();
        $output = '';
        // Validate value
        if ($value != 'NOW' && $value != 'TODAY' && !preg_match("/\\d{4}-\\d{2}-\\d{2}.*/", $value) && !strstr($value, 'HOUR') && !strstr($value, 'DAY') && !strstr($value, 'WEEK') && !strstr($value, 'MONTH') && !strstr($value, 'YEAR')) {
            $value = "";
        }
        if (empty($value)) {
            $value = 'NOW';
        }
        $specific_value = date("Y-m-d H:i:s");
        if (preg_match("/\\d{4}-\\d{2}-\\d{2}.*/", $value)) {
            $specific_value = $value;
            $value = 0;
        }
        $output .= "<table width='100%'><tr><td width='50%'>";
        $dates = Html::getGenericDateTimeSearchItems($p);
        $output .= Dropdown::showFromArray("_select_{$element}", $dates, array('value' => $value, 'display' => false, 'rand' => $rand));
        $field_id = Html::cleanId("dropdown__select_{$element}{$rand}");
        $output .= "</td><td width='50%'>";
        $contentid = Html::cleanId("displaygenericdate{$element}{$rand}");
        $output .= "<span id='{$contentid}'></span>";
        $params = array('value' => '__VALUE__', 'name' => $element, 'withtime' => $p['with_time'], 'specificvalue' => $specific_value);
        $output .= Ajax::updateItemOnSelectEvent($field_id, $contentid, $CFG_GLPI["root_doc"] . "/ajax/genericdate.php", $params, false);
        $params['value'] = $value;
        $output .= Ajax::updateItem($contentid, $CFG_GLPI["root_doc"] . "/ajax/genericdate.php", $params, '', false);
        $output .= "</td></tr></table>";
        if ($p['display']) {
            echo $output;
            return $rand;
        }
        return $output;
    }

Usage Example

コード例 #1
0
 /**
  * Select a field using standard system
  *
  * @since version 0.83
  *
  * @param $field_id_or_search_options  integer/string/array id of the search option field
  *                                                             or field name
  *                                                             or search option array
  * @param $name                        string               name of the select (if empty use linkfield)
  *                                                          (default '')
  * @param $values                                           mixed default value to display
  *                                                          (default '')
  * @param $options                     array                of possible options:
  * Parameters which could be used in options array :
  *    - comments : boolean / is the comments displayed near the value (default false)
  *    - any others options passed to specific display method
  *
  * @return return the string to display
  **/
 function getValueToSelect($field_id_or_search_options, $name = '', $values = '', $options = array())
 {
     global $CFG_GLPI;
     $param['comments'] = false;
     $param['html'] = false;
     foreach ($param as $key => $val) {
         if (!isset($options[$key])) {
             $options[$key] = $val;
         }
     }
     $searchoptions = array();
     if (is_array($field_id_or_search_options)) {
         $searchoptions = $field_id_or_search_options;
     } else {
         $searchopt = $this->getSearchOptions();
         // Get if id of search option is passed
         if (is_numeric($field_id_or_search_options)) {
             if (isset($searchopt[$field_id_or_search_options])) {
                 $searchoptions = $searchopt[$field_id_or_search_options];
             }
         } else {
             // Get if field name is passed
             $searchoptions = $this->getSearchOptionByField('field', $field_id_or_search_options, $this->getTable());
         }
     }
     if (count($searchoptions)) {
         $field = $searchoptions['field'];
         // Normalize option
         if (is_array($values)) {
             $value = $values[$field];
         } else {
             $value = $values;
             $values = array($field => $value);
         }
         if (empty($name)) {
             $name = $searchoptions['linkfield'];
         }
         // If not set : set to specific
         if (!isset($searchoptions['datatype'])) {
             $searchoptions['datatype'] = 'specific';
         }
         $options['display'] = false;
         $unit = '';
         if (isset($searchoptions['unit'])) {
             $unit = $searchoptions['unit'];
         }
         if (isset($options[$searchoptions['table'] . '.' . $searchoptions['field']])) {
             $options = array_merge($options, $options[$searchoptions['table'] . '.' . $searchoptions['field']]);
         }
         switch ($searchoptions['datatype']) {
             case "count":
             case "number":
             case "integer":
                 $copytooption = array('min', 'max', 'step', 'toadd', 'unit');
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 $options['value'] = $value;
                 return Dropdown::showNumber($name, $options);
             case "decimal":
             case "mac":
             case "ip":
             case "string":
             case "email":
             case "weblink":
                 $this->fields[$name] = $value;
                 return Html::autocompletionTextField($this, $name, $options);
             case "text":
                 $out = '';
                 if (isset($searchoptions['htmltext']) && $searchoptions['htmltext']) {
                     $out = Html::initEditorSystem($name, '', false);
                 }
                 return $out . "<textarea cols='45' rows='5' name='{$name}'>{$value}</textarea>";
             case "bool":
                 return Dropdown::showYesNo($name, $value, -1, $options);
             case "color":
                 return Html::showColorField($name, $options);
             case "date":
             case "date_delay":
                 if (isset($options['relative_dates']) && $options['relative_dates']) {
                     if (isset($searchoptions['maybefuture']) && $searchoptions['maybefuture']) {
                         $options['with_future'] = true;
                     }
                     return Html::showGenericDateTimeSearch($name, $value, $options);
                 }
                 $copytooption = array('min', 'max', 'maybeempty', 'showyear');
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 $options['value'] = $value;
                 return Html::showDateField($name, $options);
             case "datetime":
                 if (isset($options['relative_dates']) && $options['relative_dates']) {
                     if (isset($searchoptions['maybefuture']) && $searchoptions['maybefuture']) {
                         $options['with_future'] = true;
                     }
                     $options['with_time'] = true;
                     return Html::showGenericDateTimeSearch($name, $value, $options);
                 }
                 $copytooption = array('mindate', 'maxdate', 'mintime', 'maxtime', 'maybeempty', 'timestep');
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 $options['value'] = $value;
                 return Html::showDateTimeField($name, $options);
             case "timestamp":
                 $copytooption = array('addfirstminutes', 'emptylabel', 'inhours', 'max', 'min', 'step', 'toadd', 'display_emptychoice');
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 $options['value'] = $value;
                 return Dropdown::showTimeStamp($name, $options);
             case "itemlink":
                 // Do not use dropdown if wanted to select string value instead of ID
                 if (isset($options['itemlink_as_string']) && $options['itemlink_as_string']) {
                     break;
                 }
             case "dropdown":
                 $copytooption = array('condition', 'displaywith', 'emptylabel', 'right', 'toadd');
                 $options['name'] = $name;
                 $options['value'] = $value;
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 if (!isset($options['entity'])) {
                     $options['entity'] = $_SESSION['glpiactiveentities'];
                 }
                 $itemtype = getItemTypeForTable($searchoptions['table']);
                 return $itemtype::dropdown($options);
             case "right":
                 return Profile::dropdownRights(Profile::getRightsFor($searchoptions['rightclass']), $name, $value, array('multiple' => false, 'display' => false));
             case "itemtypename":
                 if (isset($searchoptions['itemtype_list'])) {
                     $options['types'] = $CFG_GLPI[$searchoptions['itemtype_list']];
                 }
                 $copytooption = array('types');
                 $options['value'] = $value;
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 if (isset($options['types'])) {
                     return Dropdown::showItemTypes($name, $options['types'], $options);
                 }
                 return false;
             case "language":
                 $copytooption = array('emptylabel', 'display_emptychoice');
                 foreach ($copytooption as $key) {
                     if (isset($searchoptions[$key]) && !isset($options[$key])) {
                         $options[$key] = $searchoptions[$key];
                     }
                 }
                 $options['value'] = $value;
                 return Dropdown::showLanguages($name, $options);
         }
         // Get specific display if available
         $itemtype = getItemTypeForTable($searchoptions['table']);
         if ($item = getItemForItemtype($itemtype)) {
             $specific = $item->getSpecificValueToSelect($searchoptions['field'], $name, $values, $options);
             if (strlen($specific)) {
                 return $specific;
             }
         }
     }
     // default case field text
     $this->fields[$name] = $value;
     return Html::autocompletionTextField($this, $name, $options);
 }
Html