Column.php 17 KB

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