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

Размер файла: 4.05Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Advert;
  9. use App\Models\User;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\View\View;
  13.  
  14. class AdvertController extends Controller
  15. {
  16. /**
  17. * @var User
  18. */
  19. public $user;
  20.  
  21. /**
  22. * Конструктор
  23. */
  24. public function __construct()
  25. {
  26. $this->middleware('check.user');
  27.  
  28. $this->middleware(function ($request, $next) {
  29. $this->user = getUser();
  30.  
  31. return $next($request);
  32. });
  33.  
  34. if (! setting('rekusershow')) {
  35. abort(200, __('adverts.advert_closed'));
  36. }
  37. }
  38.  
  39. /**
  40. * Главная страница
  41. *
  42. * @return View
  43. */
  44. public function index(): View
  45. {
  46. $adverts = Advert::query()
  47. ->where('deleted_at', '>', SITETIME)
  48. ->orderByDesc('deleted_at')
  49. ->with('user')
  50. ->paginate(setting('rekuserpost'));
  51.  
  52. return view('adverts/index', compact('adverts'));
  53. }
  54.  
  55. /**
  56. * Покупка рекламы
  57. *
  58. * @param Request $request
  59. * @param Validator $validator
  60. *
  61. * @return View|RedirectResponse
  62. */
  63. public function create(Request $request, Validator $validator)
  64. {
  65. if ($this->user->point < setting('rekuserpoint')) {
  66. abort(200, __('adverts.advert_point', ['point' => plural(50, setting('scorename'))]));
  67. }
  68.  
  69. $total = Advert::query()->where('deleted_at', '>', SITETIME)->count();
  70. if ($total >= setting('rekusertotal')) {
  71. abort(200, __('adverts.advert_not_seats'));
  72. }
  73.  
  74. $advert = Advert::query()
  75. ->where('user_id', $this->user->id)
  76. ->where('deleted_at', '>', SITETIME)
  77. ->first();
  78.  
  79. if ($advert) {
  80. abort(200, __('adverts.advert_already_posted'));
  81. }
  82.  
  83. if ($request->isMethod('post')) {
  84. $site = $request->input('site');
  85. $name = $request->input('name');
  86. $color = $request->input('color');
  87. $bold = empty($request->input('bold')) ? 0 : 1;
  88.  
  89. $price = setting('rekuserprice');
  90.  
  91. if ($color) {
  92. $price += setting('rekuseroptprice');
  93. }
  94.  
  95. if ($bold) {
  96. $price += setting('rekuseroptprice');
  97. }
  98.  
  99. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  100. ->gte($this->user->point, setting('rekuserpoint'), __('adverts.advert_point', ['point' => plural(50, setting('scorename'))]))
  101. ->true(captchaVerify(), ['protect' => __('validator.captcha')])
  102. ->regex($site, '|^https?://([а-яa-z0-9_\-\.])+(\.([а-яa-z0-9\/\-?_=#])+)+$|iu', ['site' => __('validator.url')])
  103. ->length($site, 5, 100, ['site' => __('validator.url_text')])
  104. ->length($name, 5, 35, ['name' => __('validator.text')])
  105. ->regex($color, '|^#+[A-f0-9]{6}$|', ['color' => __('validator.color')], false)
  106. ->gte($this->user->money, $price, __('adverts.advert_not_money'));
  107.  
  108. if ($validator->isValid()) {
  109. Advert::query()->where('deleted_at', '<', SITETIME)->delete();
  110.  
  111. Advert::query()->create([
  112. 'site' => $site,
  113. 'name' => $name,
  114. 'color' => $color,
  115. 'bold' => $bold,
  116. 'user_id' => $this->user->id,
  117. 'created_at' => SITETIME,
  118. 'deleted_at' => strtotime('+' . setting('rekusertime') . ' hours', SITETIME),
  119. ]);
  120.  
  121. $this->user->decrement('money', $price);
  122.  
  123. clearCache('adverts');
  124. setFlash('success', __('adverts.advert_success_posted'));
  125.  
  126. return redirect('adverts');
  127. }
  128.  
  129. setInput($request->all());
  130. setFlash('danger', $validator->getErrors());
  131. }
  132.  
  133. return view('adverts/create');
  134. }
  135. }