UserController.php 5.3 KB

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