Column.php 18 KB

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