DateTimePicker.php 5.4 KB

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