Column.php 16 KB

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