Html::showDateTimeField PHP Method

showDateTimeField() static public method

Display DateTime form with calendar
static public showDateTimeField ( $name, $options = [] ) : rand
$name name of the element
$options array of possible options: - value : default value to display (default '') - timestep : step for time in minute (-1 use default config) (default -1) - maybeempty : may be empty ? (true by default) - canedit : could not modify element (true by default) - mindate : minimum allowed date (default '') - maxdate : maximum allowed date (default '') - mintime : minimum allowed time (default '') - maxtime : maximum allowed time (default '') - showyear : should we set/diplay the year? (true by default) - display : boolean display or get string (default true) - rand : specific random value (default generated one)
return rand value used if displayes else string
    static function showDateTimeField($name, $options = array())
    {
        global $CFG_GLPI;
        $p['value'] = '';
        $p['maybeempty'] = true;
        $p['canedit'] = true;
        $p['mindate'] = '';
        $p['maxdate'] = '';
        $p['mintime'] = '';
        $p['maxtime'] = '';
        $p['timestep'] = -1;
        $p['showyear'] = true;
        $p['display'] = true;
        $p['rand'] = mt_rand();
        foreach ($options as $key => $val) {
            if (isset($p[$key])) {
                $p[$key] = $val;
            }
        }
        if ($p['timestep'] < 0) {
            $p['timestep'] = $CFG_GLPI['time_step'];
        }
        $minHour = 0;
        $maxHour = 23;
        $minMinute = 0;
        $maxMinute = 59;
        $date_value = '';
        $hour_value = '';
        if (!empty($p['value'])) {
            list($date_value, $hour_value) = explode(' ', $p['value']);
        }
        if (!empty($p['mintime'])) {
            list($minHour, $minMinute) = explode(':', $p['mintime']);
            $minMinute = 0;
            // Check time in interval
            if (!empty($hour_value) && $hour_value < $p['mintime']) {
                $hour_value = $p['mintime'];
            }
        }
        if (!empty($p['maxtime'])) {
            list($maxHour, $maxMinute) = explode(':', $p['maxtime']);
            $maxMinute = 59;
            // Check time in interval
            if (!empty($hour_value) && $hour_value > $p['maxtime']) {
                $hour_value = $p['maxtime'];
            }
        }
        // reconstruct value to be valid
        if (!empty($date_value)) {
            $p['value'] = $date_value . ' ' . $hour_value;
        }
        $output = "<div class='no-wrap'>";
        $output .= "<input id='showdate" . $p['rand'] . "' type='text' name='_{$name}' value='" . self::convDateTime($p['value']) . "'>";
        $output .= Html::hidden($name, array('value' => $p['value'], 'id' => "hiddendate" . $p['rand']));
        if ($p['maybeempty'] && $p['canedit']) {
            $output .= "<img src='" . $CFG_GLPI['root_doc'] . "/pics/reset.png' alt=\"" . __('Clear') . "\" id='resetdate" . $p['rand'] . "' class='pointer'>";
        }
        $output .= "</div>";
        $js = "";
        if ($p['maybeempty'] && $p['canedit']) {
            $js .= "\$('#resetdate" . $p['rand'] . "').click(function(){\n                  \$('#showdate" . $p['rand'] . "').val('');\n                  \$('#hiddendate" . $p['rand'] . "').val('');\n                  });";
        }
        $js .= "\$( '#showdate" . $p['rand'] . "' ).datetimepicker({\n                  altField: '#hiddendate" . $p['rand'] . "',\n                  altFormat: 'yy-mm-dd',\n                  altTimeFormat: 'HH:mm',\n                  pickerTimeFormat : 'HH:mm',\n                  altFieldTimeOnly: false,\n                  firstDay: 1,\n                  parse: 'loose',\n                  showAnim: '',\n                  stepMinute: " . $p['timestep'] . ",\n                  showSecond: false,\n                  showOtherMonths: true,\n                  selectOtherMonths: true,\n                  showButtonPanel: true,\n                  changeMonth: true,\n                  changeYear: true,\n                  showOn: 'button',\n                  showWeek: true,\n                  controlType: 'select',\n                  buttonImage: '" . $CFG_GLPI['root_doc'] . "/pics/calendar.png',\n                  buttonImageOnly: true";
        if (!$p['canedit']) {
            $js .= ",disabled: true";
        }
        if (!empty($p['min'])) {
            $js .= ",minDate: '" . self::convDate($p['min']) . "'";
        }
        if (!empty($p['max'])) {
            $js .= ",maxDate: '" . self::convDate($p['max']) . "'";
        }
        switch ($_SESSION['glpidate_format']) {
            case 1:
                $p['showyear'] ? $format = 'dd-mm-yy' : ($format = 'dd-mm');
                break;
            case 2:
                $p['showyear'] ? $format = 'mm-dd-yy' : ($format = 'mm-dd');
                break;
            default:
                $p['showyear'] ? $format = 'yy-mm-dd' : ($format = 'mm-dd');
        }
        $js .= ",dateFormat: '" . $format . "'";
        $js .= ",timeFormat: 'HH:mm'";
        $js .= "});";
        $output .= Html::scriptBlock($js);
        if ($p['display']) {
            echo $output;
            return $p['rand'];
        }
        return $output;
    }

Usage Example

 public function displayField($canEdit = true)
 {
     if ($canEdit) {
         $required = $canEdit && $this->fields['required'] ? ' required' : '';
         $rand = mt_rand();
         Html::showDateTimeField('formcreator_field_' . $this->fields['id'], array('value' => $this->getValue(), 'rand' => $rand));
         echo '<script type="text/javascript">
               jQuery(document).ready(function($) {
                  $( "#showdate' . $rand . '" ).on("change", function() {
                     formcreatorChangeValueOf(' . $this->fields['id'] . ', this.value);
                  });
                  $( "#resetdate' . $rand . '" ).on("click", function() {
                     formcreatorChangeValueOf(' . $this->fields['id'] . ', "");
                  });
               });
            </script>';
     } else {
         echo $this->getAnswer();
     }
 }
All Usage Examples Of Html::showDateTimeField
Html