ArticleCategoryController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Admin\Extensions\Form\ValidateForm;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\ArticleCategory;
  6. use App\Models\ArticleCategoryRole;
  7. use App\Models\Role;
  8. use Encore\Admin\Controllers\HasResourceActions;
  9. use Encore\Admin\Facades\Admin as userAdmin;
  10. use Encore\Admin\Form;
  11. use Encore\Admin\Grid;
  12. use Encore\Admin\Layout\Column;
  13. use Encore\Admin\Layout\Content;
  14. use Encore\Admin\Layout\Row;
  15. use Encore\Admin\Show;
  16. use Encore\Admin\Tree;
  17. use Encore\Admin\Widgets\Box;
  18. //use Encore\Admin\Facades\Admin;
  19. class ArticleCategoryController extends Controller
  20. {
  21. use HasResourceActions;
  22. /**
  23. * Index interface.
  24. *
  25. * @param Content $content
  26. * @return Content
  27. */
  28. public function index(Content $content)
  29. {
  30. return $content
  31. ->header(trans('新闻分类'))
  32. ->description(trans('系统默认分类不能删除!删除顶级级分类将会自动删除此分类下的子分类。'))
  33. ->row(function (Row $row) {
  34. if (userAdmin::user()->can('content_article_category_create')) {
  35. $row->column(6, $this->treeView()->render());
  36. $row->column(6, function (Column $column) {
  37. $form = new \Encore\Admin\Widgets\Form();
  38. $form->action(route('article.category.index'));
  39. $form->text('category_name', '名称')->setMustMark();
  40. $cate_option = ArticleCategory::where(array('parent_id'=>0))->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->pluck('category_name', 'id');
  41. $cate_option = $cate_option->toArray();
  42. $cate_option = array('0'=>'顶级分类')+$cate_option;
  43. $form->select('parent_id', '所属分类')->options($cate_option)->default(0)->setMustMark();
  44. $form->number('list_order', '排序')->min(0)->default(0)->help('(数字越大越靠前)')->setMustMark();
  45. $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id'));
  46. $form->text('title', 'Title')->help('(不填则为分类名称)');
  47. $form->text('keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名');
  48. $form->textarea('description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名');
  49. $form->hidden('admin_set')->value(0);
  50. $form->hidden('_token')->default(csrf_token());
  51. $form->disableReset();
  52. $column->append((new Box(trans('admin.new'), $form))->style('success'));
  53. });
  54. } else {
  55. $row->column(12, $this->treeView()->render());
  56. }
  57. });
  58. }
  59. /**
  60. * Show interface.
  61. *
  62. * @param mixed $id
  63. * @param Content $content
  64. * @return Content
  65. */
  66. public function show($id, Content $content)
  67. {
  68. return $content
  69. ->header('新闻分类')
  70. ->description(' ')
  71. ->body($this->detail($id));
  72. }
  73. /**
  74. * Edit interface.
  75. *
  76. * @param mixed $id
  77. * @param Content $content
  78. * @return Content
  79. */
  80. public function edit($id, Content $content)
  81. {
  82. return $content
  83. ->header('新闻分类')
  84. ->description(' ')
  85. ->body($this->form()->edit($id));
  86. }
  87. /**
  88. * Create interface.
  89. *
  90. * @param Content $content
  91. * @return Content
  92. */
  93. public function create(Content $content)
  94. {
  95. return $content
  96. ->header('新闻分类')
  97. ->description(' ')
  98. ->body($this->form());
  99. }
  100. /**
  101. * Make a grid builder.
  102. *
  103. * @return Grid
  104. */
  105. protected function grid()
  106. {
  107. $grid = new Grid(new ArticleCategory);
  108. $grid->id('ID');
  109. $grid->created_at('添加时间');
  110. $grid->updated_at('更新时间');
  111. return $grid;
  112. }
  113. /**
  114. * Make a show builder.
  115. *
  116. * @param mixed $id
  117. * @return Show
  118. */
  119. protected function detail($id)
  120. {
  121. $show = new Show(ArticleCategory::findOrFail($id));
  122. $show->id('ID');
  123. $show->category_name('名称');
  124. $show->parent_id('所属分类')->as(function ($parent_id) {
  125. $category_name = '顶级分类';
  126. if ($parent_id) {
  127. $cate_info = ArticleCategory::where('id', $parent_id)->first();
  128. if ($cate_info) {
  129. $category_name = $cate_info->category_name;
  130. } else {
  131. $category_name = '';
  132. }
  133. }
  134. return $category_name;
  135. });
  136. $show->list_order('排序');
  137. $show->roles(trans('admin.roles'))->as(function ($role) {
  138. return $role->pluck('name');
  139. })->label();
  140. $show->title('Title');
  141. $show->keywords('Keywords');
  142. $show->description('Description');
  143. $show->admin_set('系统分类')->as(function ($admin_set) {
  144. return $admin_set?'是':'否';
  145. });
  146. $show->created_at('添加时间');
  147. $show->updated_at('更新时间');
  148. return $show;
  149. }
  150. /**
  151. * Make a form builder.
  152. *
  153. * @return Form
  154. */
  155. protected function form()
  156. {
  157. $form = new ValidateForm(new ArticleCategory);
  158. $form->text('category_name', '名称')->rules('required|max:15', array('required'=>'名称不能为空。', 'max'=>'名称长度不能超过15。'))->setWidth(4)->setMustMark();
  159. $cate_option = ArticleCategory::where(array('parent_id'=>0))->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC')->get()->pluck('category_name', 'id');
  160. $cate_option = $cate_option->toArray();
  161. $cate_option = array('0'=>'顶级分类')+$cate_option;
  162. $form->select('parent_id', '所属分类')->options($cate_option)->default(0)->rules('required', array('required'=>'请选择所属分类。'))->setWidth(4)->setMustMark();
  163. $form->number('list_order', '排序')
  164. ->min(0)->default(0)
  165. ->rules('required', array('required'=>'排序不能为空。'))
  166. ->help('(数字越大越靠前)')
  167. ->setMustMark();
  168. $form->multipleSelect('roles', trans('admin.roles'))->options(Role::all()->pluck('name', 'id'));
  169. $form->text('title', 'Title')->help('(不填则为分类名称)')->rules('max:25', array('max'=>'Title长度不能大于25。'))->setWidth(4);
  170. $form->text('keywords', 'Keywords')
  171. ->placeholder('合理设置Keywords有利于搜索引擎排名')
  172. ->rules('max:25', array('max'=>'Keywords长度不能大于25。'));
  173. $form->textarea('description', 'Description')
  174. ->rules('max:25', array('max'=>'Description长度不能大于25。'))
  175. ->placeholder('合理设置Description有利于搜索引擎排名');
  176. $form->hidden('admin_set');
  177. $form->saving(function (Form $form) {
  178. $form->admin_set = $form->admin_set?$form->admin_set:0;
  179. });
  180. $form->footer(function ($footer) {
  181. $footer->disableViewCheck();
  182. $footer->disableEditingCheck();
  183. $footer->disableCreatingCheck();
  184. $footer->disableReset();
  185. });
  186. $form->tools(function (Form\Tools $tools) {
  187. $tools->disableDelete();
  188. $tools->disableView();
  189. });
  190. return $form;
  191. }
  192. public function stores()
  193. {
  194. $data=$this->form()->getValidateInput();
  195. $cate = new ArticleCategory();
  196. $cate->category_name = $data['category_name'];
  197. $cate->parent_id = $data['parent_id'];
  198. $cate->list_order = $data['list_order'];
  199. $cate->title = $data['title'];
  200. $cate->keywords = $data['keywords'];
  201. $cate->description = $data['description'];
  202. $cate->admin_set = $data['admin_set']?$data['admin_set']:'0';
  203. if ($cate->save()) {
  204. $cate_id = $cate->id;
  205. $roles = $data['roles'];
  206. $time = date('Y-m-d H:i:s', time());
  207. $role_data = array();
  208. if ($roles) {
  209. foreach ($roles as $k => $v) {
  210. $role_data[] = array(
  211. 'role_id'=> $v,
  212. 'article_category_id' => $cate_id,
  213. 'created_at' => $time,
  214. 'updated_at' => $time
  215. );
  216. }
  217. ArticleCategoryRole::insert($role_data);
  218. }
  219. admin_toastr(trans('admin.save_succeeded'));
  220. return redirect(admin_base_path('/content/article/category'));
  221. } else {
  222. throw new \Exception(trans('admin.save_failed'));
  223. }
  224. }
  225. /**
  226. * Make a grid builder.
  227. *
  228. * @return Tree
  229. */
  230. protected function treeView()
  231. {
  232. return ArticleCategory::tree(function (Tree $tree) {
  233. $tree->disableRefresh();
  234. $tree->disableSave();
  235. $tree->branch(function ($branch) {
  236. return "{$branch['category_name']} (id:{$branch['id']})";
  237. });
  238. $tree->setView(array(
  239. 'tree' => 'admin::tree',
  240. 'branch' => 'admin.content.article_branch'
  241. ));
  242. $tree->query(function ($model) {
  243. $not_ids = ArticleCategory::categoryIds();
  244. if ($not_ids) {
  245. return $model->whereNotIn('id', $not_ids)->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC');
  246. } else {
  247. return $model->OrderBy('list_order', 'DESC')->orderBy('created_at', 'DESC');
  248. }
  249. });
  250. });
  251. }
  252. public function destroy($id)
  253. {
  254. $id_arr = explode(',', $id);
  255. $children_ids = ArticleCategory::whereIn('parent_id', $id_arr)->select('id')->get()->pluck('id')->toArray();
  256. if ($children_ids) {
  257. $children_ids = implode(',', $children_ids);
  258. } else {
  259. $children_ids = '';
  260. }
  261. if ($this->form()->destroy($id)) {
  262. $data = [
  263. 'status' => true,
  264. 'message' => trans('admin.delete_succeeded'),
  265. ];
  266. //删除分类与角色的对应关系
  267. if ($children_ids) {
  268. $where = array();
  269. $ids = explode(',', $children_ids);
  270. ArticleCategoryRole::whereIn('article_category_id', $ids)->delete();
  271. }
  272. } else {
  273. $data = [
  274. 'status' => false,
  275. 'message' => trans('admin.delete_failed'),
  276. ];
  277. }
  278. return response()->json($data);
  279. }
  280. }