Filter.php 16 KB

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