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

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