ArticlePropertyController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\ArticleProperty;
  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 ArticlePropertyController 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 ArticleProperty);
  76. $grid->model()->orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC');
  77. $grid->category_name('属性名称');
  78. $grid->admin_set('系统属性')->display(function ($admin_set) {
  79. return $admin_set?"是":"否";
  80. });
  81. $grid->list_order('排序');
  82. $grid->created_at('添加时间');
  83. $grid->actions(function ($actions) {
  84. //行操作
  85. if (Admin::user()->can('content_article_property_edit')) {
  86. $actions->disableEdit(false);
  87. }
  88. if (Admin::user()->can('content_article_property_delete') && $actions->row['admin_set']!=1) {
  89. $actions->disableDelete(false);
  90. }
  91. });
  92. $grid->disableFilter();
  93. //新增按钮
  94. if (Admin::user()->can('content_article_property_create')) {
  95. $grid->disableCreateButton(false);
  96. }
  97. //批量删除
  98. if (Admin::user()->can('content_article_property_delete')) {
  99. $grid->tools(function ($tools) {
  100. $tools->batch(function ($batch) {
  101. $batch->disableDelete(false);
  102. });
  103. });
  104. } else {
  105. $grid->disableRowSelector();
  106. }
  107. return $grid;
  108. }
  109. /**
  110. * Make a show builder.
  111. *
  112. * @param mixed $id
  113. * @return Show
  114. */
  115. protected function detail($id)
  116. {
  117. $show = new Show(ArticleProperty::findOrFail($id));
  118. $show->id('ID');
  119. $show->category_name('属性名称');
  120. $show->admin_set('系统分类')->as(function ($admin_set) {
  121. return $admin_set?'是':'否';
  122. });
  123. $show->list_order('排序');
  124. $show->created_at('添加时间');
  125. $show->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 ArticleProperty);
  136. $form->text('category_name', '属性名称')->rules('required|max:25', array('required'=>'属性名称不能为空。','max'=>'属性名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
  137. $form->number('list_order', '排序')->min(0)->default(0)->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)')->setMustMark();
  138. $form->hidden('admin_set')->value(0);
  139. $form->footer(function ($footer) {
  140. $footer->disableViewCheck();
  141. $footer->disableEditingCheck();
  142. $footer->disableCreatingCheck();
  143. $footer->disableReset();
  144. });
  145. $form->tools(function (Form\Tools $tools) {
  146. $tools->disableDelete();
  147. $tools->disableView();
  148. });
  149. return $form;
  150. }
  151. public function destroy($id)
  152. {
  153. $id_arr = explode(',', $id);
  154. $admin_set_cates = ArticleProperty::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
  155. foreach ($id_arr as $k => $v) {
  156. if (in_array($v, $admin_set_cates)) {
  157. unset($id_arr[$k]);
  158. }
  159. }
  160. $filter_id = implode(',', $id_arr);
  161. if ($this->form()->destroy($filter_id)) {
  162. $data = [
  163. 'status' => true,
  164. 'message' => trans('admin.delete_succeeded'),
  165. ];
  166. } else {
  167. $data = [
  168. 'status' => false,
  169. 'message' => trans('admin.delete_failed'),
  170. ];
  171. }
  172. return response()->json($data);
  173. }
  174. }