GridHelper.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 string $field 字段名
  171. * @param string $label 标签名
  172. * @return Grid\Column
  173. */
  174. public function columnDateTime($field, $label = '')
  175. {
  176. return $this->grid->column($field, $label)->display(function ($value) {
  177. // 检查空值(但不包括0,因为0是有效的时间戳)
  178. if (is_null($value) || $value === '') {
  179. return '-';
  180. }
  181. // 如果是时间戳,转换为日期时间字符串
  182. if (is_numeric($value)) {
  183. return date('Y-m-d H:i:s', $value);
  184. }
  185. // 如果是Carbon实例或DateTime对象
  186. if ($value instanceof \Carbon\Carbon || $value instanceof \DateTime) {
  187. return $value->format('Y-m-d H:i:s');
  188. }
  189. // 如果是字符串,尝试转换为标准格式
  190. if (is_string($value)) {
  191. try {
  192. $date = new \DateTime($value);
  193. return $date->format('Y-m-d H:i:s');
  194. } catch (\Exception $e) {
  195. return $value; // 如果转换失败,返回原值
  196. }
  197. }
  198. return $value;
  199. })->sortable();
  200. }
  201. /**
  202. * 使用枚举展示
  203. *
  204. * @param $field
  205. * @param $enmu
  206. * @return $this
  207. */
  208. public function columnUseing($field, $enmu, $label = '')
  209. {
  210. $this->grid->column($field, $label)->use($enmu);
  211. return $this;
  212. }
  213. /**
  214. * 使用枚举展示
  215. *
  216. * @param $field
  217. * @param $enmu
  218. * @param $label
  219. * @return Grid\Column
  220. */
  221. public function columnUseingEnmu($field, $enmuClass, $label = ''): Grid\Column
  222. {
  223. $option= $enmuClass::getValueDescription();
  224. $default = '';
  225. // dump($option);
  226. return $this->grid->column($field, $label)->display(function ($value) use ($option, $default) {
  227. if (is_null($value)) {
  228. return $default;
  229. }
  230. if ($value instanceof \UnitEnum) {
  231. $value = $value->value();
  232. }
  233. return Arr::get($option, $value, $default);
  234. });
  235. }
  236. public function columnUsingVk($field, $enmu, $label = '')
  237. {
  238. $res = array_flip($enmu);
  239. // dd($res);
  240. return $this->grid->column($field, $label)->using($res);
  241. }
  242. public function columnUsingkv($field, $enmu, $label = '')
  243. {
  244. // dump($enmu);
  245. return $this->grid->column($field, $label)->using($enmu);
  246. }
  247. /**
  248. * 使用枚举展示.new
  249. *
  250. * @param $name
  251. * @param array $keys
  252. * @return Grid\Column
  253. */
  254. public function fieldUseing($name, array $keys, $label = '')
  255. {
  256. return $this->grid->column($name, $label)->using($this->useing($name, $keys));
  257. }
  258. /**
  259. * 使用lab
  260. *
  261. * @param $name
  262. * @param array $keys
  263. * @return Grid\Column
  264. */
  265. public function fieldLable1($name, $label = '')
  266. {
  267. $c = $this->controller;
  268. return $this->grid->column($name, $label)->display(function ($value, Grid\Column $column) use ($c) {
  269. // $list = explode(',',$c)
  270. $list = [];
  271. foreach ($value as $item) {
  272. // dd($this);
  273. $list[] = $c->_option($column->getName() . '-' . $item);
  274. }
  275. return $list;
  276. })->label();
  277. }
  278. /**
  279. * 模型字段展示-cats
  280. *
  281. * @param $name
  282. * @param $default
  283. * @param $label
  284. * @return Grid\Column
  285. * @throws \Exception
  286. */
  287. public function columnModelCats($name, $default = null, $label = '', $edit = false)
  288. {
  289. $cates = $this->grid->model()->repository()->model()->getCasts();
  290. $enmu = $cates[$name] ?? "";
  291. if ($enmu === '') {
  292. throw new \Exception("$name is not a model casts");
  293. }
  294. $values = $enmu::getValueDescription();
  295. if ($edit) {
  296. $res = $this->grid->column($name)->display(function ($value) use ($values, $default) {
  297. if (is_null($value)) {
  298. return $default;
  299. }
  300. if ($value instanceof \UnitEnum) {
  301. $value = $value->value();
  302. }
  303. return $value;
  304. })->radio($values);
  305. } else {
  306. $res = $this->grid->column($name, $label)->display(function ($value) use ($values, $default) {
  307. if (is_null($value)) {
  308. return $default;
  309. }
  310. if ($value instanceof \UnitEnum) {
  311. $value = $value->value();
  312. }
  313. return Arr::get($values, $value, $default);
  314. });
  315. }
  316. return $res;
  317. }
  318. }