DateRangePicker.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. format: '$format',
  88. timePicker: $timePicker,
  89. timePickerIncrement: 30,
  90. timePicker12Hour: false,
  91. $defaultDates
  92. $maxDate
  93. $minDate
  94. ranges: {
  95. '".addslashes(get_lang('Today'))."': [moment(), moment()],
  96. '".addslashes(get_lang('ThisWeek'))."': [moment().weekday(1), moment().weekday(5)],
  97. '".addslashes(get_lang('NextWeek'))."': [moment().weekday(8), moment().weekday(12)]
  98. },
  99. //showDropdowns : true,
  100. separator: ' / ',
  101. locale: {
  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. });
  110. </script>";
  111. return $js;
  112. }
  113. /**
  114. * @param array $dateRange
  115. *
  116. * @return array
  117. */
  118. public function parseDateRange($dateRange)
  119. {
  120. $dates = explode('/', $dateRange);
  121. $dates = array_map('trim', $dates);
  122. $start = isset($dates[0]) ? $dates[0] : '';
  123. $end = isset($dates[1]) ? $dates[1] : '';
  124. return array(
  125. 'start' => $start,
  126. 'end' => $end
  127. );
  128. }
  129. /**
  130. * @param array $dates result of parseDateRange()
  131. *
  132. * @return bool
  133. */
  134. public function validateDates($dates, $format = null)
  135. {
  136. if (empty($dates['start']) || empty($dates['end'])) {
  137. return false;
  138. }
  139. $format = $format ? $format : 'Y-m-d H:i';
  140. $d = DateTime::createFromFormat($format, $dates['start']);
  141. $resultStart = $d && $d->format($format) == $dates['start'];
  142. $d = DateTime::createFromFormat($format, $dates['end']);
  143. $resultEnd = $d && $d->format($format) == $dates['end'];
  144. if (!($resultStart) || !$resultEnd) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. }