DatePicker.php 6.9 KB

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