123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace Dcat\Admin\Grid\Filter;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Grid\Filter\Presenter\DateTime;
- use Illuminate\Support\Arr;
- class Between extends AbstractFilter
- {
- /**
- * {@inheritdoc}
- */
- protected $view = 'admin::filter.between';
- /**
- * @var int
- */
- protected $width = 12;
- /**
- * @var bool
- */
- protected $timestamp = false;
- /**
- * Convert the datetime into unix timestamp.
- *
- * @return $this
- */
- public function toTimestamp()
- {
- $this->timestamp = true;
- return $this;
- }
- /**
- * Format id.
- *
- * @param string $column
- *
- * @return array|string
- */
- public function formatId($column)
- {
- $id = str_replace('.', '_', $column);
- $prefix = 'filter_column_'.$this->parent->grid()->getName().'_';
- return ['start' => "{$prefix}{$id}_start", 'end' => "{$prefix}{$id}_end"];
- }
- /**
- * Format two field names of this filter.
- *
- * @param string $column
- *
- * @return array
- */
- protected function formatName($column)
- {
- $gridName = $this->parent->grid()->getName();
- $prefix = $gridName ? $gridName.'_' : '';
- $columns = explode('.', $column);
- if (count($columns) == 1) {
- $name = $prefix.$columns[0];
- } else {
- $name = $prefix.array_shift($columns);
- foreach ($columns as $column) {
- $name .= "[$column]";
- }
- }
- return ['start' => "{$name}[start]", 'end' => "{$name}[end]"];
- }
- /**
- * Get condition of this filter.
- *
- * @param array $inputs
- *
- * @return mixed
- */
- public function condition($inputs)
- {
- if (! Arr::has($inputs, $this->column)) {
- return;
- }
- $this->value = Arr::get($inputs, $this->column);
- $value = array_filter($this->value, function ($val) {
- return $val !== '';
- });
- if ($this->timestamp) {
- $value = array_map(function ($v) {
- if ($v) {
- return strtotime($v);
- }
- }, $value);
- }
- if (empty($value)) {
- return;
- }
- if (! isset($value['start']) && isset($value['end'])) {
- return $this->buildCondition($this->column, '<=', $value['end']);
- }
- if (! isset($value['end']) && isset($value['start'])) {
- return $this->buildCondition($this->column, '>=', $value['start']);
- }
- $this->query = 'whereBetween';
- return $this->buildCondition($this->column, [$value['start'], $value['end']]);
- }
- /**
- * @param array $options
- *
- * @return $this
- */
- public function datetime($options = [])
- {
- $this->view = 'admin::filter.between-datetime';
- DateTime::collectAssets();
- $this->setupDatetime($options);
- return $this;
- }
- /**
- * @param array $options
- */
- protected function setupDatetime($options = [])
- {
- $options['format'] = Arr::get($options, 'format', 'YYYY-MM-DD HH:mm:ss');
- $options['locale'] = Arr::get($options, 'locale', config('app.locale'));
- $startOptions = json_encode($options);
- $endOptions = json_encode($options + ['useCurrent' => false]);
- $script = <<<JS
- $('#{$this->id['start']}').datetimepicker($startOptions);
- $('#{$this->id['end']}').datetimepicker($endOptions);
- $("#{$this->id['start']}").on("dp.change", function (e) {
- $('#{$this->id['end']}').data("DateTimePicker").minDate(e.date);
- });
- $("#{$this->id['end']}").on("dp.change", function (e) {
- $('#{$this->id['start']}').data("DateTimePicker").maxDate(e.date);
- });
- JS;
- Admin::script($script);
- }
- }
|