View file app/Http/Controllers/VoteController.php

File size: 7.08Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Polling;
  9. use App\Models\Vote;
  10. use App\Models\VoteAnswer;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Arr;
  14. use Illuminate\View\View;
  15.  
  16. class VoteController extends Controller
  17. {
  18. /**
  19. * Главная страница
  20. *
  21. * @return View
  22. */
  23. public function index(): View
  24. {
  25. $votes = Vote::query()
  26. ->where('closed', 0)
  27. ->orderByDesc('created_at')
  28. ->with('topic')
  29. ->paginate(setting('allvotes'));
  30.  
  31. return view('votes/index', compact('votes'));
  32. }
  33.  
  34. /**
  35. * Просмотр голосования
  36. *
  37. * @param int $id
  38. * @param Request $request
  39. * @param Validator $validator
  40. *
  41. * @return View|RedirectResponse
  42. */
  43. public function view(int $id, Request $request, Validator $validator)
  44. {
  45. $show = $request->input('show');
  46.  
  47. /** @var Vote $vote */
  48. $vote = Vote::query()->find($id);
  49.  
  50. if (! $vote) {
  51. abort(404, __('votes.voting_not_exist'));
  52. }
  53.  
  54. if ($vote->closed) {
  55. abort(200, __('votes.voting_closed'));
  56. }
  57.  
  58. $vote->answers = VoteAnswer::query()
  59. ->where('vote_id', $vote->id)
  60. ->orderBy('id')
  61. ->get();
  62.  
  63. if ($vote->answers->isEmpty()) {
  64. abort(200, __('votes.voting_not_answers'));
  65. }
  66.  
  67. $vote->poll = $vote->pollings()
  68. ->where('user_id', getUser('id'))
  69. ->first();
  70.  
  71. if ($request->isMethod('post')) {
  72. $poll = int($request->input('poll'));
  73.  
  74. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  75. ->empty($vote->poll, __('votes.voting_passed'))
  76. ->notEmpty($poll, __('votes.answer_not_chosen'));
  77.  
  78. if ($validator->isValid()) {
  79. $answer = $vote->answers()
  80. ->where('id', $poll)
  81. ->where('vote_id', $vote->id)
  82. ->first();
  83. $validator->notEmpty($answer, __('votes.answer_not_found'));
  84. }
  85.  
  86. if ($validator->isValid()) {
  87. $vote->increment('count');
  88. $answer->increment('result');
  89.  
  90. Polling::query()->create([
  91. 'relate_type' => Vote::$morphName,
  92. 'relate_id' => $vote->id,
  93. 'user_id' => getUser('id'),
  94. 'vote' => $answer->answer,
  95. 'created_at' => SITETIME,
  96. ]);
  97.  
  98. setFlash('success', __('votes.voting_success'));
  99.  
  100. return redirect('votes/'.$vote->id);
  101. }
  102.  
  103. setInput($request->all());
  104. setFlash('danger', $validator->getErrors());
  105. }
  106.  
  107. $voted = Arr::pluck($vote->answers, 'result', 'answer');
  108. $max = max($voted);
  109.  
  110. arsort($voted);
  111.  
  112. $info['voted'] = $voted;
  113. $info['sum'] = $vote->count > 0 ? $vote->count : 1;
  114. $info['max'] = $max > 0 ? $max : 1;
  115.  
  116. return view('votes/view', compact('vote', 'show', 'info'));
  117. }
  118.  
  119. /**
  120. * Проголосовавшие
  121. *
  122. * @param int $id
  123. *
  124. * @return View
  125. */
  126. public function voters(int $id): View
  127. {
  128. /** @var Vote $vote */
  129. $vote = Vote::query()->find($id);
  130.  
  131. if (! $vote) {
  132. abort(404, __('votes.voting_not_exist'));
  133. }
  134.  
  135. $voters = $vote->pollings()
  136. ->limit(50)
  137. ->with('user')
  138. ->get();
  139.  
  140. return view('votes/voters', compact('vote', 'voters'));
  141. }
  142.  
  143. /**
  144. * История голосований
  145. *
  146. * @return View
  147. */
  148. public function history(): View
  149. {
  150. $votes = Vote::query()
  151. ->where('closed', 1)
  152. ->orderByDesc('created_at')
  153. ->with('topic')
  154. ->paginate(setting('allvotes'));
  155.  
  156. return view('votes/history', compact('votes'));
  157. }
  158.  
  159. /**
  160. * Результаты истории голосований
  161. *
  162. * @param int $id
  163. *
  164. * @return View
  165. */
  166. public function viewHistory(int $id): View
  167. {
  168. /** @var Vote $vote */
  169. $vote = Vote::query()->find($id);
  170.  
  171. if (! $vote) {
  172. abort(404, __('votes.voting_not_exist'));
  173. }
  174.  
  175. if (! $vote->closed) {
  176. abort(200, __('votes.voting_not_archive'));
  177. }
  178.  
  179. $vote->answers = VoteAnswer::query()
  180. ->where('vote_id', $vote->id)
  181. ->orderBy('id')
  182. ->get();
  183.  
  184. if ($vote->answers->isEmpty()) {
  185. abort(200, __('votes.voting_not_answers'));
  186. }
  187.  
  188. $voted = Arr::pluck($vote->answers, 'result', 'answer');
  189. $max = max($voted);
  190.  
  191. arsort($voted);
  192.  
  193. $info['voted'] = $voted;
  194. $info['sum'] = $vote->count > 0 ? $vote->count : 1;
  195. $info['max'] = $max > 0 ? $max : 1;
  196.  
  197. return view('votes/view_history', compact('vote', 'info'));
  198. }
  199.  
  200. /**
  201. * Создание голосования
  202. *
  203. * @param Request $request
  204. * @param Validator $validator
  205. *
  206. * @return View|RedirectResponse
  207. */
  208. public function create(Request $request, Validator $validator)
  209. {
  210. if ($request->isMethod('post')) {
  211. $question = $request->input('question');
  212. $description = $request->input('description');
  213. $answers = (array) $request->input('answers');
  214.  
  215. $answers = array_unique(array_diff($answers, ['']));
  216.  
  217. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  218. ->length($question, 5, 100, ['question' => __('validator.text')])
  219. ->length($description, 5, 1000, ['description' => __('validator.text')], false)
  220. ->between(count($answers), 2, 10, ['answer' => __('votes.answer_not_enough')]);
  221.  
  222. foreach ($answers as $answer) {
  223. if (utfStrlen($answer) > 50) {
  224. $validator->addError(['answer' => __('votes.answer_wrong_length')]);
  225. break;
  226. }
  227. }
  228.  
  229. if ($validator->isValid()) {
  230. /** @var Vote $vote */
  231. $vote = Vote::query()->create([
  232. 'title' => $question,
  233. 'description' => $description,
  234. 'created_at' => SITETIME,
  235. ]);
  236.  
  237. $prepareAnswers = [];
  238. foreach ($answers as $answer) {
  239. $prepareAnswers[] = [
  240. 'vote_id' => $vote->id,
  241. 'answer' => $answer
  242. ];
  243. }
  244.  
  245. VoteAnswer::query()->insert($prepareAnswers);
  246.  
  247. setFlash('success', __('votes.voting_success_created'));
  248.  
  249. return redirect('votes/' . $vote->id);
  250. }
  251.  
  252. setInput($request->all());
  253. setFlash('danger', $validator->getErrors());
  254. }
  255.  
  256. return view('votes/create');
  257. }
  258. }