DateRangePicker.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. return $js.parent::toHtml();
  28. }
  29. /**
  30. * @param string $value
  31. */
  32. public function setValue($value)
  33. {
  34. $this->updateAttributes(
  35. array(
  36. 'value' => $value
  37. )
  38. );
  39. }
  40. /**
  41. * Get the necessary javascript for this datepicker
  42. * @return string
  43. */
  44. private function getElementJS()
  45. {
  46. $js = null;
  47. $id = $this->getAttribute('id');
  48. $dateRange = $this->getAttribute('value');
  49. $defaultDates = null;
  50. if (!empty($dateRange)) {
  51. $dates = $this->parseDateRange($dateRange);
  52. $defaultDates = "
  53. startDate: '".$dates['start']."',
  54. endDate: '".$dates['end']."', ";
  55. }
  56. $minDate = null;
  57. $minDateValue = $this->getAttribute('minDate');
  58. if (!empty($minDateValue)) {
  59. $minDate = "
  60. minDate: '{$minDateValue}',
  61. ";
  62. }
  63. $maxDate = null;
  64. $maxDateValue = $this->getAttribute('maxDate');
  65. if (!empty($maxDateValue)) {
  66. $maxDate = "
  67. maxDate: '{$maxDateValue}',
  68. ";
  69. }
  70. $format = 'YYYY-MM-DD HH:mm';
  71. $formatValue = $this->getAttribute('format');
  72. if (!empty($formatValue)) {
  73. $format = $formatValue;
  74. }
  75. $timePicker = 'true';
  76. $timePickerValue = $this->getAttribute('timePicker');
  77. if (!empty($timePickerValue)) {
  78. $timePicker = $timePickerValue;
  79. }
  80. // timeFormat: 'hh:mm'
  81. $js .= "<script>
  82. $(function() {
  83. $('#$id').daterangepicker({
  84. format: '$format',
  85. timePicker: $timePicker,
  86. timePickerIncrement: 30,
  87. timePicker12Hour: false,
  88. $defaultDates
  89. $maxDate
  90. $minDate
  91. ranges: {
  92. '".addslashes(get_lang('Today'))."': [moment(), moment()],
  93. '".addslashes(get_lang('ThisWeek'))."': [moment().weekday(1), moment().weekday(5)],
  94. '".addslashes(get_lang('NextWeek'))."': [moment().weekday(8), moment().weekday(12)]
  95. },
  96. //showDropdowns : true,
  97. separator: ' / ',
  98. locale: {
  99. applyLabel: '".addslashes(get_lang('Ok'))."',
  100. cancelLabel: '".addslashes(get_lang('Cancel'))."',
  101. fromLabel: '".addslashes(get_lang('From'))."',
  102. toLabel: '".addslashes(get_lang('Until'))."',
  103. customRangeLabel: '".addslashes(get_lang('CustomRange'))."',
  104. }
  105. });
  106. });
  107. </script>";
  108. return $js;
  109. }
  110. /**
  111. * @param array $dateRange
  112. *
  113. * @return array
  114. */
  115. public function parseDateRange($dateRange)
  116. {
  117. $dates = explode('/', $dateRange);
  118. $dates = array_map('trim', $dates);
  119. $start = isset($dates[0]) ? $dates[0] : '';
  120. $end = isset($dates[1]) ? $dates[1] : '';
  121. return array(
  122. 'start' => $start,
  123. 'end' => $end
  124. );
  125. }
  126. /**
  127. * @param array $dates result of parseDateRange()
  128. *
  129. * @return bool
  130. */
  131. public function validateDates($dates)
  132. {
  133. if (empty($dates['start']) || empty($dates['end'])) {
  134. return false;
  135. }
  136. $format = 'Y-m-d H:i';
  137. $d = DateTime::createFromFormat($format, $dates['start']);
  138. $resultStart = $d && $d->format($format) == $dates['start'];
  139. $d = DateTime::createFromFormat($format, $dates['end']);
  140. $resultEnd = $d && $d->format($format) == $dates['end'];
  141. if (!($resultStart) || !$resultEnd) {
  142. return false;
  143. }
  144. return true;
  145. }
  146. }