Between.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Dcat\Admin\Grid\Filter;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\Filter\Presenter\DateTime;
  5. use Illuminate\Support\Arr;
  6. class Between extends AbstractFilter
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. protected $view = 'admin::filter.between';
  12. /**
  13. * @var int
  14. */
  15. protected $width = 12;
  16. /**
  17. * @var bool
  18. */
  19. protected $timestamp = false;
  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. * Format id.
  32. *
  33. * @param string $column
  34. *
  35. * @return array|string
  36. */
  37. public function formatId($column)
  38. {
  39. $id = str_replace('.', '_', $column);
  40. $prefix = 'filter_column_'.$this->parent->grid()->getName().'_';
  41. return ['start' => "{$prefix}{$id}_start", 'end' => "{$prefix}{$id}_end"];
  42. }
  43. /**
  44. * Format two field names of this filter.
  45. *
  46. * @param string $column
  47. *
  48. * @return array
  49. */
  50. protected function formatName($column)
  51. {
  52. $gridName = $this->parent->grid()->getName();
  53. $prefix = $gridName ? $gridName.'_' : '';
  54. $columns = explode('.', $column);
  55. if (count($columns) == 1) {
  56. $name = $prefix.$columns[0];
  57. } else {
  58. $name = $prefix.array_shift($columns);
  59. foreach ($columns as $column) {
  60. $name .= "[$column]";
  61. }
  62. }
  63. return ['start' => "{$name}[start]", 'end' => "{$name}[end]"];
  64. }
  65. /**
  66. * Get condition of this filter.
  67. *
  68. * @param array $inputs
  69. *
  70. * @return mixed
  71. */
  72. public function condition($inputs)
  73. {
  74. if (! Arr::has($inputs, $this->column)) {
  75. return;
  76. }
  77. $this->value = Arr::get($inputs, $this->column);
  78. $value = array_filter($this->value, function ($val) {
  79. return $val !== '';
  80. });
  81. if ($this->timestamp) {
  82. $value = array_map(function ($v) {
  83. if ($v) {
  84. return strtotime($v);
  85. }
  86. }, $value);
  87. }
  88. if (empty($value)) {
  89. return;
  90. }
  91. if (! isset($value['start']) && isset($value['end'])) {
  92. return $this->buildCondition($this->column, '<=', $value['end']);
  93. }
  94. if (! isset($value['end']) && isset($value['start'])) {
  95. return $this->buildCondition($this->column, '>=', $value['start']);
  96. }
  97. $this->query = 'whereBetween';
  98. return $this->buildCondition($this->column, [$value['start'], $value['end']]);
  99. }
  100. /**
  101. * @param array $options
  102. *
  103. * @return $this
  104. */
  105. public function datetime($options = [])
  106. {
  107. $this->view = 'admin::filter.between-datetime';
  108. DateTime::collectAssets();
  109. $this->setupDatetime($options);
  110. return $this;
  111. }
  112. /**
  113. * @param array $options
  114. */
  115. protected function setupDatetime($options = [])
  116. {
  117. $options['format'] = Arr::get($options, 'format', 'YYYY-MM-DD HH:mm:ss');
  118. $options['locale'] = Arr::get($options, 'locale', config('app.locale'));
  119. $startOptions = json_encode($options);
  120. $endOptions = json_encode($options + ['useCurrent' => false]);
  121. $script = <<<JS
  122. $('#{$this->id['start']}').datetimepicker($startOptions);
  123. $('#{$this->id['end']}').datetimepicker($endOptions);
  124. $("#{$this->id['start']}").on("dp.change", function (e) {
  125. $('#{$this->id['end']}').data("DateTimePicker").minDate(e.date);
  126. });
  127. $("#{$this->id['end']}").on("dp.change", function (e) {
  128. $('#{$this->id['start']}').data("DateTimePicker").maxDate(e.date);
  129. });
  130. JS;
  131. Admin::script($script);
  132. }
  133. }