DateRangePicker.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form element to select a range of dates (with popup datepicker)
  5. */
  6. class DateRangePicker extends HTML_QuickForm_text
  7. {
  8. /**
  9. * Constructor
  10. */
  11. public function __construct($elementName = null, $elementLabel = null, $attributes = null)
  12. {
  13. if (!isset($attributes['id'])) {
  14. $attributes['id'] = $elementName;
  15. }
  16. $attributes['class'] = 'form-control';
  17. parent::__construct($elementName, $elementLabel, $attributes);
  18. $this->_appendName = true;
  19. $this->_type = 'date_range_picker';
  20. }
  21. /**
  22. * @return string
  23. */
  24. public function toHtml()
  25. {
  26. $js = $this->getElementJS();
  27. $this->removeAttribute('format');
  28. $this->removeAttribute('timepicker');
  29. $this->removeAttribute('validate_format');
  30. return $js.parent::toHtml();
  31. }
  32. /**
  33. * @param string $value
  34. */
  35. public function setValue($value)
  36. {
  37. $this->updateAttributes(
  38. array(
  39. 'value' => $value
  40. )
  41. );
  42. }
  43. /**
  44. * Get the necessary javascript for this datepicker
  45. * @return string
  46. */
  47. private function getElementJS()
  48. {
  49. $js = null;
  50. $id = $this->getAttribute('id');
  51. $dateRange = $this->getAttribute('value');
  52. $defaultDates = null;
  53. if (!empty($dateRange)) {
  54. $dates = $this->parseDateRange($dateRange);
  55. $defaultDates = "
  56. startDate: '".$dates['start']."',
  57. endDate: '".$dates['end']."', ";
  58. }
  59. $minDate = null;
  60. $minDateValue = $this->getAttribute('minDate');
  61. if (!empty($minDateValue)) {
  62. $minDate = "
  63. minDate: '{$minDateValue}',
  64. ";
  65. }
  66. $maxDate = null;
  67. $maxDateValue = $this->getAttribute('maxDate');
  68. if (!empty($maxDateValue)) {
  69. $maxDate = "
  70. maxDate: '{$maxDateValue}',
  71. ";
  72. }
  73. $format = 'YYYY-MM-DD HH:mm';
  74. $formatValue = $this->getAttribute('format');
  75. if (!empty($formatValue)) {
  76. $format = $formatValue;
  77. }
  78. $timePicker = 'true';
  79. $timePickerValue = $this->getAttribute('timePicker');
  80. if (!empty($timePickerValue)) {
  81. $timePicker = $timePickerValue;
  82. }
  83. // timeFormat: 'hh:mm'
  84. $js .= "<script>
  85. $(function() {
  86. $('#$id').daterangepicker({
  87. timePicker: $timePicker,
  88. timePickerIncrement: 30,
  89. timePicker12Hour: false,
  90. $defaultDates
  91. $maxDate
  92. $minDate
  93. ranges: {
  94. '".addslashes(get_lang('Today'))."': [moment(), moment()],
  95. '".addslashes(get_lang('ThisWeek'))."': [moment().weekday(1), moment().weekday(5)],
  96. '".addslashes(get_lang('NextWeek'))."': [moment().weekday(8), moment().weekday(12)]
  97. },
  98. //showDropdowns : true,
  99. locale: {
  100. separator: ' / ',
  101. format: '$format',
  102. applyLabel: '".addslashes(get_lang('Ok'))."',
  103. cancelLabel: '".addslashes(get_lang('Cancel'))."',
  104. fromLabel: '".addslashes(get_lang('From'))."',
  105. toLabel: '".addslashes(get_lang('Until'))."',
  106. customRangeLabel: '".addslashes(get_lang('CustomRange'))."',
  107. }
  108. });
  109. $('#$id').on('change', function() {
  110. var myPickedDates = $('#$id').val().split('/');
  111. var {$id}_start = myPickedDates[0].trim();
  112. var {$id}_end = myPickedDates[1].trim();
  113. $('input[name={$id}_start]').val({$id}_start);
  114. $('input[name={$id}_end]').val({$id}_end);
  115. });
  116. });
  117. </script>";
  118. return $js;
  119. }
  120. /**
  121. * @param array $dateRange
  122. *
  123. * @return array
  124. */
  125. public function parseDateRange($dateRange)
  126. {
  127. $dates = explode('/', $dateRange);
  128. $dates = array_map('trim', $dates);
  129. $start = isset($dates[0]) ? $dates[0] : '';
  130. $end = isset($dates[1]) ? $dates[1] : '';
  131. return array(
  132. 'start' => $start,
  133. 'end' => $end
  134. );
  135. }
  136. /**
  137. * @param array $dates result of parseDateRange()
  138. *
  139. * @return bool
  140. */
  141. public function validateDates($dates, $format = null)
  142. {
  143. if (empty($dates['start']) || empty($dates['end'])) {
  144. return false;
  145. }
  146. $format = $format ? $format : 'Y-m-d H:i';
  147. $d = DateTime::createFromFormat($format, $dates['start']);
  148. $resultStart = $d && $d->format($format) == $dates['start'];
  149. $d = DateTime::createFromFormat($format, $dates['end']);
  150. $resultEnd = $d && $d->format($format) == $dates['end'];
  151. if (!($resultStart) || !$resultEnd) {
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * @param mixed $value
  158. * @param array $submitValues
  159. * @param array $errors
  160. * @return string
  161. */
  162. public function getSubmitValue($value, &$submitValues, &$errors)
  163. {
  164. /** @var DateRangePicker $element */
  165. $elementName = $this->getName();
  166. $parsedDates = $this->parseDateRange($value);
  167. $validateFormat = $this->getAttribute('validate_format');
  168. if (!$this->validateDates($parsedDates, $validateFormat)) {
  169. $errors[$elementName] = get_lang('CheckDates');
  170. }
  171. $submitValues[$elementName.'_start'] = $parsedDates['start'];
  172. $submitValues[$elementName.'_end'] = $parsedDates['end'];
  173. return $value;
  174. }
  175. }