Column.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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\Traits\HasBuilderEvents;
  7. use Dcat\Admin\Traits\HasDefinitions;
  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 = '')
  18. * @method $this switchGroup($columns = [], string $color = '')
  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 = [])
  26. * @method $this radio($options = [])
  27. * @method $this expand($callbackOrButton = null)
  28. * @method $this table($titles = [])
  29. * @method $this select($options = [])
  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. HasDefinitions,
  56. Grid\Column\HasHeader,
  57. Grid\Column\HasDisplayers,
  58. Macroable {
  59. __call as __macroCall;
  60. }
  61. const SELECT_COLUMN_NAME = '__row_selector__';
  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 = $name;
  169. $this->label = $this->formatLabel($label);
  170. $this->callResolving();
  171. }
  172. /**
  173. * Extend column displayer.
  174. *
  175. * @param $name
  176. * @param $displayer
  177. */
  178. public static function extend($name, $displayer)
  179. {
  180. static::$displayers[$name] = $displayer;
  181. }
  182. /**
  183. * @return array
  184. */
  185. public static function extensions()
  186. {
  187. return static::$displayers;
  188. }
  189. /**
  190. * Set grid instance for column.
  191. *
  192. * @param Grid $grid
  193. */
  194. public function setGrid(Grid $grid)
  195. {
  196. $this->grid = $grid;
  197. }
  198. /**
  199. * @return Grid
  200. */
  201. public function grid()
  202. {
  203. return $this->grid;
  204. }
  205. /**
  206. * Set original data for column.
  207. *
  208. * @param Collection $collection
  209. */
  210. public static function setOriginalGridModels(Collection $collection)
  211. {
  212. static::$originalGridModels = $collection;
  213. }
  214. /**
  215. * Set width for column.
  216. *
  217. * @param string $width
  218. *
  219. * @return $this|string
  220. */
  221. public function width(?string $width)
  222. {
  223. $this->titleHtmlAttributes['width'] = $width;
  224. return $this;
  225. }
  226. /**
  227. * @example
  228. * $grid->config
  229. * ->if(function () {
  230. * return $this->config ? true : false;
  231. * })
  232. * ->display($view)
  233. * ->expand(...)
  234. * ->else()
  235. * ->emptyString()
  236. *
  237. * $grid->config
  238. * ->if(function () {
  239. * return $this->config ? true : false;
  240. * })
  241. * ->then(function (Column $column) {
  242. * $column ->display($view)->expand(...);
  243. * })
  244. * ->else(function (Column $column) {
  245. * $column->emptyString();
  246. * })
  247. *
  248. * @param \Closure $condition
  249. *
  250. * @return Column\Condition
  251. */
  252. public function if(\Closure $condition)
  253. {
  254. return $this->conditions[] = new Grid\Column\Condition($condition, $this);
  255. }
  256. /**
  257. * Set column attributes.
  258. *
  259. * @param array $attributes
  260. *
  261. * @return $this
  262. */
  263. public function setAttributes(array $attributes = [])
  264. {
  265. $this->htmlAttributes = array_merge($this->htmlAttributes, $attributes);
  266. return $this;
  267. }
  268. /**
  269. * Get column attributes.
  270. *
  271. * @param string $name
  272. *
  273. * @return mixed
  274. */
  275. public function getAttributes()
  276. {
  277. return $this->htmlAttributes;
  278. }
  279. /**
  280. * @return $this
  281. */
  282. public function hide()
  283. {
  284. return $this->responsive(0);
  285. }
  286. /**
  287. * data-priority=”1″ 保持可见,但可以在下拉列表筛选隐藏。
  288. * data-priority=”2″ 480px 分辨率以下可见
  289. * data-priority=”3″ 640px 以下可见
  290. * data-priority=”4″ 800px 以下可见
  291. * data-priority=”5″ 960px 以下可见
  292. * data-priority=”6″ 1120px 以下可见
  293. *
  294. * @param int $priority
  295. *
  296. * @return $this
  297. */
  298. public function responsive(?int $priority = 1)
  299. {
  300. $this->grid->responsive();
  301. return $this->setHeaderAttributes(['data-priority' => $priority]);
  302. }
  303. /**
  304. * @return int|null
  305. */
  306. public function getDataPriority()
  307. {
  308. return isset($this->titleHtmlAttributes['data-priority']) ? $this->titleHtmlAttributes['data-priority'] : null;
  309. }
  310. /**
  311. * Set style of this column.
  312. *
  313. * @param string $style
  314. *
  315. * @return Column
  316. */
  317. public function style($style)
  318. {
  319. return $this->setAttributes(compact('style'));
  320. }
  321. /**
  322. * Get name of this column.
  323. *
  324. * @return mixed
  325. */
  326. public function getName()
  327. {
  328. return $this->name;
  329. }
  330. /**
  331. * @param array|Model $model
  332. */
  333. public function setOriginalModel($model)
  334. {
  335. if (is_array($model)) {
  336. $model = new Fluent($model);
  337. }
  338. $this->originalModel = $model;
  339. }
  340. /**
  341. * @return Fluent|Model
  342. */
  343. public function getOriginalModel()
  344. {
  345. return $this->originalModel;
  346. }
  347. /**
  348. * @return mixed
  349. */
  350. public function getOriginal()
  351. {
  352. return $this->original;
  353. }
  354. /**
  355. * @return mixed
  356. */
  357. public function getValue()
  358. {
  359. return $this->value;
  360. }
  361. /**
  362. * Format label.
  363. *
  364. * @param string $label
  365. *
  366. * @return mixed
  367. */
  368. protected function formatLabel($label)
  369. {
  370. $label = $label ?: admin_trans_field($this->name);
  371. return str_replace(['.', '_'], ' ', $label);
  372. }
  373. /**
  374. * Get label of the column.
  375. *
  376. * @return mixed
  377. */
  378. public function getLabel()
  379. {
  380. return $this->label;
  381. }
  382. /**
  383. * @param string $label
  384. *
  385. * @return $this
  386. */
  387. public function setLabel($label)
  388. {
  389. $this->label = $label;
  390. return $this;
  391. }
  392. /**
  393. * Add a display callback.
  394. *
  395. * @param \Closure|string $callback
  396. * @param array $params
  397. *
  398. * @return $this
  399. */
  400. public function display($callback, ...$params)
  401. {
  402. $this->displayCallbacks[] = [&$callback, &$params];
  403. return $this;
  404. }
  405. /**
  406. * If has display callbacks.
  407. *
  408. * @return bool
  409. */
  410. public function hasDisplayCallbacks()
  411. {
  412. return ! empty($this->displayCallbacks);
  413. }
  414. /**
  415. * @param array $callbacks
  416. *
  417. * @return void
  418. */
  419. public function setDisplayCallbacks(array $callbacks)
  420. {
  421. $this->displayCallbacks = $callbacks;
  422. }
  423. /**
  424. * @return \Closure[]
  425. */
  426. public function getDisplayCallbacks()
  427. {
  428. return $this->displayCallbacks;
  429. }
  430. /**
  431. * Call all of the "display" callbacks column.
  432. *
  433. * @param mixed $value
  434. *
  435. * @return mixed
  436. */
  437. protected function callDisplayCallbacks($value)
  438. {
  439. foreach ($this->displayCallbacks as $callback) {
  440. [$callback, $params] = $callback;
  441. if (! $callback instanceof \Closure) {
  442. $value = $callback;
  443. continue;
  444. }
  445. $previous = $value;
  446. $callback = $this->bindOriginalRowModel($callback);
  447. $value = $callback($value, $this, ...$params);
  448. if (
  449. $value instanceof static
  450. && ($last = array_pop($this->displayCallbacks))
  451. ) {
  452. [$last, $params] = $last;
  453. $last = $this->bindOriginalRowModel($last);
  454. $value = call_user_func($last, $previous, $this, ...$params);
  455. }
  456. }
  457. return $value;
  458. }
  459. /**
  460. * Set original grid data to column.
  461. *
  462. * @param Closure $callback
  463. *
  464. * @return Closure
  465. */
  466. protected function bindOriginalRowModel(Closure $callback)
  467. {
  468. return $callback->bindTo($this->getOriginalModel());
  469. }
  470. /**
  471. * Fill all data to every column.
  472. *
  473. * @param array $data
  474. */
  475. public function fill(array &$data)
  476. {
  477. if (static::hasDefinition($this->name)) {
  478. $this->useDefinedColumn();
  479. }
  480. $i = 0;
  481. foreach ($data as $key => &$row) {
  482. $i++;
  483. if (! isset($row['#'])) {
  484. $row['#'] = $i;
  485. }
  486. $this->original = $value = Arr::get($row, $this->name);
  487. $this->value = $value = $this->htmlEntityEncode($value);
  488. $this->setOriginalModel(static::$originalGridModels[$key]);
  489. $this->processConditions();
  490. Arr::set($row, $this->name, $value);
  491. if ($this->hasDisplayCallbacks()) {
  492. $value = $this->callDisplayCallbacks($this->original);
  493. Arr::set($row, $this->name, $value);
  494. }
  495. }
  496. $this->value = $value ?? null;
  497. }
  498. /**
  499. * @return void
  500. */
  501. protected function processConditions()
  502. {
  503. foreach ($this->conditions as $condition) {
  504. $condition->reset();
  505. }
  506. foreach ($this->conditions as $condition) {
  507. $condition->process();
  508. }
  509. }
  510. /**
  511. * Use a defined column.
  512. *
  513. * @throws \Exception
  514. */
  515. protected function useDefinedColumn()
  516. {
  517. $class = static::$definitions[$this->name];
  518. if ($class instanceof Closure) {
  519. $this->display($class);
  520. return;
  521. }
  522. if (! class_exists($class) || ! is_subclass_of($class, AbstractDisplayer::class)) {
  523. throw new \Exception("Invalid column definition [$class]");
  524. }
  525. $this->displayUsing($class);
  526. }
  527. /**
  528. * Convert characters to HTML entities recursively.
  529. *
  530. * @param array|string $item
  531. *
  532. * @return mixed
  533. */
  534. protected function htmlEntityEncode($item)
  535. {
  536. if (is_array($item)) {
  537. array_walk_recursive($item, function (&$value) {
  538. $value = htmlentities($value);
  539. });
  540. } else {
  541. $item = htmlentities($item);
  542. }
  543. return $item;
  544. }
  545. /**
  546. * Determine if this column is currently sorted.
  547. *
  548. * @return bool
  549. */
  550. protected function isSorted()
  551. {
  552. $this->sort = app('request')->get($this->grid->model()->getSortName());
  553. if (empty($this->sort)) {
  554. return false;
  555. }
  556. return isset($this->sort['column']) && $this->sort['column'] == $this->name;
  557. }
  558. /**
  559. * Find a displayer to display column.
  560. *
  561. * @param string $abstract
  562. * @param array $arguments
  563. *
  564. * @return Column
  565. */
  566. protected function resolveDisplayer($abstract, $arguments)
  567. {
  568. if (isset(static::$displayers[$abstract])) {
  569. return $this->callBuiltinDisplayer(static::$displayers[$abstract], $arguments);
  570. }
  571. return $this->callSupportDisplayer($abstract, $arguments);
  572. }
  573. /**
  574. * Call Illuminate/Support displayer.
  575. *
  576. * @param string $abstract
  577. * @param array $arguments
  578. *
  579. * @return Column
  580. */
  581. protected function callSupportDisplayer($abstract, $arguments)
  582. {
  583. return $this->display(function ($value) use ($abstract, $arguments) {
  584. if (is_array($value) || $value instanceof Arrayable) {
  585. return call_user_func_array([collect($value), $abstract], $arguments);
  586. }
  587. if (is_string($value)) {
  588. return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments));
  589. }
  590. return $value;
  591. });
  592. }
  593. /**
  594. * Call Builtin displayer.
  595. *
  596. * @param string $abstract
  597. * @param array $arguments
  598. *
  599. * @return Column
  600. */
  601. protected function callBuiltinDisplayer($abstract, $arguments)
  602. {
  603. if ($abstract instanceof Closure) {
  604. return $this->display(function ($value) use ($abstract, $arguments) {
  605. return $abstract->call($this, ...array_merge([$value], $arguments));
  606. });
  607. }
  608. if (is_subclass_of($abstract, AbstractDisplayer::class)) {
  609. $grid = $this->grid;
  610. $column = $this;
  611. return $this->display(function ($value) use ($abstract, $grid, $column, $arguments) {
  612. /** @var AbstractDisplayer $displayer */
  613. $displayer = new $abstract($value, $grid, $column, $this);
  614. return $displayer->display(...$arguments);
  615. });
  616. }
  617. return $this;
  618. }
  619. /**
  620. * Set column title attributes.
  621. *
  622. * @param array $attributes
  623. *
  624. * @return $this
  625. */
  626. public function setHeaderAttributes(array $attributes = [])
  627. {
  628. $this->titleHtmlAttributes = array_merge($this->titleHtmlAttributes, $attributes);
  629. return $this;
  630. }
  631. /**
  632. * Set column title default attributes.
  633. *
  634. * @param array $attributes
  635. *
  636. * @return $this
  637. */
  638. public function setDefaultHeaderAttribute(array $attributes)
  639. {
  640. foreach ($attributes as $key => $value) {
  641. if (isset($this->titleHtmlAttributes[$key])) {
  642. continue;
  643. }
  644. $this->titleHtmlAttributes[$key] = $value;
  645. }
  646. return $this;
  647. }
  648. /**
  649. * @return string
  650. */
  651. public function formatTitleAttributes()
  652. {
  653. $attrArr = [];
  654. foreach ($this->titleHtmlAttributes as $name => $val) {
  655. $attrArr[] = "$name=\"$val\"";
  656. }
  657. return implode(' ', $attrArr);
  658. }
  659. /**
  660. * Passes through all unknown calls to builtin displayer or supported displayer.
  661. *
  662. * Allow fluent calls on the Column object.
  663. *
  664. * @param string $method
  665. * @param array $arguments
  666. *
  667. * @return $this
  668. */
  669. public function __call($method, $arguments)
  670. {
  671. if (
  672. ! isset(static::$displayers[$method])
  673. && static::hasMacro($method)
  674. ) {
  675. return $this->__macroCall($method, $arguments);
  676. }
  677. return $this->resolveDisplayer($method, $arguments);
  678. }
  679. }