DateTimePicker.php 6.2 KB

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