FeatureSortController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Models\FeatureSort;
  4. use App\Http\Controllers\Controller;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Layout\Content;
  9. use Encore\Admin\Show;
  10. class FeatureSortController extends Controller
  11. {
  12. use HasResourceActions;
  13. /**
  14. * Index interface.
  15. *
  16. * @param Content $content
  17. * @return Content
  18. */
  19. public function index(Content $content)
  20. {
  21. if (request()->has('_expand_')) {
  22. $FeatureSort=FeatureSort::find(request()->get('_expand_'));
  23. if (!$FeatureSort || $FeatureSort->children->isEmpty()) {
  24. $data['has_children'] =0;
  25. return response()->json($data);
  26. }
  27. $data['has_children'] =1;
  28. $data['html']=view(
  29. 'admin.grid.policy_category_expand',
  30. [
  31. 'parent_group'=>implode(" ", explode(",", request()->get('parent_group'))),
  32. 'key'=>request()->get('_expand_'),
  33. 'list'=>$FeatureSort->children,
  34. 'level'=>request()->get('level')+1,
  35. ]
  36. )->render();
  37. return response()->json($data);
  38. }
  39. return $content
  40. ->header('行业分类')
  41. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  42. ->body($this->grid());
  43. }
  44. /**
  45. * Show interface.
  46. *
  47. * @param mixed $id
  48. * @param Content $content
  49. * @return Content
  50. */
  51. public function show($id, Content $content)
  52. {
  53. return $content
  54. ->header('行业分类')
  55. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  56. ->body($this->detail($id));
  57. }
  58. /**
  59. * Edit interface.
  60. *
  61. * @param mixed $id
  62. * @param Content $content
  63. * @return Content
  64. */
  65. public function edit($id, Content $content)
  66. {
  67. return $content
  68. ->header('行业分类')
  69. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  70. ->body($this->editForm()->edit($id));
  71. }
  72. /**
  73. * Create interface.
  74. *
  75. * @param Content $content
  76. * @return Content
  77. */
  78. public function create(Content $content)
  79. {
  80. return $content
  81. ->header('行业分类')
  82. ->description('分类信息谨慎修改,如需修改请提前咨询系统客服。')
  83. ->body($this->form());
  84. }
  85. /**
  86. * Make a grid builder.
  87. *
  88. * @return Grid
  89. */
  90. protected function grid()
  91. {
  92. $grid = new Grid(new FeatureSort);
  93. $grid->model()->where('parent_id',0)->orderBy('category_order', 'desc');
  94. $grid->id('id');
  95. $grid->categoryname('名称')->ajaxExpand();
  96. $grid->admin_set('类型')->display(function ($admin_set) {
  97. if ($admin_set == 1) {
  98. return '系统分类';
  99. }else{
  100. return '自定义分类 ';
  101. }
  102. });
  103. $grid->category_order('排序');
  104. $grid->disableCreateButton(false);
  105. $grid->actions(function ($actions) {
  106. $actions->disableEdit(false);
  107. $actions->disableDelete(false);
  108. });
  109. $grid->filter(function ($filter) {
  110. $filter->like('categoryname', '名称');
  111. });
  112. return $grid;
  113. }
  114. /**
  115. * Make a show builder.
  116. *
  117. * @param mixed $id
  118. * @return Show
  119. */
  120. protected function detail($id)
  121. {
  122. $show = new Show(FeatureSort::findOrFail($id));
  123. $show->id('ID');
  124. $show->created_at('Created at');
  125. $show->updated_at('Updated at');
  126. return $show;
  127. }
  128. /**
  129. * Make a form builder.
  130. *
  131. * @return Form
  132. */
  133. protected function form()
  134. {
  135. $form = new Form(new FeatureSort);
  136. $array = $this->featureSortArr(FeatureSort::all()->toArray(),0);
  137. $array = array_pluck($array,'categoryname','id');
  138. $option = ['0'=>'顶级'] +$array;
  139. $form->select('parent_id', '所属上级')->options($option)->setMustMark();
  140. $form->text('categoryname', '名称');
  141. $form->text('title', 'Title');
  142. $form->text('description', 'Description');
  143. $form->number('category_order', '排序')->default(0)->min(0);
  144. $form->hidden('admin_set')->value(0);
  145. return $form;
  146. }
  147. protected function editForm()
  148. {
  149. $form = new Form(new FeatureSort);
  150. $array = $this->featureSortArr(FeatureSort::all()->toArray(),0);
  151. $array = array_pluck($array,'categoryname','id');
  152. $option = ['0'=>'顶级'] +$array;
  153. $form->select('parent_id', '所属上级')->options($option)->setMustMark();
  154. $form->text('categoryname', '名称');
  155. $form->text('title', 'Title');
  156. $form->text('description', 'Description');
  157. $form->number('category_order', '排序')->default(0)->min(0);
  158. return $form;
  159. }
  160. public function update($id)
  161. {
  162. return $this->editForm()->update($id);
  163. }
  164. //递归查询
  165. protected function featureSortArr($data,$pid,$level=''){
  166. static $arr=array();
  167. foreach($data as $k => $v){
  168. //如果该分类pid=0
  169. if($v['parent_id']==$pid){
  170. $v['categoryname']= $level.$v['categoryname'];
  171. $arr[]=$v;
  172. self::featureSortArr($data,$v['id'],$level.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
  173. }
  174. }
  175. return $arr;
  176. }
  177. public function destroy($id)
  178. {
  179. \DB::beginTransaction();
  180. try {
  181. $res = FeatureSort::all()->toArray();
  182. $list = $this->featureSortArr($res,$id);
  183. $ids = array_pluck($list,'id');
  184. FeatureSort::where('id',$id)->delete();
  185. FeatureSort::whereIn('id',$ids)->delete();
  186. $data = [
  187. 'status' => true,
  188. 'message' => '删除成功!',
  189. ];
  190. \DB::commit();
  191. return response()->json($data);
  192. } catch (\Exception $e) {
  193. \DB::rollback();
  194. $data = [
  195. 'status' => false,
  196. 'message' => '删除失败!',
  197. ];
  198. return response()->json($data);
  199. }
  200. }
  201. }