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

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