AbstractFilter.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. $label = $label ?: admin_trans_field($this->column);
  126. return str_replace(['.', '_'], ' ', $label);
  127. }
  128. /**
  129. * Set the column width.
  130. *
  131. * @param int|string $width
  132. *
  133. * @return $this
  134. */
  135. public function width($width)
  136. {
  137. if (is_numeric($width)) {
  138. $this->width = $width;
  139. } else {
  140. $this->style = "width:$width;padding-left:10px;padding-right:10px";
  141. $this->width = ' ';
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Format name.
  147. *
  148. * @param string $column
  149. *
  150. * @return string
  151. */
  152. protected function formatName($column)
  153. {
  154. $columns = explode('.', $column);
  155. if (count($columns) == 1) {
  156. $name = $columns[0];
  157. } else {
  158. $name = array_shift($columns);
  159. foreach ($columns as $column) {
  160. $name .= "[$column]";
  161. }
  162. }
  163. return $this->parent->grid()->makeName($name);
  164. }
  165. /**
  166. * Format id.
  167. *
  168. * @param $columns
  169. *
  170. * @return array|string
  171. */
  172. protected function formatId($columns)
  173. {
  174. return $this->parent->grid()->makeName('filter-column-'.str_replace('.', '-', $columns));
  175. }
  176. /**
  177. * @param Filter $filter
  178. */
  179. public function setParent(Filter $filter)
  180. {
  181. $this->parent = $filter;
  182. $this->id = $this->formatId($this->column);
  183. }
  184. /**
  185. * @return Filter
  186. */
  187. public function parent()
  188. {
  189. return $this->parent;
  190. }
  191. /**
  192. * Get siblings of current filter.
  193. *
  194. * @param null $index
  195. *
  196. * @return AbstractFilter[]|mixed
  197. */
  198. public function siblings($index = null)
  199. {
  200. if (! is_null($index)) {
  201. return Arr::get($this->parent->filters(), $index);
  202. }
  203. return $this->parent->filters();
  204. }
  205. /**
  206. * Get previous filter.
  207. *
  208. * @param int $step
  209. *
  210. * @return AbstractFilter[]|mixed
  211. */
  212. public function previous($step = 1)
  213. {
  214. return $this->siblings(
  215. array_search($this, $this->parent->filters()) - $step
  216. );
  217. }
  218. /**
  219. * Get next filter.
  220. *
  221. * @param int $step
  222. *
  223. * @return AbstractFilter[]|mixed
  224. */
  225. public function next($step = 1)
  226. {
  227. return $this->siblings(
  228. array_search($this, $this->parent->filters()) + $step
  229. );
  230. }
  231. /**
  232. * Get query condition from filter.
  233. *
  234. * @param array $inputs
  235. *
  236. * @return array|mixed|null
  237. */
  238. public function condition($inputs)
  239. {
  240. $value = Arr::get($inputs, $this->column);
  241. if ($value === null) {
  242. return;
  243. }
  244. $this->value = $value;
  245. return $this->buildCondition($this->column, $this->value);
  246. }
  247. /**
  248. * Ignore this query filter.
  249. *
  250. * @return $this
  251. */
  252. public function ignore()
  253. {
  254. $this->ignore = true;
  255. return $this;
  256. }
  257. /**
  258. * Select filter.
  259. *
  260. * @param array $options
  261. *
  262. * @return Select
  263. */
  264. public function select($options = [])
  265. {
  266. return $this->setPresenter(new Select($options));
  267. }
  268. /**
  269. * @param array $options
  270. *
  271. * @return MultipleSelect
  272. */
  273. public function multipleSelect($options = [])
  274. {
  275. return $this->setPresenter(new MultipleSelect($options));
  276. }
  277. /**
  278. * @param LazyRenderable $table
  279. *
  280. * @return Filter\Presenter\SelectTable
  281. */
  282. public function selectTable(LazyRenderable $table)
  283. {
  284. return $this->setPresenter(new Filter\Presenter\SelectTable($table));
  285. }
  286. /**
  287. * @param LazyRenderable $table
  288. *
  289. * @return Filter\Presenter\MultipleSelectTable
  290. */
  291. public function multipleSelectTable(LazyRenderable $table)
  292. {
  293. return $this->setPresenter(new Filter\Presenter\MultipleSelectTable($table));
  294. }
  295. /**
  296. * @param array $options
  297. *
  298. * @return Radio
  299. */
  300. public function radio($options = [])
  301. {
  302. return $this->setPresenter(new Radio($options));
  303. }
  304. /**
  305. * @param array $options
  306. *
  307. * @return Checkbox
  308. */
  309. public function checkbox($options = [])
  310. {
  311. return $this->setPresenter(new Checkbox($options));
  312. }
  313. /**
  314. * Datetime filter.
  315. *
  316. * @param array $options
  317. *
  318. * @return DateTime
  319. */
  320. public function datetime($options = [])
  321. {
  322. return $this->setPresenter(new DateTime($options));
  323. }
  324. /**
  325. * Date filter.
  326. *
  327. * @return DateTime
  328. */
  329. public function date()
  330. {
  331. return $this->datetime(['format' => 'YYYY-MM-DD']);
  332. }
  333. /**
  334. * Time filter.
  335. *
  336. * @return DateTime
  337. */
  338. public function time()
  339. {
  340. return $this->datetime(['format' => 'HH:mm:ss']);
  341. }
  342. /**
  343. * Day filter.
  344. *
  345. * @return DateTime
  346. */
  347. public function day()
  348. {
  349. return $this->datetime(['format' => 'DD']);
  350. }
  351. /**
  352. * Month filter.
  353. *
  354. * @return DateTime
  355. */
  356. public function month()
  357. {
  358. return $this->datetime(['format' => 'YYYY-MM']);
  359. }
  360. /**
  361. * Year filter.
  362. *
  363. * @return DateTime
  364. */
  365. public function year()
  366. {
  367. return $this->datetime(['format' => 'YYYY']);
  368. }
  369. /**
  370. * Set presenter object of filter.
  371. *
  372. * @param Presenter $presenter
  373. *
  374. * @return mixed
  375. */
  376. public function setPresenter(Presenter $presenter)
  377. {
  378. $presenter->setParent($this);
  379. $presenter::requireAssets();
  380. return $this->presenter = $presenter;
  381. }
  382. /**
  383. * Get presenter object of filter.
  384. *
  385. * @return Presenter
  386. */
  387. protected function presenter()
  388. {
  389. if (! $this->presenter) {
  390. $this->setupDefaultPresenter();
  391. }
  392. return $this->presenter;
  393. }
  394. /**
  395. * Set default value for filter.
  396. *
  397. * @param null $default
  398. *
  399. * @return $this
  400. */
  401. public function default($default = null)
  402. {
  403. if ($default) {
  404. $this->defaultValue = $default;
  405. }
  406. return $this;
  407. }
  408. public function getDefault()
  409. {
  410. return $this->defaultValue;
  411. }
  412. /**
  413. * Get element id.
  414. *
  415. * @return array|string
  416. */
  417. public function getId()
  418. {
  419. return $this->id;
  420. }
  421. /**
  422. * Set element id.
  423. *
  424. * @param string $id
  425. *
  426. * @return $this
  427. */
  428. public function setId($id)
  429. {
  430. $this->id = $this->formatId($id);
  431. return $this;
  432. }
  433. /**
  434. * Get column name of current filter.
  435. *
  436. * @return string
  437. */
  438. public function column()
  439. {
  440. return $this->formatColumnClass($this->column);
  441. }
  442. /**
  443. * @param string $column
  444. *
  445. * @return string
  446. */
  447. public function formatColumnClass($column)
  448. {
  449. return $this->parent->grid()->makeName(str_replace('.', '-', $column));
  450. }
  451. /**
  452. * @return string
  453. */
  454. public function getLabel()
  455. {
  456. return $this->label;
  457. }
  458. /**
  459. * Get value of current filter.
  460. *
  461. * @return array|string
  462. */
  463. public function getValue()
  464. {
  465. return $this->value;
  466. }
  467. /**
  468. * @param mixed $value
  469. *
  470. * @return $this
  471. */
  472. public function setValue($value)
  473. {
  474. $this->value = $value;
  475. return $this;
  476. }
  477. /**
  478. * Build conditions of filter.
  479. *
  480. * @return mixed
  481. */
  482. protected function buildCondition(...$params)
  483. {
  484. if ($this->ignore) {
  485. return;
  486. }
  487. $column = explode('.', $this->column);
  488. if (count($column) == 1) {
  489. return [$this->query => &$params];
  490. }
  491. return $this->buildRelationQuery(...$params);
  492. }
  493. /**
  494. * @param mixed ...$params
  495. *
  496. * @return array
  497. */
  498. protected function buildRelationQuery(...$params)
  499. {
  500. $column = explode('.', $this->column);
  501. $params[0] = array_pop($column);
  502. // 增加对whereHasIn的支持
  503. $method = class_exists(WhereHasInServiceProvider::class) ? 'whereHasIn' : 'whereHas';
  504. return [$method => [implode('.', $column), function ($q) use ($params) {
  505. call_user_func_array([$q, $this->query], $params);
  506. }]];
  507. }
  508. /**
  509. * Variables for filter view.
  510. *
  511. * @return array
  512. */
  513. protected function defaultVariables()
  514. {
  515. return array_merge([
  516. 'id' => $this->id,
  517. 'name' => $this->formatName($this->column),
  518. 'label' => $this->label,
  519. 'value' => $this->normalizeValue(),
  520. 'width' => $this->width,
  521. 'style' => $this->style,
  522. ], $this->presenter()->variables());
  523. }
  524. protected function normalizeValue()
  525. {
  526. if ($this->value === '' || $this->value === null) {
  527. $this->value = Arr::get($this->parent->inputs(), $this->column);
  528. }
  529. return $this->value === '' || $this->value === null ? $this->defaultValue : $this->value;
  530. }
  531. /**
  532. * Render this filter.
  533. *
  534. * @return string
  535. */
  536. public function render()
  537. {
  538. $variables = $this->variables();
  539. $variables['presenter'] = $this->renderPresenter();
  540. return Admin::view($this->view, $variables);
  541. }
  542. /**
  543. * @return string
  544. *
  545. * @throws \Throwable
  546. */
  547. protected function renderPresenter()
  548. {
  549. return function () {
  550. return Admin::view($this->presenter->view(), $this->variables());
  551. };
  552. }
  553. /**
  554. * Render this filter.
  555. *
  556. * @return \Illuminate\View\View|string
  557. */
  558. public function __toString()
  559. {
  560. return $this->render();
  561. }
  562. /**
  563. * @param $method
  564. * @param $params
  565. *
  566. * @throws \Exception
  567. *
  568. * @return mixed
  569. */
  570. public function __call($method, $params)
  571. {
  572. if (method_exists($this->presenter(), $method)) {
  573. return $this->presenter()->{$method}(...$params);
  574. }
  575. throw new RuntimeException(sprintf(
  576. 'Call to undefined method %s::%s()', static::class, $method
  577. ));
  578. }
  579. }