123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- <?php
- namespace Dcat\Admin\Grid\Filter;
- use Dcat\Admin\Grid\Filter;
- use Dcat\Admin\Grid\Filter\Presenter\Checkbox;
- use Dcat\Admin\Grid\Filter\Presenter\DateTime;
- use Dcat\Admin\Grid\Filter\Presenter\MultipleSelect;
- use Dcat\Admin\Grid\Filter\Presenter\Presenter;
- use Dcat\Admin\Grid\Filter\Presenter\Radio;
- use Dcat\Admin\Grid\Filter\Presenter\Select;
- use Dcat\Admin\Grid\Filter\Presenter\Text;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- /**
- * Class AbstractFilter.
- *
- * @method Text url()
- * @method Text email()
- * @method Text integer()
- * @method Text decimal($options = [])
- * @method Text currency($options = [])
- * @method Text percentage($options = [])
- * @method Text ip()
- * @method Text mac()
- * @method Text mobile($mask = '19999999999')
- * @method Text inputmask($options = [], $icon = '')
- * @method Text placeholder($placeholder = '')
- */
- abstract class AbstractFilter
- {
- /**
- * Element id.
- *
- * @var array|string
- */
- protected $id;
- /**
- * Label of presenter.
- *
- * @var string
- */
- protected $label;
- /**
- * @var array|string
- */
- protected $value;
- /**
- * @var array|string
- */
- protected $defaultValue;
- /**
- * @var string
- */
- protected $column;
- /**
- * Presenter object.
- *
- * @var Presenter
- */
- protected $presenter;
- /**
- * Query for filter.
- *
- * @var string
- */
- protected $query = 'where';
- /**
- * @var Filter
- */
- protected $parent;
- /**
- * @var int
- */
- protected $width = 10;
- /**
- * @var string
- */
- protected $style;
- /**
- * @var string
- */
- protected $view = 'admin::filter.where';
- /**
- * @var Collection
- */
- public $group;
- /**
- * AbstractFilter constructor.
- *
- * @param $column
- * @param string $label
- */
- public function __construct($column, $label = '')
- {
- $this->column = $column;
- $this->label = $this->formatLabel($label);
- }
- /**
- * Setup default presenter.
- *
- * @return void
- */
- protected function setupDefaultPresenter()
- {
- $this->setPresenter(new Text($this->label));
- }
- /**
- * Format label.
- *
- * @param string $label
- *
- * @return string
- */
- protected function formatLabel($label)
- {
- $label = $label ?: admin_trans_field($this->column);
- return str_replace(['.', '_'], ' ', $label);
- }
- /**
- * Set the column width.
- *
- * @param int|string $width
- *
- * @return $this
- */
- public function width($width)
- {
- if (is_numeric($width)) {
- $this->width = $width;
- } else {
- $this->style = "width:$width;padding-left:10px;padding-right:10px";
- $this->width = ' ';
- }
- return $this;
- }
- /**
- * Format name.
- *
- * @param string $column
- *
- * @return string
- */
- protected function formatName($column)
- {
- $columns = explode('.', $column);
- if (count($columns) == 1) {
- $name = $columns[0];
- } else {
- $name = array_shift($columns);
- foreach ($columns as $column) {
- $name .= "[$column]";
- }
- }
- $parenName = $this->parent->getName();
- return $parenName ? "{$parenName}_{$name}" : $name;
- }
- /**
- * Format id.
- *
- * @param $columns
- *
- * @return array|string
- */
- protected function formatId($columns)
- {
- return 'filter_column_'.$this->parent->grid()->getName().'_'.str_replace('.', '_', $columns);
- }
- /**
- * @param Filter $filter
- */
- public function setParent(Filter $filter)
- {
- $this->parent = $filter;
- $this->id = $this->formatId($this->column);
- }
- /**
- * Get siblings of current filter.
- *
- * @param null $index
- *
- * @return AbstractFilter[]|mixed
- */
- public function siblings($index = null)
- {
- if (! is_null($index)) {
- return Arr::get($this->parent->filters(), $index);
- }
- return $this->parent->filters();
- }
- /**
- * Get previous filter.
- *
- * @param int $step
- *
- * @return AbstractFilter[]|mixed
- */
- public function previous($step = 1)
- {
- return $this->siblings(
- array_search($this, $this->parent->filters()) - $step
- );
- }
- /**
- * Get next filter.
- *
- * @param int $step
- *
- * @return AbstractFilter[]|mixed
- */
- public function next($step = 1)
- {
- return $this->siblings(
- array_search($this, $this->parent->filters()) + $step
- );
- }
- /**
- * Get query condition from filter.
- *
- * @param array $inputs
- *
- * @return array|mixed|null
- */
- public function condition($inputs)
- {
- $value = Arr::get($inputs, $this->column);
- if ($value === null) {
- return;
- }
- $this->value = $value;
- return $this->buildCondition($this->column, $this->value);
- }
- /**
- * Select filter.
- *
- * @param array $options
- *
- * @return Select
- */
- public function select($options = [])
- {
- return $this->setPresenter(new Select($options));
- }
- /**
- * @param array $options
- *
- * @return MultipleSelect
- */
- public function multipleSelect($options = [])
- {
- return $this->setPresenter(new MultipleSelect($options));
- }
- /**
- * @param mixed $source
- *
- * @return Filter\Presenter\SelectResource
- */
- public function selectResource($source = null)
- {
- return $this->setPresenter(new Filter\Presenter\SelectResource($source));
- }
- /**
- * @param array $options
- *
- * @return Radio
- */
- public function radio($options = [])
- {
- return $this->setPresenter(new Radio($options));
- }
- /**
- * @param array $options
- *
- * @return Checkbox
- */
- public function checkbox($options = [])
- {
- return $this->setPresenter(new Checkbox($options));
- }
- /**
- * Datetime filter.
- *
- * @param array $options
- *
- * @return DateTime
- */
- public function datetime($options = [])
- {
- return $this->setPresenter(new DateTime($options));
- }
- /**
- * Date filter.
- *
- * @return DateTime
- */
- public function date()
- {
- return $this->datetime(['format' => 'YYYY-MM-DD']);
- }
- /**
- * Time filter.
- *
- * @return DateTime
- */
- public function time()
- {
- return $this->datetime(['format' => 'HH:mm:ss']);
- }
- /**
- * Day filter.
- *
- * @return DateTime
- */
- public function day()
- {
- return $this->datetime(['format' => 'DD']);
- }
- /**
- * Month filter.
- *
- * @return DateTime
- */
- public function month()
- {
- return $this->datetime(['format' => 'YYYY-MM']);
- }
- /**
- * Year filter.
- *
- * @return DateTime
- */
- public function year()
- {
- return $this->datetime(['format' => 'YYYY']);
- }
- /**
- * Set presenter object of filter.
- *
- * @param Presenter $presenter
- *
- * @return mixed
- */
- public function setPresenter(Presenter $presenter)
- {
- $presenter->setParent($this);
- $presenter::collectAssets();
- return $this->presenter = $presenter;
- }
- /**
- * Get presenter object of filter.
- *
- * @return Presenter
- */
- protected function presenter()
- {
- if (! $this->presenter) {
- $this->setupDefaultPresenter();
- }
- return $this->presenter;
- }
- /**
- * Set default value for filter.
- *
- * @param null $default
- *
- * @return $this
- */
- public function default($default = null)
- {
- if ($default) {
- $this->defaultValue = $default;
- }
- return $this;
- }
- public function getDefault()
- {
- return $this->defaultValue;
- }
- /**
- * Get element id.
- *
- * @return array|string
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set element id.
- *
- * @param string $id
- *
- * @return $this
- */
- public function setId($id)
- {
- $this->id = $this->formatId($id);
- return $this;
- }
- /**
- * Get column name of current filter.
- *
- * @return string
- */
- public function column()
- {
- $parenName = $this->parent->getName();
- return $parenName ? "{$parenName}_{$this->column}" : $this->column;
- }
- /**
- * Get value of current filter.
- *
- * @return array|string
- */
- public function getValue()
- {
- return $this->value;
- }
- /**
- * @param mixed $value
- *
- * @return $this
- */
- public function setValue($value)
- {
- $this->value = $value;
- return $this;
- }
- /**
- * Build conditions of filter.
- *
- * @return mixed
- */
- protected function buildCondition(...$params)
- {
- $column = explode('.', $this->column);
- if (count($column) == 1) {
- return [$this->query => &$params];
- }
- return $this->buildRelationQuery(...$params);
- }
- /**
- * @param mixed ...$params
- *
- * @return array
- */
- protected function buildRelationQuery(...$params)
- {
- $relation = substr($this->column, 0, strrpos($this->column, '.'));
- $params[0] = Arr::last(explode('.', $this->column));
- return ['whereHas' => [$relation, function ($relation) use ($params) {
- call_user_func_array([$relation, $this->query], $params);
- }]];
- }
- /**
- * Variables for filter view.
- *
- * @return array
- */
- protected function variables()
- {
- $variables = $this->presenter()->variables();
- $value = $this->value ?: Arr::get($this->parent->inputs(), $this->column);
- return array_merge([
- 'id' => $this->id,
- 'name' => $this->formatName($this->column),
- 'label' => $this->label,
- 'value' => $value ?: $this->defaultValue,
- 'presenter' => $this->presenter(),
- 'width' => $this->width,
- 'style' => $this->style,
- ], $variables);
- }
- /**
- * Render this filter.
- *
- * @return \Illuminate\View\View|string
- */
- public function render()
- {
- return view($this->view, $this->variables());
- }
- /**
- * Render this filter.
- *
- * @return \Illuminate\View\View|string
- */
- public function __toString()
- {
- return $this->render();
- }
- /**
- * @param $method
- * @param $params
- *
- * @throws \Exception
- *
- * @return mixed
- */
- public function __call($method, $params)
- {
- if (method_exists($this->presenter(), $method)) {
- return $this->presenter()->{$method}(...$params);
- }
- throw new \BadMethodCallException(sprintf(
- 'Call to undefined method %s::%s()', static::class, $method
- ));
- }
- }
|