DatePicker.php 6.1 KB

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