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

Размер файла: 4.15Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Flood;
  9. use App\Models\Ignore;
  10. use App\Models\Wall;
  11. use Illuminate\Http\Request;
  12.  
  13. class WallController extends BaseController
  14. {
  15. /**
  16. * Главная страница
  17. *
  18. * @param string $login
  19. *
  20. * @return string
  21. */
  22. public function index(string $login): string
  23. {
  24. $user = getUserByLogin($login);
  25.  
  26. if (! $user) {
  27. abort(404, __('validator.user'));
  28. }
  29.  
  30. $newWall = getUser('newwall');
  31.  
  32. $messages = Wall::query()
  33. ->where('user_id', $user->id)
  34. ->orderByDesc('created_at')
  35. ->with('user', 'author')
  36. ->paginate(setting('wallpost'));
  37.  
  38. if ($newWall && getUser('id') === $user->id) {
  39. $user->update([
  40. 'newwall' => 0,
  41. ]);
  42. }
  43.  
  44. return view('walls/index', compact('messages', 'user', 'newWall'));
  45. }
  46.  
  47. /**
  48. * Добавление сообщения
  49. *
  50. * @param string $login
  51. * @param Request $request
  52. * @param Validator $validator
  53. * @param Flood $flood
  54. *
  55. * @return void
  56. */
  57. public function create($login, Request $request, Validator $validator, Flood $flood): void
  58. {
  59. if (! getUser()) {
  60. abort(403, __('main.not_authorized'));
  61. }
  62.  
  63. $user = getUserByLogin($login);
  64.  
  65. if (! $user) {
  66. abort(404, __('validator.user'));
  67. }
  68.  
  69. if ($request->isMethod('post')) {
  70. $msg = $request->input('msg');
  71.  
  72. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  73. ->length($msg, 5, setting('comment_length'), ['msg' => __('validator.text')])
  74. ->false($flood->isFlood(), ['msg' => __('validator.flood', ['sec' => $flood->getPeriod()])]);
  75.  
  76. $ignoring = Ignore::query()
  77. ->where('user_id', $user->id)
  78. ->where('ignore_id', getUser('id'))
  79. ->first();
  80.  
  81. $validator->empty($ignoring, __('ignores.you_are_ignoring'));
  82.  
  83. if ($validator->isValid()) {
  84. if (getUser() && getUser('id') !== $user->id) {
  85. $user->increment('newwall');
  86. }
  87.  
  88. Wall::query()->create([
  89. 'user_id' => $user->id,
  90. 'author_id' => getUser('id'),
  91. 'text' => antimat($msg),
  92. 'created_at' => SITETIME,
  93. ]);
  94.  
  95. $flood->saveState();
  96. sendNotify($msg, '/walls/' . $user->login, __('index.wall_posts_login', ['login' => $user->getName()]));
  97.  
  98. setFlash('success', __('main.record_added_success'));
  99. } else {
  100. setInput($request->all());
  101. setFlash('danger', $validator->getErrors());
  102. }
  103.  
  104. redirect('/walls/' . $user->login);
  105. }
  106. }
  107.  
  108. /**
  109. * Удаление сообщений
  110. *
  111. * @param string $login
  112. * @param Request $request
  113. * @param Validator $validator
  114. *
  115. * @return void
  116. */
  117. public function delete(string $login, Request $request, Validator $validator): void
  118. {
  119. $id = int($request->input('id'));
  120. $user = getUserByLogin($login);
  121.  
  122. if (! $user) {
  123. abort(404, __('validator.user'));
  124. }
  125.  
  126. $validator
  127. ->true($request->ajax(), __('validator.not_ajax'))
  128. ->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  129. ->notEmpty($id, __('validator.deletion'))
  130. ->notEmpty($user, __('validator.user'))
  131. ->true(isAdmin() || getUser('id') === $user->id, __('main.deleted_only_admins'));
  132.  
  133. if ($validator->isValid()) {
  134. Wall::query()
  135. ->where('id', $id)
  136. ->where('user_id', $user->id)
  137. ->delete();
  138.  
  139. echo json_encode(['status' => 'success']);
  140. } else {
  141. echo json_encode([
  142. 'status' => 'error',
  143. 'message' => current($validator->getErrors())
  144. ]);
  145. }
  146. }
  147. }