DateTimeRangePicker.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form element to select a date.
  5. *
  6. * Class DatePicker
  7. */
  8. class DateTimeRangePicker extends DateRangePicker
  9. {
  10. /**
  11. * HTML code to display this datepicker.
  12. *
  13. * @return string
  14. */
  15. public function toHtml()
  16. {
  17. if ($this->_flagFrozen) {
  18. return $this->getFrozenHtml();
  19. }
  20. $id = $this->getAttribute('id');
  21. $dateRange = $this->getValue();
  22. $value = '';
  23. if (!empty($dateRange)) {
  24. $dates = $this->parseDateRange($dateRange);
  25. $value = api_format_date($dates['date'], DATE_FORMAT_LONG_NO_DAY);
  26. }
  27. return '
  28. <div class="input-group">
  29. <span class="input-group-addon cursor-pointer">
  30. <input '.$this->_getAttrString($this->_attributes).'>
  31. </span>
  32. <p class="form-control disabled" id="'.$id.'_alt_text">'.$value.'</p>
  33. <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
  34. <span class="input-group-btn">
  35. <button class="btn btn-default" type="button"
  36. title="'.sprintf(get_lang('ResetFieldX'), $this->_label).'">
  37. <span class="fa fa-trash text-danger" aria-hidden="true"></span>
  38. <span class="sr-only">'.sprintf(get_lang('ResetFieldX'), $this->_label).'</span>
  39. </button>
  40. </span>
  41. </div>
  42. '.$this->getElementJS();
  43. }
  44. /**
  45. * @param string $layout
  46. *
  47. * @return string
  48. */
  49. public function getTemplate($layout)
  50. {
  51. $size = $this->calculateSize();
  52. $id = $this->getAttribute('id');
  53. switch ($layout) {
  54. case FormValidator::LAYOUT_INLINE:
  55. return '
  56. <div class="form-group {error_class}">
  57. <label {label-for} >
  58. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  59. {label}
  60. </label>
  61. {element}
  62. </div>';
  63. break;
  64. case FormValidator::LAYOUT_HORIZONTAL:
  65. return '
  66. <span id="'.$id.'_date_time_wrapper">
  67. <div class="form-group {error_class}">
  68. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  69. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  70. {label}
  71. </label>
  72. <div class="col-sm-'.$size[1].'">
  73. {icon}
  74. {element}
  75. <!-- BEGIN label_2 -->
  76. <p class="help-block">{label_2}</p>
  77. <!-- END label_2 -->
  78. <!-- BEGIN error -->
  79. <span class="help-inline help-block">{error}</span>
  80. <!-- END error -->
  81. </div>
  82. <div class="col-sm-'.$size[2].'">
  83. <!-- BEGIN label_3 -->
  84. {label_3}
  85. <!-- END label_3 -->
  86. </div>
  87. </div>
  88. <div class="form-group {error_class}">
  89. <label class="col-sm-'.$size[0].' control-label" >
  90. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  91. '.get_lang('Hour').'
  92. </label>
  93. <div class="col-sm-'.$size[1].'">
  94. <div class="input-group">
  95. <p id="'.$id.'_time_range">
  96. <input type="text" id="'.$id.'_time_range_start" name="'.$id.'_time_range_start" class="time start" autocomplete="off">
  97. '.get_lang('To').'
  98. <input type="text" id="'.$id.'_time_range_end" name="'.$id.'_time_range_end" class="time end " autocomplete="off">
  99. </p>
  100. </div>
  101. </div>
  102. </div>
  103. </span>
  104. ';
  105. break;
  106. case FormValidator::LAYOUT_BOX_NO_LABEL:
  107. return '
  108. <label {label-for}>{label}</label>
  109. <div class="input-group">
  110. {icon}
  111. {element}
  112. </div>';
  113. break;
  114. }
  115. }
  116. /**
  117. * @param array $dateRange
  118. *
  119. * @return array
  120. */
  121. public function parseDateRange($dateRange)
  122. {
  123. $dateRange = Security::remove_XSS($dateRange);
  124. $dates = explode('@@', $dateRange);
  125. $dates = array_map('trim', $dates);
  126. $start = isset($dates[0]) ? $dates[0] : '';
  127. $end = isset($dates[1]) ? $dates[1] : '';
  128. $date = substr($start, 0, 10);
  129. $start = isset($dates[0]) ? $dates[0] : '';
  130. //$start = substr($start, 11, strlen($start));
  131. //$end = substr($end, 11, strlen($end));
  132. return [
  133. 'date' => $date,
  134. 'start_time' => $start,
  135. 'end_time' => $end,
  136. ];
  137. }
  138. /**
  139. * @param string $value
  140. */
  141. public function setValue($value)
  142. {
  143. $this->updateAttributes(
  144. [
  145. 'value' => $value,
  146. ]
  147. );
  148. }
  149. /**
  150. * Get the necessary javascript for this datepicker.
  151. *
  152. * @return string
  153. */
  154. private function getElementJS()
  155. {
  156. $js = null;
  157. $id = $this->getAttribute('id');
  158. $dateRange = $this->getValue();
  159. $defaultDate = '';
  160. $startTime = '';
  161. $endTime = '';
  162. if (!empty($dateRange)) {
  163. $dates = $this->parseDateRange($dateRange);
  164. $defaultDate = $dates['date'];
  165. $startTime = $dates['start_time'];
  166. $endTime = $dates['end_time'];
  167. }
  168. $js .= "<script>
  169. $(function() {
  170. var txtDate = $('#$id'),
  171. inputGroup = txtDate.parents('.input-group'),
  172. txtDateAlt = $('#{$id}_alt'),
  173. txtDateAltText = $('#{$id}_alt_text');
  174. txtDate
  175. .hide()
  176. .datepicker({
  177. defaultDate: '".$defaultDate."',
  178. dateFormat: 'yy-mm-dd',
  179. altField: '#{$id}_alt',
  180. altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
  181. showOn: 'both',
  182. buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  183. buttonImageOnly: true,
  184. buttonText: '".get_lang('SelectDate')."',
  185. changeMonth: true,
  186. changeYear: true,
  187. yearRange: 'c-60y:c+5y'
  188. })
  189. .on('change', function (e) {
  190. txtDateAltText.text(txtDateAlt.val());
  191. });
  192. txtDateAltText.on('click', function () {
  193. txtDate.datepicker('show');
  194. });
  195. inputGroup
  196. .find('button')
  197. .on('click', function (e) {
  198. e.preventDefault();
  199. $('#$id, #{$id}_alt').val('');
  200. $('#{$id}_alt_text').html('');
  201. });
  202. $('#".$id."_time_range .time').timepicker({
  203. 'showDuration': true,
  204. 'timeFormat': 'H:i:s',
  205. 'scrollDefault': 'now',
  206. });
  207. $('#".$id."_time_range_start').timepicker('setTime', new Date('".$startTime."'));
  208. $('#".$id."_time_range_end').timepicker('setTime', new Date('".$endTime."'));
  209. var timeOnlyExampleEl = document.getElementById('".$id."_time_range');
  210. var timeOnlyDatepair = new Datepair(timeOnlyExampleEl);
  211. });
  212. </script>";
  213. return $js;
  214. }
  215. }