DateTimePicker.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * @return string
  23. */
  24. public function toHtml()
  25. {
  26. if ($this->_flagFrozen) {
  27. return $this->getFrozenHtml();
  28. }
  29. $id = $this->getAttribute('id');
  30. $value = $this->getValue();
  31. if (!empty($value)) {
  32. $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
  33. }
  34. return '
  35. <div class="input-group">
  36. <span class="input-group-addon cursor-pointer">
  37. <input ' . $this->_getAttrString($this->_attributes).'>
  38. </span>
  39. <p class="form-control disabled" id="' . $id.'_alt_text">'.$value.'</p>
  40. <input class="form-control" type="hidden" 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. public 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. txtDateTimeAlt = $('#{$id}_alt'),
  76. txtDateTimeAltText = $('#{$id}_alt_text');
  77. txtDateTime
  78. .hide()
  79. .datetimepicker({
  80. defaultDate: '".$this->getValue()."',
  81. dateFormat: 'yy-mm-dd',
  82. timeFormat: 'HH:mm',
  83. altField: '#{$id}_alt',
  84. altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
  85. altTimeFormat: \"" . get_lang('TimeFormatNoSecJS')."\",
  86. altSeparator: \" " . get_lang('AtTime')." \",
  87. altFieldTimeOnly: false,
  88. showOn: 'both',
  89. buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  90. buttonImageOnly: true,
  91. buttonText: '" . get_lang('SelectDate')."',
  92. changeMonth: true,
  93. changeYear: true
  94. })
  95. .on('change', function (e) {
  96. txtDateTimeAltText.text(txtDateTimeAlt.val());
  97. });
  98. txtDateTimeAltText.on('click', function () {
  99. txtDateTime.datepicker('show');
  100. });
  101. inputGroup
  102. .find('button')
  103. .on('click', function (e) {
  104. e.preventDefault();
  105. $('#$id, #{$id}_alt').val('');
  106. $('#{$id}_alt_text').html('');
  107. });
  108. });
  109. </script>";
  110. return $js;
  111. }
  112. /**
  113. * @param string $layout
  114. *
  115. * @return string
  116. */
  117. public function getTemplate($layout)
  118. {
  119. $size = $this->getColumnsSize();
  120. $id = $this->getAttribute('id');
  121. $value = $this->getValue();
  122. if (empty($size)) {
  123. $sizeTemp = $this->getInputSize();
  124. if (empty($size)) {
  125. $sizeTemp = 8;
  126. }
  127. $size = array(2, $sizeTemp, 2);
  128. } else {
  129. if (is_array($size)) {
  130. if (count($size) != 3) {
  131. $sizeTemp = $this->getInputSize();
  132. if (empty($size)) {
  133. $sizeTemp = 8;
  134. }
  135. $size = array(2, $sizeTemp, 2);
  136. }
  137. // else just keep the $size array as received
  138. } else {
  139. $size = array(2, intval($size), 2);
  140. }
  141. }
  142. if (!empty($value)) {
  143. $value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
  144. }
  145. switch ($layout) {
  146. case FormValidator::LAYOUT_INLINE:
  147. return '
  148. <div class="form-group {error_class}">
  149. <label {label-for} >
  150. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  151. {label}
  152. </label>
  153. {element}
  154. </div>';
  155. break;
  156. case FormValidator::LAYOUT_HORIZONTAL:
  157. return '
  158. <div class="form-group {error_class}">
  159. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  160. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  161. {label}
  162. </label>
  163. <div class="col-sm-'.$size[1].'">
  164. {icon}
  165. {element}
  166. <!-- BEGIN label_2 -->
  167. <p class="help-block">{label_2}</p>
  168. <!-- END label_2 -->
  169. <!-- BEGIN error -->
  170. <span class="help-inline">{error}</span>
  171. <!-- END error -->
  172. </div>
  173. <div class="col-sm-'.$size[2].'">
  174. <!-- BEGIN label_3 -->
  175. {label_3}
  176. <!-- END label_3 -->
  177. </div>
  178. </div>';
  179. break;
  180. case FormValidator::LAYOUT_BOX_NO_LABEL:
  181. return '{element}';
  182. break;
  183. }
  184. }
  185. }