Between.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Dcat\Admin\Grid\Column\Filter;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\Column\Filter;
  5. use Dcat\Admin\Grid\Model;
  6. class Between extends Filter
  7. {
  8. protected $dateFormat = null;
  9. /**
  10. * @var bool
  11. */
  12. protected $timestamp = false;
  13. public function __construct()
  14. {
  15. $this->class = [
  16. 'start' => uniqid('column-filter-start-'),
  17. 'end' => uniqid('column-filter-end-'),
  18. ];
  19. }
  20. /**
  21. * Convert the datetime into unix timestamp.
  22. *
  23. * @return $this
  24. */
  25. public function toTimestamp()
  26. {
  27. $this->timestamp = true;
  28. return $this;
  29. }
  30. /**
  31. * Date filter.
  32. *
  33. * @return $this
  34. */
  35. public function date()
  36. {
  37. return $this->setDateFormat('YYYY-MM-DD');
  38. }
  39. /**
  40. * Time filter.
  41. *
  42. * @return $this
  43. */
  44. public function time()
  45. {
  46. return $this->setDateFormat('HH:mm:ss');
  47. }
  48. /**
  49. * Datetime filter.
  50. *
  51. * @return $this
  52. */
  53. public function datetime()
  54. {
  55. return $this->setDateFormat('YYYY-MM-DD HH:mm:ss');
  56. }
  57. /**
  58. * @param string $format
  59. * @return $this
  60. */
  61. protected function setDateFormat($format)
  62. {
  63. $this->dateFormat = $format;
  64. $this->requireAssets();
  65. return $this;
  66. }
  67. /**
  68. * Add a binding to the query.
  69. *
  70. * @param mixed $value
  71. * @param Model $model
  72. */
  73. public function addBinding($value, Model $model)
  74. {
  75. $value = array_filter((array) $value);
  76. if (empty($value)) {
  77. return;
  78. }
  79. if ($this->timestamp) {
  80. $value = array_map(function ($v) {
  81. if ($v) {
  82. return strtotime($v);
  83. }
  84. }, $value);
  85. }
  86. if (! isset($value['start'])) {
  87. $this->withQuery($model, 'where', ['<=', $value['end']]);
  88. return;
  89. }
  90. if (! isset($value['end'])) {
  91. $this->withQuery($model, 'where', ['>=', $value['start']]);
  92. return;
  93. }
  94. $this->withQuery($model, 'whereBetween', [array_values($value)]);
  95. }
  96. protected function addScript()
  97. {
  98. if (! $this->dateFormat) {
  99. return;
  100. }
  101. $options = [
  102. 'locale' => config('app.locale'),
  103. 'allowInputToggle' => true,
  104. 'format' => $this->dateFormat,
  105. 'extraFormats' => ['DD-MM-YYYY', 'DD/MM/YYYY', 'DD.MM.YYYY', 'DD,MM,YYYY'],
  106. ];
  107. $options = json_encode($options);
  108. Admin::script(<<<JS
  109. $('.{$this->class['start']}').datetimepicker($options);
  110. $('.{$this->class['end']}').datetimepicker($options);
  111. JS
  112. );
  113. // $('.{$this->class['start']}').on("dp.change", function (e) {
  114. // $('.{$this->class['end']}').data("DateTimePicker").minDate(e.date);
  115. // });
  116. // $('.{$this->class['end']}').on("dp.change", function (e) {
  117. // $('.{$this->class['start']}').data("DateTimePicker").maxDate(e.date);
  118. // });
  119. }
  120. /**
  121. * Render this filter.
  122. *
  123. * @return string
  124. */
  125. public function render()
  126. {
  127. if (! $this->shouldDisplay()) {
  128. return;
  129. }
  130. $script = <<<'JS'
  131. $('.dropdown-menu input').on('click', function(e) {
  132. e.stopPropagation();
  133. });
  134. JS;
  135. Admin::script($script);
  136. $this->addScript();
  137. $value = $this->value(['start' => '', 'end' => '']);
  138. $active = empty(array_filter($value)) ? '' : 'active';
  139. return <<<EOT
  140. &nbsp;<span class="dropdown">
  141. <form action="{$this->formAction()}" pjax-container style="display: inline-block;">
  142. <a href="javascript:void(0);" class="{$active}" data-toggle="dropdown">
  143. <i class="feather icon-filter"></i>
  144. </a>
  145. <ul class="dropdown-menu" role="menu" style="min-width: 180px;padding: 10px;left: -70px;border-radius: 0;font-weight:normal;background:#fff">
  146. <li class="dropdown-item">
  147. <input type="text"
  148. class="form-control input-sm {$this->class['start']}"
  149. name="{$this->getQueryName()}[start]"
  150. placeholder="{$this->trans('between_start')}"
  151. value="{$value['start']}"
  152. autocomplete="off" />
  153. </li>
  154. <li style="margin: 5px;"></li>
  155. <li class="dropdown-item">
  156. <input type="text"
  157. class="form-control input-sm {$this->class['end']}"
  158. name="{$this->getQueryName()}[end]"
  159. placeholder="{$this->trans('between_end')}"
  160. value="{$value['end']}"
  161. autocomplete="off"/>
  162. </li>
  163. {$this->renderFormButtons()}
  164. </ul>
  165. </form>
  166. </span>
  167. EOT;
  168. }
  169. protected function requireAssets()
  170. {
  171. Admin::requireAssets(['moment', 'bootstrap-datetimepicker']);
  172. }
  173. }