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. });
  84. });
  85. </script>";
  86. return $js;
  87. }
  88. /**
  89. * @param string $layout
  90. *
  91. * @return string
  92. */
  93. public function getTemplate($layout)
  94. {
  95. $size = $this->getColumnsSize();
  96. $id = $this->getAttribute('id');
  97. $value = $this->getValue();
  98. if (empty($size)) {
  99. $sizeTemp = $this->getInputSize();
  100. if (empty($size)) {
  101. $sizeTemp = 8;
  102. }
  103. $size = array(2, $sizeTemp, 2);
  104. } else {
  105. if (is_array($size)) {
  106. if (count($size) != 3) {
  107. $sizeTemp = $this->getInputSize();
  108. if (empty($size)) {
  109. $sizeTemp = 8;
  110. }
  111. $size = array(2, $sizeTemp, 2);
  112. }
  113. // else just keep the $size array as received
  114. } else {
  115. $size = array(2, intval($size), 2);
  116. }
  117. }
  118. if (!empty($value)) {
  119. $value = api_format_date($value, DATE_FORMAT_LONG_NO_DAY);
  120. }
  121. switch ($layout) {
  122. case FormValidator::LAYOUT_INLINE:
  123. return '
  124. <div class="form-group {error_class}">
  125. <label {label-for} >
  126. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  127. {label}
  128. </label>
  129. {element}
  130. </div>';
  131. break;
  132. case FormValidator::LAYOUT_HORIZONTAL:
  133. return '
  134. <div class="form-group {error_class}">
  135. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  136. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  137. {label}
  138. </label>
  139. <div class="col-sm-'.$size[1].'">
  140. {icon}
  141. {element}
  142. <!-- BEGIN label_2 -->
  143. <p class="help-block">{label_2}</p>
  144. <!-- END label_2 -->
  145. <!-- BEGIN error -->
  146. <span class="help-inline">{error}</span>
  147. <!-- END error -->
  148. </div>
  149. <div class="col-sm-'.$size[2].'">
  150. <!-- BEGIN label_3 -->
  151. {label_3}
  152. <!-- END label_3 -->
  153. </div>
  154. </div>';
  155. break;
  156. case FormValidator::LAYOUT_BOX_NO_LABEL:
  157. return '{element}';
  158. break;
  159. }
  160. }
  161. }