Column.php 18 KB

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