DateTimePicker.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form element to select a date and hour.
  5. */
  6. class DateTimePicker extends HTML_QuickForm_text
  7. {
  8. /**
  9. * DateTimePicker constructor.
  10. *
  11. * @param string $elementName
  12. * @param string|array $elementLabel
  13. * @param array $attributes
  14. */
  15. public function __construct($elementName, $elementLabel = null, $attributes = null)
  16. {
  17. if (!isset($attributes['id'])) {
  18. $attributes['id'] = $elementName;
  19. }
  20. $attributes['class'] = 'form-control';
  21. parent::__construct($elementName, $elementLabel, $attributes);
  22. $this->_appendName = true;
  23. }
  24. /**
  25. * HTML code to display this datepicker.
  26. *
  27. * @return string
  28. */
  29. public function toHtml()
  30. {
  31. if ($this->_flagFrozen) {
  32. return $this->getFrozenHtml();
  33. }
  34. $id = $this->getAttribute('id');
  35. $value = $this->getValue();
  36. $formattedValue = '';
  37. if (!empty($value)) {
  38. $formattedValue = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
  39. }
  40. $label = $this->getLabel();
  41. if (is_array($label) && isset($label[0])) {
  42. $label = $label[0];
  43. }
  44. $resetFieldX = sprintf(get_lang('Reset %s'), $label);
  45. return '
  46. <div class="input-group mb-3" id="date_time_wrapper_'.$id.'">
  47. <span class="input-group-prepend">
  48. <input '.$this->_getAttrString($this->_attributes).'>
  49. </span>
  50. <p class="form-control disabled" id="'.$id.'_alt_text">'.$formattedValue.'</p>
  51. <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
  52. <div class="input-group-append">
  53. <button class="btn btn-light" type="button"
  54. title="'.$resetFieldX.'">
  55. <span class="fa fa-trash text-danger" aria-hidden="true"></span>
  56. <span class="sr-only">'.$resetFieldX.'</span>
  57. </button>
  58. </div>
  59. </div>
  60. '.$this->getElementJS();
  61. }
  62. /**
  63. * @param string $value
  64. */
  65. public function setValue($value)
  66. {
  67. $value = substr($value, 0, 16);
  68. $this->updateAttributes(['value' => $value]);
  69. }
  70. /**
  71. * @param string $layout
  72. *
  73. * @return string
  74. */
  75. public function getTemplate($layout)
  76. {
  77. $size = $this->calculateSize();
  78. switch ($layout) {
  79. case FormValidator::LAYOUT_INLINE:
  80. return '
  81. <div class="form-group {error_class}">
  82. <label {label-for} >
  83. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  84. {label}
  85. </label>
  86. {element}
  87. </div>';
  88. break;
  89. case FormValidator::LAYOUT_HORIZONTAL:
  90. return '
  91. <div class="form-group row {error_class}">
  92. <label {label-for} class="col-sm-'.$size[0].' col-form-label {extra_label_class}" >
  93. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  94. {label}
  95. </label>
  96. <div class="col-sm-'.$size[1].'">
  97. {icon}
  98. {element}
  99. <!-- BEGIN label_2 -->
  100. <p class="help-block">{label_2}</p>
  101. <!-- END label_2 -->
  102. <!-- BEGIN error -->
  103. <span class="help-inline help-block">{error}</span>
  104. <!-- END error -->
  105. </div>
  106. <div class="col-sm-'.$size[2].'">
  107. <!-- BEGIN label_3 -->
  108. {label_3}
  109. <!-- END label_3 -->
  110. </div>
  111. </div>';
  112. break;
  113. case FormValidator::LAYOUT_BOX_NO_LABEL:
  114. return '{element}';
  115. break;
  116. }
  117. }
  118. /**
  119. * Get the necessary javascript for this datepicker.
  120. *
  121. * @return string
  122. */
  123. private function getElementJS()
  124. {
  125. $js = null;
  126. $id = $this->getAttribute('id');
  127. //timeFormat: 'hh:mm'
  128. $js .= "<script>
  129. $(function() {
  130. var txtDateTime = $('#$id'),
  131. inputGroup = txtDateTime.parents('.input-group'),
  132. txtDateTimeAlt = $('#{$id}_alt'),
  133. txtDateTimeAltText = $('#{$id}_alt_text');
  134. txtDateTime
  135. .hide()
  136. .datetimepicker({
  137. defaultDate: '".$this->getValue()."',
  138. dateFormat: 'yy-mm-dd',
  139. timeFormat: 'HH:mm',
  140. altField: '#{$id}_alt',
  141. altFormat: \"".get_lang('MM dd, yy')."\",
  142. altTimeFormat: \"".get_lang('HH:mm')."\",
  143. altSeparator: \" ".get_lang(' at')." \",
  144. altFieldTimeOnly: false,
  145. showOn: 'both',
  146. buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  147. buttonImageOnly: true,
  148. buttonText: '".get_lang('Select date')."',
  149. changeMonth: true,
  150. changeYear: true
  151. })
  152. .on('change', function (e) {
  153. txtDateTimeAltText.text(txtDateTimeAlt.val());
  154. });
  155. txtDateTimeAltText.on('click', function () {
  156. txtDateTime.datepicker('show');
  157. });
  158. inputGroup
  159. .find('button')
  160. .on('click', function (e) {
  161. e.preventDefault();
  162. $('#$id, #{$id}_alt').val('');
  163. $('#{$id}_alt_text').html('');
  164. });
  165. });
  166. </script>";
  167. return $js;
  168. }
  169. }