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

Размер файла: 2.31Kb
  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\User;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\View\View;
  12.  
  13. class DelUserController extends AdminController
  14. {
  15. /**
  16. * Главная страница
  17. *
  18. * @param Request $request
  19. *
  20. * @return View
  21. */
  22. public function index(Request $request): View
  23. {
  24. $users = collect();
  25. $period = int($request->input('period'));
  26. $point = int($request->input('point'));
  27.  
  28. if ($request->isMethod('post')) {
  29. if ($period < 180) {
  30. abort(200, __('admin.delusers.invalid_period'));
  31. }
  32.  
  33. $users = User::query()
  34. ->where('updated_at', '<', strtotime('-' . $period . ' days', SITETIME))
  35. ->where('point', '<=', $point)
  36. ->get();
  37.  
  38. if ($users->isEmpty()) {
  39. abort(200, __('admin.delusers.users_not_found'));
  40. }
  41. }
  42.  
  43. $total = User::query()->count();
  44.  
  45. return view('admin/delusers/index', compact('users', 'total', 'period', 'point'));
  46. }
  47.  
  48. /**
  49. * Очистка пользователей
  50. *
  51. * @param Request $request
  52. * @param Validator $validator
  53. *
  54. * @return RedirectResponse
  55. */
  56. public function clear(Request $request, Validator $validator): RedirectResponse
  57. {
  58. $period = int($request->input('period'));
  59. $point = int($request->input('point'));
  60.  
  61. $validator
  62. ->equal($request->input('_token'), csrf_token(), __('validator.token'))
  63. ->gte($period, 180, __('admin.delusers.invalid_period'));
  64.  
  65. $users = User::query()
  66. ->where('updated_at', '<', strtotime('-' . $period . ' days', SITETIME))
  67. ->where('point', '<=', $point)
  68. ->get();
  69.  
  70. $validator->true($users->isNotEmpty(), __('admin.delusers.users_not_found'));
  71.  
  72. if ($validator->isValid()) {
  73. foreach ($users as $user) {
  74. $user->deleteAlbum();
  75. $user->delete();
  76. }
  77.  
  78. setFlash('success', __('admin.delusers.success_deleted'));
  79. } else {
  80. setFlash('danger', $validator->getErrors());
  81. }
  82.  
  83. return redirect('admin/delusers');
  84. }
  85. }