DatePicker.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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|array $elementLabel
  13. * @param array $attributes
  14. */
  15. public function __construct($elementName, $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. if (!empty($value)) {
  37. $value = api_format_date($value, DATE_FORMAT_LONG_NO_DAY);
  38. }
  39. return '
  40. <div class="input-group">
  41. <span class="input-group-addon cursor-pointer">
  42. <input '.$this->_getAttrString($this->_attributes).'>
  43. </span>
  44. <p class="form-control disabled" id="'.$id.'_alt_text">'.$value.'</p>
  45. <input class="form-control" type="hidden" id="'.$id.'_alt" value="'.$value.'">
  46. <span class="input-group-btn">
  47. <button class="btn btn-default" type="button"
  48. title="'.sprintf(get_lang('Reset %s'), $this->_label).'">
  49. <span class="fa fa-trash text-danger" aria-hidden="true"></span>
  50. <span class="sr-only">'.sprintf(get_lang('Reset %s'), $this->_label).'</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. [
  64. 'value' => $value,
  65. ]
  66. );
  67. }
  68. /**
  69. * @param string $layout
  70. *
  71. * @return string
  72. */
  73. public function getTemplate($layout)
  74. {
  75. $size = $this->calculateSize();
  76. switch ($layout) {
  77. case FormValidator::LAYOUT_INLINE:
  78. return '
  79. <div class="form-group {error_class}">
  80. <label {label-for} >
  81. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  82. {label}
  83. </label>
  84. {element}
  85. </div>';
  86. break;
  87. case FormValidator::LAYOUT_HORIZONTAL:
  88. return '
  89. <div class="form-group row {error_class}">
  90. <label {label-for} class="col-sm-'.$size[0].' col-form-label {extra_label_class}" >
  91. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  92. {label}
  93. </label>
  94. <div class="col-sm-'.$size[1].'">
  95. {icon}
  96. {element}
  97. <!-- BEGIN label_2 -->
  98. <p class="help-block">{label_2}</p>
  99. <!-- END label_2 -->
  100. <!-- BEGIN error -->
  101. <span class="help-inline help-block">{error}</span>
  102. <!-- END error -->
  103. </div>
  104. <div class="col-sm-'.$size[2].'">
  105. <!-- BEGIN label_3 -->
  106. {label_3}
  107. <!-- END label_3 -->
  108. </div>
  109. </div>';
  110. break;
  111. case FormValidator::LAYOUT_BOX_NO_LABEL:
  112. return '{element}';
  113. break;
  114. }
  115. }
  116. /**
  117. * Get the necessary javascript for this datepicker.
  118. *
  119. * @return string
  120. */
  121. private function getElementJS()
  122. {
  123. $js = null;
  124. $id = $this->getAttribute('id');
  125. $js .= "<script>
  126. $(function() {
  127. var txtDate = $('#$id'),
  128. inputGroup = txtDate.parents('.input-group'),
  129. txtDateAlt = $('#{$id}_alt'),
  130. txtDateAltText = $('#{$id}_alt_text');
  131. txtDate
  132. .hide()
  133. .datepicker({
  134. defaultDate: '".$this->getValue()."',
  135. dateFormat: 'yy-mm-dd',
  136. altField: '#{$id}_alt',
  137. altFormat: \"".get_lang('MM dd, yy')."\",
  138. showOn: 'both',
  139. buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  140. buttonImageOnly: true,
  141. buttonText: '".get_lang('Select date')."',
  142. changeMonth: true,
  143. changeYear: true,
  144. yearRange: 'c-60y:c+5y'
  145. })
  146. .on('change', function (e) {
  147. txtDateAltText.text(txtDateAlt.val());
  148. });
  149. txtDateAltText.on('click', function () {
  150. txtDate.datepicker('show');
  151. });
  152. inputGroup
  153. .find('button')
  154. .on('click', function (e) {
  155. e.preventDefault();
  156. $('#$id, #{$id}_alt').val('');
  157. $('#{$id}_alt_text').html('');
  158. });
  159. });
  160. </script>";
  161. return $js;
  162. }
  163. }