AbstractFilter.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. namespace Dcat\Admin\Grid\Filter;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Exception\RuntimeException;
  5. use Dcat\Admin\Grid\Filter;
  6. use Dcat\Admin\Grid\Filter\Presenter\Checkbox;
  7. use Dcat\Admin\Grid\Filter\Presenter\DateTime;
  8. use Dcat\Admin\Grid\Filter\Presenter\MultipleSelect;
  9. use Dcat\Admin\Grid\Filter\Presenter\Presenter;
  10. use Dcat\Admin\Grid\Filter\Presenter\Radio;
  11. use Dcat\Admin\Grid\Filter\Presenter\Select;
  12. use Dcat\Admin\Grid\Filter\Presenter\Text;
  13. use Dcat\Admin\Grid\LazyRenderable;
  14. use Dcat\Admin\Traits\HasVariables;
  15. use Dcat\Laravel\Database\WhereHasInServiceProvider;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Support\Collection;
  18. /**
  19. * Class AbstractFilter.
  20. *
  21. * @method Text url()
  22. * @method Text email()
  23. * @method Text integer()
  24. * @method Text decimal($options = [])
  25. * @method Text currency($options = [])
  26. * @method Text percentage($options = [])
  27. * @method Text ip()
  28. * @method Text mac()
  29. * @method Text mobile($mask = '19999999999')
  30. * @method Text inputmask($options = [], $icon = '')
  31. * @method Text placeholder($placeholder = '')
  32. */
  33. abstract class AbstractFilter
  34. {
  35. use HasVariables;
  36. /**
  37. * Element id.
  38. *
  39. * @var array|string
  40. */
  41. protected $id;
  42. /**
  43. * Label of presenter.
  44. *
  45. * @var string
  46. */
  47. protected $label;
  48. /**
  49. * @var array|string
  50. */
  51. protected $value;
  52. /**
  53. * @var array|string
  54. */
  55. protected $defaultValue;
  56. /**
  57. * @var string
  58. */
  59. protected $column;
  60. /**
  61. * Presenter object.
  62. *
  63. * @var Presenter
  64. */
  65. protected $presenter;
  66. /**
  67. * Query for filter.
  68. *
  69. * @var string
  70. */
  71. protected $query = 'where';
  72. /**
  73. * @var Filter
  74. */
  75. protected $parent;
  76. /**
  77. * @var int
  78. */
  79. protected $width = 10;
  80. /**
  81. * @var string
  82. */
  83. protected $style;
  84. /**
  85. * @var string
  86. */
  87. protected $view = 'admin::filter.where';
  88. /**
  89. * @var Collection
  90. */
  91. public $group;
  92. /**
  93. * @var bool
  94. */
  95. protected $ignore = false;
  96. /**
  97. * AbstractFilter constructor.
  98. *
  99. * @param $column
  100. * @param string $label
  101. */
  102. public function __construct($column, $label = '')
  103. {
  104. $this->column = $column;
  105. $this->label = $this->formatLabel($label);
  106. }
  107. /**
  108. * Setup default presenter.
  109. *
  110. * @return void
  111. */
  112. protected function setupDefaultPresenter()
  113. {
  114. $this->setPresenter(new Text($this->label));
  115. }
  116. /**
  117. * Format label.
  118. *
  119. * @param string $label
  120. *
  121. * @return string
  122. */
  123. protected function formatLabel($label)
  124. {
  125. if ($label) {
  126. return $label;
  127. }
  128. $label = admin_trans_field($this->column);
  129. return str_replace('_', ' ', $label);
  130. }
  131. /**
  132. * Set the column width.
  133. *
  134. * @param int|string $width
  135. *
  136. * @return $this
  137. */
  138. public function width($width)
  139. {
  140. if (is_numeric($width)) {
  141. $this->width = $width;
  142. } else {
  143. $this->style = "width:$width;padding-left:10px;padding-right:10px";
  144. $this->width = ' ';
  145. }
  146. return $this;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getElementName()
  152. {
  153. return $this->parent->grid()->makeName($this->originalColumn());
  154. }
  155. /**
  156. * Format name.
  157. *
  158. * @param string $column
  159. *
  160. * @return string
  161. */
  162. protected function formatName($column)
  163. {
  164. $columns = explode('.', $column);
  165. if (count($columns) == 1) {
  166. $name = $columns[0];
  167. } else {
  168. $name = array_shift($columns);
  169. foreach ($columns as $column) {
  170. $name .= "[$column]";
  171. }
  172. }
  173. return $this->parent->grid()->makeName($name);
  174. }
  175. /**
  176. * Format id.
  177. *
  178. * @param string|array $columns
  179. *
  180. * @return array|string
  181. */
  182. protected function formatId($columns)
  183. {
  184. if (is_array($columns)) {
  185. foreach ($columns as &$column) {
  186. $column = $this->formatId($column);
  187. }
  188. return $columns;
  189. }
  190. return $this->parent->grid()->makeName('filter-column-'.str_replace('.', '-', $columns));
  191. }
  192. /**
  193. * @param Filter $filter
  194. */
  195. public function setParent(Filter $filter)
  196. {
  197. $this->parent = $filter;
  198. $this->id = $this->formatId($this->column);
  199. }
  200. /**
  201. * @return Filter
  202. */
  203. public function parent()
  204. {
  205. return $this->parent;
  206. }
  207. /**
  208. * Get siblings of current filter.
  209. *
  210. * @param null $index
  211. *
  212. * @return AbstractFilter[]|mixed
  213. */
  214. public function siblings($index = null)
  215. {
  216. if (! is_null($index)) {
  217. return Arr::get($this->parent->filters(), $index);
  218. }
  219. return $this->parent->filters();
  220. }
  221. /**
  222. * Get previous filter.
  223. *
  224. * @param int $step
  225. *
  226. * @return AbstractFilter[]|mixed
  227. */
  228. public function previous($step = 1)
  229. {
  230. return $this->siblings(
  231. array_search($this, $this->parent->filters()) - $step
  232. );
  233. }
  234. /**
  235. * Get next filter.
  236. *
  237. * @param int $step
  238. *
  239. * @return AbstractFilter[]|mixed
  240. */
  241. public function next($step = 1)
  242. {
  243. return $this->siblings(
  244. array_search($this, $this->parent->filters()) + $step
  245. );
  246. }
  247. /**
  248. * Get query condition from filter.
  249. *
  250. * @param array $inputs
  251. *
  252. * @return array|mixed|null
  253. */
  254. public function condition($inputs)
  255. {
  256. $value = Arr::get($inputs, $this->column);
  257. if ($value === null) {
  258. return;
  259. }
  260. $this->value = $value;
  261. return $this->buildCondition($this->column, $this->value);
  262. }
  263. /**
  264. * Ignore this query filter.
  265. *
  266. * @return $this
  267. */
  268. public function ignore()
  269. {
  270. $this->ignore = true;
  271. return $this;
  272. }
  273. /**
  274. * Select filter.
  275. *
  276. * @param array $options
  277. *
  278. * @return Select
  279. */
  280. public function select($options = [])
  281. {
  282. return $this->setPresenter(new Select($options));
  283. }
  284. /**
  285. * @param array $options
  286. *
  287. * @return MultipleSelect
  288. */
  289. public function multipleSelect($options = [])
  290. {
  291. return $this->setPresenter(new MultipleSelect($options));
  292. }
  293. /**
  294. * @param LazyRenderable $table
  295. *
  296. * @return Filter\Presenter\SelectTable
  297. */
  298. public function selectTable(LazyRenderable $table)
  299. {
  300. return $this->setPresenter(new Filter\Presenter\SelectTable($table));
  301. }
  302. /**
  303. * @param LazyRenderable $table
  304. *
  305. * @return Filter\Presenter\MultipleSelectTable
  306. */
  307. public function multipleSelectTable(LazyRenderable $table)
  308. {
  309. return $this->setPresenter(new Filter\Presenter\MultipleSelectTable($table));
  310. }
  311. /**
  312. * @param array $options
  313. *
  314. * @return Radio
  315. */
  316. public function radio($options = [])
  317. {
  318. return $this->setPresenter(new Radio($options));
  319. }
  320. /**
  321. * @param array $options
  322. *
  323. * @return Checkbox
  324. */
  325. public function checkbox($options = [])
  326. {
  327. return $this->setPresenter(new Checkbox($options));
  328. }
  329. /**
  330. * Datetime filter.
  331. *
  332. * @param array $options
  333. *
  334. * @return DateTime
  335. */
  336. public function datetime($options = [])
  337. {
  338. return $this->setPresenter(new DateTime($options));
  339. }
  340. /**
  341. * Date filter.
  342. *
  343. * @return DateTime
  344. */
  345. public function date()
  346. {
  347. return $this->datetime(['format' => 'YYYY-MM-DD']);
  348. }
  349. /**
  350. * Time filter.
  351. *
  352. * @return DateTime
  353. */
  354. public function time()
  355. {
  356. return $this->datetime(['format' => 'HH:mm:ss']);
  357. }
  358. /**
  359. * Day filter.
  360. *
  361. * @return DateTime
  362. */
  363. public function day()
  364. {
  365. return $this->datetime(['format' => 'DD']);
  366. }
  367. /**
  368. * Month filter.
  369. *
  370. * @return DateTime
  371. */
  372. public function month()
  373. {
  374. return $this->datetime(['format' => 'YYYY-MM']);
  375. }
  376. /**
  377. * Year filter.
  378. *
  379. * @return DateTime
  380. */
  381. public function year()
  382. {
  383. return $this->datetime(['format' => 'YYYY']);
  384. }
  385. /**
  386. * Set presenter object of filter.
  387. *
  388. * @param Presenter $presenter
  389. *
  390. * @return mixed
  391. */
  392. public function setPresenter(Presenter $presenter)
  393. {
  394. $presenter->setParent($this);
  395. $presenter::requireAssets();
  396. return $this->presenter = $presenter;
  397. }
  398. /**
  399. * Get presenter object of filter.
  400. *
  401. * @return Presenter
  402. */
  403. protected function presenter()
  404. {
  405. if (! $this->presenter) {
  406. $this->setupDefaultPresenter();
  407. }
  408. return $this->presenter;
  409. }
  410. /**
  411. * Set default value for filter.
  412. *
  413. * @param null $default
  414. *
  415. * @return $this
  416. */
  417. public function default($default = null)
  418. {
  419. if ($default) {
  420. $this->defaultValue = $default;
  421. }
  422. return $this;
  423. }
  424. public function getDefault()
  425. {
  426. return $this->defaultValue;
  427. }
  428. /**
  429. * Get element id.
  430. *
  431. * @return array|string
  432. */
  433. public function getId()
  434. {
  435. return $this->id;
  436. }
  437. /**
  438. * Set element id.
  439. *
  440. * @param string $id
  441. *
  442. * @return $this
  443. */
  444. public function setId($id)
  445. {
  446. $this->id = $this->formatId($id);
  447. return $this;
  448. }
  449. /**
  450. * Get column name of current filter.
  451. *
  452. * @return string
  453. */
  454. public function column()
  455. {
  456. return $this->formatColumnClass($this->column);
  457. }
  458. public function originalColumn()
  459. {
  460. return $this->column;
  461. }
  462. /**
  463. * @param string $column
  464. *
  465. * @return string
  466. */
  467. public function formatColumnClass($column)
  468. {
  469. return $this->parent->grid()->makeName(str_replace('.', '-', $column));
  470. }
  471. /**
  472. * @return string
  473. */
  474. public function getLabel()
  475. {
  476. return $this->label;
  477. }
  478. /**
  479. * Get value of current filter.
  480. *
  481. * @return array|string
  482. */
  483. public function getValue()
  484. {
  485. return $this->value;
  486. }
  487. /**
  488. * @param mixed $value
  489. *
  490. * @return $this
  491. */
  492. public function setValue($value)
  493. {
  494. $this->value = $value;
  495. return $this;
  496. }
  497. /**
  498. * Build conditions of filter.
  499. *
  500. * @return mixed
  501. */
  502. protected function buildCondition(...$params)
  503. {
  504. if ($this->ignore) {
  505. return;
  506. }
  507. $column = explode('.', $this->column);
  508. if (count($column) == 1) {
  509. return [$this->query => &$params];
  510. }
  511. return $this->buildRelationQuery(...$params);
  512. }
  513. /**
  514. * @param string|callable $relColumn
  515. * @param mixed ...$params
  516. *
  517. * @return array
  518. */
  519. protected function buildRelationQuery($relColumn, ...$params)
  520. {
  521. $column = explode('.', $this->column);
  522. $col = array_pop($column);
  523. $relColumn = is_callable($relColumn) ? $relColumn : $col;
  524. // 增加对whereHasIn的支持
  525. $method = class_exists(WhereHasInServiceProvider::class) ? 'whereHasIn' : 'whereHas';
  526. return [$method => [implode('.', $column), function ($q) use ($relColumn, $params) {
  527. $relColumn = is_string($relColumn) ? $q->getModel()->getTable().'.'.$relColumn : $relColumn;
  528. call_user_func_array([$q, $this->query], [$relColumn, $params]);
  529. }]];
  530. }
  531. /**
  532. * Variables for filter view.
  533. *
  534. * @return array
  535. */
  536. protected function defaultVariables()
  537. {
  538. return array_merge([
  539. 'id' => $this->id,
  540. 'name' => $this->formatName($this->column),
  541. 'label' => $this->label,
  542. 'value' => $this->normalizeValue(),
  543. 'width' => $this->width,
  544. 'style' => $this->style,
  545. ], $this->presenter()->variables());
  546. }
  547. protected function normalizeValue()
  548. {
  549. if ($this->value === '' || $this->value === null) {
  550. $this->value = Arr::get($this->parent->inputs(), $this->column);
  551. }
  552. return $this->value === '' || $this->value === null ? $this->defaultValue : $this->value;
  553. }
  554. /**
  555. * Render this filter.
  556. *
  557. * @return string
  558. */
  559. public function render()
  560. {
  561. $variables = $this->variables();
  562. $variables['presenter'] = $this->renderPresenter();
  563. return Admin::view($this->view, $variables);
  564. }
  565. /**
  566. * @return string
  567. *
  568. * @throws \Throwable
  569. */
  570. protected function renderPresenter()
  571. {
  572. return function () {
  573. return Admin::view($this->presenter->view(), $this->variables());
  574. };
  575. }
  576. /**
  577. * Render this filter.
  578. *
  579. * @return \Illuminate\View\View|string
  580. */
  581. public function __toString()
  582. {
  583. return $this->render();
  584. }
  585. /**
  586. * @param $method
  587. * @param $params
  588. *
  589. * @throws \Exception
  590. *
  591. * @return mixed
  592. */
  593. public function __call($method, $params)
  594. {
  595. if (method_exists($this->presenter(), $method)) {
  596. return $this->presenter()->{$method}(...$params);
  597. }
  598. throw new RuntimeException(sprintf(
  599. 'Call to undefined method %s::%s()', static::class, $method
  600. ));
  601. }
  602. }