DateTimePicker.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. public $addLibrary = false;
  10. /**
  11. * Constructor
  12. */
  13. public function DateTimePicker($elementName = null, $elementLabel = null, $attributes = null)
  14. {
  15. if (!isset($attributes['id'])) {
  16. $attributes['id'] = $elementName;
  17. }
  18. HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  19. $this->_appendName = true;
  20. $this->_type = 'datetimepicker';
  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. function setValue($value)
  31. {
  32. $value = substr($value, 0, 16);
  33. $this->updateAttributes(
  34. array(
  35. 'value'=>$value
  36. )
  37. );
  38. }
  39. /**
  40. * Get the necessary javascript for this datepicker
  41. */
  42. private function getElementJS()
  43. {
  44. $js = null;
  45. if ($this->addLibrary == true) {
  46. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/jquery-ui-timepicker-addon.js" type="text/javascript"></script>';
  47. $js .='<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css" />';
  48. $isocode = api_get_language_isocode();
  49. if ($isocode != 'en') {
  50. $js .= '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/datetimepicker/i18n/jquery-ui-timepicker-'.$isocode.'.js" type="text/javascript"></script>';
  51. $js .= api_get_js('jquery-ui/jquery-ui-i18n.min.js');
  52. $js .= '<script>
  53. $(function(){
  54. $.datepicker.setDefaults($.datepicker.regional["'.$isocode.'"]);
  55. });
  56. </script>';
  57. }
  58. }
  59. $id = $this->getAttribute('id');
  60. //timeFormat: 'hh:mm'
  61. $js .= "<script>
  62. $(function() {
  63. $('#$id').datetimepicker({
  64. dateFormat: 'yy-mm-dd'
  65. });
  66. });
  67. </script>";
  68. return $js;
  69. }
  70. }