LinkCategoryController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\LinkCategory;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. class LinkCategoryController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @param Content $content
  18. * @return Content
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header('链接分类')
  24. ->description('通过友情链接分类可以在多个页面调用不同的友情链接!')
  25. ->body($this->grid());
  26. }
  27. /**
  28. * Show interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function show($id, Content $content)
  35. {
  36. return $content
  37. ->header('链接分类')
  38. ->description(' ')
  39. ->body($this->detail($id));
  40. }
  41. /**
  42. * Edit interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function edit($id, Content $content)
  49. {
  50. return $content
  51. ->header('链接分类')
  52. ->description(' ')
  53. ->body($this->form()->edit($id));
  54. }
  55. /**
  56. * Create interface.
  57. *
  58. * @param Content $content
  59. * @return Content
  60. */
  61. public function create(Content $content)
  62. {
  63. return $content
  64. ->header('链接分类')
  65. ->description(' ')
  66. ->body($this->form());
  67. }
  68. /**
  69. * Make a grid builder.
  70. *
  71. * @return Grid
  72. */
  73. protected function grid()
  74. {
  75. $grid = new Grid(new LinkCategory);
  76. $grid->category_name('分类名称');
  77. $grid->alias('调用名称');
  78. $grid->admin_set('类型')->display(function () {
  79. return $this->admin_set?'系统分类':'自定义分类';
  80. });
  81. $grid->created_at('添加时间');
  82. /*$grid->updated_at('更新时间');
  83. $grid->tools(function (Grid\Tools $tools) {
  84. $tools->disableRefreshButton();
  85. });*/
  86. $grid->disableFilter();
  87. $grid->actions(function ($actions) {
  88. if (Admin::user()->can('content_link_category_edit')) {
  89. $actions->disableEdit(false);
  90. }
  91. if (Admin::user()->can('content_link_category_delete') && $actions->row['admin_set']!=1) {
  92. $actions->disableDelete(false);
  93. }
  94. });
  95. //新增按钮
  96. if (Admin::user()->can('content_link_category_create')) {
  97. $grid->disableCreateButton(false);
  98. }
  99. //批量删除
  100. if (Admin::user()->can('content_link_category_delete')) {
  101. $grid->tools(function ($tools) {
  102. $tools->batch(function ($batch) {
  103. $batch->disableDelete(false);
  104. });
  105. });
  106. } else {
  107. $grid->disableRowSelector();
  108. }
  109. return $grid;
  110. }
  111. /**
  112. * Make a show builder.
  113. *
  114. * @param mixed $id
  115. * @return Show
  116. */
  117. protected function detail($id)
  118. {
  119. $show = new Show(LinkCategory::findOrFail($id));
  120. $show->id('ID');
  121. $show->category_name('分类名称');
  122. $show->alias('调用名称');
  123. $show->admin_set('系统分类')->as(function ($admin_set) {
  124. return $admin_set?'是':'否';
  125. });
  126. $show->created_at('添加时间');
  127. $show->updated_at('更新时间');
  128. /*$show->panel()
  129. ->tools(function ($tools) {
  130. $tools->disableEdit();
  131. $tools->disableDelete();
  132. });*/
  133. return $show;
  134. }
  135. /**
  136. * Make a form builder.
  137. *
  138. * @return Form
  139. */
  140. protected function form()
  141. {
  142. $form = new Form(new LinkCategory);
  143. $form->text('category_name', '分类名称')->rules('required|max:25', array('required'=>'分类名称不能为空。','max'=>'分类名称长度不能大于25。'))->setWidth(4)->setMustMark();
  144. $form->text('alias', '调用名称')->rules('required|max:25', array('required'=>'调用名称不能为空。','max'=>'调用名称长度不能大于25。'))->setWidth(4)->help('(自定义链接分类调用名不可以以“AIX_”开头)')->setMustMark();
  145. $form->footer(function ($footer) {
  146. $footer->disableViewCheck();
  147. $footer->disableEditingCheck();
  148. $footer->disableCreatingCheck();
  149. $footer->disableReset();
  150. });
  151. $form->tools(function (Form\Tools $tools) {
  152. $tools->disableDelete();
  153. $tools->disableView();
  154. });
  155. return $form;
  156. }
  157. public function destroy($id)
  158. {
  159. $id_arr = explode(',', $id);
  160. $admin_set_cates = LinkCategory::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
  161. foreach ($id_arr as $k => $v) {
  162. if (in_array($v, $admin_set_cates)) {
  163. unset($id_arr[$k]);
  164. }
  165. }
  166. $filter_id = implode(',', $id_arr);
  167. if ($this->form()->destroy($filter_id)) {
  168. $data = [
  169. 'status' => true,
  170. 'message' => trans('admin.delete_succeeded'),
  171. ];
  172. } else {
  173. $data = [
  174. 'status' => false,
  175. 'message' => trans('admin.delete_failed'),
  176. ];
  177. }
  178. return response()->json($data);
  179. }
  180. }