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

Размер файла: 5.59Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Rating;
  9. use App\Models\User;
  10. use Exception;
  11. use Illuminate\Http\JsonResponse;
  12. use Illuminate\Http\RedirectResponse;
  13. use Illuminate\Http\Request;
  14. use Illuminate\View\View;
  15.  
  16. class RatingController extends Controller
  17. {
  18. /**
  19. * @var User
  20. */
  21. public $user;
  22.  
  23. /**
  24. * Конструктор
  25. */
  26. public function __construct()
  27. {
  28. $this->middleware('check.user');
  29.  
  30. $this->middleware(function ($request, $next) {
  31. $this->user = getUser();
  32.  
  33. return $next($request);
  34. });
  35. }
  36.  
  37. /**
  38. * Изменение рейтинга
  39. *
  40. * @param string $login
  41. * @param Request $request
  42. * @param Validator $validator
  43. *
  44. * @return View|RedirectResponse
  45. */
  46. public function index(string $login, Request $request, Validator $validator)
  47. {
  48. $vote = $request->input('vote');
  49. $user = getUserByLogin($login);
  50.  
  51. if (! $user) {
  52. abort(404, __('validator.user'));
  53. }
  54.  
  55. if ($this->user->id === $user->id) {
  56. abort(200, __('ratings.reputation_yourself'));
  57. }
  58.  
  59. if ($this->user->point < setting('editratingpoint')) {
  60. abort(200, __('ratings.reputation_point', ['point' => plural(setting('editratingpoint'), setting('scorename'))]));
  61. }
  62.  
  63. // Голосовать за того же пользователя можно через 90 дней
  64. $getRating = Rating::query()
  65. ->where('user_id', $this->user->id)
  66. ->where('recipient_id', $user->id)
  67. ->where('created_at', '>', strtotime('-3 month', SITETIME))
  68. ->first();
  69.  
  70. if ($getRating) {
  71. abort(200, __('ratings.reputation_already_changed'));
  72. }
  73.  
  74. if ($request->isMethod('post')) {
  75. $text = $request->input('text');
  76.  
  77. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  78. ->length($text, 5, 250, ['text' => __('validator.text')]);
  79.  
  80. if ($vote === 'minus' && $this->user->rating < 1) {
  81. $validator->addError(__('ratings.reputation_positive'));
  82. }
  83.  
  84. if ($validator->isValid()) {
  85. $text = antimat($text);
  86.  
  87. Rating::query()->create([
  88. 'user_id' => $this->user->id,
  89. 'recipient_id' => $user->id,
  90. 'text' => $text,
  91. 'vote' => $vote === 'plus' ? '+' : '-',
  92. 'created_at' => SITETIME,
  93. ]);
  94.  
  95. if ($vote === 'plus') {
  96. $user->increment('posrating');
  97. $user->update(['rating' => $user->posrating - $user->negrating]);
  98. } else {
  99. $user->increment('negrating');
  100. $user->update(['rating' => $user->posrating - $user->negrating]);
  101. }
  102.  
  103. $message = textNotice('rating', ['login' => $this->user->login, 'rating' => $user->rating, 'comment' => $text, 'vote' => __('main.' . $vote)]);
  104. $user->sendMessage(null, $message);
  105.  
  106. setFlash('success', __('ratings.reputation_success_changed'));
  107.  
  108. return redirect('users/'.$user->login);
  109. }
  110.  
  111. setInput($request->all());
  112. setFlash('danger', $validator->getErrors());
  113. }
  114.  
  115. return view('ratings/index', compact('user', 'vote'));
  116. }
  117.  
  118. /**
  119. * Полученные голоса
  120. *
  121. * @param string $login
  122. *
  123. * @return View
  124. */
  125. public function received(string $login): View
  126. {
  127. $user = getUserByLogin($login);
  128.  
  129. if (! $user) {
  130. abort(404, __('validator.user'));
  131. }
  132.  
  133. $ratings = Rating::query()
  134. ->where('recipient_id', $user->id)
  135. ->orderByDesc('created_at')
  136. ->with('user')
  137. ->paginate(setting('ratinglist'));
  138.  
  139. return view('ratings/rathistory', compact('ratings', 'user'));
  140. }
  141.  
  142. /**
  143. * Отданные голоса
  144. *
  145. * @param string $login
  146. *
  147. * @return View
  148. */
  149. public function gave(string $login): View
  150. {
  151. $user = getUserByLogin($login);
  152.  
  153. if (! $user) {
  154. abort(404, __('validator.user'));
  155. }
  156.  
  157. $ratings = Rating::query()
  158. ->where('user_id', $user->id)
  159. ->orderByDesc('created_at')
  160. ->with('recipient')
  161. ->paginate(setting('ratinglist'));
  162.  
  163. return view('ratings/rathistory_gave', compact('ratings', 'user'));
  164. }
  165.  
  166. /**
  167. * Удаление истории
  168. *
  169. * @param Request $request
  170. * @param Validator $validator
  171. *
  172. * @return JsonResponse
  173. * @throws Exception
  174. */
  175. public function delete(Request $request, Validator $validator): JsonResponse
  176. {
  177. $id = int($request->input('id'));
  178.  
  179. $validator
  180. ->true($request->ajax(), __('validator.not_ajax'))
  181. ->true(isAdmin(User::ADMIN), __('main.page_only_admins'))
  182. ->equal($request->input('_token'), csrf_token(), __('validator.token'))
  183. ->notEmpty($id, [__('validator.deletion')]);
  184.  
  185. if ($validator->isValid()) {
  186. $rating = Rating::query()->find($id);
  187.  
  188. if ($rating) {
  189. $rating->delete();
  190. }
  191.  
  192. return response()->json(['success' => true]);
  193. }
  194.  
  195. return response()->json([
  196. 'success' => false,
  197. 'message' => current($validator->getErrors()),
  198. ]);
  199. }
  200. }