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