Column.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Closure;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Grid\Displayers\AbstractDisplayer;
  6. use Dcat\Admin\Grid\Displayers\Editable;
  7. use Dcat\Admin\Grid\Displayers\Orderable;
  8. use Dcat\Admin\Traits\HasBuilderEvents;
  9. use Dcat\Admin\Traits\HasDefinitions;
  10. use Illuminate\Contracts\Support\Arrayable;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Support\Arr;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Fluent;
  15. use Illuminate\Support\Str;
  16. use Illuminate\Support\Traits\Macroable;
  17. /**
  18. * @method $this editable(bool $refresh = false)
  19. * @method $this switch(string $color = '')
  20. * @method $this switchGroup($columns = [], string $color = '')
  21. * @method $this image($server = '', int $width = 200, int $height = 200)
  22. * @method $this label($style = 'primary', int $max = null)
  23. * @method $this button($style = 'success');
  24. * @method $this link($href = '', $target = '_blank');
  25. * @method $this badge($style = 'primary', int $max = null);
  26. * @method $this progressBar($style = 'primary', $size = 'sm', $max = 100)
  27. * @method $this checkbox($options = [])
  28. * @method $this radio($options = [])
  29. * @method $this expand($callbackOrButton = null)
  30. * @method $this table($titles = [])
  31. * @method $this select($options = [])
  32. * @method $this modal($title = '', \Closure $callback = null)
  33. * @method $this showTreeInDialog($callbackOrNodes = null)
  34. * @method $this qrcode($formatter = null, $width = 150, $height = 150)
  35. * @method $this downloadable($server = '', $disk = null)
  36. * @method $this copyable()
  37. * @method $this orderable()
  38. * @method $this limit(int $limit = 100, string $end = '...')
  39. * @method $this ascii()
  40. * @method $this camel()
  41. * @method $this finish($cap)
  42. * @method $this lower()
  43. * @method $this words($words = 100, $end = '...')
  44. * @method $this upper()
  45. * @method $this title()
  46. * @method $this slug($separator = '-')
  47. * @method $this snake($delimiter = '_')
  48. * @method $this studly()
  49. * @method $this substr($start, $length = null)
  50. * @method $this ucfirst()
  51. *
  52. * @mixin Collection
  53. */
  54. class Column
  55. {
  56. use HasBuilderEvents,
  57. HasDefinitions,
  58. Grid\Column\HasHeader,
  59. Grid\Column\HasDisplayers,
  60. Macroable {
  61. __call as __macroCall;
  62. }
  63. const SELECT_COLUMN_NAME = '__row_selector__';
  64. /**
  65. * Displayers for grid column.
  66. *
  67. * @var array
  68. */
  69. protected static $displayers = [
  70. 'switch' => Displayers\SwitchDisplay::class,
  71. 'switchGroup' => Displayers\SwitchGroup::class,
  72. 'select' => Displayers\Select::class,
  73. 'image' => Displayers\Image::class,
  74. 'label' => Displayers\Label::class,
  75. 'button' => Displayers\Button::class,
  76. 'link' => Displayers\Link::class,
  77. 'badge' => Displayers\Badge::class,
  78. 'progressBar' => Displayers\ProgressBar::class,
  79. 'radio' => Displayers\Radio::class,
  80. 'checkbox' => Displayers\Checkbox::class,
  81. 'table' => Displayers\Table::class,
  82. 'expand' => Displayers\Expand::class,
  83. 'modal' => Displayers\Modal::class,
  84. 'showTreeInDialog' => Displayers\DialogTree::class,
  85. 'qrcode' => Displayers\QRCode::class,
  86. 'downloadable' => Displayers\Downloadable::class,
  87. 'copyable' => Displayers\Copyable::class,
  88. 'orderable' => Displayers\Orderable::class,
  89. 'limit' => Displayers\Limit::class,
  90. 'editable' => Displayers\Editable::class,
  91. ];
  92. /**
  93. * Original grid data.
  94. *
  95. * @var Collection
  96. */
  97. protected static $originalGridModels;
  98. /**
  99. * @var Grid
  100. */
  101. protected $grid;
  102. /**
  103. * Name of column.
  104. *
  105. * @var string
  106. */
  107. protected $name;
  108. /**
  109. * @var array
  110. */
  111. protected $htmlAttributes = [];
  112. /**
  113. * Label of column.
  114. *
  115. * @var string
  116. */
  117. protected $label;
  118. /**
  119. * @var Fluent
  120. */
  121. protected $originalModel;
  122. /**
  123. * Original value of column.
  124. *
  125. * @var mixed
  126. */
  127. protected $original;
  128. /**
  129. * @var mixed
  130. */
  131. protected $value;
  132. /**
  133. * Sort arguments.
  134. *
  135. * @var array
  136. */
  137. protected $sort;
  138. /**
  139. * @var string
  140. */
  141. protected $width;
  142. /**
  143. * Attributes of column.
  144. *
  145. * @var array
  146. */
  147. protected $attributes = [];
  148. /**
  149. * @var []Closure
  150. */
  151. protected $displayCallbacks = [];
  152. /**
  153. * @var array
  154. */
  155. protected $titleHtmlAttributes = [];
  156. /**
  157. * @var Model
  158. */
  159. protected static $model;
  160. /**
  161. * @var Grid\Column\Condition
  162. */
  163. protected $conditions = [];
  164. /**
  165. * @param string $name
  166. * @param string $label
  167. */
  168. public function __construct($name, $label)
  169. {
  170. $this->name = $name;
  171. $this->label = $this->formatLabel($label);
  172. $this->callResolving();
  173. }
  174. /**
  175. * Extend column displayer.
  176. *
  177. * @param $name
  178. * @param $displayer
  179. */
  180. public static function extend($name, $displayer)
  181. {
  182. static::$displayers[$name] = $displayer;
  183. }
  184. /**
  185. * @return array
  186. */
  187. public static function extensions()
  188. {
  189. return static::$displayers;
  190. }
  191. /**
  192. * Set grid instance for column.
  193. *
  194. * @param Grid $grid
  195. */
  196. public function setGrid(Grid $grid)
  197. {
  198. $this->grid = $grid;
  199. }
  200. /**
  201. * @return Grid
  202. */
  203. public function grid()
  204. {
  205. return $this->grid;
  206. }
  207. /**
  208. * Set original data for column.
  209. *
  210. * @param Collection $collection
  211. */
  212. public static function setOriginalGridModels(Collection $collection)
  213. {
  214. static::$originalGridModels = $collection;
  215. }
  216. /**
  217. * Set width for column.
  218. *
  219. * @param string $width
  220. *
  221. * @return $this|string
  222. */
  223. public function width(?string $width)
  224. {
  225. $this->titleHtmlAttributes['width'] = $width;
  226. return $this;
  227. }
  228. /**
  229. * @example
  230. * $grid->config
  231. * ->if(function () {
  232. * return $this->config ? true : false;
  233. * })
  234. * ->display($view)
  235. * ->expand(...)
  236. * ->else()
  237. * ->emptyString()
  238. *
  239. * $grid->config
  240. * ->if(function () {
  241. * return $this->config ? true : false;
  242. * })
  243. * ->then(function (Column $column) {
  244. * $column ->display($view)->expand(...);
  245. * })
  246. * ->else(function (Column $column) {
  247. * $column->emptyString();
  248. * })
  249. *
  250. * @param \Closure $condition
  251. *
  252. * @return Column\Condition
  253. */
  254. public function if(\Closure $condition)
  255. {
  256. return $this->conditions[] = new Grid\Column\Condition($condition, $this);
  257. }
  258. /**
  259. * Set column attributes.
  260. *
  261. * @param array $attributes
  262. *
  263. * @return $this
  264. */
  265. public function setAttributes(array $attributes = [])
  266. {
  267. $this->htmlAttributes = array_merge($this->htmlAttributes, $attributes);
  268. return $this;
  269. }
  270. /**
  271. * Get column attributes.
  272. *
  273. * @param string $name
  274. *
  275. * @return mixed
  276. */
  277. public function getAttributes()
  278. {
  279. return $this->htmlAttributes;
  280. }
  281. /**
  282. * @return $this
  283. */
  284. public function hide()
  285. {
  286. return $this->responsive(0);
  287. }
  288. /**
  289. * data-priority=”1″ 保持可见,但可以在下拉列表筛选隐藏。
  290. * data-priority=”2″ 480px 分辨率以下可见
  291. * data-priority=”3″ 640px 以下可见
  292. * data-priority=”4″ 800px 以下可见
  293. * data-priority=”5″ 960px 以下可见
  294. * data-priority=”6″ 1120px 以下可见
  295. *
  296. * @param int $priority
  297. *
  298. * @return $this
  299. */
  300. public function responsive(?int $priority = 1)
  301. {
  302. $this->grid->responsive();
  303. return $this->setHeaderAttributes(['data-priority' => $priority]);
  304. }
  305. /**
  306. * @return int|null
  307. */
  308. public function getDataPriority()
  309. {
  310. return isset($this->titleHtmlAttributes['data-priority']) ? $this->titleHtmlAttributes['data-priority'] : null;
  311. }
  312. /**
  313. * Set style of this column.
  314. *
  315. * @param string $style
  316. *
  317. * @return Column
  318. */
  319. public function style($style)
  320. {
  321. return $this->setAttributes(compact('style'));
  322. }
  323. /**
  324. * Get name of this column.
  325. *
  326. * @return mixed
  327. */
  328. public function getName()
  329. {
  330. return $this->name;
  331. }
  332. /**
  333. * @param array|Model $model
  334. */
  335. public function setOriginalModel($model)
  336. {
  337. if (is_array($model)) {
  338. $model = new Fluent($model);
  339. }
  340. $this->originalModel = $model;
  341. }
  342. /**
  343. * @return Fluent|Model
  344. */
  345. public function getOriginalModel()
  346. {
  347. return $this->originalModel;
  348. }
  349. /**
  350. * @return mixed
  351. */
  352. public function getOriginal()
  353. {
  354. return $this->original;
  355. }
  356. /**
  357. * @return mixed
  358. */
  359. public function getValue()
  360. {
  361. return $this->value;
  362. }
  363. /**
  364. * Format label.
  365. *
  366. * @param string $label
  367. *
  368. * @return mixed
  369. */
  370. protected function formatLabel($label)
  371. {
  372. if (! $label) {
  373. $label = admin_trans_field($this->name);
  374. }
  375. $label = $label ?: $this->name;
  376. return str_replace(['.', '_'], ' ', $label);
  377. }
  378. /**
  379. * Get label of the column.
  380. *
  381. * @return mixed
  382. */
  383. public function getLabel()
  384. {
  385. return $this->label;
  386. }
  387. /**
  388. * @param string $label
  389. *
  390. * @return $this
  391. */
  392. public function setLabel($label)
  393. {
  394. $this->label = $label;
  395. return $this;
  396. }
  397. /**
  398. * Add a display callback.
  399. *
  400. * @param \Closure|string $callback
  401. * @param array $params
  402. *
  403. * @return $this
  404. */
  405. public function display($callback, ...$params)
  406. {
  407. $this->displayCallbacks[] = [&$callback, &$params];
  408. return $this;
  409. }
  410. /**
  411. * Display column using a grid row action.
  412. *
  413. * @param string $action
  414. *
  415. * @return $this
  416. */
  417. public function action($action)
  418. {
  419. if (! is_subclass_of($action, RowAction::class)) {
  420. throw new \InvalidArgumentException("Action class [$action] must be sub-class of [Dcat\Admin\Grid\RowAction]");
  421. }
  422. $grid = $this->grid;
  423. return $this->display(function ($_, $column) use ($action, $grid) {
  424. /** @var RowAction $action */
  425. $action = $action::make();
  426. return $action
  427. ->setGrid($grid)
  428. ->setColumn($column)
  429. ->setRow($this);
  430. });
  431. }
  432. /**
  433. * If has display callbacks.
  434. *
  435. * @return bool
  436. */
  437. public function hasDisplayCallbacks()
  438. {
  439. return ! empty($this->displayCallbacks);
  440. }
  441. /**
  442. * @param array $callbacks
  443. *
  444. * @return void
  445. */
  446. public function setDisplayCallbacks(array $callbacks)
  447. {
  448. $this->displayCallbacks = $callbacks;
  449. }
  450. /**
  451. * @return \Closure[]
  452. */
  453. public function getDisplayCallbacks()
  454. {
  455. return $this->displayCallbacks;
  456. }
  457. /**
  458. * Call all of the "display" callbacks column.
  459. *
  460. * @param mixed $value
  461. *
  462. * @return mixed
  463. */
  464. protected function callDisplayCallbacks($value)
  465. {
  466. foreach ($this->displayCallbacks as $callback) {
  467. [$callback, $params] = $callback;
  468. if (! $callback instanceof \Closure) {
  469. $value = $callback;
  470. continue;
  471. }
  472. $previous = $value;
  473. $callback = $this->bindOriginalRowModel($callback);
  474. $value = $callback($value, $this, ...$params);
  475. if (
  476. $value instanceof static
  477. && ($last = array_pop($this->displayCallbacks))
  478. ) {
  479. [$last, $params] = $last;
  480. $last = $this->bindOriginalRowModel($last);
  481. $value = call_user_func($last, $previous, $this, ...$params);
  482. }
  483. }
  484. return $value;
  485. }
  486. /**
  487. * Set original grid data to column.
  488. *
  489. * @param Closure $callback
  490. *
  491. * @return Closure
  492. */
  493. protected function bindOriginalRowModel(Closure $callback)
  494. {
  495. return $callback->bindTo($this->getOriginalModel());
  496. }
  497. /**
  498. * Fill all data to every column.
  499. *
  500. * @param array $data
  501. */
  502. public function fill(array &$data)
  503. {
  504. if (static::hasDefinition($this->name)) {
  505. $this->useDefinedColumn();
  506. }
  507. $i = 0;
  508. foreach ($data as $key => &$row) {
  509. $i++;
  510. if (! isset($row['#'])) {
  511. $row['#'] = $i;
  512. }
  513. $this->original = $value = Arr::get($row, $this->name);
  514. $this->value = $value = $this->htmlEntityEncode($value);
  515. $this->setOriginalModel(static::$originalGridModels[$key]);
  516. $this->processConditions();
  517. Arr::set($row, $this->name, $value);
  518. if ($this->hasDisplayCallbacks()) {
  519. $value = $this->callDisplayCallbacks($this->original);
  520. Arr::set($row, $this->name, $value);
  521. }
  522. }
  523. $this->value = $value ?? null;
  524. }
  525. /**
  526. * @return void
  527. */
  528. protected function processConditions()
  529. {
  530. foreach ($this->conditions as $condition) {
  531. $condition->reset();
  532. }
  533. foreach ($this->conditions as $condition) {
  534. $condition->process();
  535. }
  536. }
  537. /**
  538. * Use a defined column.
  539. *
  540. * @throws \Exception
  541. */
  542. protected function useDefinedColumn()
  543. {
  544. $class = static::$definitions[$this->name];
  545. if ($class instanceof Closure) {
  546. $this->display($class);
  547. return;
  548. }
  549. if (! class_exists($class) || ! is_subclass_of($class, AbstractDisplayer::class)) {
  550. throw new \Exception("Invalid column definition [$class]");
  551. }
  552. $this->displayUsing($class);
  553. }
  554. /**
  555. * Convert characters to HTML entities recursively.
  556. *
  557. * @param array|string $item
  558. *
  559. * @return mixed
  560. */
  561. protected function htmlEntityEncode($item)
  562. {
  563. if (is_array($item)) {
  564. array_walk_recursive($item, function (&$value) {
  565. $value = htmlentities($value);
  566. });
  567. } else {
  568. $item = htmlentities($item);
  569. }
  570. return $item;
  571. }
  572. /**
  573. * Determine if this column is currently sorted.
  574. *
  575. * @return bool
  576. */
  577. protected function isSorted()
  578. {
  579. $this->sort = app('request')->get($this->grid->model()->getSortName());
  580. if (empty($this->sort)) {
  581. return false;
  582. }
  583. return isset($this->sort['column']) && $this->sort['column'] == $this->name;
  584. }
  585. /**
  586. * Find a displayer to display column.
  587. *
  588. * @param string $abstract
  589. * @param array $arguments
  590. *
  591. * @return Column
  592. */
  593. protected function resolveDisplayer($abstract, $arguments)
  594. {
  595. if (isset(static::$displayers[$abstract])) {
  596. return $this->callBuiltinDisplayer(static::$displayers[$abstract], $arguments);
  597. }
  598. return $this->callSupportDisplayer($abstract, $arguments);
  599. }
  600. /**
  601. * Call Illuminate/Support displayer.
  602. *
  603. * @param string $abstract
  604. * @param array $arguments
  605. *
  606. * @return Column
  607. */
  608. protected function callSupportDisplayer($abstract, $arguments)
  609. {
  610. return $this->display(function ($value) use ($abstract, $arguments) {
  611. if (is_array($value) || $value instanceof Arrayable) {
  612. return call_user_func_array([collect($value), $abstract], $arguments);
  613. }
  614. if (is_string($value)) {
  615. return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments));
  616. }
  617. return $value;
  618. });
  619. }
  620. /**
  621. * Call Builtin displayer.
  622. *
  623. * @param string $abstract
  624. * @param array $arguments
  625. *
  626. * @return Column
  627. */
  628. protected function callBuiltinDisplayer($abstract, $arguments)
  629. {
  630. if ($abstract instanceof Closure) {
  631. return $this->display(function ($value) use ($abstract, $arguments) {
  632. return $abstract->call($this, ...array_merge([$value], $arguments));
  633. });
  634. }
  635. if (is_subclass_of($abstract, AbstractDisplayer::class)) {
  636. $grid = $this->grid;
  637. $column = $this;
  638. return $this->display(function ($value) use ($abstract, $grid, $column, $arguments) {
  639. /** @var AbstractDisplayer $displayer */
  640. $displayer = new $abstract($value, $grid, $column, $this);
  641. return $displayer->display(...$arguments);
  642. });
  643. }
  644. return $this;
  645. }
  646. /**
  647. * Set column title attributes.
  648. *
  649. * @param array $attributes
  650. *
  651. * @return $this
  652. */
  653. public function setHeaderAttributes(array $attributes = [])
  654. {
  655. $this->titleHtmlAttributes = array_merge($this->titleHtmlAttributes, $attributes);
  656. return $this;
  657. }
  658. /**
  659. * Set column title default attributes.
  660. *
  661. * @param array $attributes
  662. *
  663. * @return $this
  664. */
  665. public function setDefaultHeaderAttribute(array $attributes)
  666. {
  667. foreach ($attributes as $key => $value) {
  668. if (isset($this->titleHtmlAttributes[$key])) {
  669. continue;
  670. }
  671. $this->titleHtmlAttributes[$key] = $value;
  672. }
  673. return $this;
  674. }
  675. /**
  676. * @return string
  677. */
  678. public function formatTitleAttributes()
  679. {
  680. $attrArr = [];
  681. foreach ($this->titleHtmlAttributes as $name => $val) {
  682. $attrArr[] = "$name=\"$val\"";
  683. }
  684. return implode(' ', $attrArr);
  685. }
  686. /**
  687. * Passes through all unknown calls to builtin displayer or supported displayer.
  688. *
  689. * Allow fluent calls on the Column object.
  690. *
  691. * @param string $method
  692. * @param array $arguments
  693. *
  694. * @return $this
  695. */
  696. public function __call($method, $arguments)
  697. {
  698. if (
  699. ! isset(static::$displayers[$method])
  700. && static::hasMacro($method)
  701. ) {
  702. return $this->__macroCall($method, $arguments);
  703. }
  704. return $this->resolveDisplayer($method, $arguments);
  705. }
  706. }