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

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