DateTimeRangePicker.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form element to select a date.
  5. *
  6. * Class DatePicker
  7. */
  8. class DateTimeRangePicker extends DateRangePicker
  9. {
  10. /**
  11. * HTML code to display this datepicker.
  12. *
  13. * @return string
  14. */
  15. public function toHtml()
  16. {
  17. if ($this->_flagFrozen) {
  18. return $this->getFrozenHtml();
  19. }
  20. $id = $this->getAttribute('id');
  21. $value = $this->getValue();
  22. $label = $this->getLabel();
  23. if (!empty($value)) {
  24. $value = api_format_date($value, DATE_FORMAT_LONG_NO_DAY);
  25. }
  26. return '
  27. <div class="input-group">
  28. <span class="input-group-addon cursor-pointer">
  29. <input '.$this->_getAttrString($this->_attributes).'>
  30. </span>
  31. <p class="form-control disabled" id="'.$id.'_alt_text">'.$value.'</p>
  32. <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
  33. <span class="input-group-btn">
  34. <button class="btn btn-default" type="button"
  35. title="'.sprintf(get_lang('ResetFieldX'), $this->_label).'">
  36. <span class="fa fa-trash text-danger" aria-hidden="true"></span>
  37. <span class="sr-only">'.sprintf(get_lang('ResetFieldX'), $this->_label).'</span>
  38. </button>
  39. </span>
  40. </div>
  41. <div class="input-group">
  42. <br />
  43. <p id="'.$id.'_time_range">
  44. <input type="text" name="'.$id.'_time_range_start" class="time start" autocomplete="off">
  45. '.get_lang('To').'
  46. <input type="text" name="'.$id.'_time_range_end" class="time end " autocomplete="off">
  47. </p>
  48. </div>
  49. '.$this->getElementJS();
  50. }
  51. /**
  52. * Get the necessary javascript for this datepicker.
  53. *
  54. * @return string
  55. */
  56. private function getElementJS()
  57. {
  58. $js = null;
  59. $id = $this->getAttribute('id');
  60. $js .= "<script>
  61. $(function() {
  62. var txtDate = $('#$id'),
  63. inputGroup = txtDate.parents('.input-group'),
  64. txtDateAlt = $('#{$id}_alt'),
  65. txtDateAltText = $('#{$id}_alt_text');
  66. txtDate
  67. .hide()
  68. .datepicker({
  69. defaultDate: '".$this->getValue()."',
  70. dateFormat: 'yy-mm-dd',
  71. altField: '#{$id}_alt',
  72. altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
  73. showOn: 'both',
  74. buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  75. buttonImageOnly: true,
  76. buttonText: '".get_lang('SelectDate')."',
  77. changeMonth: true,
  78. changeYear: true,
  79. yearRange: 'c-60y:c+5y'
  80. })
  81. .on('change', function (e) {
  82. txtDateAltText.text(txtDateAlt.val());
  83. });
  84. txtDateAltText.on('click', function () {
  85. txtDate.datepicker('show');
  86. });
  87. inputGroup
  88. .find('button')
  89. .on('click', function (e) {
  90. e.preventDefault();
  91. $('#$id, #{$id}_alt').val('');
  92. $('#{$id}_alt_text').html('');
  93. });
  94. $('#".$id."_time_range .time').timepicker({
  95. 'showDuration': true,
  96. 'timeFormat': 'H:i:s',
  97. 'scrollDefault': 'now',
  98. });
  99. var timeOnlyExampleEl = document.getElementById('".$id."_time_range');
  100. var timeOnlyDatepair = new Datepair(timeOnlyExampleEl);
  101. });
  102. </script>";
  103. return $js;
  104. }
  105. }