Column.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. $label = $label ?: admin_trans_field($this->name);
  365. return str_replace(['.', '_'], ' ', $label);
  366. }
  367. /**
  368. * Get label of the column.
  369. *
  370. * @return mixed
  371. */
  372. public function getLabel()
  373. {
  374. return $this->label;
  375. }
  376. /**
  377. * @param string $label
  378. *
  379. * @return $this
  380. */
  381. public function setLabel($label)
  382. {
  383. $this->label = $label;
  384. return $this;
  385. }
  386. /**
  387. * Add a display callback.
  388. *
  389. * @param \Closure|string $callback
  390. * @param array $params
  391. *
  392. * @return $this
  393. */
  394. public function display($callback, ...$params)
  395. {
  396. $this->displayCallbacks[] = [&$callback, &$params];
  397. return $this;
  398. }
  399. /**
  400. * If has display callbacks.
  401. *
  402. * @return bool
  403. */
  404. public function hasDisplayCallbacks()
  405. {
  406. return ! empty($this->displayCallbacks);
  407. }
  408. /**
  409. * @param array $callbacks
  410. *
  411. * @return void
  412. */
  413. public function setDisplayCallbacks(array $callbacks)
  414. {
  415. $this->displayCallbacks = $callbacks;
  416. }
  417. /**
  418. * @return \Closure[]
  419. */
  420. public function getDisplayCallbacks()
  421. {
  422. return $this->displayCallbacks;
  423. }
  424. /**
  425. * Call all of the "display" callbacks column.
  426. *
  427. * @param mixed $value
  428. *
  429. * @return mixed
  430. */
  431. protected function callDisplayCallbacks($value)
  432. {
  433. foreach ($this->displayCallbacks as $callback) {
  434. [$callback, $params] = $callback;
  435. if (! $callback instanceof \Closure) {
  436. $value = $callback;
  437. continue;
  438. }
  439. $previous = $value;
  440. $callback = $this->bindOriginalRowModel($callback);
  441. $value = $callback($value, $this, ...$params);
  442. if (
  443. $value instanceof static
  444. && ($last = array_pop($this->displayCallbacks))
  445. ) {
  446. [$last, $params] = $last;
  447. $last = $this->bindOriginalRowModel($last);
  448. $value = call_user_func($last, $previous, $this, ...$params);
  449. }
  450. }
  451. return $value;
  452. }
  453. /**
  454. * Set original grid data to column.
  455. *
  456. * @param Closure $callback
  457. *
  458. * @return Closure
  459. */
  460. protected function bindOriginalRowModel(Closure $callback)
  461. {
  462. return $callback->bindTo($this->getOriginalModel());
  463. }
  464. /**
  465. * Fill all data to every column.
  466. *
  467. * @param array $data
  468. */
  469. public function fill(array &$data)
  470. {
  471. $i = 0;
  472. foreach ($data as $key => &$row) {
  473. $i++;
  474. if (! isset($row['#'])) {
  475. $row['#'] = $i;
  476. }
  477. $this->original = $value = Arr::get($row, $this->name);
  478. $this->value = $value = $this->htmlEntityEncode($value);
  479. $this->setOriginalModel(static::$originalGridModels[$key]);
  480. $this->processConditions();
  481. Arr::set($row, $this->name, $value);
  482. if ($this->hasDisplayCallbacks()) {
  483. $value = $this->callDisplayCallbacks($this->original);
  484. Arr::set($row, $this->name, $value);
  485. }
  486. }
  487. $this->value = $value ?? null;
  488. }
  489. /**
  490. * @return void
  491. */
  492. protected function processConditions()
  493. {
  494. foreach ($this->conditions as $condition) {
  495. $condition->reset();
  496. }
  497. foreach ($this->conditions as $condition) {
  498. $condition->process();
  499. }
  500. }
  501. /**
  502. * Convert characters to HTML entities recursively.
  503. *
  504. * @param array|string $item
  505. *
  506. * @return mixed
  507. */
  508. protected function htmlEntityEncode($item)
  509. {
  510. return Helper::htmlEntityEncode($item);
  511. }
  512. /**
  513. * Determine if this column is currently sorted.
  514. *
  515. * @return bool
  516. */
  517. protected function isSorted()
  518. {
  519. $this->sort = app('request')->get($this->grid->model()->getSortName());
  520. if (empty($this->sort)) {
  521. return false;
  522. }
  523. return isset($this->sort['column']) && $this->sort['column'] == $this->name;
  524. }
  525. /**
  526. * Find a displayer to display column.
  527. *
  528. * @param string $abstract
  529. * @param array $arguments
  530. *
  531. * @return Column
  532. */
  533. protected function resolveDisplayer($abstract, $arguments)
  534. {
  535. if (isset(static::$displayers[$abstract])) {
  536. return $this->callBuiltinDisplayer(static::$displayers[$abstract], $arguments);
  537. }
  538. return $this->callSupportDisplayer($abstract, $arguments);
  539. }
  540. /**
  541. * Call Illuminate/Support displayer.
  542. *
  543. * @param string $abstract
  544. * @param array $arguments
  545. *
  546. * @return Column
  547. */
  548. protected function callSupportDisplayer($abstract, $arguments)
  549. {
  550. return $this->display(function ($value) use ($abstract, $arguments) {
  551. if (is_array($value) || $value instanceof Arrayable) {
  552. return call_user_func_array([collect($value), $abstract], $arguments);
  553. }
  554. if (is_string($value)) {
  555. return call_user_func_array([Str::class, $abstract], array_merge([$value], $arguments));
  556. }
  557. return $value;
  558. });
  559. }
  560. /**
  561. * Call Builtin displayer.
  562. *
  563. * @param string $abstract
  564. * @param array $arguments
  565. *
  566. * @return Column
  567. */
  568. protected function callBuiltinDisplayer($abstract, $arguments)
  569. {
  570. if ($abstract instanceof Closure) {
  571. return $this->display(function ($value) use ($abstract, $arguments) {
  572. return $abstract->call($this, ...array_merge([$value], $arguments));
  573. });
  574. }
  575. if (is_subclass_of($abstract, AbstractDisplayer::class)) {
  576. $grid = $this->grid;
  577. $column = $this;
  578. return $this->display(function ($value) use ($abstract, $grid, $column, $arguments) {
  579. /** @var AbstractDisplayer $displayer */
  580. $displayer = new $abstract($value, $grid, $column, $this);
  581. return $displayer->display(...$arguments);
  582. });
  583. }
  584. return $this;
  585. }
  586. /**
  587. * Set column title attributes.
  588. *
  589. * @param array $attributes
  590. *
  591. * @return $this
  592. */
  593. public function setHeaderAttributes(array $attributes = [])
  594. {
  595. $this->titleHtmlAttributes = array_merge($this->titleHtmlAttributes, $attributes);
  596. return $this;
  597. }
  598. /**
  599. * Set column title default attributes.
  600. *
  601. * @param array $attributes
  602. *
  603. * @return $this
  604. */
  605. public function setDefaultHeaderAttribute(array $attributes)
  606. {
  607. foreach ($attributes as $key => $value) {
  608. if (isset($this->titleHtmlAttributes[$key])) {
  609. continue;
  610. }
  611. $this->titleHtmlAttributes[$key] = $value;
  612. }
  613. return $this;
  614. }
  615. /**
  616. * @return string
  617. */
  618. public function formatTitleAttributes()
  619. {
  620. $attrArr = [];
  621. foreach ($this->titleHtmlAttributes as $name => $val) {
  622. $attrArr[] = "$name=\"$val\"";
  623. }
  624. return implode(' ', $attrArr);
  625. }
  626. /**
  627. * @param mixed $value
  628. * @param callable $callback
  629. *
  630. * @return $this|mixed
  631. */
  632. public function when($value, $callback)
  633. {
  634. if ($value) {
  635. return $callback($this, $value) ?: $this;
  636. }
  637. return $this;
  638. }
  639. /**
  640. * Passes through all unknown calls to builtin displayer or supported displayer.
  641. *
  642. * Allow fluent calls on the Column object.
  643. *
  644. * @param string $method
  645. * @param array $arguments
  646. *
  647. * @return $this
  648. */
  649. public function __call($method, $arguments)
  650. {
  651. if (
  652. ! isset(static::$displayers[$method])
  653. && static::hasMacro($method)
  654. ) {
  655. return $this->__macroCall($method, $arguments);
  656. }
  657. return $this->resolveDisplayer($method, $arguments);
  658. }
  659. }