Column.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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()
  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 $help
  340. * @param null $style
  341. * @return $this
  342. */
  343. public function help($help = '', $style = null)
  344. {
  345. return $this->addHelp($help, $style);
  346. }
  347. /**
  348. * Set column filter.
  349. *
  350. * @param Grid\Column\Filter $builder
  351. *
  352. * @return $this
  353. */
  354. public function filter(Grid\Column\Filter $filter)
  355. {
  356. return $this->addFilter($filter);
  357. }
  358. /**
  359. * Add a display callback.
  360. *
  361. * @param $callback
  362. * @param array $params
  363. * @return $this
  364. */
  365. public function display($callback, ...$params)
  366. {
  367. $this->displayCallbacks[] = [&$callback, &$params];
  368. return $this;
  369. }
  370. /**
  371. * If has display callbacks.
  372. *
  373. * @return bool
  374. */
  375. protected function hasDisplayCallbacks()
  376. {
  377. return !empty($this->displayCallbacks);
  378. }
  379. /**
  380. * Call all of the "display" callbacks column.
  381. *
  382. * @param mixed $value
  383. * @param int $key
  384. *
  385. * @return mixed
  386. */
  387. protected function callDisplayCallbacks($value, $key)
  388. {
  389. foreach ($this->displayCallbacks as $callback) {
  390. list($callback, $params) = $callback;
  391. if (!$callback instanceof \Closure) {
  392. $value = $callback;
  393. continue;
  394. }
  395. $previous = $value;
  396. $callback = $this->bindOriginalRowModel($callback, $key);
  397. $value = $callback($value, $this, ...$params);
  398. if (($value instanceof static) &&
  399. ($last = array_pop($this->displayCallbacks))
  400. ) {
  401. list($last, $params) = $last;
  402. $last = $this->bindOriginalRowModel($last, $key);
  403. $value = call_user_func($last, $previous, $this, ...$params);
  404. }
  405. }
  406. return $value;
  407. }
  408. /**
  409. * Set original grid data to column.
  410. *
  411. * @param Closure $callback
  412. * @param int $key
  413. *
  414. * @return Closure
  415. */
  416. protected function bindOriginalRowModel(Closure $callback, $key)
  417. {
  418. $rowModel = static::$originalGridModels[$key];
  419. if (is_array($rowModel)) {
  420. $rowModel = new Fluent($rowModel);
  421. }
  422. return $callback->bindTo($rowModel);
  423. }
  424. /**
  425. * Fill all data to every column.
  426. *
  427. * @param array $data
  428. */
  429. public function fill(array &$data)
  430. {
  431. if (static::hasDefinition($this->name)) {
  432. $this->useDefinedColumn();
  433. }
  434. $i = 0;
  435. foreach ($data as $key => &$row) {
  436. $i++;
  437. if (!isset($row['#'])) {
  438. $row['#'] = $i;
  439. }
  440. $this->original = $value = Arr::get($row, $this->name);
  441. $this->value = $value = $this->htmlEntityEncode($value);
  442. Arr::set($row, $this->name, $value);
  443. if ($this->hasDisplayCallbacks()) {
  444. $value = $this->callDisplayCallbacks($this->original, $key);
  445. Arr::set($row, $this->name, $value);
  446. }
  447. }
  448. $this->value = $value ?? null;
  449. }
  450. /**
  451. * Use a defined column.
  452. *
  453. * @throws \Exception
  454. */
  455. protected function useDefinedColumn()
  456. {
  457. $class = static::$definitions[$this->name];
  458. if ($class instanceof Closure) {
  459. $this->display($class);
  460. return;
  461. }
  462. if (!class_exists($class) || !is_subclass_of($class, AbstractDisplayer::class)) {
  463. throw new \Exception("Invalid column definition [$class]");
  464. }
  465. $this->displayUsing($class);
  466. }
  467. /**
  468. * Convert characters to HTML entities recursively.
  469. *
  470. * @param array|string $item
  471. *
  472. * @return mixed
  473. */
  474. protected function htmlEntityEncode($item)
  475. {
  476. if (is_array($item)) {
  477. array_walk_recursive($item, function (&$value) {
  478. $value = htmlentities($value);
  479. });
  480. } else {
  481. $item = htmlentities($item);
  482. }
  483. return $item;
  484. }
  485. /**
  486. * Determine if this column is currently sorted.
  487. *
  488. * @return bool
  489. */
  490. protected function isSorted()
  491. {
  492. $this->sort = app('request')->get($this->grid->model()->getSortName());
  493. if (empty($this->sort)) {
  494. return false;
  495. }
  496. return isset($this->sort['column']) && $this->sort['column'] == $this->name;
  497. }
  498. /**
  499. * Find a displayer to display column.
  500. *
  501. * @param string $abstract
  502. * @param array $arguments
  503. *
  504. * @return Column
  505. */
  506. protected function resolveDisplayer($abstract, $arguments)
  507. {
  508. if (isset(static::$displayers[$abstract])) {
  509. return $this->callBuiltinDisplayer(static::$displayers[$abstract], $arguments);
  510. }
  511. return $this->callSupportDisplayer($abstract, $arguments);
  512. }
  513. /**
  514. * Call Illuminate/Support displayer.
  515. *
  516. * @param string $abstract
  517. * @param array $arguments
  518. *
  519. * @return Column
  520. */
  521. protected function callSupportDisplayer($abstract, $arguments)
  522. {
  523. return $this->display(function ($value) use ($abstract, $arguments) {
  524. if (is_array($value) || $value instanceof Arrayable) {
  525. return call_user_func_array([collect($value), $abstract], $arguments);
  526. }
  527. if (is_string($value)) {
  528. return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments));
  529. }
  530. return $value;
  531. });
  532. }
  533. /**
  534. * Call Builtin displayer.
  535. *
  536. * @param string $abstract
  537. * @param array $arguments
  538. *
  539. * @return Column
  540. */
  541. protected function callBuiltinDisplayer($abstract, $arguments)
  542. {
  543. if ($abstract instanceof Closure) {
  544. return $this->display(function ($value) use ($abstract, $arguments) {
  545. return $abstract->call($this, ...array_merge([$value], $arguments));
  546. });
  547. }
  548. if (is_subclass_of($abstract, AbstractDisplayer::class)) {
  549. $grid = $this->grid;
  550. $column = $this;
  551. return $this->display(function ($value) use ($abstract, $grid, $column, $arguments) {
  552. /** @var AbstractDisplayer $displayer */
  553. $displayer = new $abstract($value, $grid, $column, $this);
  554. return $displayer->display(...$arguments);
  555. });
  556. }
  557. return $this;
  558. }
  559. /**
  560. * Set column title attributes.
  561. *
  562. * @param array $attributes
  563. * @return $this
  564. */
  565. public function setHeaderAttributes(array $attributes = [])
  566. {
  567. $this->titleHtmlAttributes = array_merge($this->titleHtmlAttributes, $attributes);
  568. return $this;
  569. }
  570. /**
  571. * Set column title default attributes.
  572. *
  573. * @param array $attributes
  574. * @return $this
  575. */
  576. public function setDefaultHeaderAttribute(array $attributes)
  577. {
  578. foreach ($attributes as $key => $value) {
  579. if (isset($this->titleHtmlAttributes[$key])) {
  580. continue;
  581. }
  582. $this->titleHtmlAttributes[$key] = $value;
  583. }
  584. return $this;
  585. }
  586. /**
  587. * @return string
  588. */
  589. public function formatTitleAttributes()
  590. {
  591. $attrArr = [];
  592. foreach ($this->titleHtmlAttributes as $name => $val) {
  593. $attrArr[] = "$name=\"$val\"";
  594. }
  595. return implode(' ', $attrArr);
  596. }
  597. /**
  598. * Passes through all unknown calls to builtin displayer or supported displayer.
  599. *
  600. * Allow fluent calls on the Column object.
  601. *
  602. * @param string $method
  603. * @param array $arguments
  604. *
  605. * @return $this
  606. */
  607. public function __call($method, $arguments)
  608. {
  609. return $this->resolveDisplayer($method, $arguments);
  610. }
  611. }