HasDisplayers.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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\Support\Helper;
  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) use (&$val) {
  105. if (is_array($v)) {
  106. array_unshift($v, $val);
  107. return $v;
  108. } elseif ($v instanceof Collection) {
  109. return $v->prepend($val);
  110. }
  111. return $val.$v;
  112. });
  113. }
  114. /**
  115. * @param string $val
  116. *
  117. * @return $this
  118. */
  119. public function append($val)
  120. {
  121. return $this->display(function ($v) use (&$val) {
  122. if (is_array($v)) {
  123. array_push($v, $val);
  124. return $v;
  125. } elseif ($v instanceof Collection) {
  126. return $v->push($val);
  127. }
  128. return $v.$val;
  129. });
  130. }
  131. /**
  132. * Split a string by string.
  133. *
  134. * @param string $d
  135. *
  136. * @return $this
  137. */
  138. public function explode(string $d = ',')
  139. {
  140. return $this->display(function ($v) use ($d) {
  141. if (is_array($v) || $v instanceof Arrayable) {
  142. return $v;
  143. }
  144. return $v ? explode($d, $v) : [];
  145. });
  146. }
  147. /**
  148. * Display the fields in the email format as gavatar.
  149. *
  150. * @param int $size
  151. *
  152. * @return $this
  153. */
  154. public function gravatar($size = 30)
  155. {
  156. return $this->display(function ($value) use ($size) {
  157. $src = sprintf(
  158. 'https://www.gravatar.com/avatar/%s?s=%d',
  159. md5(strtolower($value)),
  160. $size
  161. );
  162. return "<img src='$src' class='img img-circle'/>";
  163. });
  164. }
  165. /**
  166. * Limit the number of characters in a string, or the number of element in a array.
  167. *
  168. * @param int $limit
  169. * @param string $end
  170. *
  171. * @return $this
  172. */
  173. public function limit($limit = 100, $end = '...')
  174. {
  175. return $this->display(function ($value) use ($limit, $end) {
  176. if ($value !== null && ! is_scalar($value)) {
  177. $value = Helper::array($value);
  178. if (count($value) <= $limit) {
  179. return $value;
  180. }
  181. $value = array_slice($value, 0, $limit);
  182. array_push($value, $end);
  183. return $value;
  184. }
  185. if (mb_strlen($value, 'UTF-8') <= $limit) {
  186. return $value;
  187. }
  188. return mb_substr($value, 0, $limit).$end;
  189. });
  190. }
  191. /**
  192. * @return $this
  193. */
  194. public function emptyString()
  195. {
  196. return $this->display('');
  197. }
  198. /**
  199. * Show children of current node.
  200. *
  201. * @param bool $showAll
  202. * @param bool $sortable
  203. *
  204. * @return $this
  205. */
  206. public function tree(bool $showAll = false, bool $sortable = true)
  207. {
  208. $this->grid->model()->enableTree($showAll, $sortable);
  209. $this->grid->fetching(function () use ($showAll) {
  210. if ($this->grid->model()->getParentIdFromRequest()) {
  211. $this->grid->disableFilter();
  212. $this->grid->disableToolbar();
  213. if ($showAll) {
  214. $this->grid->disablePagination();
  215. }
  216. }
  217. });
  218. return $this->displayUsing(Grid\Displayers\Tree::class);
  219. }
  220. }