Between.php 3.8 KB

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