AdminArticleController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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\portal\model\PortalCategoryPostModel;
  13. use app\portal\model\PortalTagPostModel;
  14. use app\portal\model\RecycleBinModel;
  15. use cmf\controller\AdminBaseController;
  16. use app\portal\model\PortalPostModel;
  17. use app\portal\service\PostService;
  18. use app\portal\model\PortalCategoryModel;
  19. use app\admin\model\ThemeModel;
  20. class AdminArticleController extends AdminBaseController
  21. {
  22. /**
  23. * 文章列表
  24. * @adminMenu(
  25. * 'name' => '文章管理',
  26. * 'parent' => 'portal/AdminIndex/default',
  27. * 'display'=> true,
  28. * 'hasView'=> true,
  29. * 'order' => 10000,
  30. * 'icon' => '',
  31. * 'remark' => '文章列表',
  32. * 'param' => ''
  33. * )
  34. * @return mixed
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. */
  39. public function index()
  40. {
  41. $content = hook_one('portal_admin_article_index_view');
  42. if (!empty($content)) {
  43. return $content;
  44. }
  45. $param = $this->request->param();
  46. $categoryId = $this->request->param('category', 0, 'intval');
  47. $postService = new PostService();
  48. $data = $postService->adminArticleList($param);
  49. $data->appends($param);
  50. $portalCategoryModel = new PortalCategoryModel();
  51. $categoryTree = $portalCategoryModel->adminCategoryTree($categoryId);
  52. $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
  53. $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
  54. $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
  55. $this->assign('articles', $data->items());
  56. $this->assign('category_tree', $categoryTree);
  57. $this->assign('category', $categoryId);
  58. $this->assign('page', $data->render());
  59. return $this->fetch();
  60. }
  61. /**
  62. * 添加文章
  63. * @adminMenu(
  64. * 'name' => '添加文章',
  65. * 'parent' => 'index',
  66. * 'display'=> false,
  67. * 'hasView'=> true,
  68. * 'order' => 10000,
  69. * 'icon' => '',
  70. * 'remark' => '添加文章',
  71. * 'param' => ''
  72. * )
  73. * @return mixed
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @throws \think\exception\DbException
  77. */
  78. public function add()
  79. {
  80. $content = hook_one('portal_admin_article_add_view');
  81. if (!empty($content)) {
  82. return $content;
  83. }
  84. $themeModel = new ThemeModel();
  85. $articleThemeFiles = $themeModel->getActionThemeFiles('portal/Article/index');
  86. $this->assign('article_theme_files', $articleThemeFiles);
  87. return $this->fetch();
  88. }
  89. /**
  90. * 添加文章提交
  91. * @adminMenu(
  92. * 'name' => '添加文章提交',
  93. * 'parent' => 'index',
  94. * 'display'=> false,
  95. * 'hasView'=> false,
  96. * 'order' => 10000,
  97. * 'icon' => '',
  98. * 'remark' => '添加文章提交',
  99. * 'param' => ''
  100. * )
  101. */
  102. public function addPost()
  103. {
  104. if ($this->request->isPost()) {
  105. $data = $this->request->param();
  106. //状态只能设置默认值。未发布、未置顶、未推荐
  107. $data['post']['post_status'] = 0;
  108. $data['post']['is_top'] = 0;
  109. $data['post']['recommended'] = 0;
  110. $post = $data['post'];
  111. $result = $this->validate($post, 'AdminArticle');
  112. if ($result !== true) {
  113. $this->error($result);
  114. }
  115. $portalPostModel = new PortalPostModel();
  116. if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
  117. $data['post']['more']['photos'] = [];
  118. foreach ($data['photo_urls'] as $key => $url) {
  119. $photoUrl = cmf_asset_relative_url($url);
  120. array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
  121. }
  122. }
  123. if (!empty($data['file_names']) && !empty($data['file_urls'])) {
  124. $data['post']['more']['files'] = [];
  125. foreach ($data['file_urls'] as $key => $url) {
  126. $fileUrl = cmf_asset_relative_url($url);
  127. array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
  128. }
  129. }
  130. $portalPostModel->adminAddArticle($data['post'], $data['post']['categories']);
  131. $data['post']['id'] = $portalPostModel->id;
  132. $hookParam = [
  133. 'is_add' => true,
  134. 'article' => $data['post']
  135. ];
  136. hook('portal_admin_after_save_article', $hookParam);
  137. $this->success('添加成功!', url('AdminArticle/edit', ['id' => $portalPostModel->id]));
  138. }
  139. }
  140. /**
  141. * 编辑文章
  142. * @adminMenu(
  143. * 'name' => '编辑文章',
  144. * 'parent' => 'index',
  145. * 'display'=> false,
  146. * 'hasView'=> true,
  147. * 'order' => 10000,
  148. * 'icon' => '',
  149. * 'remark' => '编辑文章',
  150. * 'param' => ''
  151. * )
  152. * @return mixed
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. * @throws \think\exception\DbException
  156. */
  157. public function edit()
  158. {
  159. $content = hook_one('portal_admin_article_edit_view');
  160. if (!empty($content)) {
  161. return $content;
  162. }
  163. $id = $this->request->param('id', 0, 'intval');
  164. $portalPostModel = new PortalPostModel();
  165. $post = $portalPostModel->where('id', $id)->find();
  166. $postCategories = $post->categories()->alias('a')->column('a.name', 'a.id');
  167. $postCategoryIds = implode(',', array_keys($postCategories));
  168. $themeModel = new ThemeModel();
  169. $articleThemeFiles = $themeModel->getActionThemeFiles('portal/Article/index');
  170. $this->assign('article_theme_files', $articleThemeFiles);
  171. $this->assign('post', $post);
  172. $this->assign('post_categories', $postCategories);
  173. $this->assign('post_category_ids', $postCategoryIds);
  174. return $this->fetch();
  175. }
  176. /**
  177. * 编辑文章提交
  178. * @adminMenu(
  179. * 'name' => '编辑文章提交',
  180. * 'parent' => 'index',
  181. * 'display'=> false,
  182. * 'hasView'=> false,
  183. * 'order' => 10000,
  184. * 'icon' => '',
  185. * 'remark' => '编辑文章提交',
  186. * 'param' => ''
  187. * )
  188. * @throws \think\Exception
  189. */
  190. public function editPost()
  191. {
  192. if ($this->request->isPost()) {
  193. $data = $this->request->param();
  194. //需要抹除发布、置顶、推荐的修改。
  195. unset($data['post']['post_status']);
  196. unset($data['post']['is_top']);
  197. unset($data['post']['recommended']);
  198. $post = $data['post'];
  199. $result = $this->validate($post, 'AdminArticle');
  200. if ($result !== true) {
  201. $this->error($result);
  202. }
  203. $portalPostModel = new PortalPostModel();
  204. if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
  205. $data['post']['more']['photos'] = [];
  206. foreach ($data['photo_urls'] as $key => $url) {
  207. $photoUrl = cmf_asset_relative_url($url);
  208. array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
  209. }
  210. }
  211. if (!empty($data['file_names']) && !empty($data['file_urls'])) {
  212. $data['post']['more']['files'] = [];
  213. foreach ($data['file_urls'] as $key => $url) {
  214. $fileUrl = cmf_asset_relative_url($url);
  215. array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
  216. }
  217. }
  218. $portalPostModel->adminEditArticle($data['post'], $data['post']['categories']);
  219. $hookParam = [
  220. 'is_add' => false,
  221. 'article' => $data['post']
  222. ];
  223. hook('portal_admin_after_save_article', $hookParam);
  224. $this->success('保存成功!');
  225. }
  226. }
  227. /**
  228. * 文章删除
  229. * @adminMenu(
  230. * 'name' => '文章删除',
  231. * 'parent' => 'index',
  232. * 'display'=> false,
  233. * 'hasView'=> false,
  234. * 'order' => 10000,
  235. * 'icon' => '',
  236. * 'remark' => '文章删除',
  237. * 'param' => ''
  238. * )
  239. * @throws \think\Exception
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\ModelNotFoundException
  242. * @throws \think\exception\DbException
  243. * @throws \think\exception\PDOException
  244. */
  245. public function delete()
  246. {
  247. $param = $this->request->param();
  248. $portalPostModel = new PortalPostModel();
  249. if (isset($param['id'])) {
  250. $id = $this->request->param('id', 0, 'intval');
  251. $result = $portalPostModel->where('id', $id)->find();
  252. $data = [
  253. 'object_id' => $result['id'],
  254. 'create_time' => time(),
  255. 'table_name' => 'portal_post',
  256. 'name' => $result['post_title'],
  257. 'user_id' => cmf_get_current_admin_id()
  258. ];
  259. $resultPortal = $portalPostModel
  260. ->where('id', $id)
  261. ->update(['delete_time' => time()]);
  262. if ($resultPortal) {
  263. PortalCategoryPostModel::where('post_id', $id)->update(['status' => 0]);
  264. PortalTagPostModel::where('post_id', $id)->update(['status' => 0]);
  265. RecycleBinModel::insert($data);
  266. }
  267. $this->success("删除成功!", '');
  268. }
  269. if (isset($param['ids'])) {
  270. $ids = $this->request->param('ids/a');
  271. $recycle = $portalPostModel->where('id', 'in', $ids)->select();
  272. $result = $portalPostModel->where('id', 'in', $ids)->update(['delete_time' => time()]);
  273. if ($result) {
  274. PortalCategoryPostModel::where('post_id', 'in', $ids)->update(['status' => 0]);
  275. PortalTagPostModel::where('post_id', 'in', $ids)->update(['status' => 0]);
  276. foreach ($recycle as $value) {
  277. $data = [
  278. 'object_id' => $value['id'],
  279. 'create_time' => time(),
  280. 'table_name' => 'portal_post',
  281. 'name' => $value['post_title'],
  282. 'user_id' => cmf_get_current_admin_id()
  283. ];
  284. RecycleBinModel::insert($data);
  285. }
  286. $this->success("删除成功!", '');
  287. }
  288. }
  289. }
  290. /**
  291. * 文章发布
  292. * @adminMenu(
  293. * 'name' => '文章发布',
  294. * 'parent' => 'index',
  295. * 'display'=> false,
  296. * 'hasView'=> false,
  297. * 'order' => 10000,
  298. * 'icon' => '',
  299. * 'remark' => '文章发布',
  300. * 'param' => ''
  301. * )
  302. */
  303. public function publish()
  304. {
  305. $param = $this->request->param();
  306. $portalPostModel = new PortalPostModel();
  307. if (isset($param['ids']) && isset($param["yes"])) {
  308. $ids = $this->request->param('ids/a');
  309. $portalPostModel->where('id', 'in', $ids)->update(['post_status' => 1, 'published_time' => time()]);
  310. $this->success("发布成功!", '');
  311. }
  312. if (isset($param['ids']) && isset($param["no"])) {
  313. $ids = $this->request->param('ids/a');
  314. $portalPostModel->where('id', 'in', $ids)->update(['post_status' => 0]);
  315. $this->success("取消发布成功!", '');
  316. }
  317. }
  318. /**
  319. * 文章置顶
  320. * @adminMenu(
  321. * 'name' => '文章置顶',
  322. * 'parent' => 'index',
  323. * 'display'=> false,
  324. * 'hasView'=> false,
  325. * 'order' => 10000,
  326. * 'icon' => '',
  327. * 'remark' => '文章置顶',
  328. * 'param' => ''
  329. * )
  330. */
  331. public function top()
  332. {
  333. $param = $this->request->param();
  334. $portalPostModel = new PortalPostModel();
  335. if (isset($param['ids']) && isset($param["yes"])) {
  336. $ids = $this->request->param('ids/a');
  337. $portalPostModel->where('id', 'in', $ids)->update(['is_top' => 1]);
  338. $this->success("置顶成功!", '');
  339. }
  340. if (isset($_POST['ids']) && isset($param["no"])) {
  341. $ids = $this->request->param('ids/a');
  342. $portalPostModel->where('id', 'in', $ids)->update(['is_top' => 0]);
  343. $this->success("取消置顶成功!", '');
  344. }
  345. }
  346. /**
  347. * 文章推荐
  348. * @adminMenu(
  349. * 'name' => '文章推荐',
  350. * 'parent' => 'index',
  351. * 'display'=> false,
  352. * 'hasView'=> false,
  353. * 'order' => 10000,
  354. * 'icon' => '',
  355. * 'remark' => '文章推荐',
  356. * 'param' => ''
  357. * )
  358. */
  359. public function recommend()
  360. {
  361. $param = $this->request->param();
  362. $portalPostModel = new PortalPostModel();
  363. if (isset($param['ids']) && isset($param["yes"])) {
  364. $ids = $this->request->param('ids/a');
  365. $portalPostModel->where('id', 'in', $ids)->update(['recommended' => 1]);
  366. $this->success("推荐成功!", '');
  367. }
  368. if (isset($param['ids']) && isset($param["no"])) {
  369. $ids = $this->request->param('ids/a');
  370. $portalPostModel->where('id', 'in', $ids)->update(['recommended' => 0]);
  371. $this->success("取消推荐成功!", '');
  372. }
  373. }
  374. /**
  375. * 文章排序
  376. * @adminMenu(
  377. * 'name' => '文章排序',
  378. * 'parent' => 'index',
  379. * 'display'=> false,
  380. * 'hasView'=> false,
  381. * 'order' => 10000,
  382. * 'icon' => '',
  383. * 'remark' => '文章排序',
  384. * 'param' => ''
  385. * )
  386. */
  387. public function listOrder()
  388. {
  389. parent::listOrders('portal_category_post');
  390. $this->success("排序更新成功!", '');
  391. }
  392. }