DatePicker.php 1.6 KB

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