UserController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Tests\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Dcat\Admin\Controllers\HasResourceActions;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Show;
  9. use Tests\Models\Tag;
  10. use Tests\Repositories\User;
  11. class UserController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @return Content
  18. */
  19. public function index(Content $content)
  20. {
  21. $content->header('All users');
  22. $content->description('description');
  23. return $content->body($this->grid());
  24. }
  25. /**
  26. * Edit interface.
  27. *
  28. * @param $id
  29. *
  30. * @return Content
  31. */
  32. public function edit(Content $content, $id)
  33. {
  34. $content->header('Edit user');
  35. $content->description('description');
  36. $content->body($this->form()->edit($id));
  37. return $content;
  38. }
  39. /**
  40. * Create interface.
  41. *
  42. * @return Content
  43. */
  44. public function create(Content $content)
  45. {
  46. $content->header('Create user');
  47. return $content->body($this->form());
  48. }
  49. /**
  50. * Show interface.
  51. *
  52. * @param mixed $id
  53. * @param Content $content
  54. *
  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. *
  125. * @return Show
  126. */
  127. protected function detail($id)
  128. {
  129. $show = new Show(new User());
  130. $show->setId($id);
  131. $show->id('ID');
  132. $show->username();
  133. $show->email;
  134. $show->divider();
  135. $show->full_name();
  136. $show->field('profile.postcode');
  137. $show->tags->json();
  138. return $show;
  139. }
  140. /**
  141. * Make a form builder.
  142. *
  143. * @return Form
  144. */
  145. protected function form()
  146. {
  147. Form::extend('map', Form\Field\Map::class);
  148. Form::extend('editor', Form\Field\Editor::class);
  149. $form = new Form(new User());
  150. $form->disableDeleteButton();
  151. $form->display('id', 'ID');
  152. $form->text('username');
  153. $form->email('email')->rules('required');
  154. $form->mobile('mobile');
  155. $form->image('avatar')->help('上传头像', 'fa-image');
  156. $form->ignore(['password_confirmation']);
  157. $form->password('password')->rules('confirmed');
  158. $form->password('password_confirmation');
  159. $form->divider();
  160. $form->text('profile.first_name');
  161. $form->text('profile.last_name');
  162. $form->text('profile.postcode')->help('Please input your postcode');
  163. $form->textarea('profile.address')->rows(15);
  164. $form->map('profile.latitude', 'profile.longitude', 'Position');
  165. $form->color('profile.color');
  166. $form->datetime('profile.start_at');
  167. $form->datetime('profile.end_at');
  168. // $tags = Tag::all()->pluck('name', 'id');
  169. // print_r($tags);die;
  170. $form->multipleSelect('tags', 'Tags')->options(Tag::all()->pluck('name', 'id'))->customFormat(function ($value) {
  171. if (! $value) {
  172. return [];
  173. }
  174. return array_column($value, 'id');
  175. }); //->rules('max:10|min:3');
  176. $form->display('created_at', 'Created At');
  177. $form->display('updated_at', 'Updated At');
  178. $form->html('<a html-field>html...</a>');
  179. return $form;
  180. }
  181. }