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

Размер файла: 2.98Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Admin;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Comment;
  9. use App\Models\Guestbook;
  10. use App\Models\Message;
  11. use App\Models\Post;
  12. use App\Models\Spam;
  13. use App\Models\User;
  14. use App\Models\Wall;
  15. use Exception;
  16. use Illuminate\Http\Request;
  17.  
  18. class SpamController extends AdminController
  19. {
  20. /**
  21. * @var array
  22. */
  23. private $types;
  24.  
  25. /**
  26. * @var string
  27. */
  28. private $type;
  29.  
  30. /**
  31. * @var array
  32. */
  33. private $total = [];
  34.  
  35. public function __construct()
  36. {
  37. parent::__construct();
  38.  
  39. if (! isAdmin(User::MODER)) {
  40. abort(403, __('errors.forbidden'));
  41. }
  42.  
  43. $this->types = [
  44. 'posts' => __('index.forums'),
  45. 'guestbook' => __('index.guestbook'),
  46. 'messages' => __('index.messages'),
  47. 'walls' => __('index.wall_posts'),
  48. 'comments' => __('main.comments'),
  49. ];
  50.  
  51. $type = request()->input('type');
  52. $this->type = isset($this->types[$type]) ? $type : 'posts';
  53.  
  54. $spam = Spam::query()
  55. ->selectRaw('relate_type, count(*) as total')
  56. ->groupBy('relate_type')
  57. ->pluck('total', 'relate_type')
  58. ->all();
  59.  
  60. foreach ($this->types as $key => $value) {
  61. $this->total[$key] = $spam[$key] ?? 0;
  62. }
  63. }
  64.  
  65. /**
  66. * Главная страница
  67. *
  68. * @return string
  69. */
  70. public function index(): string
  71. {
  72. $type = $this->type;
  73. $types = $this->types;
  74.  
  75. /** @var Spam $records */
  76. $records = Spam::query()
  77. ->where('relate_type', $type)
  78. ->orderByDesc('created_at')
  79. ->with('user')
  80. ->paginate(setting('spamlist'))
  81. ->appends(['type' => $type]);
  82.  
  83. if (in_array($type, ['message', 'wall'])) {
  84. $records->load('relate.author');
  85. } else {
  86. $records->load('relate.user');
  87. }
  88.  
  89. $total = $this->total;
  90.  
  91. return view('admin/spam/index', compact('records', 'total', 'type', 'types'));
  92. }
  93.  
  94. /**
  95. * Удаление жалоб
  96. *
  97. * @param Request $request
  98. * @param Validator $validator
  99. *
  100. * @return void
  101. * @throws Exception
  102. */
  103. public function delete(Request $request, Validator $validator): void
  104. {
  105. $id = int($request->input('id'));
  106.  
  107. $validator
  108. ->true($request->ajax(), __('validator.not_ajax'))
  109. ->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  110. ->notEmpty($id, __('validator.deletion'));
  111.  
  112. if ($validator->isValid()) {
  113. $spam = Spam::query()->find($id);
  114.  
  115. if ($spam) {
  116. $spam->delete();
  117. }
  118.  
  119. echo json_encode(['status' => 'success']);
  120. } else {
  121. echo json_encode([
  122. 'status' => 'error',
  123. 'message' => current($validator->getErrors())
  124. ]);
  125. }
  126. }
  127. }