GridHelper.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace UCore\DcatAdmin;
  3. use UCore\DcatAdmin\Grid\Views\GridHeader;
  4. use UCore\DcatAdmin\Traits\AdminId;
  5. use UCore\DcatAdmin\Traits\UserID;
  6. use Carbon\CarbonInterface;
  7. use Carbon\Traits\Converter;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Widgets\Table;
  10. use Illuminate\Support\Arr;
  11. class GridHelper
  12. {
  13. use \UCore\DcatAdmin\Traits\Options, UserID, AdminId;
  14. /**
  15. * @var Grid
  16. */
  17. public $grid;
  18. /**
  19. * @var AdminController
  20. */
  21. public $controller;
  22. public function __construct($grid, $controller)
  23. {
  24. $this->grid = $grid;
  25. $grid->filter(function (Grid\Filter $filter) {
  26. // $filter->panel();
  27. // $filter->expand();
  28. });
  29. $this->controller = $controller;
  30. if (request('in_iframe')) {
  31. $grid->model()->setConstraints([ 'in_iframe' => 1 ]);
  32. }
  33. GridHeader::gridHeader($grid);
  34. }
  35. public function columnId()
  36. {
  37. return $this->grid->column('id', 'ID')->sortable();
  38. }
  39. /**
  40. * 创建时间
  41. *
  42. * @return Grid\Column
  43. */
  44. public function columnCreatedAt()
  45. {
  46. return $this->grid->column('created_at', '创建时间')->sortable();
  47. }
  48. /**
  49. * 更新时间
  50. * @return Grid\Column
  51. */
  52. public function columnUpdatedAt()
  53. {
  54. return $this->grid->column('updated_at', '更新时间')->sortable();
  55. }
  56. public function columnIdDesc()
  57. {
  58. $this->grid->column('id', 'ID')->sortable();
  59. $this->grid->model()->orderByDesc('id');
  60. return $this;
  61. }
  62. public function disableAll()
  63. {
  64. $this->grid->disableCreateButton();
  65. $this->grid->disableBatchActions();
  66. $this->grid->disableDeleteButton();
  67. $this->grid->disableEditButton();
  68. $this->grid->disableQuickEditButton();
  69. $this->grid->disableViewButton();
  70. // $this->grid->disableBatchActions();
  71. return $this;
  72. }
  73. /**
  74. *
  75. * @param $field
  76. * @param $view
  77. * @return $this
  78. */
  79. public function columnView($field, $view, $label = '')
  80. {
  81. $this->grid->column($field, $label)->view($view);
  82. return $this;
  83. }
  84. public function columnImg($field, $width = 100, $hei = 100, $label = '')
  85. {
  86. $this->grid->column($field, $label)->display(function ($p2) {
  87. // dump($p2);
  88. return Img::img2imgurl($p2);
  89. })->image('', $width, $hei);
  90. }
  91. public function columnExpand($field, $fields = [], $label = '')
  92. {
  93. $controller = $this->controller;
  94. $this->grid->column($field, $label)->expand(function (Grid\Displayers\Expand $exped) use ($fields, $controller) {
  95. // dump(func_get_args());
  96. $headers = [
  97. 'admin.kv.name',
  98. 'admin.kv.value',
  99. ];
  100. $data = [];
  101. // $exped->row;
  102. // dd($exped);
  103. foreach ($fields as $f) {
  104. $data[] = [
  105. $controller->_label($f), $exped->row->$f
  106. ];
  107. }
  108. return Table::make($headers, $data);
  109. });
  110. }
  111. /**
  112. * 翻译 字段内容
  113. *
  114. * @param $field
  115. * @return void
  116. */
  117. public function columnTranslation($field, $label = '')
  118. {
  119. $this->grid->column($field, $label)->display(function ($value, $column, $controller) {
  120. // dd(func_get_args());
  121. /**
  122. * @var AdminController $controller
  123. */
  124. return $controller->_translation('key.' . $value);
  125. }, $this->controller);;
  126. }
  127. /**
  128. * 展示的时候除以100
  129. *
  130. * @param string $field
  131. * @return void
  132. */
  133. public function columnc100($field, $label = '')
  134. {
  135. $this->grid->column($field, $label)->display(function ($p2) {
  136. return $p2 / 100;
  137. });
  138. }
  139. public function columnc1000($field, $label = '')
  140. {
  141. return $this->grid->column($field, $label)->display(function ($p2) {
  142. return $p2 / 1000;
  143. });
  144. }
  145. /**
  146. * 展示 时间戳
  147. *
  148. * @param string $field
  149. * @return void
  150. */
  151. public function columnAt($field, $label = '')
  152. {
  153. $this->grid->column($field, $label)->display(function ($p2) {
  154. return date(\DateTime::W3C, $p2);
  155. });
  156. }
  157. public function columnAtd($field, $label = '')
  158. {
  159. $this->grid->column($field, $label)->display(function (/* \Carbon\Carbon*/ $p2) {
  160. if (!$p2) {
  161. return null;
  162. }
  163. // dd($p2);
  164. return $p2->format(CarbonInterface::DEFAULT_TO_STRING_FORMAT);
  165. })->width(105);
  166. }
  167. /**
  168. * 使用枚举展示
  169. *
  170. * @param $field
  171. * @param $enmu
  172. * @return $this
  173. */
  174. public function columnUseing($field, $enmu, $label = '')
  175. {
  176. $this->grid->column($field, $label)->use($enmu);
  177. return $this;
  178. }
  179. /**
  180. * 使用枚举展示
  181. *
  182. * @param $field
  183. * @param $enmu
  184. * @param $label
  185. * @return Grid\Column
  186. */
  187. public function columnUseingEnmu($field, $enmuClass, $label = ''): Grid\Column
  188. {
  189. $option= $enmuClass::getValueDescription();
  190. $default = '';
  191. // dump($option);
  192. return $this->grid->column($field, $label)->display(function ($value) use ($option, $default) {
  193. if (is_null($value)) {
  194. return $default;
  195. }
  196. if ($value instanceof \UnitEnum) {
  197. $value = $value->value();
  198. }
  199. return Arr::get($option, $value, $default);
  200. });
  201. }
  202. public function columnUsingVk($field, $enmu, $label = '')
  203. {
  204. $res = array_flip($enmu);
  205. // dd($res);
  206. return $this->grid->column($field, $label)->using($res);
  207. }
  208. public function columnUsingkv($field, $enmu, $label = '')
  209. {
  210. // dump($enmu);
  211. return $this->grid->column($field, $label)->using($enmu);
  212. }
  213. /**
  214. * 使用枚举展示.new
  215. *
  216. * @param $name
  217. * @param array $keys
  218. * @return Grid\Column
  219. */
  220. public function fieldUseing($name, array $keys, $label = '')
  221. {
  222. return $this->grid->column($name, $label)->using($this->useing($name, $keys));
  223. }
  224. /**
  225. * 使用lab
  226. *
  227. * @param $name
  228. * @param array $keys
  229. * @return Grid\Column
  230. */
  231. public function fieldLable1($name, $label = '')
  232. {
  233. $c = $this->controller;
  234. return $this->grid->column($name, $label)->display(function ($value, Grid\Column $column) use ($c) {
  235. // $list = explode(',',$c)
  236. $list = [];
  237. foreach ($value as $item) {
  238. // dd($this);
  239. $list[] = $c->_option($column->getName() . '-' . $item);
  240. }
  241. return $list;
  242. })->label();
  243. }
  244. /**
  245. * 模型字段展示-cats
  246. *
  247. * @param $name
  248. * @param $default
  249. * @param $label
  250. * @return Grid\Column
  251. * @throws \Exception
  252. */
  253. public function columnModelCats($name, $default = null, $label = '', $edit = false)
  254. {
  255. $cates = $this->grid->model()->repository()->model()->getCasts();
  256. $enmu = $cates[$name] ?? "";
  257. if ($enmu === '') {
  258. throw new \Exception("$name is not a model casts");
  259. }
  260. $values = $enmu::getValueDescription();
  261. if ($edit) {
  262. $res = $this->grid->column($name)->display(function ($value) use ($values, $default) {
  263. if (is_null($value)) {
  264. return $default;
  265. }
  266. if ($value instanceof \UnitEnum) {
  267. $value = $value->value();
  268. }
  269. return $value;
  270. })->radio($values);
  271. } else {
  272. $res = $this->grid->column($name, $label)->display(function ($value) use ($values, $default) {
  273. if (is_null($value)) {
  274. return $default;
  275. }
  276. if ($value instanceof \UnitEnum) {
  277. $value = $value->value();
  278. }
  279. return Arr::get($values, $value, $default);
  280. });
  281. }
  282. return $res;
  283. }
  284. }