Html::showDateField PHP Méthode

showDateField() static public méthode

Display Date form with calendar
static public showDateField ( $name, $options = [] ) : rand
$name name of the element
$options array of possible options: - value : default value to display (default '') - maybeempty : may be empty ? (true by default) - canedit : could not modify element (true by default) - min : minimum allowed date (default '') - max : maximum allowed date (default '') - showyear : should we set/diplay the year? (true by default) - display : boolean display of return string (default true) - rand : specific rand value (default generated one) - yearrange : set a year range to show in drop-down (default '')
Résultat rand value used if displayes else string
    static function showDateField($name, $options = array())
    {
        global $CFG_GLPI;
        $p['value'] = '';
        $p['maybeempty'] = true;
        $p['canedit'] = true;
        $p['min'] = '';
        $p['max'] = '';
        $p['showyear'] = true;
        $p['display'] = true;
        $p['rand'] = mt_rand();
        $p['yearrange'] = '';
        foreach ($options as $key => $val) {
            if (isset($p[$key])) {
                $p[$key] = $val;
            }
        }
        $output = "<div class='no-wrap'>";
        $output .= "<input id='showdate" . $p['rand'] . "' type='text' size='10' name='_{$name}' " . "value='" . self::convDate($p['value']) . "'>";
        $output .= Html::hidden($name, array('value' => $p['value'], 'id' => "hiddendate" . $p['rand'], 'size' => 10));
        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'] . "' ).datepicker({\n                  altField: '#hiddendate" . $p['rand'] . "',\n                  altFormat: 'yy-mm-dd',\n                  firstDay: 1,\n                  showOtherMonths: true,\n                  selectOtherMonths: true,\n                  showButtonPanel: true,\n                  changeMonth: true,\n                  changeYear: true,\n                  showOn: 'button',\n                  showWeek: true,\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']) . "'";
        }
        if (!empty($p['yearrange'])) {
            $js .= ",yearRange: '" . $p['yearrange'] . "'";
        }
        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 .= "});";
        $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::showDateField('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::showDateField
Html