DistAdminDistributorController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\DistAdminDistributor;
  4. use App\Admin\Repositories\DistAppearance;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. use Dcat\Admin\Layout\Content;
  10. use Dcat\Admin\Admin;
  11. class DistAdminDistributorController extends AdminController
  12. {
  13. /**
  14. * page index
  15. */
  16. public function index(Content $content)
  17. {
  18. return $content
  19. ->header('Distro Management')
  20. ->description('all')
  21. ->breadcrumb(['text'=>'list','url'=>''])
  22. ->body($this->grid());
  23. }
  24. /**
  25. * Make a grid builder.
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. return Grid::make(DistAdminDistributor::with(['appearance']), function (Grid $grid) {
  32. $grid->column('id')->sortable();
  33. $grid->column('company_name','Company Name');
  34. $grid->column('site_name');
  35. $grid->column('level_domain');
  36. $grid->column('country');
  37. $grid->column('contact_number');
  38. $grid->column('service_hotline');
  39. $grid->column('appearance.title' ,'appearance');
  40. $grid->column('enabled')->switch();
  41. $grid->column('created_at');
  42. $grid->column('updated_at')->sortable();
  43. // 过滤
  44. $grid->filter(function (Grid\Filter $filter) {
  45. $filter->panel();
  46. $filter->expand();
  47. $filter->equal('company_name', 'company name')->width(2);
  48. $filter->equal('site_name')->width(2);
  49. $filter->equal('level_domain')->width(2);
  50. $filter->equal('enabled', 'enabled')->select(config('dictionary.enabled'))->width(2);
  51. });
  52. // 排序
  53. $grid->model()->orderBy("id",'desc');
  54. //按钮
  55. $grid->showQuickEditButton();
  56. $grid->enableDialogCreate();
  57. $grid->disableEditButton();
  58. $grid->disableDeleteButton();
  59. $grid->disableBatchDelete();
  60. });
  61. }
  62. /**
  63. * Make a show builder.
  64. *
  65. * @param mixed $id
  66. *
  67. * @return Show
  68. */
  69. protected function detail($id)
  70. {
  71. return Show::make($id, DistAdminDistributor::with(['appearance']), function (Show $show) {
  72. $show->field('id');
  73. $show->field('company_name','Company Name');
  74. $show->field('company_address','Company Address');
  75. $show->field('site_name');
  76. $show->field('level_domain');
  77. $show->field('appearance.title' ,'appearance');
  78. $show->field('country');
  79. $show->field('contact_number');
  80. $show->field('service_hotline');
  81. $show->field('whats_app');
  82. $show->field('facebook');
  83. $show->field('instagram');
  84. $show->field('youtube');
  85. $show->field('linkedin');
  86. $show->field('tiktok');
  87. $show->field('remark');
  88. $show->field('enabled')->using(config('dictionary.enabled'));
  89. $show->field('created_at');
  90. $show->field('updated_at');
  91. // 按钮
  92. $show->disableDeleteButton();
  93. });
  94. }
  95. /**
  96. * Make a form builder.
  97. *
  98. * @return Form
  99. */
  100. protected function form()
  101. {
  102. return Form::make(new DistAdminDistributor(), function (Form $form) {
  103. $form->display('id');
  104. $form->text('company_name', 'Company Name')->required();
  105. $form->text('company_address','Company Address');
  106. $form->text('site_name')->required();
  107. $form->text('level_domain')->required();
  108. $form->select('appearance_id','select appearance')->options(DistAppearance::selectOptions())->required();
  109. $form->text('country')->required();
  110. $form->text('contact_number');
  111. $form->text('service_hotline');
  112. $form->text('whats_app');
  113. $form->text('facebook');
  114. $form->text('instagram');
  115. $form->text('youtube');
  116. $form->text('linkedin');
  117. $form->text('tiktok');
  118. $form->textarea('remark');
  119. $form->switch('enabled')->default(1);
  120. //保存前回调
  121. $form->saving(function (Form $form) {
  122. if (!$form->isCreating()) {
  123. //如果appearance_id有变化,则更新模版与变量
  124. if ($form->model()->appearance_id != $form->input('appearance_id')) {
  125. $id = $form->model()->id;
  126. DistAppearance::switchTheme($form->input('appearance_id'), $id);
  127. }
  128. }
  129. });
  130. //保存后回调
  131. $form->saved(function (Form $form, $result) {
  132. if ($form->isCreating()) {
  133. $newId = $form->getKey();
  134. $distProductCategory = new DistProductCategory();
  135. $distProductCategory->create([
  136. 'parent_id' => 0,
  137. 'name' => 'Default Category',
  138. 'order' => 0,
  139. 'enabled' => 1,
  140. 'dist_id'=>$newId,
  141. ]);
  142. //更新模版与变量
  143. DistAppearance::switchTheme($form->input('appearance_id'), $newId);
  144. }
  145. });
  146. });
  147. }
  148. }