Column.php 18 KB

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