HasDisplayers.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Dcat\Admin\Grid\Column;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Grid\Column;
  6. use Dcat\Admin\Grid\Displayers\AbstractDisplayer;
  7. use Dcat\Admin\Grid\RowAction;
  8. use Illuminate\Contracts\Support\Arrayable;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Collection;
  11. /**
  12. * @property Grid $grid
  13. */
  14. trait HasDisplayers
  15. {
  16. /**
  17. * Display using display abstract.
  18. *
  19. * @param string $abstract
  20. * @param array $arguments
  21. *
  22. * @return Column
  23. */
  24. public function displayUsing($abstract, $arguments = [])
  25. {
  26. $grid = $this->grid;
  27. $column = $this;
  28. return $this->display(function ($value) use ($grid, $column, $abstract, $arguments) {
  29. /** @var AbstractDisplayer $displayer */
  30. $displayer = new $abstract($value, $grid, $column, $this);
  31. return $displayer->display(...$arguments);
  32. });
  33. }
  34. /**
  35. * Display column using array value map.
  36. *
  37. * @param array $values
  38. * @param null $default
  39. *
  40. * @return $this
  41. */
  42. public function using(array $values, $default = null)
  43. {
  44. return $this->display(function ($value) use ($values, $default) {
  45. if (is_null($value)) {
  46. return $default;
  47. }
  48. return Arr::get($values, $value, $default);
  49. });
  50. }
  51. /**
  52. * @param string $color
  53. *
  54. * @return $this
  55. */
  56. public function bold($color = null)
  57. {
  58. $color = $color ?: Admin::color()->dark80();
  59. return $this->display(function ($value) use ($color) {
  60. if (! $value) {
  61. return $value;
  62. }
  63. return "<b style='color: {$color}'>$value</b>";
  64. });
  65. }
  66. /**
  67. * Display column with "long2ip".
  68. *
  69. * @param null $default
  70. *
  71. * @return $this
  72. */
  73. public function long2ip($default = null)
  74. {
  75. return $this->display(function ($value) use ($default) {
  76. if (! $value) {
  77. return $default;
  78. }
  79. return long2ip($value);
  80. });
  81. }
  82. /**
  83. * Render this column with the given view.
  84. *
  85. * @param string $view
  86. *
  87. * @return $this
  88. */
  89. public function view($view)
  90. {
  91. $name = $this->name;
  92. return $this->display(function ($value) use ($view, $name) {
  93. $model = $this;
  94. return view($view, compact('model', 'value', 'name'))->render();
  95. });
  96. }
  97. /**
  98. * @param string $val
  99. *
  100. * @return $this
  101. */
  102. public function prepend($val)
  103. {
  104. return $this->display(function ($v, $column) use (&$val) {
  105. if ($val instanceof \Closure) {
  106. $val = $val->call($this, $v, $column->getOriginal(), $column);
  107. }
  108. if (is_array($v)) {
  109. array_unshift($v, $val);
  110. return $v;
  111. } elseif ($v instanceof Collection) {
  112. return $v->prepend($val);
  113. }
  114. return $val.$v;
  115. });
  116. }
  117. /**
  118. * @param string $val
  119. *
  120. * @return $this
  121. */
  122. public function append($val)
  123. {
  124. return $this->display(function ($v, $column) use (&$val) {
  125. if ($val instanceof \Closure) {
  126. $val = $val->call($this, $v, $column->getOriginal(), $column);
  127. }
  128. if (is_array($v)) {
  129. array_push($v, $val);
  130. return $v;
  131. } elseif ($v instanceof Collection) {
  132. return $v->push($val);
  133. }
  134. return $v.$val;
  135. });
  136. }
  137. /**
  138. * Split a string by string.
  139. *
  140. * @param string $d
  141. *
  142. * @return $this
  143. */
  144. public function explode(string $d = ',')
  145. {
  146. return $this->display(function ($v) use ($d) {
  147. if (is_array($v) || $v instanceof Arrayable) {
  148. return $v;
  149. }
  150. return $v ? explode($d, $v) : [];
  151. });
  152. }
  153. /**
  154. * Display the fields in the email format as gavatar.
  155. *
  156. * @param int $size
  157. *
  158. * @return $this
  159. */
  160. public function gravatar($size = 30)
  161. {
  162. return $this->display(function ($value) use ($size) {
  163. $src = sprintf(
  164. 'https://www.gravatar.com/avatar/%s?s=%d',
  165. md5(strtolower($value)),
  166. $size
  167. );
  168. return "<img src='$src' class='img img-circle'/>";
  169. });
  170. }
  171. /**
  172. * Add a `dot` before column text.
  173. *
  174. * @param array $options
  175. * @param string $default
  176. *
  177. * @return $this
  178. */
  179. public function dot($options = [], $default = 'default')
  180. {
  181. return $this->prepend(function ($_, $original) use ($options, $default) {
  182. $style = is_null($original) ? $default : Arr::get((array) $options, $original, $default);
  183. $style = $style === 'default' ? 'dark70' : $style;
  184. $background = Admin::color()->get($style);
  185. return "<i class='fa fa-circle' style='font-size: 13px;color: {$background}'></i>&nbsp;&nbsp;";
  186. });
  187. }
  188. /**
  189. * @return $this
  190. */
  191. public function emptyString()
  192. {
  193. return $this->display('');
  194. }
  195. /**
  196. * Show children of current node.
  197. *
  198. * @param bool $showAll
  199. * @param bool $sortable
  200. *
  201. * @return $this
  202. */
  203. public function tree(bool $showAll = false, bool $sortable = true)
  204. {
  205. $this->grid->model()->enableTree($showAll, $sortable);
  206. $this->grid->fetching(function () use ($showAll) {
  207. if ($this->grid->model()->getParentIdFromRequest()) {
  208. $this->grid->disableFilter();
  209. $this->grid->disableToolbar();
  210. if ($showAll) {
  211. $this->grid->disablePagination();
  212. }
  213. }
  214. });
  215. return $this->displayUsing(Grid\Displayers\Tree::class);
  216. }
  217. /**
  218. * Display column using a grid row action.
  219. *
  220. * @param string $action
  221. *
  222. * @return $this
  223. */
  224. public function action($action)
  225. {
  226. if (! is_subclass_of($action, RowAction::class)) {
  227. throw new \InvalidArgumentException("Action class [$action] must be sub-class of [Dcat\Admin\Grid\RowAction]");
  228. }
  229. $grid = $this->grid;
  230. return $this->display(function ($_, $column) use ($action, $grid) {
  231. /** @var RowAction $action */
  232. $action = $action::make();
  233. return $action
  234. ->setGrid($grid)
  235. ->setColumn($column)
  236. ->setRow($this);
  237. });
  238. }
  239. }