UserController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Tests\Controllers;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Http\Controllers\AdminController;
  6. use Dcat\Admin\Layout\Content;
  7. use Dcat\Admin\Show;
  8. use Tests\Models\Tag;
  9. use Tests\Repositories\User;
  10. class UserController extends AdminController
  11. {
  12. /**
  13. * Index interface.
  14. *
  15. * @return Content
  16. */
  17. public function index(Content $content)
  18. {
  19. $content->header('All users');
  20. $content->description('description');
  21. return $content->body($this->grid());
  22. }
  23. /**
  24. * Edit interface.
  25. *
  26. * @param $id
  27. *
  28. * @return Content
  29. */
  30. public function edit($id, Content $content)
  31. {
  32. $content->header('Edit user');
  33. $content->description('description');
  34. $content->body($this->form()->edit($id));
  35. return $content;
  36. }
  37. /**
  38. * Create interface.
  39. *
  40. * @return Content
  41. */
  42. public function create(Content $content)
  43. {
  44. $content->header('Create user');
  45. return $content->body($this->form());
  46. }
  47. /**
  48. * Show interface.
  49. *
  50. * @param mixed $id
  51. * @param Content $content
  52. *
  53. * @return Content
  54. */
  55. public function show($id, Content $content)
  56. {
  57. return $content
  58. ->header('User')
  59. ->description('Detail')
  60. ->body($this->detail($id));
  61. }
  62. /**
  63. * Make a grid builder.
  64. *
  65. * @return Grid
  66. */
  67. protected function grid()
  68. {
  69. $grid = new Grid(new User());
  70. $grid->model()->with(['tags', 'profile']);
  71. $grid->number();
  72. $grid->id('ID')->sortable();
  73. $grid->username();
  74. $grid->email();
  75. $grid->mobile();
  76. $grid->full_name;
  77. $grid->avatar()->display(function ($avatar) {
  78. return "<img src='{$avatar}' />";
  79. });
  80. $grid->column('profile.postcode', 'Post code');
  81. $grid->column('profile.address');
  82. $grid->column('profile.color');
  83. $grid->column('profile.start_at', '开始时间');
  84. $grid->column('profile.end_at', '结束时间');
  85. $grid->column('column1_not_in_table')->display(function () {
  86. return 'full name:'.$this->full_name;
  87. });
  88. $grid->column('column2_not_in_table')->display(function () {
  89. return $this->email.'#'.$this->profile['color'];
  90. });
  91. $grid->tags()->display(function ($tags) {
  92. $tags = collect($tags)->map(function ($tag) {
  93. return "<code>{$tag['name']}</code>";
  94. })->toArray();
  95. return implode('', $tags);
  96. });
  97. $grid->created_at();
  98. $grid->updated_at();
  99. $grid->quickSearch('profile.postcode', 'username');
  100. $grid->filter(function (Grid\Filter $filter) {
  101. $filter->equal('id');
  102. $filter->like('username');
  103. $filter->like('email');
  104. $filter->equal('profile.postcode')->select('api/placard-classify');
  105. $filter->between('profile.start_at')->datetime();
  106. $filter->between('profile.end_at')->datetime();
  107. });
  108. $grid->actions(function (Grid\Displayers\Actions $actions) {
  109. if ($actions->getKey() % 2 == 0) {
  110. $actions->append('<a href="/" class="btn btn-xs btn-danger">detail</a>');
  111. }
  112. });
  113. return $grid;
  114. }
  115. /**
  116. * Make a show builder.
  117. *
  118. * @param mixed $id
  119. *
  120. * @return Show
  121. */
  122. protected function detail($id)
  123. {
  124. return Show::make($id, new User(), function (Show $show) {
  125. $show->id('ID');
  126. $show->username();
  127. $show->email;
  128. $show->divider();
  129. $show->full_name();
  130. $show->field('profile.postcode');
  131. $show->tags->json();
  132. });
  133. }
  134. /**
  135. * Make a form builder.
  136. *
  137. * @return Form
  138. */
  139. protected function form()
  140. {
  141. Form::extend('map', Form\Field\Map::class);
  142. Form::extend('editor', Form\Field\Editor::class);
  143. $form = new Form(new User());
  144. $form->disableDeleteButton();
  145. $form->display('id', 'ID');
  146. $form->text('username');
  147. $form->email('email')->rules('required');
  148. $form->mobile('mobile');
  149. $form->image('avatar')->help('上传头像', 'fa-image');
  150. $form->ignore(['password_confirmation']);
  151. $form->password('password')->rules('confirmed');
  152. $form->password('password_confirmation');
  153. $form->divider();
  154. $form->text('profile.first_name');
  155. $form->text('profile.last_name');
  156. $form->text('profile.postcode')->help('Please input your postcode');
  157. $form->textarea('profile.address')->rows(15);
  158. //$form->map('profile.latitude', 'profile.longitude', 'Position');
  159. $form->text('profile.color');
  160. $form->datetime('profile.start_at');
  161. $form->datetime('profile.end_at');
  162. $form->multipleSelect('tags', 'Tags')->options(Tag::all()->pluck('name', 'id'))->customFormat(function ($value) {
  163. if (! $value) {
  164. return [];
  165. }
  166. return array_column($value, 'id');
  167. });
  168. $form->display('created_at', 'Created At');
  169. $form->display('updated_at', 'Updated At');
  170. $form->html('<a html-field>html...</a>');
  171. return $form;
  172. }
  173. }