Html::getGenericDateTimeSearchItems PHP Method

getGenericDateTimeSearchItems() static public method

Get items to display for showGenericDateTimeSearch
See also: showGenericDateTimeSearch
static public getGenericDateTimeSearchItems ( $options ) : array
$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 array of posible values
    static function getGenericDateTimeSearchItems($options)
    {
        $params['with_time'] = false;
        $params['with_future'] = false;
        $params['with_days'] = true;
        $params['with_specific_date'] = true;
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $params[$key] = $val;
            }
        }
        $dates = array();
        if ($params['with_time']) {
            $dates['NOW'] = __('Now');
            if ($params['with_days']) {
                $dates['TODAY'] = __('Today');
            }
        } else {
            $dates['NOW'] = __('Today');
        }
        if ($params['with_specific_date']) {
            $dates[0] = __('Specify a date');
        }
        if ($params['with_time']) {
            for ($i = 1; $i <= 24; $i++) {
                $dates['-' . $i . 'HOUR'] = sprintf(_n('- %d hour', '- %d hours', $i), $i);
            }
        }
        for ($i = 1; $i <= 7; $i++) {
            $dates['-' . $i . 'DAY'] = sprintf(_n('- %d day', '- %d days', $i), $i);
        }
        if ($params['with_days']) {
            $dates['LASTSUNDAY'] = __('last Sunday');
            $dates['LASTMONDAY'] = __('last Monday');
            $dates['LASTTUESDAY'] = __('last Tuesday');
            $dates['LASTWEDNESDAY'] = __('last Wednesday');
            $dates['LASTTHURSDAY'] = __('last Thursday');
            $dates['LASTFRIDAY'] = __('last Friday');
            $dates['LASTSATURDAY'] = __('last Saturday');
        }
        for ($i = 1; $i <= 10; $i++) {
            $dates['-' . $i . 'WEEK'] = sprintf(_n('- %d week', '- %d weeks', $i), $i);
        }
        if ($params['with_days']) {
            $dates['BEGINMONTH'] = __('Beginning of the month');
        }
        for ($i = 1; $i <= 12; $i++) {
            $dates['-' . $i . 'MONTH'] = sprintf(_n('- %d month', '- %d months', $i), $i);
        }
        if ($params['with_days']) {
            $dates['BEGINYEAR'] = __('Beginning of the year');
        }
        for ($i = 1; $i <= 10; $i++) {
            $dates['-' . $i . 'YEAR'] = sprintf(_n('- %d year', '- %d years', $i), $i);
        }
        if ($params['with_future']) {
            if ($params['with_time']) {
                for ($i = 1; $i <= 24; $i++) {
                    $dates[$i . 'HOUR'] = sprintf(_n('+ %d hour', '+ %d hours', $i), $i);
                }
            }
            for ($i = 1; $i <= 7; $i++) {
                $dates[$i . 'DAY'] = sprintf(_n('+ %d day', '+ %d days', $i), $i);
            }
            for ($i = 1; $i <= 10; $i++) {
                $dates[$i . 'WEEK'] = sprintf(_n('+ %d week', '+ %d weeks', $i), $i);
            }
            for ($i = 1; $i <= 12; $i++) {
                $dates[$i . 'MONTH'] = sprintf(_n('+ %d month', '+ %d months', $i), $i);
            }
            for ($i = 1; $i <= 10; $i++) {
                $dates[$i . 'YEAR'] = sprintf(_n('+ %d year', '+ %d years', $i), $i);
            }
        }
        return $dates;
    }

Usage Example

Example #1
0
 /**
  * Show generic date search
  *
  * @param $element         name of the html element
  * @param $value           default value (default '')
  * @param $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><tr><td>";
     $output .= "<select id='genericdate{$element}{$rand}' name='_select_{$element}'>";
     $dates = Html::getGenericDateTimeSearchItems($options);
     foreach ($dates as $key => $val) {
         $output .= "<option value='{$key}' " . ($value === $key ? 'selected' : '') . ">{$val}</option>";
     }
     $output .= "</select>";
     $output .= "</td><td>";
     $output .= "<div id='displaygenericdate{$element}{$rand}'></div>";
     $params = array('value' => '__VALUE__', 'name' => $element, 'withtime' => $p['with_time'], 'specificvalue' => $specific_value);
     $output .= Ajax::updateItemOnSelectEvent("genericdate{$element}{$rand}", "displaygenericdate{$element}{$rand}", $CFG_GLPI["root_doc"] . "/ajax/genericdate.php", $params, false);
     $params['value'] = $value;
     $output .= Ajax::updateItem("displaygenericdate{$element}{$rand}", $CFG_GLPI["root_doc"] . "/ajax/genericdate.php", $params, '', false);
     $output .= "</td></tr></table>";
     if ($p['display']) {
         echo $output;
         return $rand;
     }
     return $output;
 }
All Usage Examples Of Html::getGenericDateTimeSearchItems
Html