DateRangePicker.php 3.2 KB

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