DateTimePicker.php 5.5 KB

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