AdminCategoryController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\portal\controller;
  12. use app\admin\model\RouteModel;
  13. use app\portal\model\PortalCategoryPostModel;
  14. use app\portal\model\RecycleBinModel;
  15. use cmf\controller\AdminBaseController;
  16. use app\portal\model\PortalCategoryModel;
  17. use app\admin\model\ThemeModel;
  18. class AdminCategoryController extends AdminBaseController
  19. {
  20. /**
  21. * 文章分类列表
  22. * @adminMenu(
  23. * 'name' => '分类管理',
  24. * 'parent' => 'portal/AdminIndex/default',
  25. * 'display'=> true,
  26. * 'hasView'=> true,
  27. * 'order' => 10000,
  28. * 'icon' => '',
  29. * 'remark' => '文章分类列表',
  30. * 'param' => ''
  31. * )
  32. * @return mixed
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. public function index()
  38. {
  39. $content = hook_one('portal_admin_category_index_view');
  40. if (!empty($content)) {
  41. return $content;
  42. }
  43. $portalCategoryModel = new PortalCategoryModel();
  44. $keyword = $this->request->param('keyword');
  45. if (empty($keyword)) {
  46. $categoryTree = $portalCategoryModel->adminCategoryTableTree();
  47. $this->assign('category_tree', $categoryTree);
  48. } else {
  49. $categories = $portalCategoryModel->where('name', 'like', "%{$keyword}%")
  50. ->where('delete_time', 0)->select();
  51. $this->assign('categories', $categories);
  52. }
  53. $this->assign('keyword', $keyword);
  54. return $this->fetch();
  55. }
  56. /**
  57. * 添加文章分类
  58. * @adminMenu(
  59. * 'name' => '添加文章分类',
  60. * 'parent' => 'index',
  61. * 'display'=> false,
  62. * 'hasView'=> true,
  63. * 'order' => 10000,
  64. * 'icon' => '',
  65. * 'remark' => '添加文章分类',
  66. * 'param' => ''
  67. * )
  68. * @return mixed
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @throws \think\exception\DbException
  72. */
  73. public function add()
  74. {
  75. $content = hook_one('portal_admin_category_add_view');
  76. if (!empty($content)) {
  77. return $content;
  78. }
  79. $parentId = $this->request->param('parent', 0, 'intval');
  80. $portalCategoryModel = new PortalCategoryModel();
  81. $categoriesTree = $portalCategoryModel->adminCategoryTree($parentId);
  82. $themeModel = new ThemeModel();
  83. $listThemeFiles = $themeModel->getActionThemeFiles('portal/List/index');
  84. $articleThemeFiles = $themeModel->getActionThemeFiles('portal/Article/index');
  85. $this->assign('list_theme_files', $listThemeFiles);
  86. $this->assign('article_theme_files', $articleThemeFiles);
  87. $this->assign('categories_tree', $categoriesTree);
  88. return $this->fetch();
  89. }
  90. /**
  91. * 添加文章分类提交
  92. * @adminMenu(
  93. * 'name' => '添加文章分类提交',
  94. * 'parent' => 'index',
  95. * 'display'=> false,
  96. * 'hasView'=> false,
  97. * 'order' => 10000,
  98. * 'icon' => '',
  99. * 'remark' => '添加文章分类提交',
  100. * 'param' => ''
  101. * )
  102. */
  103. public function addPost()
  104. {
  105. $portalCategoryModel = new PortalCategoryModel();
  106. $data = $this->request->param();
  107. $result = $this->validate($data, 'PortalCategory');
  108. if ($result !== true) {
  109. $this->error($result);
  110. }
  111. $result = $portalCategoryModel->addCategory($data);
  112. if ($result === false) {
  113. $this->error('添加失败!');
  114. }
  115. $this->success('添加成功!', url('AdminCategory/index'));
  116. }
  117. /**
  118. * 编辑文章分类
  119. * @adminMenu(
  120. * 'name' => '编辑文章分类',
  121. * 'parent' => 'index',
  122. * 'display'=> false,
  123. * 'hasView'=> true,
  124. * 'order' => 10000,
  125. * 'icon' => '',
  126. * 'remark' => '编辑文章分类',
  127. * 'param' => ''
  128. * )
  129. * @return mixed
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. * @throws \think\exception\DbException
  133. */
  134. public function edit()
  135. {
  136. $content = hook_one('portal_admin_category_edit_view');
  137. if (!empty($content)) {
  138. return $content;
  139. }
  140. $id = $this->request->param('id', 0, 'intval');
  141. if ($id > 0) {
  142. $portalCategoryModel = new PortalCategoryModel();
  143. $category = $portalCategoryModel->find($id)->toArray();
  144. $categoriesTree = $portalCategoryModel->adminCategoryTree($category['parent_id'], $id);
  145. $themeModel = new ThemeModel();
  146. $listThemeFiles = $themeModel->getActionThemeFiles('portal/List/index');
  147. $articleThemeFiles = $themeModel->getActionThemeFiles('portal/Article/index');
  148. $routeModel = new RouteModel();
  149. $alias = $routeModel->getUrl('portal/List/index', ['id' => $id]);
  150. $category['alias'] = $alias;
  151. $this->assign($category);
  152. $this->assign('list_theme_files', $listThemeFiles);
  153. $this->assign('article_theme_files', $articleThemeFiles);
  154. $this->assign('categories_tree', $categoriesTree);
  155. return $this->fetch();
  156. } else {
  157. $this->error('操作错误!');
  158. }
  159. }
  160. /**
  161. * 编辑文章分类提交
  162. * @adminMenu(
  163. * 'name' => '编辑文章分类提交',
  164. * 'parent' => 'index',
  165. * 'display'=> false,
  166. * 'hasView'=> false,
  167. * 'order' => 10000,
  168. * 'icon' => '',
  169. * 'remark' => '编辑文章分类提交',
  170. * 'param' => ''
  171. * )
  172. */
  173. public function editPost()
  174. {
  175. $data = $this->request->param();
  176. $result = $this->validate($data, 'PortalCategory');
  177. if ($result !== true) {
  178. $this->error($result);
  179. }
  180. $portalCategoryModel = new PortalCategoryModel();
  181. $result = $portalCategoryModel->editCategory($data);
  182. if ($result === false) {
  183. $this->error('保存失败!');
  184. }
  185. $this->success('保存成功!');
  186. }
  187. /**
  188. * 文章分类选择对话框
  189. * @adminMenu(
  190. * 'name' => '文章分类选择对话框',
  191. * 'parent' => 'index',
  192. * 'display'=> false,
  193. * 'hasView'=> true,
  194. * 'order' => 10000,
  195. * 'icon' => '',
  196. * 'remark' => '文章分类选择对话框',
  197. * 'param' => ''
  198. * )
  199. * @return mixed
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. * @throws \think\exception\DbException
  203. */
  204. public function select()
  205. {
  206. $ids = $this->request->param('ids');
  207. $selectedIds = explode(',', $ids);
  208. $portalCategoryModel = new PortalCategoryModel();
  209. $tpl = <<<tpl
  210. <tr class='data-item-tr'>
  211. <td>
  212. <input type='checkbox' class='js-check' data-yid='js-check-y' data-xid='js-check-x' name='ids[]'
  213. value='\$id' data-name='\$name' \$checked>
  214. </td>
  215. <td>\$id</td>
  216. <td>\$spacer <a href='\$url' target='_blank'>\$name</a></td>
  217. </tr>
  218. tpl;
  219. $categoryTree = $portalCategoryModel->adminCategoryTableTree($selectedIds, $tpl);
  220. $categories = $portalCategoryModel->where('delete_time', 0)->select();
  221. $this->assign('categories', $categories);
  222. $this->assign('selectedIds', $selectedIds);
  223. $this->assign('categories_tree', $categoryTree);
  224. return $this->fetch();
  225. }
  226. /**
  227. * 文章分类排序
  228. * @adminMenu(
  229. * 'name' => '文章分类排序',
  230. * 'parent' => 'index',
  231. * 'display'=> false,
  232. * 'hasView'=> false,
  233. * 'order' => 10000,
  234. * 'icon' => '',
  235. * 'remark' => '文章分类排序',
  236. * 'param' => ''
  237. * )
  238. */
  239. public function listOrder()
  240. {
  241. parent::listOrders('portal_category');
  242. $this->success("排序更新成功!", '');
  243. }
  244. /**
  245. * 文章分类显示隐藏
  246. * @adminMenu(
  247. * 'name' => '文章分类显示隐藏',
  248. * 'parent' => 'index',
  249. * 'display'=> false,
  250. * 'hasView'=> false,
  251. * 'order' => 10000,
  252. * 'icon' => '',
  253. * 'remark' => '文章分类显示隐藏',
  254. * 'param' => ''
  255. * )
  256. */
  257. public function toggle()
  258. {
  259. $data = $this->request->param();
  260. $portalCategoryModel = new PortalCategoryModel();
  261. $ids = $this->request->param('ids/a');
  262. if (isset($data['ids']) && !empty($data["display"])) {
  263. $portalCategoryModel->where('id', 'in', $ids)->update(['status' => 1]);
  264. $this->success("更新成功!");
  265. }
  266. if (isset($data['ids']) && !empty($data["hide"])) {
  267. $portalCategoryModel->where('id', 'in', $ids)->update(['status' => 0]);
  268. $this->success("更新成功!");
  269. }
  270. }
  271. /**
  272. * 删除文章分类
  273. * @adminMenu(
  274. * 'name' => '删除文章分类',
  275. * 'parent' => 'index',
  276. * 'display'=> false,
  277. * 'hasView'=> false,
  278. * 'order' => 10000,
  279. * 'icon' => '',
  280. * 'remark' => '删除文章分类',
  281. * 'param' => ''
  282. * )
  283. */
  284. public function delete()
  285. {
  286. $portalCategoryModel = new PortalCategoryModel();
  287. $id = $this->request->param('id');
  288. //获取删除的内容
  289. $findCategory = $portalCategoryModel->where('id', $id)->find();
  290. if (empty($findCategory)) {
  291. $this->error('分类不存在!');
  292. }
  293. //判断此分类有无子分类(不算被删除的子分类)
  294. $categoryChildrenCount = $portalCategoryModel->where(['parent_id' => $id, 'delete_time' => 0])->count();
  295. if ($categoryChildrenCount > 0) {
  296. $this->error('此分类有子类无法删除!');
  297. }
  298. $categoryPostCount = PortalCategoryPostModel::where('category_id', $id)->count();
  299. if ($categoryPostCount > 0) {
  300. $this->error('此分类有文章无法删除!');
  301. }
  302. $data = [
  303. 'object_id' => $findCategory['id'],
  304. 'create_time' => time(),
  305. 'table_name' => 'portal_category',
  306. 'name' => $findCategory['name']
  307. ];
  308. $result = $portalCategoryModel
  309. ->where('id', $id)
  310. ->update(['delete_time' => time()]);
  311. if ($result) {
  312. RecycleBinModel::insert($data);
  313. $this->success('删除成功!');
  314. } else {
  315. $this->error('删除失败');
  316. }
  317. }
  318. }