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

Размер файла: 6.06Kb
  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\User;
  9. use App\Models\Vote;
  10. use App\Models\VoteAnswer;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Http\Request;
  14. use Illuminate\View\View;
  15. use Throwable;
  16.  
  17. class VoteController extends AdminController
  18. {
  19. /**
  20. * Главная страница
  21. *
  22. * @return View
  23. */
  24. public function index(): View
  25. {
  26. $votes = Vote::query()
  27. ->where('closed', 0)
  28. ->orderByDesc('created_at')
  29. ->with('topic')
  30. ->paginate(setting('allvotes'));
  31.  
  32. return view('admin/votes/index', compact('votes'));
  33. }
  34.  
  35. /**
  36. * Архив голосований
  37. *
  38. * @return View
  39. */
  40. public function history(): View
  41. {
  42. $votes = Vote::query()
  43. ->where('closed', 1)
  44. ->orderByDesc('created_at')
  45. ->with('topic')
  46. ->paginate(setting('allvotes'));
  47.  
  48. return view('admin/votes/history', compact('votes'));
  49. }
  50.  
  51. /**
  52. * Редактирование голосования
  53. *
  54. * @param int $id
  55. * @param Request $request
  56. * @param Validator $validator
  57. *
  58. * @return View|RedirectResponse
  59. */
  60. public function edit(int $id, Request $request, Validator $validator)
  61. {
  62. $vote = Vote::query()->where('id', $id)->first();
  63.  
  64. if (! $vote) {
  65. abort(404, __('votes.voting_not_exist'));
  66. }
  67.  
  68. if ($request->isMethod('post')) {
  69. $question = $request->input('question');
  70. $description = $request->input('description');
  71. $answers = (array) $request->input('answers');
  72.  
  73. $answers = array_unique(array_diff($answers, ['']));
  74.  
  75. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  76. ->length($question, 3, 100, ['question' => __('validator.text')])
  77. ->length($description, 5, 1000, ['description' => __('validator.text')], false)
  78. ->between(count($answers), 2, 10, ['answer' => __('votes.answer_not_enough')]);
  79.  
  80. foreach ($answers as $answer) {
  81. if (utfStrlen($answer) > 50) {
  82. $validator->addError(['answers' => __('votes.answer_wrong_length')]);
  83. break;
  84. }
  85. }
  86.  
  87. if ($validator->isValid()) {
  88. $vote->update([
  89. 'title' => $question,
  90. 'description' => $description,
  91. ]);
  92.  
  93. $countAnswers = $vote->answers()->count();
  94.  
  95. foreach ($answers as $answerId => $answer) {
  96. /** @var VoteAnswer $ans */
  97. $ans = $vote->answers()->firstOrNew(['id' => $answerId]);
  98.  
  99. if ($ans->exists) {
  100. $ans->update(['answer' => $answer]);
  101. } elseif ($countAnswers < 10) {
  102. $ans->fill(['answer' => $answer])->save();
  103. $countAnswers++;
  104. }
  105. }
  106.  
  107. setFlash('success', __('votes.voting_success_changed'));
  108.  
  109. return redirect('admin/votes/edit/'.$vote->id);
  110. }
  111.  
  112. setInput($request->all());
  113. setFlash('danger', $validator->getErrors());
  114. }
  115.  
  116. $vote->getAnswers = $vote->answers->pluck('answer', 'id')->all();
  117.  
  118. return view('admin/votes/edit', compact('vote'));
  119. }
  120.  
  121. /**
  122. * Удаление голосования
  123. *
  124. * @param int $id
  125. * @param Request $request
  126. *
  127. * @return RedirectResponse
  128. * @throws Throwable
  129. */
  130. public function delete(int $id, Request $request): RedirectResponse
  131. {
  132. $vote = Vote::query()->where('id', $id)->first();
  133.  
  134. if (! $vote) {
  135. abort(404, __('votes.voting_not_exist'));
  136. }
  137.  
  138. if (! isAdmin(User::BOSS)) {
  139. abort(403, __('errors.forbidden'));
  140. }
  141.  
  142. if ($request->input('_token') === csrf_token()) {
  143. DB::transaction(static function () use ($vote) {
  144. $vote->delete();
  145. $vote->answers()->delete();
  146. $vote->pollings()->delete();
  147. });
  148.  
  149. setFlash('success', __('votes.voting_success_deleted'));
  150. } else {
  151. setFlash('danger', __('validator.token'));
  152. }
  153.  
  154. return redirect('admin/votes');
  155. }
  156.  
  157. /**
  158. * Открытие-закрытие голосования
  159. *
  160. * @param int $id
  161. * @param Request $request
  162. *
  163. * @return RedirectResponse
  164. */
  165. public function close(int $id, Request $request): RedirectResponse
  166. {
  167. $vote = Vote::query()->where('id', $id)->first();
  168.  
  169. if (! $vote) {
  170. abort(404, __('votes.voting_not_exist'));
  171. }
  172.  
  173. if ($request->input('_token') === csrf_token()) {
  174. $status = __('votes.voting_success_open');
  175. $closed = $vote->closed ^ 1;
  176.  
  177. $vote->update([
  178. 'closed' => $closed,
  179. ]);
  180.  
  181. if ($closed) {
  182. $vote->pollings()->delete();
  183. $status = __('votes.voting_success_closed');
  184. }
  185.  
  186. setFlash('success', $status);
  187. } else {
  188. setFlash('danger', __('validator.token'));
  189. }
  190.  
  191. if (empty($closed)) {
  192. return redirect('admin/votes');
  193. }
  194.  
  195. return redirect('admin/votes/history');
  196. }
  197.  
  198. /**
  199. * Пересчет голосов
  200. *
  201. * @param Request $request
  202. *
  203. * @return RedirectResponse
  204. */
  205. public function restatement(Request $request): RedirectResponse
  206. {
  207. if (! isAdmin(User::BOSS)) {
  208. abort(403, __('errors.forbidden'));
  209. }
  210.  
  211. if ($request->input('_token') === csrf_token()) {
  212. restatement('votes');
  213.  
  214. setFlash('success', __('main.success_recounted'));
  215. } else {
  216. setFlash('danger', __('validator.token'));
  217. }
  218.  
  219. return redirect('admin/votes');
  220. }
  221. }