DatePicker.php 6.6 KB

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