Просмотр файла app/Http/Controllers/Admin/ArticleController.php

Размер файла: 10.69Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers\Admin;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Article;
  9. use App\Models\Blog;
  10. use App\Models\User;
  11. use Exception;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\View\View;
  15.  
  16. class ArticleController extends AdminController
  17. {
  18. /**
  19. * Главная страница
  20. *
  21. * @return View
  22. */
  23. public function index(): View
  24. {
  25. $categories = Blog::query()
  26. ->where('parent_id', 0)
  27. ->orderBy('sort')
  28. ->with('children', 'new', 'children.new')
  29. ->get();
  30.  
  31. return view('admin/blogs/index', compact('categories'));
  32. }
  33.  
  34. /**
  35. * Создание раздела
  36. *
  37. * @param Request $request
  38. * @param Validator $validator
  39. *
  40. * @return RedirectResponse
  41. */
  42. public function create(Request $request, Validator $validator): RedirectResponse
  43. {
  44. if (! isAdmin(User::BOSS)) {
  45. abort(403, __('errors.forbidden'));
  46. }
  47.  
  48. $name = $request->input('name');
  49.  
  50. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  51. ->length($name, 3, 50, ['name' => __('validator.text')]);
  52.  
  53. if ($validator->isValid()) {
  54. $max = Blog::query()->max('sort') + 1;
  55.  
  56. /** @var Blog $category */
  57. $category = Blog::query()->create([
  58. 'name' => $name,
  59. 'sort' => $max,
  60. ]);
  61.  
  62. setFlash('success', __('blogs.category_success_created'));
  63.  
  64. return redirect('admin/blogs/edit/' . $category->id);
  65. }
  66.  
  67. setInput($request->all());
  68. setFlash('danger', $validator->getErrors());
  69.  
  70. return redirect('admin/blogs');
  71. }
  72.  
  73. /**
  74. * Редактирование раздела
  75. *
  76. * @param int $id
  77. * @param Request $request
  78. * @param Validator $validator
  79. *
  80. * @return View|RedirectResponse
  81. */
  82. public function edit(int $id, Request $request, Validator $validator)
  83. {
  84. if (! isAdmin(User::BOSS)) {
  85. abort(403, __('errors.forbidden'));
  86. }
  87.  
  88. /** @var Blog $category */
  89. $category = Blog::query()->with('children')->find($id);
  90.  
  91. if (! $category) {
  92. abort(404, __('blogs.category_not_exist'));
  93. }
  94.  
  95. $categories = Blog::query()
  96. ->where('parent_id', 0)
  97. ->orderBy('sort')
  98. ->get();
  99.  
  100. if ($request->isMethod('post')) {
  101. $parent = int($request->input('parent'));
  102. $name = $request->input('name');
  103. $sort = int($request->input('sort'));
  104. $closed = empty($request->input('closed')) ? 0 : 1;
  105.  
  106. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  107. ->length($name, 3, 50, ['title' => __('validator.text')])
  108. ->notEqual($parent, $category->id, ['parent' => __('blogs.category_not_exist')]);
  109.  
  110. if (! empty($parent) && $category->children->isNotEmpty()) {
  111. $validator->addError(['parent' => __('blogs.category_has_subcategories')]);
  112. }
  113.  
  114. if ($validator->isValid()) {
  115. $category->update([
  116. 'parent_id' => $parent,
  117. 'name' => $name,
  118. 'sort' => $sort,
  119. 'closed' => $closed,
  120. ]);
  121.  
  122. setFlash('success', __('blogs.category_success_edited'));
  123.  
  124. return redirect('admin/blogs');
  125. }
  126.  
  127. setInput($request->all());
  128. setFlash('danger', $validator->getErrors());
  129. }
  130.  
  131. return view('admin/blogs/edit', compact('categories', 'category'));
  132. }
  133.  
  134. /**
  135. * Удаление раздела
  136. *
  137. * @param int $id
  138. * @param Request $request
  139. * @param Validator $validator
  140. *
  141. * @return RedirectResponse
  142. * @throws Exception
  143. */
  144. public function delete(int $id, Request $request, Validator $validator): RedirectResponse
  145. {
  146. if (! isAdmin(User::BOSS)) {
  147. abort(403, __('errors.forbidden'));
  148. }
  149.  
  150. /** @var Blog $category */
  151. $category = Blog::query()->with('children')->find($id);
  152.  
  153. if (! $category) {
  154. abort(404, __('blogs.category_not_exist'));
  155. }
  156.  
  157. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  158. ->true($category->children->isEmpty(), __('blogs.category_has_subcategories'));
  159.  
  160. $article = Article::query()->where('category_id', $category->id)->first();
  161. if ($article) {
  162. $validator->addError(__('blogs.articles_in_category'));
  163. }
  164.  
  165. if ($validator->isValid()) {
  166. $category->delete();
  167.  
  168. setFlash('success', __('blogs.category_success_deleted'));
  169. } else {
  170. setFlash('danger', $validator->getErrors());
  171. }
  172.  
  173. return redirect('admin/blogs');
  174. }
  175.  
  176. /**
  177. * Пересчет данных
  178. *
  179. * @param Request $request
  180. *
  181. * @return RedirectResponse
  182. */
  183. public function restatement(Request $request): RedirectResponse
  184. {
  185. if (! isAdmin(User::BOSS)) {
  186. abort(403, __('errors.forbidden'));
  187. }
  188.  
  189. if ($request->input('_token') === csrf_token()) {
  190. restatement('blogs');
  191.  
  192. setFlash('success', __('main.success_recounted'));
  193. } else {
  194. setFlash('danger', __('validator.token'));
  195. }
  196.  
  197. return redirect('admin/blogs');
  198. }
  199.  
  200. /**
  201. * Список блогов
  202. *
  203. * @param int $id
  204. *
  205. * @return View
  206. */
  207. public function blog(int $id): View
  208. {
  209. $category = Blog::query()->with('parent')->find($id);
  210.  
  211. if (! $category) {
  212. abort(404, __('blogs.category_not_exist'));
  213. }
  214.  
  215. $articles = Article::query()
  216. ->where('category_id', $id)
  217. ->orderByDesc('created_at')
  218. ->with('user')
  219. ->paginate(setting('blogpost'));
  220.  
  221. return view('admin/blogs/blog', compact('articles', 'category'));
  222. }
  223.  
  224. /**
  225. * Редактирование статьи
  226. *
  227. * @param int $id
  228. * @param Request $request
  229. * @param Validator $validator
  230. *
  231. * @return View|RedirectResponse
  232. */
  233. public function editArticle(int $id, Request $request, Validator $validator)
  234. {
  235. /** @var Article $article */
  236. $article = Article::query()->find($id);
  237.  
  238. if (! $article) {
  239. abort(404, __('blogs.article_not_exist'));
  240. }
  241.  
  242. if ($request->isMethod('post')) {
  243. $title = $request->input('title');
  244. $text = $request->input('text');
  245. $tags = $request->input('tags');
  246.  
  247. $validator
  248. ->equal($request->input('_token'), csrf_token(), __('validator.token'))
  249. ->length($title, 3, 50, ['title' => __('validator.text')])
  250. ->length($text, 100, setting('maxblogpost'), ['text' => __('validator.text')])
  251. ->length($tags, 2, 50, ['tags' => __('blogs.article_error_tags')]);
  252.  
  253. if ($validator->isValid()) {
  254. $article->update([
  255. 'title' => $title,
  256. 'text' => $text,
  257. 'tags' => $tags,
  258. ]);
  259.  
  260. clearCache(['statArticles', 'recentArticles']);
  261. setFlash('success', __('blogs.article_success_edited'));
  262.  
  263. return redirect('articles/' . $article->id);
  264. }
  265.  
  266. setInput($request->all());
  267. setFlash('danger', $validator->getErrors());
  268. }
  269.  
  270. return view('admin/blogs/edit_blog', compact('article'));
  271. }
  272.  
  273. /**
  274. * Перенос статьи
  275. *
  276. * @param int $id
  277. * @param Request $request
  278. * @param Validator $validator
  279. *
  280. * @return View|RedirectResponse
  281. */
  282. public function moveArticle(int $id, Request $request, Validator $validator)
  283. {
  284. /** @var Article $article */
  285. $article = Article::query()->find($id);
  286.  
  287. if (! $article) {
  288. abort(404, __('blogs.article_not_exist'));
  289. }
  290.  
  291. if ($request->isMethod('post')) {
  292. $cid = int($request->input('cid'));
  293.  
  294. /** @var Blog $category */
  295. $category = Blog::query()->find($cid);
  296.  
  297. $validator
  298. ->equal($request->input('_token'), csrf_token(), __('validator.token'))
  299. ->notEmpty($category, ['cid' => __('blogs.category_not_exist')]);
  300.  
  301. if ($category) {
  302. $validator->empty($category->closed, ['cid' => __('blogs.category_closed')]);
  303. $validator->notEqual($article->category_id, $category->id, ['cid' => __('blogs.article_error_moving')]);
  304. }
  305.  
  306. if ($validator->isValid()) {
  307. // Обновление счетчиков
  308. $category->increment('count_articles');
  309. Blog::query()->where('id', $article->category_id)->decrement('count_articles');
  310.  
  311. $article->update([
  312. 'category_id' => $category->id,
  313. ]);
  314.  
  315. setFlash('success', __('blogs.article_success_moved'));
  316.  
  317. return redirect('articles/' . $article->id);
  318. }
  319.  
  320. setInput($request->all());
  321. setFlash('danger', $validator->getErrors());
  322. }
  323.  
  324. $categories = Blog::query()
  325. ->where('parent_id', 0)
  326. ->with('children')
  327. ->orderBy('sort')
  328. ->get();
  329.  
  330. return view('admin/blogs/move_blog', compact('article', 'categories'));
  331. }
  332.  
  333. /**
  334. * Удаление статьи
  335. *
  336. * @param int $id
  337. * @param Request $request
  338. * @param Validator $validator
  339. *
  340. * @return RedirectResponse
  341. * @throws Exception
  342. */
  343. public function deleteArticle(int $id, Request $request, Validator $validator): RedirectResponse
  344. {
  345. $page = int($request->input('page', 1));
  346.  
  347. /** @var Article $article */
  348. $article = Article::query()->find($id);
  349.  
  350. if (! $article) {
  351. abort(404, __('blogs.article_not_exist'));
  352. }
  353.  
  354. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'));
  355.  
  356. if ($validator->isValid()) {
  357. $article->comments()->delete();
  358. $article->delete();
  359.  
  360. $article->category->decrement('count_articles');
  361.  
  362. clearCache(['statArticles', 'recentArticles']);
  363. setFlash('success', __('blogs.article_success_deleted'));
  364. } else {
  365. setFlash('danger', $validator->getErrors());
  366. }
  367.  
  368. return redirect('admin/blogs/' . $article->category_id . '?page=' . $page);
  369. }
  370. }