Column.php 18 KB

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