DatePicker.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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('ResetFieldX'), $this->_label).'">
  49. <span class="fa fa-trash text-danger" aria-hidden="true"></span>
  50. <span class="sr-only">'.sprintf(get_lang('ResetFieldX'), $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. case FormValidator::LAYOUT_HORIZONTAL:
  87. return '
  88. <div class="form-group {error_class}">
  89. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  90. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  91. {label}
  92. </label>
  93. <div class="col-sm-'.$size[1].'">
  94. {icon}
  95. {element}
  96. <!-- BEGIN label_2 -->
  97. <p class="help-block">{label_2}</p>
  98. <!-- END label_2 -->
  99. <!-- BEGIN error -->
  100. <span class="help-inline help-block">{error}</span>
  101. <!-- END error -->
  102. </div>
  103. <div class="col-sm-'.$size[2].'">
  104. <!-- BEGIN label_3 -->
  105. {label_3}
  106. <!-- END label_3 -->
  107. </div>
  108. </div>';
  109. case FormValidator::LAYOUT_BOX_NO_LABEL:
  110. return '{element}';
  111. }
  112. }
  113. /**
  114. * Get the necessary javascript for this datepicker.
  115. *
  116. * @return string
  117. */
  118. private function getElementJS()
  119. {
  120. $js = null;
  121. $id = $this->getAttribute('id');
  122. $js .= "<script>
  123. $(function() {
  124. var txtDate = $('#$id'),
  125. inputGroup = txtDate.parents('.input-group'),
  126. txtDateAlt = $('#{$id}_alt'),
  127. txtDateAltText = $('#{$id}_alt_text');
  128. txtDate
  129. .hide()
  130. .datepicker({
  131. defaultDate: '".$this->getValue()."',
  132. dateFormat: 'yy-mm-dd',
  133. altField: '#{$id}_alt',
  134. altFormat: \"".get_lang('DateFormatLongNoDayJS')."\",
  135. showOn: 'both',
  136. buttonImage: '".Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true)."',
  137. buttonImageOnly: true,
  138. buttonText: '".get_lang('SelectDate')."',
  139. changeMonth: true,
  140. changeYear: true,
  141. yearRange: 'c-60y:c+5y'
  142. })
  143. .on('change', function (e) {
  144. txtDateAltText.text(txtDateAlt.val());
  145. });
  146. txtDateAltText.on('click', function () {
  147. txtDate.datepicker('show');
  148. });
  149. inputGroup
  150. .find('button')
  151. .on('click', function (e) {
  152. e.preventDefault();
  153. $('#$id, #{$id}_alt').val('');
  154. $('#{$id}_alt_text').html('');
  155. });
  156. });
  157. </script>";
  158. return $js;
  159. }
  160. }