DateTimePicker.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 DateTimePicker extends HTML_QuickForm_text
  7. {
  8. /**
  9. * Constructor
  10. */
  11. public function DateTimePicker($elementName = null, $elementLabel = null, $attributes = null)
  12. {
  13. if (!isset($attributes['id'])) {
  14. $attributes['id'] = $elementName;
  15. }
  16. HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  17. $this->_appendName = true;
  18. $this->_type = 'date_time_picker';
  19. }
  20. /**
  21. * HTML code to display this datepicker
  22. */
  23. public function toHtml()
  24. {
  25. $js = $this->getElementJS();
  26. return $js.parent::toHtml();
  27. }
  28. /**
  29. * @param string $value
  30. */
  31. function setValue($value)
  32. {
  33. $value = substr($value, 0, 16);
  34. $this->updateAttributes(
  35. array(
  36. 'value'=>$value
  37. )
  38. );
  39. }
  40. /**
  41. * Get the necessary javascript for this datepicker
  42. */
  43. private function getElementJS()
  44. {
  45. $js = null;
  46. $id = $this->getAttribute('id');
  47. //timeFormat: 'hh:mm'
  48. $js .= "<script>
  49. $(function() {
  50. $('#$id').datetimepicker({
  51. dateFormat: 'yy-mm-dd',
  52. timeFormat: 'HH:mm'
  53. });
  54. });
  55. </script>";
  56. return $js;
  57. }
  58. }