DateTimePicker.php 1.5 KB

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