Filter.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\Filter\AbstractFilter;
  5. use Dcat\Admin\Grid\Filter\Between;
  6. use Dcat\Admin\Grid\Filter\Date;
  7. use Dcat\Admin\Grid\Filter\Day;
  8. use Dcat\Admin\Grid\Filter\EndWith;
  9. use Dcat\Admin\Grid\Filter\Equal;
  10. use Dcat\Admin\Grid\Filter\Group;
  11. use Dcat\Admin\Grid\Filter\Gt;
  12. use Dcat\Admin\Grid\Filter\Hidden;
  13. use Dcat\Admin\Grid\Filter\Ilike;
  14. use Dcat\Admin\Grid\Filter\In;
  15. use Dcat\Admin\Grid\Filter\Layout\Layout;
  16. use Dcat\Admin\Grid\Filter\Like;
  17. use Dcat\Admin\Grid\Filter\Lt;
  18. use Dcat\Admin\Grid\Filter\Month;
  19. use Dcat\Admin\Grid\Filter\Newline;
  20. use Dcat\Admin\Grid\Filter\Ngt;
  21. use Dcat\Admin\Grid\Filter\Nlt;
  22. use Dcat\Admin\Grid\Filter\NotEqual;
  23. use Dcat\Admin\Grid\Filter\NotIn;
  24. use Dcat\Admin\Grid\Filter\Scope;
  25. use Dcat\Admin\Grid\Filter\StartWith;
  26. use Dcat\Admin\Grid\Filter\Where;
  27. use Dcat\Admin\Grid\Filter\Year;
  28. use Dcat\Admin\Traits\HasBuilderEvents;
  29. use Illuminate\Contracts\Support\Arrayable;
  30. use Illuminate\Contracts\Support\Renderable;
  31. use Illuminate\Support\Arr;
  32. use Illuminate\Support\Collection;
  33. use Illuminate\Support\Str;
  34. /**
  35. * Class Filter.
  36. *
  37. * @method Equal equal($column, $label = '')
  38. * @method NotEqual notEqual($column, $label = '')
  39. * @method Like like($column, $label = '')
  40. * @method Ilike ilike($column, $label = '')
  41. * @method StartWith startWith($column, $label = '')
  42. * @method EndWith endWith($column, $label = '')
  43. * @method Gt gt($column, $label = '')
  44. * @method Lt lt($column, $label = '')
  45. * @method Ngt ngt($column, $label = '')
  46. * @method Nlt nlt($column, $label = '')
  47. * @method Between between($column, $label = '')
  48. * @method In in($column, $label = '')
  49. * @method NotIn notIn($column, $label = '')
  50. * @method Where where($colum, $callback, $label = '')
  51. * @method Date date($column, $label = '')
  52. * @method Day day($column, $label = '')
  53. * @method Month month($column, $label = '')
  54. * @method Year year($column, $label = '')
  55. * @method Hidden hidden($name, $value)
  56. * @method Group group($column, $builder = null, $label = '')
  57. * @method Newline newline()
  58. */
  59. class Filter implements Renderable
  60. {
  61. use HasBuilderEvents;
  62. const MODE_PANEL = 'panel';
  63. const MODE_RIGHT_SIDE = 'right-side';
  64. /**
  65. * @var array
  66. */
  67. protected static $supports = [];
  68. /**
  69. * @var array
  70. */
  71. protected static $defaultFilters = [
  72. 'equal' => Equal::class,
  73. 'notEqual' => NotEqual::class,
  74. 'ilike' => Ilike::class,
  75. 'like' => Like::class,
  76. 'startWith' => StartWith::class,
  77. 'endWith' => EndWith::class,
  78. 'gt' => Gt::class,
  79. 'lt' => Lt::class,
  80. 'ngt' => Ngt::class,
  81. 'nlt' => Nlt::class,
  82. 'between' => Between::class,
  83. 'group' => Group::class,
  84. 'where' => Where::class,
  85. 'in' => In::class,
  86. 'notIn' => NotIn::class,
  87. 'date' => Date::class,
  88. 'day' => Day::class,
  89. 'month' => Month::class,
  90. 'year' => Year::class,
  91. 'hidden' => Hidden::class,
  92. 'newline' => Newline::class,
  93. ];
  94. /**
  95. * @var Model
  96. */
  97. protected $model;
  98. /**
  99. * @var AbstractFilter[]
  100. */
  101. protected $filters = [];
  102. /**
  103. * Action of search form.
  104. *
  105. * @var string
  106. */
  107. protected $action;
  108. /**
  109. * @var string
  110. */
  111. protected $view;
  112. /**
  113. * @var string
  114. */
  115. protected $filterID;
  116. /**
  117. * @var string
  118. */
  119. protected $name = '';
  120. /**
  121. * @var bool
  122. */
  123. public $expand = false;
  124. /**
  125. * @var Collection
  126. */
  127. protected $scopes;
  128. /**
  129. * @var Layout
  130. */
  131. protected $layout;
  132. /**
  133. * Primary key of giving model.
  134. *
  135. * @var mixed
  136. */
  137. protected $primaryKey;
  138. /**
  139. * @var string
  140. */
  141. protected $style = 'padding:18px 15px 8px';
  142. /**
  143. * @var bool
  144. */
  145. protected $disableResetButton = false;
  146. /**
  147. * @var string
  148. */
  149. protected $border = 'border-top:1px solid #f4f4f4;';
  150. /**
  151. * @var string
  152. */
  153. protected $containerClass = '';
  154. /**
  155. * @var bool
  156. */
  157. protected $disableCollapse = false;
  158. /**
  159. * @var array
  160. */
  161. protected $inputs;
  162. /**
  163. * @var string
  164. */
  165. protected $mode = self::MODE_RIGHT_SIDE;
  166. /**
  167. * Create a new filter instance.
  168. *
  169. * @param Model $model
  170. */
  171. public function __construct(Model $model)
  172. {
  173. $this->model = $model;
  174. $this->primaryKey = $model->getKeyName();
  175. $this->filterID = $this->formatFilterId();
  176. $this->initLayout();
  177. $this->scopes = new Collection();
  178. $this->callResolving();
  179. }
  180. /**
  181. * Initialize filter layout.
  182. */
  183. protected function initLayout()
  184. {
  185. $this->layout = new Filter\Layout\Layout($this);
  186. }
  187. /**
  188. * @return string
  189. */
  190. protected function formatFilterId()
  191. {
  192. $gridName = $this->model->grid()->getName();
  193. return 'filter-box'.($gridName ? '-'.$gridName : '');
  194. }
  195. /**
  196. * Set action of search form.
  197. *
  198. * @param string $action
  199. *
  200. * @return $this
  201. */
  202. public function setAction($action)
  203. {
  204. $this->action = $action;
  205. return $this;
  206. }
  207. /**
  208. * @return $this
  209. */
  210. public function withoutInputBorder()
  211. {
  212. $this->containerClass = 'input-no-border';
  213. return $this;
  214. }
  215. /**
  216. * @param bool $disabled
  217. *
  218. * @return $this
  219. */
  220. public function disableCollapse(bool $disabled = true)
  221. {
  222. $this->disableCollapse = $disabled;
  223. return $this;
  224. }
  225. /**
  226. * @param bool $disabled
  227. *
  228. * @return $this
  229. */
  230. public function disableResetButton(bool $disabled = true)
  231. {
  232. $this->disableResetButton = $disabled;
  233. return $this;
  234. }
  235. /**
  236. * Get input data.
  237. *
  238. * @param string $key
  239. * @param null $value
  240. *
  241. * @return array|mixed
  242. */
  243. public function input($key = null, $default = null)
  244. {
  245. $inputs = $this->inputs();
  246. if ($key === null) {
  247. return $inputs;
  248. }
  249. return Arr::get($inputs, $key, $default);
  250. }
  251. /**
  252. * Get grid model.
  253. *
  254. * @return Model
  255. */
  256. public function model()
  257. {
  258. return $this->model;
  259. }
  260. /**
  261. * Get grid.
  262. *
  263. * @return \Dcat\Admin\Grid
  264. */
  265. public function grid()
  266. {
  267. return $this->model->grid();
  268. }
  269. /**
  270. * Set ID of search form.
  271. *
  272. * @param string $filterID
  273. *
  274. * @return $this
  275. */
  276. public function setFilterID($filterID)
  277. {
  278. $this->filterID = $filterID;
  279. return $this;
  280. }
  281. /**
  282. * @param string|null $mode
  283. *
  284. * @return $this|string
  285. */
  286. public function mode(string $mode = null)
  287. {
  288. if ($mode === null) {
  289. return $this->mode;
  290. }
  291. $this->mode = $mode;
  292. return $this;
  293. }
  294. /**
  295. * @return $this
  296. */
  297. public function panel()
  298. {
  299. return $this->mode(self::MODE_PANEL);
  300. }
  301. /**
  302. * @return $this
  303. */
  304. public function rightSide()
  305. {
  306. return $this->mode(self::MODE_RIGHT_SIDE);
  307. }
  308. /**
  309. * Get filter ID.
  310. *
  311. * @return string
  312. */
  313. public function filterID()
  314. {
  315. return $this->filterID;
  316. }
  317. /**
  318. * @param string $name
  319. *
  320. * @return $this
  321. */
  322. public function setName($name)
  323. {
  324. $this->name = $name;
  325. $this->setFilterID("{$this->name}-{$this->filterID}");
  326. return $this;
  327. }
  328. /**
  329. * @return string
  330. */
  331. public function getName()
  332. {
  333. return $this->name;
  334. }
  335. public function withoutBorder()
  336. {
  337. return $this->withBorder('');
  338. }
  339. public function withBorder($border = null)
  340. {
  341. $this->border = is_null($border) ? 'border-top:1px solid #f4f4f4;' : $border;
  342. return $this;
  343. }
  344. /**
  345. * Remove filter by column.
  346. *
  347. * @param string|array $column
  348. */
  349. public function removeFilter($column)
  350. {
  351. $this->filters = array_filter($this->filters, function (AbstractFilter $filter) use (&$column) {
  352. if (is_array($column)) {
  353. return ! in_array($filter->column(), $column);
  354. }
  355. return $filter->column() != $column;
  356. });
  357. }
  358. /**
  359. * @return array
  360. */
  361. public function inputs()
  362. {
  363. if (! is_null($this->inputs)) {
  364. return $this->inputs;
  365. }
  366. $this->inputs = Arr::dot(request()->all());
  367. $this->inputs = array_filter($this->inputs, function ($input) {
  368. return $input !== '' && ! is_null($input);
  369. });
  370. $this->sanitizeInputs($this->inputs);
  371. return $this->inputs;
  372. }
  373. /**
  374. * Get all conditions of the filters.
  375. *
  376. * @return array
  377. */
  378. public function conditions()
  379. {
  380. $inputs = $this->inputs();
  381. if (empty($inputs)) {
  382. return [];
  383. }
  384. $params = [];
  385. foreach ($inputs as $key => $value) {
  386. Arr::set($params, $key, $value);
  387. }
  388. $conditions = [];
  389. foreach ($this->filters() as $filter) {
  390. $conditions[] = $filter->condition($params);
  391. }
  392. return tap(array_filter($conditions), function ($conditions) {
  393. if (! empty($conditions)) {
  394. $this->expand();
  395. }
  396. });
  397. }
  398. /**
  399. * @param array $inputs
  400. *
  401. * @return void
  402. */
  403. protected function sanitizeInputs(&$inputs)
  404. {
  405. if (! $this->name) {
  406. return $inputs;
  407. }
  408. $inputs = collect($inputs)->filter(function ($input, $key) {
  409. return Str::startsWith($key, "{$this->name}_");
  410. })->mapWithKeys(function ($val, $key) {
  411. $key = str_replace("{$this->name}_", '', $key);
  412. return [$key => $val];
  413. })->toArray();
  414. }
  415. /**
  416. * Add a filter to grid.
  417. *
  418. * @param AbstractFilter $filter
  419. *
  420. * @return AbstractFilter
  421. */
  422. protected function addFilter(AbstractFilter $filter)
  423. {
  424. $this->layout->addFilter($filter);
  425. $filter->setParent($this);
  426. return $this->filters[] = $filter;
  427. }
  428. /**
  429. * Use a custom filter.
  430. *
  431. * @param AbstractFilter $filter
  432. *
  433. * @return AbstractFilter
  434. */
  435. public function use(AbstractFilter $filter)
  436. {
  437. return $this->addFilter($filter);
  438. }
  439. /**
  440. * Get all filters.
  441. *
  442. * @return AbstractFilter[]
  443. */
  444. public function filters()
  445. {
  446. return $this->filters;
  447. }
  448. /**
  449. * @param string $key
  450. * @param string $label
  451. *
  452. * @return Scope
  453. */
  454. public function scope($key, $label = '')
  455. {
  456. $scope = new Scope($key, $label);
  457. $this->scopes->push($scope);
  458. return $scope;
  459. }
  460. /**
  461. * Get all filter scopes.
  462. *
  463. * @return Collection
  464. */
  465. public function scopes()
  466. {
  467. return $this->scopes;
  468. }
  469. /**
  470. * Get current scope.
  471. *
  472. * @return Scope|null
  473. */
  474. public function currentScope()
  475. {
  476. $key = request(Scope::QUERY_NAME);
  477. return $this->scopes->first(function ($scope) use ($key) {
  478. return $scope->key == $key;
  479. });
  480. }
  481. /**
  482. * Get the name of current scope.
  483. *
  484. * @return string
  485. */
  486. public function currentScopeName()
  487. {
  488. return request(Scope::QUERY_NAME);
  489. }
  490. /**
  491. * Get scope conditions.
  492. *
  493. * @return array
  494. */
  495. protected function scopeConditions()
  496. {
  497. if ($scope = $this->currentScope()) {
  498. return $scope->condition();
  499. }
  500. return [];
  501. }
  502. /**
  503. * Expand filter container.
  504. *
  505. * @param bool $value
  506. *
  507. * @return $this
  508. */
  509. public function expand(bool $value = true)
  510. {
  511. $this->expand = $value;
  512. return $this;
  513. }
  514. /**
  515. * Execute the filter with conditions.
  516. *
  517. * @param bool $toArray
  518. *
  519. * @return array|Collection|mixed
  520. */
  521. public function execute(bool $toArray = true)
  522. {
  523. $conditions = array_merge(
  524. $this->conditions(),
  525. $this->scopeConditions()
  526. );
  527. return $this->model->addConditions($conditions)->buildData($toArray);
  528. }
  529. /**
  530. * @param string $top
  531. * @param string $right
  532. * @param string $bottom
  533. * @param string $left
  534. *
  535. * @return Filter
  536. */
  537. public function padding($top = '15px', $right = '15px', $bottom = '5px', $left = '')
  538. {
  539. return $this->style("padding:$top $right $bottom $left");
  540. }
  541. /**
  542. * @param string $style
  543. *
  544. * @return $this
  545. */
  546. public function style(?string $style)
  547. {
  548. $this->style = $style;
  549. return $this;
  550. }
  551. public function resetPosition()
  552. {
  553. return $this->style('padding:0;left:-4px;');
  554. }
  555. public function hiddenResetButtonText()
  556. {
  557. Admin::style(".{$this->containerClass} a.reset .hidden-xs{display:none}");
  558. return $this;
  559. }
  560. /**
  561. * Get the string contents of the filter view.
  562. *
  563. * @return \Illuminate\View\View|string
  564. */
  565. public function render()
  566. {
  567. if (empty($this->filters)) {
  568. return '';
  569. }
  570. $this->callComposing();
  571. $this->view = $this->mode === self::MODE_RIGHT_SIDE ? 'admin::filter.right-side-container' : 'admin::filter.container';
  572. return view($this->view)->with([
  573. 'action' => $this->action ?: $this->urlWithoutFilters(),
  574. 'layout' => $this->layout,
  575. 'filterID' => $this->disableCollapse ? '' : $this->filterID,
  576. 'expand' => $this->expand,
  577. 'style' => $this->style,
  578. 'border' => $this->border,
  579. 'containerClass' => $this->containerClass,
  580. 'disableResetButton' => $this->disableResetButton,
  581. ])->render();
  582. }
  583. /**
  584. * Get url without filter queryString.
  585. *
  586. * @return string
  587. */
  588. public function urlWithoutFilters()
  589. {
  590. $filters = collect($this->filters);
  591. /** @var Collection $columns */
  592. $columns = $filters->map->column()->flatten();
  593. $pageKey = 'page';
  594. if ($gridName = $this->model->grid()->getName()) {
  595. $pageKey = "{$gridName}_{$pageKey}";
  596. }
  597. $columns->push($pageKey);
  598. $groupNames = $filters->filter(function ($filter) {
  599. return $filter instanceof Group;
  600. })->map(function (AbstractFilter $filter) {
  601. return "{$filter->getId()}_group";
  602. });
  603. return $this->fullUrlWithoutQuery(
  604. $columns->merge($groupNames)
  605. );
  606. }
  607. /**
  608. * Get url without scope queryString.
  609. *
  610. * @return string
  611. */
  612. public function urlWithoutScopes()
  613. {
  614. return $this->fullUrlWithoutQuery(Scope::QUERY_NAME);
  615. }
  616. /**
  617. * Get full url without query strings.
  618. *
  619. * @param Arrayable|array|string $keys
  620. *
  621. * @return string
  622. */
  623. protected function fullUrlWithoutQuery($keys)
  624. {
  625. if ($keys instanceof Arrayable) {
  626. $keys = $keys->toArray();
  627. }
  628. $keys = (array) $keys;
  629. $request = request();
  630. $query = $request->query();
  631. Arr::forget($query, $keys);
  632. $question = $request->getBaseUrl().$request->getPathInfo() == '/' ? '/?' : '?';
  633. return count($request->query()) > 0
  634. ? $request->url().$question.http_build_query($query)
  635. : $request->fullUrl();
  636. }
  637. /**
  638. * Generate a filter object and add to grid.
  639. *
  640. * @param string $method
  641. * @param array $arguments
  642. *
  643. * @return AbstractFilter|$this
  644. */
  645. public function __call($method, $arguments)
  646. {
  647. if (! empty(static::$supports[$method])) {
  648. $class = static::$supports[$method];
  649. if (! is_subclass_of($class, AbstractFilter::class)) {
  650. throw new \InvalidArgumentException("The class [{$class}] must be a type of ".AbstractFilter::class.'.');
  651. }
  652. return $this->addFilter(new $class(...$arguments));
  653. }
  654. if (isset(static::$defaultFilters[$method])) {
  655. return $this->addFilter(new static::$defaultFilters[$method](...$arguments));
  656. }
  657. return $this;
  658. }
  659. /**
  660. * @param string $name
  661. * @param string $filterClass
  662. */
  663. public static function extend($name, $filterClass)
  664. {
  665. static::$supports[$name] = $filterClass;
  666. }
  667. /**
  668. * @return array
  669. */
  670. public static function extensions()
  671. {
  672. return static::$supports;
  673. }
  674. }