DateTimePicker.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $this->_type = 'date_time_picker';
  20. }
  21. /**
  22. * HTML code to display this datepicker
  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. if (!empty($value)) {
  33. $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
  34. }
  35. return '
  36. <div class="input-group">
  37. <span class="input-group-addon">
  38. <input ' . $this->_getAttrString($this->_attributes) . '>
  39. </span>
  40. <input class="form-control" type="text" readonly id="' . $id . '_alt" value="' . $value . '">
  41. <span class="input-group-btn">
  42. <button class="btn btn-default" type="button">
  43. <span class="fa fa-times text-danger" aria-hidden="true"></span>
  44. <span class="sr-only">' . get_lang('Reset') . '</span>
  45. </button>
  46. </span>
  47. </div>
  48. ' . $this->getElementJS();
  49. }
  50. /**
  51. * @param string $value
  52. */
  53. function setValue($value)
  54. {
  55. $value = substr($value, 0, 16);
  56. $this->updateAttributes(
  57. array(
  58. 'value'=>$value
  59. )
  60. );
  61. }
  62. /**
  63. * Get the necessary javascript for this datepicker
  64. * @return string
  65. */
  66. private function getElementJS()
  67. {
  68. $js = null;
  69. $id = $this->getAttribute('id');
  70. //timeFormat: 'hh:mm'
  71. $js .= "<script>
  72. $(function() {
  73. var txtDateTime = $('#$id'),
  74. inputGroup = txtDateTime.parents('.input-group');
  75. txtDateTime
  76. .hide()
  77. .datetimepicker({
  78. defaultDate: '" . $this->getValue() . "',
  79. dateFormat: 'yy-mm-dd',
  80. timeFormat: 'HH:mm',
  81. altField: '#{$id}_alt',
  82. altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
  83. altTimeFormat: \"" . get_lang('TimeFormatNoSecJS') . "\",
  84. altSeparator: \" " . get_lang('AtTime') . " \",
  85. altFieldTimeOnly: false,
  86. showOn: 'both',
  87. buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
  88. buttonImageOnly: true,
  89. buttonText: '" . get_lang('SelectDate') . "',
  90. changeMonth: true,
  91. changeYear: true
  92. });
  93. inputGroup
  94. .find('button')
  95. .on('click', function (e) {
  96. e.preventDefault();
  97. $('#$id').datetimepicker({
  98. });
  99. });
  100. </script>";
  101. return $js;
  102. }
  103. /**
  104. * @param string $layout
  105. *
  106. * @return string
  107. */
  108. public function getTemplate($layout)
  109. {
  110. $size = $this->getColumnsSize();
  111. $id = $this->getAttribute('id');
  112. $value = $this->getValue();
  113. if (empty($size)) {
  114. $sizeTemp = $this->getInputSize();
  115. if (empty($size)) {
  116. $sizeTemp = 8;
  117. }
  118. $size = array(2, $sizeTemp, 2);
  119. } else {
  120. if (is_array($size)) {
  121. if (count($size) != 3) {
  122. $sizeTemp = $this->getInputSize();
  123. if (empty($size)) {
  124. $sizeTemp = 8;
  125. }
  126. $size = array(2, $sizeTemp, 2);
  127. }
  128. // else just keep the $size array as received
  129. } else {
  130. $size = array(2, intval($size), 2);
  131. }
  132. }
  133. if (!empty($value)) {
  134. $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
  135. }
  136. switch ($layout) {
  137. case FormValidator::LAYOUT_INLINE:
  138. return '
  139. <div class="form-group {error_class}">
  140. <label {label-for} >
  141. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  142. {label}
  143. </label>
  144. {element}
  145. </div>';
  146. break;
  147. case FormValidator::LAYOUT_HORIZONTAL:
  148. return '
  149. <div class="form-group {error_class}">
  150. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  151. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  152. {label}
  153. </label>
  154. <div class="col-sm-'.$size[1].'">
  155. {icon}
  156. {element}
  157. <!-- BEGIN label_2 -->
  158. <p class="help-block">{label_2}</p>
  159. <!-- END label_2 -->
  160. <!-- BEGIN error -->
  161. <span class="help-inline">{error}</span>
  162. <!-- END error -->
  163. </div>
  164. <div class="col-sm-'.$size[2].'">
  165. <!-- BEGIN label_3 -->
  166. {label_3}
  167. <!-- END label_3 -->
  168. </div>
  169. </div>';
  170. break;
  171. case FormValidator::LAYOUT_BOX_NO_LABEL:
  172. return '{element}';
  173. break;
  174. }
  175. }
  176. }