DateRangePicker.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'HTML/QuickForm/date.php';
  4. /**
  5. * Form element to select a date and hour (with popup datepicker)
  6. */
  7. class DateRangePicker extends HTML_QuickForm_text
  8. {
  9. /**
  10. * Constructor
  11. */
  12. public function DateRangePicker($elementName = null, $elementLabel = null, $attributes = null)
  13. {
  14. if (!isset($attributes['id'])) {
  15. $attributes['id'] = $elementName;
  16. }
  17. $attributes['class'] = 'span3';
  18. HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  19. $this->_appendName = true;
  20. $this->_type = 'date_range_picker';
  21. }
  22. /**
  23. * HTML code to display this datepicker
  24. */
  25. public function toHtml()
  26. {
  27. $js = $this->getElementJS();
  28. return $js.parent::toHtml();
  29. }
  30. /**
  31. * @param string $value
  32. */
  33. public function setValue($value)
  34. {
  35. $this->updateAttributes(
  36. array(
  37. 'value' => $value
  38. )
  39. );
  40. }
  41. /**
  42. * Get the necessary javascript for this datepicker
  43. * @return string
  44. */
  45. private function getElementJS()
  46. {
  47. $js = null;
  48. $id = $this->getAttribute('id');
  49. $dateRange = $this->getAttribute('value');
  50. $defaultDates = null;
  51. if (!empty($dateRange)) {
  52. $dates = $this->parseDateRange($dateRange);
  53. $defaultDates = "
  54. startDate: '".$dates['start']."',
  55. endDate: '".$dates['end']."', ";
  56. }
  57. //timeFormat: 'hh:mm'
  58. $js .= "<script>
  59. $(function() {
  60. $('#$id').daterangepicker({
  61. format: 'YYYY-MM-DD HH:mm',
  62. timePicker: true,
  63. timePickerIncrement: 30,
  64. timePicker12Hour: false,
  65. $defaultDates
  66. ranges: {
  67. '".addslashes(get_lang('Today'))."': [moment(), moment()],
  68. '".addslashes(get_lang('ThisWeek'))."': [moment().weekday(1), moment().weekday(5)],
  69. '".addslashes(get_lang('NextWeek'))."': [moment().weekday(8), moment().weekday(12)]
  70. },
  71. //showDropdowns : true,
  72. separator: ' / ',
  73. locale: {
  74. applyLabel: '".addslashes(get_lang('Ok'))."',
  75. cancelLabel: '".addslashes(get_lang('Cancel'))."',
  76. fromLabel: '".addslashes(get_lang('From'))."',
  77. toLabel: '".addslashes(get_lang('Until'))."',
  78. customRangeLabel: '".addslashes(get_lang('CustomRange'))."',
  79. }
  80. });
  81. });
  82. </script>";
  83. return $js;
  84. }
  85. /**
  86. * @param array $dateRange
  87. *
  88. * @return array
  89. */
  90. public function parseDateRange($dateRange)
  91. {
  92. $dates = explode('/', $dateRange);
  93. $dates = array_map('trim', $dates);
  94. return array(
  95. 'start' => $dates[0],
  96. 'end' => $dates[1]
  97. );
  98. }
  99. /**
  100. * @param array $dates result of parseDateRange()
  101. *
  102. * @return bool
  103. */
  104. public function validateDates($dates)
  105. {
  106. if (empty($dates['start']) || empty($dates['end'])) {
  107. return false;
  108. }
  109. $format = 'Y-m-d H:i';
  110. $d = DateTime::createFromFormat($format, $dates['start']);
  111. $resultStart = $d && $d->format($format) == $dates['start'];
  112. $d = DateTime::createFromFormat($format, $dates['end']);
  113. $resultEnd = $d && $d->format($format) == $dates['end'];
  114. if (!($resultStart) || !$resultEnd) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. }