Column.php 17 KB

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