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

Размер файла: 7.33Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Admin;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\PaidAdvert;
  9. use App\Models\User;
  10. use Exception;
  11. use Illuminate\Http\Request;
  12.  
  13. class PaidAdvertController extends AdminController
  14. {
  15. /**
  16. * Construct
  17. */
  18. public function __construct()
  19. {
  20. parent::__construct();
  21.  
  22. if (! isAdmin(User::BOSS)) {
  23. abort(403, __('errors.forbidden'));
  24. }
  25. }
  26.  
  27. /**
  28. * List adverts
  29. *
  30. * @param Request $request
  31. *
  32. * @return string
  33. */
  34. public function index(Request $request): string
  35. {
  36. $place = check($request->input('place', 'top_all'));
  37.  
  38. if (! in_array($place, PaidAdvert::PLACES, true)) {
  39. $place = 'top_all';
  40. }
  41.  
  42. $advertTotal = PaidAdvert::query()
  43. ->selectRaw('place, count(*) as total')
  44. ->groupBy('place')
  45. ->pluck('total', 'place')
  46. ->all();
  47.  
  48. $totals = [];
  49. $places = PaidAdvert::PLACES;
  50. foreach ($places as $placeName) {
  51. $totals[$placeName] = $advertTotal[$placeName] ?? 0;
  52. }
  53.  
  54. $adverts = PaidAdvert::query()
  55. ->where('place', $place)
  56. ->orderBy('created_at')
  57. ->with('user')
  58. ->get();
  59.  
  60. return view('admin/paid-adverts/index', compact('adverts', 'place', 'places', 'totals'));
  61. }
  62.  
  63. /**
  64. * Create advert
  65. *
  66. * @param Request $request
  67. * @param Validator $validator
  68. *
  69. * @return string
  70. */
  71. public function create(Request $request, Validator $validator): string
  72. {
  73. $places = PaidAdvert::PLACES;
  74. $advert = new PaidAdvert();
  75.  
  76. if ($request->isMethod('post')) {
  77. $site = $request->input('site');
  78. $place = $request->input('place');
  79. $names = (array) $request->input('names');
  80. $color = $request->input('color');
  81. $bold = empty($request->input('bold')) ? 0 : 1;
  82. $term = (string) $request->input('term');
  83. $comment = $request->input('comment');
  84.  
  85. $term = strtotime($term);
  86. $names = array_unique(array_diff($names, ['']));
  87.  
  88. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  89. ->in($place, $places, ['place' => __('admin.paid_adverts.place_invalid')])
  90. ->url($site, ['site' => __('validator.url')])
  91. ->length($site, 5, 100, ['site' => __('validator.url_text')])
  92. ->regex($color, '|^#+[A-f0-9]{6}$|', ['color' => __('validator.color')], false)
  93. ->gt($term, SITETIME, ['term' => __('admin.paid_adverts.term_invalid')])
  94. ->length($comment, 0, 255, ['comment' => __('validator.text_long')])
  95. ->gte(count($names), 1, ['names' => __('admin.paid_adverts.names_count')]);
  96.  
  97. foreach ($names as $name) {
  98. $validator->length($name, 5, 35, ['names' => __('validator.text')]);
  99. }
  100.  
  101. if ($validator->isValid()) {
  102. PaidAdvert::query()->create([
  103. 'user_id' => getUser('id'),
  104. 'place' => $place,
  105. 'site' => $site,
  106. 'names' => array_values($names),
  107. 'color' => $color,
  108. 'bold' => $bold,
  109. 'comment' => $comment,
  110. 'created_at' => SITETIME,
  111. 'deleted_at' => $term,
  112. ]);
  113.  
  114. clearCache('paidAdverts');
  115. setFlash('success', __('main.record_added_success'));
  116. redirect('/admin/paid-adverts?place=' . $place);
  117. } else {
  118. setInput($request->all());
  119. setFlash('danger', $validator->getErrors());
  120. }
  121. }
  122.  
  123. return view('admin/paid-adverts/create', compact('advert', 'places'));
  124. }
  125.  
  126. /**
  127. * Change advert
  128. *
  129. * @param int $id
  130. * @param Request $request
  131. * @param Validator $validator
  132. *
  133. * @return string
  134. */
  135. public function edit(int $id, Request $request, Validator $validator): string
  136. {
  137. $places = PaidAdvert::PLACES;
  138.  
  139. /** @var PaidAdvert $advert */
  140. $advert = PaidAdvert::query()->find($id);
  141.  
  142. if (! $advert) {
  143. abort(404, __('admin.paid_adverts.not_found'));
  144. }
  145.  
  146. if ($request->isMethod('post')) {
  147. $site = $request->input('site');
  148. $place = $request->input('place');
  149. $names = (array) $request->input('names');
  150. $color = $request->input('color');
  151. $bold = empty($request->input('bold')) ? 0 : 1;
  152. $term = (string) $request->input('term');
  153. $comment = $request->input('comment');
  154.  
  155. $term = strtotime($term);
  156. $names = array_unique(array_diff($names, ['']));
  157.  
  158. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  159. ->in($place, $places, ['place' => __('admin.paid_adverts.place_invalid')])
  160. ->url($site, ['site' => __('validator.url')])
  161. ->length($site, 5, 100, ['site' => __('validator.url_text')])
  162. ->regex($color, '|^#+[A-f0-9]{6}$|', ['color' => __('validator.color')], false)
  163. ->gt($term, SITETIME, ['term' => __('admin.paid_adverts.term_invalid')])
  164. ->length($comment, 0, 255, ['comment' => __('validator.text_long')])
  165. ->gte(count($names), 1, ['names' => __('admin.paid_adverts.names_count')]);
  166.  
  167. foreach ($names as $name) {
  168. $validator->length($name, 5, 35, ['names' => __('validator.text')]);
  169. }
  170.  
  171. if ($validator->isValid()) {
  172. $advert->update([
  173. 'place' => $place,
  174. 'site' => $site,
  175. 'names' => array_values($names),
  176. 'color' => $color,
  177. 'bold' => $bold,
  178. 'comment' => $comment,
  179. 'deleted_at' => $term,
  180. ]);
  181.  
  182. clearCache('paidAdverts');
  183. setFlash('success', __('main.record_saved_success'));
  184. redirect('/admin/paid-adverts?place=' . $place);
  185. } else {
  186. setInput($request->all());
  187. setFlash('danger', $validator->getErrors());
  188. }
  189. }
  190.  
  191. return view('admin/paid-adverts/edit', compact('advert', 'places'));
  192. }
  193.  
  194. /**
  195. * Delete adverts
  196. *
  197. * @param int $id
  198. * @param Request $request
  199. * @param Validator $validator
  200. *
  201. * @throws Exception
  202. */
  203. public function delete(int $id, Request $request, Validator $validator): void
  204. {
  205. /** @var PaidAdvert $advert */
  206. $advert = PaidAdvert::query()->find($id);
  207.  
  208. if (! $advert) {
  209. abort(404, __('admin.paid_adverts.not_found'));
  210. }
  211.  
  212. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'));
  213.  
  214. if ($validator->isValid()) {
  215. $advert->delete();
  216.  
  217. clearCache('paidAdverts');
  218. setFlash('success', __('main.record_deleted_success'));
  219. } else {
  220. setFlash('danger', $validator->getErrors());
  221. }
  222.  
  223. redirect('/admin/paid-adverts?place=' . $advert->place);
  224. }
  225. }