Grid.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <?php
  2. namespace Dcat\Admin;
  3. use Closure;
  4. use Dcat\Admin\Contracts\Repository;
  5. use Dcat\Admin\Grid\Column;
  6. use Dcat\Admin\Grid\Concerns;
  7. use Dcat\Admin\Grid\Model;
  8. use Dcat\Admin\Grid\Row;
  9. use Dcat\Admin\Grid\Tools;
  10. use Dcat\Admin\Support\Helper;
  11. use Dcat\Admin\Traits\HasBuilderEvents;
  12. use Dcat\Admin\Traits\HasVariables;
  13. use Illuminate\Contracts\Support\Renderable;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Support\Collection;
  16. use Illuminate\Support\Traits\Macroable;
  17. class Grid
  18. {
  19. use HasBuilderEvents;
  20. use HasVariables;
  21. use Concerns\HasEvents;
  22. use Concerns\HasNames;
  23. use Concerns\HasFilter;
  24. use Concerns\HasTools;
  25. use Concerns\HasActions;
  26. use Concerns\HasPaginator;
  27. use Concerns\HasExporter;
  28. use Concerns\HasComplexHeaders;
  29. use Concerns\HasSelector;
  30. use Concerns\HasQuickCreate;
  31. use Concerns\HasQuickSearch;
  32. use Concerns\CanFixColumns;
  33. use Concerns\CanHidesColumns;
  34. use Macroable {
  35. __call as macroCall;
  36. }
  37. const CREATE_MODE_DEFAULT = 'default';
  38. const CREATE_MODE_DIALOG = 'dialog';
  39. const IFRAME_QUERY_NAME = '_grid_iframe_';
  40. /**
  41. * The grid data model instance.
  42. *
  43. * @var \Dcat\Admin\Grid\Model
  44. */
  45. protected $model;
  46. /**
  47. * Collection of grid columns.
  48. *
  49. * @var \Illuminate\Support\Collection
  50. */
  51. protected $columns;
  52. /**
  53. * Collection of all grid columns.
  54. *
  55. * @var \Illuminate\Support\Collection
  56. */
  57. protected $allColumns;
  58. /**
  59. * Collection of all data rows.
  60. *
  61. * @var \Illuminate\Support\Collection
  62. */
  63. protected $rows;
  64. /**
  65. * All column names of the grid.
  66. *
  67. * @var array
  68. */
  69. protected $columnNames = [];
  70. /**
  71. * Grid builder.
  72. *
  73. * @var \Closure
  74. */
  75. protected $builder;
  76. /**
  77. * Mark if the grid is built.
  78. *
  79. * @var bool
  80. */
  81. protected $built = false;
  82. /**
  83. * Resource path of the grid.
  84. *
  85. * @var
  86. */
  87. protected $resourcePath;
  88. /**
  89. * Default primary key name.
  90. *
  91. * @var string
  92. */
  93. protected $keyName = 'id';
  94. /**
  95. * View for grid to render.
  96. *
  97. * @var string
  98. */
  99. protected $view = 'admin::grid.table';
  100. /**
  101. * @var Closure
  102. */
  103. protected $header;
  104. /**
  105. * @var Closure
  106. */
  107. protected $footer;
  108. /**
  109. * @var Closure
  110. */
  111. protected $wrapper;
  112. /**
  113. * @var bool
  114. */
  115. protected $addNumberColumn = false;
  116. /**
  117. * @var string
  118. */
  119. protected $tableId = 'grid-table';
  120. /**
  121. * @var Grid\Tools\RowSelector
  122. */
  123. protected $rowSelector;
  124. /**
  125. * Options for grid.
  126. *
  127. * @var array
  128. */
  129. protected $options = [
  130. 'show_pagination' => true,
  131. 'show_filter' => true,
  132. 'show_actions' => true,
  133. 'show_quick_edit_button' => false,
  134. 'show_edit_button' => true,
  135. 'show_view_button' => true,
  136. 'show_delete_button' => true,
  137. 'show_row_selector' => true,
  138. 'show_create_button' => true,
  139. 'show_bordered' => false,
  140. 'table_collapse' => true,
  141. 'show_toolbar' => true,
  142. 'create_mode' => self::CREATE_MODE_DEFAULT,
  143. 'dialog_form_area' => ['700px', '670px'],
  144. 'table_class' => ['table', 'custom-data-table', 'data-table'],
  145. ];
  146. /**
  147. * @var \Illuminate\Http\Request
  148. */
  149. protected $request;
  150. /**
  151. * Create a new grid instance.
  152. *
  153. * Grid constructor.
  154. *
  155. * @param Repository|\Illuminate\Database\Eloquent\Model|Builder|null $repository
  156. * @param null|\Closure $builder
  157. */
  158. public function __construct($repository = null, ?\Closure $builder = null, $request = null)
  159. {
  160. $this->model = new Model(request(), $repository);
  161. $this->columns = new Collection();
  162. $this->allColumns = new Collection();
  163. $this->rows = new Collection();
  164. $this->builder = $builder;
  165. $this->request = $request ?: request();
  166. $this->resourcePath = url($this->request->getPathInfo());
  167. if ($repository = $this->model->repository()) {
  168. $this->setKeyName($repository->getKeyName());
  169. }
  170. $this->model->setGrid($this);
  171. $this->setUpTools();
  172. $this->setUpFilter();
  173. $this->callResolving();
  174. }
  175. /**
  176. * Get table ID.
  177. *
  178. * @return string
  179. */
  180. public function getTableId()
  181. {
  182. return $this->tableId;
  183. }
  184. /**
  185. * Set primary key name.
  186. *
  187. * @param string $name
  188. *
  189. * @return $this
  190. */
  191. public function setKeyName(string $name)
  192. {
  193. $this->keyName = $name;
  194. return $this;
  195. }
  196. /**
  197. * Get or set primary key name.
  198. *
  199. * @return string|void
  200. */
  201. public function getKeyName()
  202. {
  203. return $this->keyName ?: 'id';
  204. }
  205. /**
  206. * Add column to Grid.
  207. *
  208. * @param string $name
  209. * @param string $label
  210. *
  211. * @return Column
  212. */
  213. public function column($name, $label = '')
  214. {
  215. return $this->addColumn($name, $label);
  216. }
  217. /**
  218. * Add number column.
  219. *
  220. * @param null|string $label
  221. *
  222. * @return Column
  223. */
  224. public function number(?string $label = null)
  225. {
  226. return $this->addColumn('#', $label ?: '#');
  227. }
  228. /**
  229. * Batch add column to grid.
  230. *
  231. * @example
  232. * 1.$grid->columns(['name' => 'Name', 'email' => 'Email' ...]);
  233. * 2.$grid->columns('name', 'email' ...)
  234. *
  235. * @param array $columns
  236. *
  237. * @return Collection|Column[]|void
  238. */
  239. public function columns($columns = null)
  240. {
  241. if ($columns === null) {
  242. return $this->columns;
  243. }
  244. if (func_num_args() == 1 && is_array($columns)) {
  245. foreach ($columns as $column => $label) {
  246. $this->column($column, $label);
  247. }
  248. return;
  249. }
  250. foreach (func_get_args() as $column) {
  251. $this->column($column);
  252. }
  253. }
  254. /**
  255. * @return Collection|Column[]
  256. */
  257. public function allColumns()
  258. {
  259. return $this->allColumns;
  260. }
  261. /**
  262. * Add column to grid.
  263. *
  264. * @param string $field
  265. * @param string $label
  266. *
  267. * @return Column
  268. */
  269. protected function addColumn($field = '', $label = '')
  270. {
  271. $column = $this->newColumn($field, $label);
  272. $this->columns->put($field, $column);
  273. $this->allColumns->put($field, $column);
  274. return $column;
  275. }
  276. /**
  277. * @param string $field
  278. * @param string $label
  279. *
  280. * @return Column
  281. */
  282. public function prependColumn($field = '', $label = '')
  283. {
  284. $column = $this->newColumn($field, $label);
  285. $this->columns->prepend($column, $field);
  286. $this->allColumns->prepend($column, $field);
  287. return $column;
  288. }
  289. /**
  290. * @param string $field
  291. * @param string $label
  292. *
  293. * @return Column
  294. */
  295. public function newColumn($field = '', $label = '')
  296. {
  297. $column = new Column($field, $label);
  298. $column->setGrid($this);
  299. return $column;
  300. }
  301. /**
  302. * Get Grid model.
  303. *
  304. * @return Model
  305. */
  306. public function model()
  307. {
  308. return $this->model;
  309. }
  310. /**
  311. * @return array
  312. */
  313. public function getColumnNames()
  314. {
  315. return $this->columnNames;
  316. }
  317. /**
  318. * Apply column filter to grid query.
  319. */
  320. protected function applyColumnFilter()
  321. {
  322. $this->columns->each->bindFilterQuery($this->model());
  323. }
  324. /**
  325. * @param string|array $class
  326. *
  327. * @return $this
  328. */
  329. public function addTableClass($class)
  330. {
  331. $this->options['table_class'] = array_merge((array) $this->options['table_class'], (array) $class);
  332. return $this;
  333. }
  334. public function formatTableClass()
  335. {
  336. if ($this->options['show_bordered']) {
  337. $this->addTableClass(['table-bordered', 'complex-headers', 'data-table']);
  338. }
  339. return implode(' ', array_unique((array) $this->options['table_class']));
  340. }
  341. /**
  342. * Build the grid.
  343. *
  344. * @return void
  345. */
  346. public function build()
  347. {
  348. if ($this->built) {
  349. return;
  350. }
  351. $collection = $this->processFilter(false);
  352. $data = $collection->toArray();
  353. $this->prependRowSelectorColumn();
  354. $this->appendActionsColumn();
  355. Column::setOriginalGridModels($collection);
  356. $this->columns->map(function (Column $column) use (&$data) {
  357. $column->fill($data);
  358. $this->columnNames[] = $column->getName();
  359. });
  360. $this->buildRows($data);
  361. $this->sortHeaders();
  362. }
  363. /**
  364. * @return void
  365. */
  366. public function callBuilder()
  367. {
  368. if ($this->builder && ! $this->built) {
  369. call_user_func($this->builder, $this);
  370. }
  371. $this->built = true;
  372. }
  373. /**
  374. * Build the grid rows.
  375. *
  376. * @param array $data
  377. *
  378. * @return void
  379. */
  380. protected function buildRows(array $data)
  381. {
  382. $this->rows = collect($data)->map(function ($model) {
  383. return new Row($this, $model);
  384. });
  385. }
  386. /**
  387. * Set grid row callback function.
  388. *
  389. * @return Collection
  390. */
  391. public function rows()
  392. {
  393. return $this->rows;
  394. }
  395. /**
  396. * Get create url.
  397. *
  398. * @return string
  399. */
  400. public function getCreateUrl()
  401. {
  402. $queryString = '';
  403. if ($constraints = $this->model()->getConstraints()) {
  404. $queryString = http_build_query($constraints);
  405. }
  406. return sprintf(
  407. '%s/create%s',
  408. $this->resource(),
  409. $queryString ? ('?'.$queryString) : ''
  410. );
  411. }
  412. /**
  413. * @param \Closure $closure
  414. *
  415. * @return Grid\Tools\RowSelector
  416. */
  417. public function rowSelector()
  418. {
  419. return $this->rowSelector ?: ($this->rowSelector = new Grid\Tools\RowSelector($this));
  420. }
  421. /**
  422. * Prepend checkbox column for grid.
  423. *
  424. * @return void
  425. */
  426. protected function prependRowSelectorColumn()
  427. {
  428. if (! $this->options['show_row_selector']) {
  429. return;
  430. }
  431. $rowSelector = $this->rowSelector();
  432. $keyName = $this->getKeyName();
  433. $this->prependColumn(
  434. Grid\Column::SELECT_COLUMN_NAME,
  435. $rowSelector->renderHeader()
  436. )->display(function () use ($rowSelector, $keyName) {
  437. return $rowSelector->renderColumn($this, $this->{$keyName});
  438. });
  439. }
  440. /**
  441. * @param string $width
  442. * @param string $height
  443. *
  444. * @return $this
  445. */
  446. public function setDialogFormDimensions(string $width, string $height)
  447. {
  448. $this->options['dialog_form_area'] = [$width, $height];
  449. return $this;
  450. }
  451. /**
  452. * Render create button for grid.
  453. *
  454. * @return string
  455. */
  456. public function renderCreateButton()
  457. {
  458. if (! $this->options['show_create_button']) {
  459. return '';
  460. }
  461. return (new Tools\CreateButton($this))->render();
  462. }
  463. /**
  464. * @param bool $value
  465. *
  466. * @return $this
  467. */
  468. public function withBorder(bool $value = true)
  469. {
  470. $this->options['show_bordered'] = $value;
  471. return $this;
  472. }
  473. /**
  474. * @param bool $value
  475. *
  476. * @return $this
  477. */
  478. public function tableCollapse(bool $value = true)
  479. {
  480. $this->options['table_collapse'] = $value;
  481. return $this;
  482. }
  483. /**
  484. * Set grid header.
  485. *
  486. * @param Closure|string|Renderable $content
  487. *
  488. * @return $this|Closure
  489. */
  490. public function header($content = null)
  491. {
  492. if (! $content) {
  493. return $this->header;
  494. }
  495. $this->header = $content;
  496. return $this;
  497. }
  498. /**
  499. * Render grid header.
  500. *
  501. * @return string
  502. */
  503. public function renderHeader()
  504. {
  505. if (! $this->header) {
  506. return '';
  507. }
  508. $content = Helper::render($this->header, [$this->processFilter(false)]);
  509. if (empty($content)) {
  510. return '';
  511. }
  512. return <<<HTML
  513. <div class="card-header clearfix" style="border-bottom: 0;background: transparent;padding: 0">{$content}</div>
  514. HTML;
  515. }
  516. /**
  517. * Set grid footer.
  518. *
  519. * @param Closure|string|Renderable $content
  520. *
  521. * @return $this|Closure
  522. */
  523. public function footer($content = null)
  524. {
  525. if (! $content) {
  526. return $this->footer;
  527. }
  528. $this->footer = $content;
  529. return $this;
  530. }
  531. /**
  532. * Render grid footer.
  533. *
  534. * @return string
  535. */
  536. public function renderFooter()
  537. {
  538. if (! $this->footer) {
  539. return '';
  540. }
  541. $content = Helper::render($this->footer, [$this->processFilter(false)]);
  542. if (empty($content)) {
  543. return '';
  544. }
  545. return <<<HTML
  546. <div class="box-footer clearfix">{$content}</div>
  547. HTML;
  548. }
  549. /**
  550. * Get or set option for grid.
  551. *
  552. * @param string|array $key
  553. * @param mixed $value
  554. *
  555. * @return $this|mixed
  556. */
  557. public function option($key, $value = null)
  558. {
  559. if (is_null($value)) {
  560. return $this->options[$key] ?? null;
  561. }
  562. if (is_array($key)) {
  563. $this->options = array_merge($this->options, $key);
  564. } else {
  565. $this->options[$key] = $value;
  566. }
  567. return $this;
  568. }
  569. protected function setUpOptions()
  570. {
  571. if ($this->options['show_bordered']) {
  572. $this->tableCollapse(false);
  573. }
  574. }
  575. /**
  576. * Disable row selector.
  577. *
  578. * @return $this
  579. */
  580. public function disableRowSelector(bool $disable = true)
  581. {
  582. $this->tools->disableBatchActions($disable);
  583. return $this->option('show_row_selector', ! $disable);
  584. }
  585. /**
  586. * Show row selector.
  587. *
  588. * @return $this
  589. */
  590. public function showRowSelector(bool $val = true)
  591. {
  592. return $this->disableRowSelector(! $val);
  593. }
  594. /**
  595. * Remove create button on grid.
  596. *
  597. * @return $this
  598. */
  599. public function disableCreateButton(bool $disable = true)
  600. {
  601. return $this->option('show_create_button', ! $disable);
  602. }
  603. /**
  604. * Show create button.
  605. *
  606. * @return $this
  607. */
  608. public function showCreateButton(bool $val = true)
  609. {
  610. return $this->disableCreateButton(! $val);
  611. }
  612. /**
  613. * If allow creation.
  614. *
  615. * @return bool
  616. */
  617. public function allowCreateButton()
  618. {
  619. return $this->options['show_create_button'];
  620. }
  621. /**
  622. * @param string $mode
  623. *
  624. * @return $this
  625. */
  626. public function createMode(string $mode)
  627. {
  628. return $this->option('create_mode', $mode);
  629. }
  630. /**
  631. * @return $this
  632. */
  633. public function enableDialogCreate()
  634. {
  635. return $this->createMode(self::CREATE_MODE_DIALOG);
  636. }
  637. /**
  638. * Get or set resource path.
  639. *
  640. * @return string
  641. */
  642. public function resource()
  643. {
  644. return $this->resourcePath;
  645. }
  646. /**
  647. * Create a grid instance.
  648. *
  649. * @param mixed ...$params
  650. *
  651. * @return $this
  652. */
  653. public static function make(...$params)
  654. {
  655. return new static(...$params);
  656. }
  657. /**
  658. * @param Closure $closure
  659. *
  660. * @return $this;
  661. */
  662. public function wrap(\Closure $closure)
  663. {
  664. $this->wrapper = $closure;
  665. return $this;
  666. }
  667. /**
  668. * @return bool
  669. */
  670. public function hasWrapper()
  671. {
  672. return $this->wrapper ? true : false;
  673. }
  674. /**
  675. * Add variables to grid view.
  676. *
  677. * @param array $variables
  678. *
  679. * @return $this
  680. */
  681. public function with(array $variables)
  682. {
  683. $this->variables = $variables;
  684. return $this;
  685. }
  686. /**
  687. * Get all variables will used in grid view.
  688. *
  689. * @return array
  690. */
  691. protected function defaultVariables()
  692. {
  693. return [
  694. 'grid' => $this,
  695. 'tableId' => $this->getTableId(),
  696. ];
  697. }
  698. /**
  699. * Set a view to render.
  700. *
  701. * @param string $view
  702. *
  703. * @return $this
  704. */
  705. public function view($view)
  706. {
  707. $this->view = $view;
  708. return $this;
  709. }
  710. /**
  711. * Set grid title.
  712. *
  713. * @param string $title
  714. *
  715. * @return $this
  716. */
  717. public function title($title)
  718. {
  719. $this->variables['title'] = $title;
  720. return $this;
  721. }
  722. /**
  723. * Set grid description.
  724. *
  725. * @param string $description
  726. *
  727. * @return $this
  728. */
  729. public function description($description)
  730. {
  731. $this->variables['description'] = $description;
  732. return $this;
  733. }
  734. /**
  735. * Set resource path for grid.
  736. *
  737. * @param string $path
  738. *
  739. * @return $this
  740. */
  741. public function setResource($path)
  742. {
  743. $this->resourcePath = admin_url($path);
  744. return $this;
  745. }
  746. /**
  747. * Get the string contents of the grid view.
  748. *
  749. * @return string
  750. */
  751. public function render()
  752. {
  753. $this->callComposing();
  754. $this->build();
  755. $this->applyFixColumns();
  756. $this->setUpOptions();
  757. return $this->doWrap();
  758. }
  759. /**
  760. * @return string
  761. */
  762. protected function doWrap()
  763. {
  764. $view = view($this->view, $this->variables());
  765. if (! $wrapper = $this->wrapper) {
  766. return $view->render();
  767. }
  768. return $wrapper($view);
  769. }
  770. /**
  771. * Add column to grid.
  772. *
  773. * @param string $name
  774. *
  775. * @return Column
  776. */
  777. public function __get($name)
  778. {
  779. return $this->addColumn($name);
  780. }
  781. /**
  782. * Dynamically add columns to the grid view.
  783. *
  784. * @param $method
  785. * @param $arguments
  786. *
  787. * @return Column
  788. */
  789. public function __call($method, $arguments)
  790. {
  791. if (static::hasMacro($method)) {
  792. return $this->macroCall($method, $arguments);
  793. }
  794. return $this->addColumn($method, $arguments[0] ?? null);
  795. }
  796. }