AdminArticleController.php 14 KB

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