Просмотр файла app/Controllers/Forum/ForumController.php

Размер файла: 8.99Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Forum;
  6.  
  7. use App\Classes\Validator;
  8. use App\Controllers\BaseController;
  9. use App\Models\Flood;
  10. use App\Models\Forum;
  11. use App\Models\Post;
  12. use App\Models\Topic;
  13. use App\Models\Vote;
  14. use App\Models\VoteAnswer;
  15. use Illuminate\Database\Capsule\Manager as DB;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Http\Request;
  18.  
  19. class ForumController extends BaseController
  20. {
  21. /**
  22. * Главная страница
  23. *
  24. * @return string
  25. */
  26. public function index(): string
  27. {
  28. $forums = Forum::query()
  29. ->where('parent_id', 0)
  30. ->with('lastTopic.lastPost.user')
  31. ->with('children')
  32. ->orderBy('sort')
  33. ->get();
  34.  
  35. if ($forums->isEmpty()) {
  36. abort('default', __('forums.empty_forums'));
  37. }
  38.  
  39. return view('forums/index', compact('forums'));
  40. }
  41.  
  42. /**
  43. * Страница списка тем
  44. *
  45. * @param int $id
  46. * @return string
  47. */
  48. public function forum(int $id): string
  49. {
  50. /** @var Forum $forum */
  51. $forum = Forum::query()->with('parent', 'children.lastTopic.lastPost.user')->find($id);
  52.  
  53. if (! $forum) {
  54. abort(404, __('forums.forum_not_exist'));
  55. }
  56.  
  57. $topics = Topic::query()
  58. ->where('forum_id', $forum->id)
  59. ->orderByDesc('locked')
  60. ->orderByDesc('updated_at')
  61. ->with('lastPost.user')
  62. ->paginate(setting('forumtem'));
  63.  
  64. return view('forums/forum', compact('forum', 'topics'));
  65. }
  66.  
  67. /**
  68. * Создание новой темы
  69. *
  70. * @param Request $request
  71. * @param Validator $validator
  72. * @param Flood $flood
  73. * @return string
  74. */
  75. public function create(Request $request, Validator $validator, Flood $flood): string
  76. {
  77. $fid = int($request->input('fid'));
  78.  
  79. $forums = Forum::query()
  80. ->where('parent_id', 0)
  81. ->with('children')
  82. ->orderBy('sort')
  83. ->get();
  84.  
  85. if ($forums->isEmpty()) {
  86. abort('default', __('forums.empty_forums'));
  87. }
  88.  
  89. if (! $user = getUser()) {
  90. abort(403);
  91. }
  92.  
  93. if ($request->isMethod('post')) {
  94. $title = check($request->input('title'));
  95. $msg = check($request->input('msg'));
  96. $token = check($request->input('token'));
  97. $vote = empty($request->input('vote')) ? 0 : 1;
  98. $question = check($request->input('question'));
  99. $answers = check((array) $request->input('answers'));
  100.  
  101. /** @var Forum $forum */
  102. $forum = Forum::query()->find($fid);
  103.  
  104. $validator->equal($token, $_SESSION['token'], __('validator.token'))
  105. ->notEmpty($forum, ['fid' => 'Форума для новой темы не существует!'])
  106. ->false($flood->isFlood(), ['msg' => __('validator.flood', ['sec' => $flood->getPeriod()])])
  107. ->length($title, 5, 50, ['title' => __('validator.text')])
  108. ->length($msg, 5, setting('forumtextlength'), ['msg' => __('validator.text')]);
  109.  
  110. if ($forum) {
  111. $validator->empty($forum->closed, ['fid' => __('forums.forum_closed')]);
  112. }
  113.  
  114. if ($vote) {
  115. $validator->length($question, 5, 100, ['question' => __('validator.text')]);
  116. $answers = array_unique(array_diff($answers, ['']));
  117.  
  118. foreach ($answers as $answer) {
  119. if (utfStrlen($answer) > 50) {
  120. $validator->addError(['answers' => __('votes.answer_wrong_length')]);
  121. break;
  122. }
  123. }
  124.  
  125. $validator->between(count($answers), 2, 10, ['answers' => __('votes.answer_not_enough')]);
  126. }
  127.  
  128. /* TODO: Сделать проверку поиска похожей темы */
  129.  
  130. if ($validator->isValid()) {
  131. $title = antimat($title);
  132. $msg = antimat($msg);
  133.  
  134. $user->increment('allforum');
  135. $user->increment('point');
  136. $user->increment('money', 5);
  137.  
  138. /** @var Topic $topic */
  139. $topic = Topic::query()->create([
  140. 'forum_id' => $forum->id,
  141. 'title' => $title,
  142. 'user_id' => getUser('id'),
  143. 'count_posts' => 1,
  144. 'created_at' => SITETIME,
  145. 'updated_at' => SITETIME,
  146. ]);
  147.  
  148. /** @var Post $post */
  149. $post = Post::query()->create([
  150. 'topic_id' => $topic->id,
  151. 'user_id' => getUser('id'),
  152. 'text' => $msg,
  153. 'created_at' => SITETIME,
  154. 'ip' => getIp(),
  155. 'brow' => getBrowser(),
  156. ]);
  157.  
  158. Topic::query()->where('id', $topic->id)->update(['last_post_id' => $post->id]);
  159.  
  160. $forum->update([
  161. 'count_topics' => DB::connection()->raw('count_topics + 1'),
  162. 'count_posts' => DB::connection()->raw('count_posts + 1'),
  163. 'last_topic_id' => $topic->id,
  164. ]);
  165.  
  166. // Обновление родительского форума
  167. if ($forum->parent->id) {
  168. $forum->parent->update([
  169. 'last_topic_id' => $topic->id
  170. ]);
  171. }
  172.  
  173. // Создание голосования
  174. if ($vote) {
  175. /** @var Vote $vote */
  176. $vote = Vote::query()->create([
  177. 'title' => $question,
  178. 'topic_id' => $topic->id,
  179. 'created_at' => SITETIME,
  180. ]);
  181.  
  182. $prepareAnswers = [];
  183. foreach ($answers as $answer) {
  184. $prepareAnswers[] = [
  185. 'vote_id' => $vote->id,
  186. 'answer' => $answer
  187. ];
  188. }
  189.  
  190. VoteAnswer::query()->insert($prepareAnswers);
  191. }
  192.  
  193. clearCache(['statForums', 'recentTopics']);
  194. $flood->saveState();
  195.  
  196. setFlash('success', __('forums.topic_success_created'));
  197. redirect('/topics/'.$topic->id);
  198. } else {
  199. setInput($request->all());
  200. setFlash('danger', $validator->getErrors());
  201. }
  202. }
  203.  
  204. return view('forums/forum_create', compact('forums', 'fid'));
  205. }
  206.  
  207. /**
  208. * RSS всех топиков
  209. *
  210. * @return string
  211. */
  212. public function rss(): string
  213. {
  214. $topics = Topic::query()
  215. ->where('closed', 0)
  216. ->with('lastPost.user')
  217. ->orderByDesc('updated_at')
  218. ->limit(15)
  219. ->get();
  220.  
  221. if ($topics->isEmpty()) {
  222. abort('default', __('forums.topics_not_created'));
  223. }
  224.  
  225. return view('forums/rss', compact('topics'));
  226. }
  227.  
  228. /**
  229. * RSS постов
  230. *
  231. * @param int $id
  232. * @return string
  233. */
  234. public function rssPosts(int $id): string
  235. {
  236. /** @var Topic $topic */
  237. $topic = Topic::query()->find($id);
  238.  
  239. if (! $topic) {
  240. abort(404, __('forums.topic_not_exist'));
  241. }
  242.  
  243. $posts = Post::query()
  244. ->where('topic_id', $topic->id)
  245. ->orderByDesc('created_at')
  246. ->with('user')
  247. ->limit(15)
  248. ->get();
  249.  
  250. return view('forums/rss_posts', compact('topic', 'posts'));
  251. }
  252.  
  253. /**
  254. * Последние темы
  255. *
  256. * @return string
  257. */
  258. public function topTopics(): string
  259. {
  260. $topics = Topic::query()
  261. ->orderByDesc('count_posts')
  262. ->orderByDesc('updated_at')
  263. ->with('forum', 'user', 'lastPost.user')
  264. ->limit(100)
  265. ->get()
  266. ->all();
  267.  
  268. $topics = paginate($topics, setting('forumtem'));
  269.  
  270. return view('forums/top', compact('topics'));
  271. }
  272.  
  273. /**
  274. * Последние сообщения
  275. *
  276. * @param Request $request
  277. * @return string
  278. */
  279. public function topPosts(Request $request): string
  280. {
  281. $period = int($request->input('period'));
  282.  
  283. $posts = Post::query()
  284. ->when($period, static function (Builder $query) use ($period) {
  285. return $query->where('created_at', '>', strtotime('-' . $period . ' day', SITETIME));
  286. })
  287. ->orderByDesc('rating')
  288. ->orderByDesc('created_at')
  289. ->with('topic', 'user')
  290. ->limit(100)
  291. ->get()
  292. ->all();
  293.  
  294. $posts = paginate($posts, setting('forumpost'), ['period' => $period]);
  295.  
  296. return view('forums/top_posts', compact('posts', 'period'));
  297. }
  298. }