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

Размер файла: 3.75Kb
  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\BlackList;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\View\View;
  12.  
  13. class BlacklistController extends AdminController
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $type;
  19.  
  20. /**
  21. * Конструктор
  22. *
  23. * @param Request $request
  24. */
  25. public function __construct(Request $request)
  26. {
  27. $types = ['email', 'login', 'domain'];
  28.  
  29. $this->type = $request->input('type', 'email');
  30.  
  31. if (! in_array($this->type, $types, true)) {
  32. abort(404, __('admin.blacklists.type_not_found'));
  33. }
  34. }
  35.  
  36. /**
  37. * Главная страница
  38. *
  39. * @param Request $request
  40. * @param Validator $validator
  41. *
  42. * @return View|RedirectResponse
  43. */
  44. public function index(Request $request, Validator $validator)
  45. {
  46. $type = $this->type;
  47.  
  48. if ($request->isMethod('post')) {
  49. $value = utfLower($request->input('value'));
  50.  
  51. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  52. ->length($value, 1, 100, ['value' => __('validator.text')]);
  53.  
  54. if ($type === 'email') {
  55. $validator->regex($value, '#^([a-z0-9_\-\.])+\@([a-z0-9_\-\.])+(\.([a-z0-9])+)+$#', ['value' => __('validator.email')]);
  56. }
  57.  
  58. if ($type === 'login') {
  59. $validator->regex($value, '|^[a-z0-9\-]+$|', ['value' => __('admin.blacklists.invalid_login')])
  60. ->length($value, 3, 20, ['value' => __('validator.text')]);
  61. }
  62.  
  63. if ($type === 'domain') {
  64. $value = siteDomain($value);
  65. $validator->regex($value, '#([а-яa-z0-9_\-\.])+(\.([а-яa-z0-9\/])+)+$#u', ['value' => __('validator.site')]);
  66. }
  67.  
  68. $duplicate = BlackList::query()->where('type', $type)->where('value', $value)->first();
  69. $validator->empty($duplicate, ['value' => __('main.record_exists')]);
  70.  
  71. if ($validator->isValid()) {
  72. BlackList::query()->create([
  73. 'type' => $type,
  74. 'value' => $value,
  75. 'user_id' => getUser('id'),
  76. 'created_at' => SITETIME,
  77. ]);
  78.  
  79. setFlash('success', __('main.record_added_success'));
  80.  
  81. return redirect('admin/blacklists?type=' . $type);
  82. }
  83.  
  84. setInput($request->all());
  85. setFlash('danger', $validator->getErrors());
  86. }
  87.  
  88. $lists = BlackList::query()
  89. ->where('type', $type)
  90. ->orderByDesc('created_at')
  91. ->with('user')
  92. ->paginate(setting('blacklist'))
  93. ->appends(['type' => $type]);
  94.  
  95. return view('admin/blacklists/index', compact('lists', 'type'));
  96. }
  97.  
  98. /**
  99. * Удаление записей
  100. *
  101. * @param Request $request
  102. * @param Validator $validator
  103. *
  104. * @return RedirectResponse
  105. */
  106. public function delete(Request $request, Validator $validator): RedirectResponse
  107. {
  108. $page = int($request->input('page', 1));
  109. $del = intar($request->input('del'));
  110. $type = $this->type;
  111.  
  112. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  113. ->true($del, __('validator.deletion'));
  114.  
  115. if ($validator->isValid()) {
  116. BlackList::query()->where('type', $type)->whereIn('id', $del)->delete();
  117.  
  118. setFlash('success', __('main.records_deleted_success'));
  119. } else {
  120. setFlash('danger', $validator->getErrors());
  121. }
  122.  
  123. return redirect('admin/blacklists?type=' . $type . '&page=' . $page);
  124. }
  125. }