123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace Dcat\Admin\Grid\Column;
- use Dcat\Admin\Grid\Column;
- use Dcat\Admin\Grid\Model;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Contracts\Support\Renderable;
- abstract class Filter implements Renderable
- {
- /**
- * @var string|array
- */
- protected $class;
- /**
- * @var Column
- */
- protected $parent;
- /**
- * @var string
- */
- protected $columnName;
- /**
- * @var \Closure[]
- */
- protected $resolvings = [];
- /**
- * @var bool
- */
- protected $display = true;
- /**
- * @param Column $column
- */
- public function setParent(Column $column)
- {
- $this->parent = $column;
- $this->parent->grid()->fetching(function () {
- $this->addResetButton();
- $this->parent->grid()->model()->treeUrlWithoutQuery(
- $this->getQueryName()
- );
- });
- foreach ($this->resolvings as $closure) {
- $closure($this);
- }
- }
- /**
- * @return Column
- */
- public function parent()
- {
- return $this->parent;
- }
- /**
- * @param \Closure $callback
- *
- * @return $this
- */
- public function resolving(\Closure $callback)
- {
- $this->resolvings[] = $callback;
- return $this;
- }
- /**
- * @param string $name
- *
- * @return $this
- */
- public function setColumnName(string $name)
- {
- $this->columnName = $name;
- return $this;
- }
- /**
- * Get column name.
- *
- * @return string
- */
- public function getColumnName()
- {
- return str_replace(['.', '->'], '_', $this->getOriginalColumnName());
- }
- /**
- * @return mixed
- */
- public function getOriginalColumnName()
- {
- return $this->columnName ?: $this->parent->getName();
- }
- /**
- * @return string
- */
- public function getQueryName()
- {
- return $this->parent->grid()->getName().
- '_filter_'.
- $this->getColumnName();
- }
- /**
- * Get filter value of this column.
- *
- * @param string $default
- *
- * @return array|\Illuminate\Http\Request|string
- */
- public function value($default = '')
- {
- return request($this->getQueryName(), $default);
- }
- /**
- * @param mixed $model
- * @param string $query
- * @param mixed array $params
- *
- * @return void
- */
- protected function withQuery($model, string $query, array $params)
- {
- Helper::withQueryCondition($model, $this->getOriginalColumnName(), $query, $params);
- }
- /**
- * Add reset button.
- */
- protected function addResetButton()
- {
- $value = $this->value();
- if ($value === '' || $value === null) {
- return;
- }
- $style = $this->shouldDisplay() ? 'style=\'margin:3px 14px\'' : '';
- return $this->parent->addHeader(
- " <a class='feather icon-rotate-ccw' href='{$this->urlWithoutFilter()}' {$style}></a>"
- );
- }
- /**
- * @return string
- */
- protected function renderFormButtons()
- {
- return <<<HMLT
- <li class="dropdown-divider"></li>
- <li>
- <button class="btn btn-sm btn-primary column-filter-submit "><i class="feather icon-search"></i></button>
- <a href="{$this->urlWithoutFilter()}" class="btn btn-sm btn-default"><i class="feather icon-rotate-ccw"></i></a>
- </li>
- HMLT;
- }
- /**
- * Get form action url.
- *
- * @return string
- */
- public function formAction()
- {
- return Helper::fullUrlWithoutQuery([
- $this->getQueryName(),
- $this->getColumnName(),
- $this->parent->grid()->model()->getPageName(),
- '_pjax',
- ]);
- }
- /**
- * @return string
- */
- protected function urlWithoutFilter()
- {
- return Helper::fullUrlWithoutQuery([
- $this->getQueryName(),
- ]);
- }
- /**
- * @param string $key
- *
- * @return array|null|string
- */
- protected function trans($key)
- {
- return __("admin.{$key}");
- }
- /**
- * @param bool $value
- *
- * @return $this
- */
- public function display(bool $value)
- {
- $this->display = $value;
- return $this;
- }
- /**
- * @return $this
- */
- public function hide()
- {
- return $this->display(false);
- }
- /**
- * @return bool
- */
- public function shouldDisplay()
- {
- return $this->display;
- }
- /**
- * Add a query binding.
- *
- * @param mixed $value
- * @param Model $model
- */
- public function addBinding($value, Model $model)
- {
- //
- }
- /**
- * {@inheritdoc}
- */
- public function render()
- {
- //
- }
- /**
- * @param array ...$params
- *
- * @return static
- */
- public static function make(...$params)
- {
- return new static(...$params);
- }
- }
|