UserController.php 5.5 KB

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