DatePicker.php 5.3 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 $elementLabel
  13. * @param array $attributes
  14. */
  15. public function DatePicker($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 $this->getElementJS() . '
  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. </div>
  48. ';
  49. }
  50. /**
  51. * @param string $value
  52. */
  53. public function setValue($value)
  54. {
  55. $value = substr($value, 0, 16);
  56. $this->updateAttributes(
  57. array(
  58. 'value' => $value
  59. )
  60. );
  61. }
  62. /**
  63. * Get the necessary javascript for this datepicker
  64. * @return string
  65. */
  66. private function getElementJS()
  67. {
  68. $js = null;
  69. $id = $this->getAttribute('id');
  70. $js .= "<script>
  71. $(function() {
  72. $('#$id').hide().datepicker({
  73. defaultDate: '" . $this->getValue() . "',
  74. dateFormat: 'yy-mm-dd',
  75. altField: '#{$id}_alt',
  76. altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
  77. showOn: 'both',
  78. buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
  79. buttonImageOnly: true,
  80. buttonText: '" . get_lang('SelectDate') . "',
  81. changeMonth: true,
  82. changeYear: true,
  83. yearRange: 'c-60y:c+5y'
  84. });
  85. });
  86. </script>";
  87. return $js;
  88. }
  89. /**
  90. * @param string $layout
  91. *
  92. * @return string
  93. */
  94. public function getTemplate($layout)
  95. {
  96. $size = $this->getColumnsSize();
  97. $id = $this->getAttribute('id');
  98. $value = $this->getValue();
  99. if (empty($size)) {
  100. $sizeTemp = $this->getInputSize();
  101. if (empty($size)) {
  102. $sizeTemp = 8;
  103. }
  104. $size = array(2, $sizeTemp, 2);
  105. } else {
  106. if (is_array($size)) {
  107. if (count($size) != 3) {
  108. $sizeTemp = $this->getInputSize();
  109. if (empty($size)) {
  110. $sizeTemp = 8;
  111. }
  112. $size = array(2, $sizeTemp, 2);
  113. }
  114. // else just keep the $size array as received
  115. } else {
  116. $size = array(2, intval($size), 2);
  117. }
  118. }
  119. if (!empty($value)) {
  120. $value = api_format_date($value, DATE_FORMAT_LONG_NO_DAY);
  121. }
  122. switch ($layout) {
  123. case FormValidator::LAYOUT_INLINE:
  124. return '
  125. <div class="form-group {error_class}">
  126. <label {label-for} >
  127. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  128. {label}
  129. </label>
  130. {element}
  131. </div>';
  132. break;
  133. case FormValidator::LAYOUT_HORIZONTAL:
  134. return '
  135. <div class="form-group {error_class}">
  136. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  137. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  138. {label}
  139. </label>
  140. <div class="col-sm-'.$size[1].'">
  141. {icon}
  142. {element}
  143. <!-- BEGIN label_2 -->
  144. <p class="help-block">{label_2}</p>
  145. <!-- END label_2 -->
  146. <!-- BEGIN error -->
  147. <span class="help-inline">{error}</span>
  148. <!-- END error -->
  149. </div>
  150. <div class="col-sm-'.$size[2].'">
  151. <!-- BEGIN label_3 -->
  152. {label_3}
  153. <!-- END label_3 -->
  154. </div>
  155. </div>';
  156. break;
  157. case FormValidator::LAYOUT_BOX_NO_LABEL:
  158. return '{element}';
  159. break;
  160. }
  161. }
  162. }