DateTimePicker.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form element to select a date and hour.
  5. */
  6. class DateTimePicker extends HTML_QuickForm_text
  7. {
  8. /**
  9. * Constructor.
  10. */
  11. public function __construct($elementName = null, $elementLabel = null, $attributes = null)
  12. {
  13. if (!isset($attributes['id'])) {
  14. $attributes['id'] = $elementName;
  15. }
  16. $attributes['class'] = 'form-control';
  17. parent::__construct($elementName, $elementLabel, $attributes);
  18. $this->_appendName = true;
  19. }
  20. /**
  21. * HTML code to display this datepicker.
  22. *
  23. * @return string
  24. */
  25. public function toHtml()
  26. {
  27. if ($this->_flagFrozen) {
  28. return $this->getFrozenHtml();
  29. }
  30. $id = $this->getAttribute('id');
  31. $value = $this->getValue();
  32. $formattedValue = '';
  33. if (!empty($value)) {
  34. $formattedValue = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
  35. }
  36. $label = $this->getLabel();
  37. if (is_array($label) && isset($label[0])) {
  38. $label = $label[0];
  39. }
  40. $resetFieldX = sprintf(get_lang('ResetFieldX'), $label);
  41. return '
  42. <div class="input-group" id="date_time_wrapper_'.$id.'">
  43. <span class="input-group-addon cursor-pointer">
  44. <input '.$this->_getAttrString($this->_attributes).'>
  45. </span>
  46. <p class="form-control disabled" id="'.$id.'_alt_text">'.$formattedValue.'</p>
  47. <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
  48. <span class="input-group-btn">
  49. <button class="btn btn-default" type="button"
  50. title="'.$resetFieldX.'">
  51. <span class="fa fa-trash text-danger" aria-hidden="true"></span>
  52. <span class="sr-only">'.$resetFieldX.'</span>
  53. </button>
  54. </span>
  55. </div>
  56. '.$this->getElementJS();
  57. }
  58. /**
  59. * @param string $value
  60. */
  61. public function setValue($value)
  62. {
  63. $value = substr($value, 0, 16);
  64. $this->updateAttributes(['value' => $value]);
  65. }
  66. /**
  67. * @param string $layout
  68. *
  69. * @return string
  70. */
  71. public function getTemplate($layout)
  72. {
  73. $size = $this->calculateSize();
  74. switch ($layout) {
  75. case FormValidator::LAYOUT_INLINE:
  76. return '
  77. <div class="form-group {error_class}">
  78. <label {label-for} >
  79. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  80. {label}
  81. </label>
  82. {element}
  83. </div>';
  84. case FormValidator::LAYOUT_HORIZONTAL:
  85. return '
  86. <div class="form-group {error_class}">
  87. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  88. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  89. {label}
  90. </label>
  91. <div class="col-sm-'.$size[1].'">
  92. {icon}
  93. {element}
  94. <!-- BEGIN label_2 -->
  95. <p class="help-block">{label_2}</p>
  96. <!-- END label_2 -->
  97. <!-- BEGIN error -->
  98. <span class="help-inline help-block">{error}</span>
  99. <!-- END error -->
  100. </div>
  101. <div class="col-sm-'.$size[2].'">
  102. <!-- BEGIN label_3 -->
  103. {label_3}
  104. <!-- END label_3 -->
  105. </div>
  106. </div>';
  107. case FormValidator::LAYOUT_BOX_NO_LABEL:
  108. return '{element}';
  109. }
  110. }
  111. /**
  112. * Get the necessary javascript for this datepicker.
  113. *
  114. * @return string
  115. */
  116. private function getElementJS()
  117. {
  118. $js = null;
  119. $id = $this->getAttribute('id');
  120. //timeFormat: 'hh:mm'
  121. $js .= "<script>
  122. $(function() {
  123. var txtDateTime = $('#$id'),
  124. inputGroup = txtDateTime.parents('.input-group'),
  125. txtDateTimeAlt = $('#{$id}_alt'),
  126. txtDateTimeAltText = $('#{$id}_alt_text');
  127. txtDateTime
  128. .hide()
  129. .datetimepicker({
  130. defaultDate: '".$this->getValue()."',
  131. dateFormat: 'yy-mm-dd',
  132. timeFormat: 'HH:mm',
  133. altField: '#{$id}_alt',
  134. altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
  135. altTimeFormat: \"".get_lang('TimeFormatNoSecJS')."\",
  136. altSeparator: \" ".get_lang('AtTime')." \",
  137. altFieldTimeOnly: false,
  138. showOn: 'both',
  139. buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  140. buttonImageOnly: true,
  141. buttonText: '".get_lang('SelectDate')."',
  142. changeMonth: true,
  143. changeYear: true
  144. })
  145. .on('change', function (e) {
  146. txtDateTimeAltText.text(txtDateTimeAlt.val());
  147. });
  148. txtDateTimeAltText.on('click', function () {
  149. txtDateTime.datepicker('show');
  150. });
  151. inputGroup
  152. .find('button')
  153. .on('click', function (e) {
  154. e.preventDefault();
  155. $('#$id, #{$id}_alt').val('');
  156. $('#{$id}_alt_text').html('');
  157. });
  158. });
  159. </script>";
  160. return $js;
  161. }
  162. }