123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /* For licensing terms, see /license.txt */
- /**
- * Form element to select a date and hour.
- */
- class DateTimePicker extends HTML_QuickForm_text
- {
- /**
- * Constructor
- */
- public function __construct($elementName = null, $elementLabel = null, $attributes = null)
- {
- if (!isset($attributes['id'])) {
- $attributes['id'] = $elementName;
- }
- $attributes['class'] = 'form-control';
- parent::__construct($elementName, $elementLabel, $attributes);
- $this->_appendName = true;
- }
- /**
- * HTML code to display this datepicker
- * @return string
- */
- public function toHtml()
- {
- if ($this->_flagFrozen) {
- return $this->getFrozenHtml();
- }
- $id = $this->getAttribute('id');
- $value = $this->getValue();
- if (!empty($value)) {
- $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
- }
- return '
- <div class="input-group">
- <span class="input-group-addon cursor-pointer">
- <input ' . $this->_getAttrString($this->_attributes).'>
- </span>
- <p class="form-control disabled" id="' . $id.'_alt_text">'.$value.'</p>
- <input class="form-control" type="hidden" id="' . $id.'_alt" value="'.$value.'">
- <span class="input-group-btn">
- <button class="btn btn-default" type="button">
- <span class="fa fa-times text-danger" aria-hidden="true"></span>
- <span class="sr-only">' . get_lang('Reset').'</span>
- </button>
- </span>
- </div>
- ' . $this->getElementJS();
- }
- /**
- * @param string $value
- */
- public function setValue($value)
- {
- $value = substr($value, 0, 16);
- $this->updateAttributes(
- array(
- 'value'=>$value
- )
- );
- }
- /**
- * Get the necessary javascript for this datepicker
- * @return string
- */
- private function getElementJS()
- {
- $js = null;
- $id = $this->getAttribute('id');
- //timeFormat: 'hh:mm'
- $js .= "<script>
- $(function() {
- var txtDateTime = $('#$id'),
- inputGroup = txtDateTime.parents('.input-group'),
- txtDateTimeAlt = $('#{$id}_alt'),
- txtDateTimeAltText = $('#{$id}_alt_text');
- txtDateTime
- .hide()
- .datetimepicker({
- defaultDate: '".$this->getValue()."',
- dateFormat: 'yy-mm-dd',
- timeFormat: 'HH:mm',
- altField: '#{$id}_alt',
- altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
- altTimeFormat: \"" . get_lang('TimeFormatNoSecJS')."\",
- altSeparator: \" " . get_lang('AtTime')." \",
- altFieldTimeOnly: false,
- showOn: 'both',
- buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
- buttonImageOnly: true,
- buttonText: '" . get_lang('SelectDate')."',
- changeMonth: true,
- changeYear: true
- })
- .on('change', function (e) {
- txtDateTimeAltText.text(txtDateTimeAlt.val());
- });
-
- txtDateTimeAltText.on('click', function () {
- txtDateTime.datepicker('show');
- });
- inputGroup
- .find('button')
- .on('click', function (e) {
- e.preventDefault();
- $('#$id, #{$id}_alt').val('');
- $('#{$id}_alt_text').html('');
- });
- });
- </script>";
- return $js;
- }
- /**
- * @param string $layout
- *
- * @return string
- */
- public function getTemplate($layout)
- {
- $size = $this->getColumnsSize();
- $id = $this->getAttribute('id');
- $value = $this->getValue();
- if (empty($size)) {
- $sizeTemp = $this->getInputSize();
- if (empty($size)) {
- $sizeTemp = 8;
- }
- $size = array(2, $sizeTemp, 2);
- } else {
- if (is_array($size)) {
- if (count($size) != 3) {
- $sizeTemp = $this->getInputSize();
- if (empty($size)) {
- $sizeTemp = 8;
- }
- $size = array(2, $sizeTemp, 2);
- }
- // else just keep the $size array as received
- } else {
- $size = array(2, intval($size), 2);
- }
- }
- if (!empty($value)) {
- $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
- }
- switch ($layout) {
- case FormValidator::LAYOUT_INLINE:
- return '
- <div class="form-group {error_class}">
- <label {label-for} >
- <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
- {label}
- </label>
- {element}
- </div>';
- break;
- case FormValidator::LAYOUT_HORIZONTAL:
- return '
- <div class="form-group {error_class}">
- <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
- <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
- {label}
- </label>
- <div class="col-sm-'.$size[1].'">
- {icon}
- {element}
- <!-- BEGIN label_2 -->
- <p class="help-block">{label_2}</p>
- <!-- END label_2 -->
- <!-- BEGIN error -->
- <span class="help-inline">{error}</span>
- <!-- END error -->
- </div>
- <div class="col-sm-'.$size[2].'">
- <!-- BEGIN label_3 -->
- {label_3}
- <!-- END label_3 -->
- </div>
- </div>';
- break;
- case FormValidator::LAYOUT_BOX_NO_LABEL:
- return '{element}';
- break;
- }
- }
- }
|