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

Размер файла: 10.37Kb
  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\Sticker;
  9. use App\Models\StickersCategory;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Http\Request;
  13. use Illuminate\View\View;
  14.  
  15. class StickerController extends AdminController
  16. {
  17. /**
  18. * Главная страница
  19. *
  20. * @return View
  21. */
  22. public function index(): View
  23. {
  24. $categories = StickersCategory::query()
  25. ->selectRaw('sc.id, sc.name, count(s.id) cnt')
  26. ->from('stickers_categories as sc')
  27. ->leftJoin('stickers as s', 's.category_id', 'sc.id')
  28. ->groupBy('sc.id')
  29. ->orderBy('sc.id')
  30. ->get();
  31.  
  32. return view('admin/stickers/index', compact('categories'));
  33. }
  34.  
  35. /**
  36. * Просмотр стикеров по категориям
  37. *
  38. * @param int $id
  39. *
  40. * @return View
  41. */
  42. public function category(int $id): View
  43. {
  44. $category = StickersCategory::query()->where('id', $id)->first();
  45.  
  46. if (! $category) {
  47. abort(404, __('stickers.category_not_exist'));
  48. }
  49.  
  50. $stickers = Sticker::query()
  51. ->where('category_id', $id)
  52. ->orderBy(DB::raw('CHAR_LENGTH(code)'))
  53. ->orderBy('name')
  54. ->with('category')
  55. ->paginate(setting('stickerlist'));
  56.  
  57. return view('admin/stickers/category', compact('category', 'stickers'));
  58. }
  59.  
  60. /**
  61. * Создание категории
  62. *
  63. * @param Request $request
  64. * @param Validator $validator
  65. *
  66. * @return RedirectResponse
  67. */
  68. public function create(Request $request, Validator $validator): RedirectResponse
  69. {
  70.  
  71. $name = $request->input('name');
  72.  
  73. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  74. ->length($name, 3, 50, ['name' => __('validator.text')]);
  75.  
  76. if ($validator->isValid()) {
  77. /** @var StickersCategory $category */
  78. $category = StickersCategory::query()->create([
  79. 'name' => $name,
  80. 'created_at' => SITETIME,
  81. ]);
  82.  
  83. setFlash('success', __('stickers.category_success_created'));
  84.  
  85. return redirect('admin/stickers/' . $category->id);
  86. }
  87.  
  88. setInput($request->all());
  89. setFlash('danger', $validator->getErrors());
  90.  
  91. return redirect('admin/stickers');
  92. }
  93.  
  94. /**
  95. * Редактирование категории
  96. *
  97. * @param int $id
  98. * @param Request $request
  99. * @param Validator $validator
  100. *
  101. * @return View|RedirectResponse
  102. */
  103. public function edit(int $id, Request $request, Validator $validator)
  104. {
  105. $category = StickersCategory::query()->find($id);
  106.  
  107. if (! $category) {
  108. abort(404, __('stickers.category_not_exist'));
  109. }
  110.  
  111. if ($request->isMethod('post')) {
  112. $name = $request->input('name');
  113.  
  114. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  115. ->length($name, 3, 50, ['name' => __('validator.text')]);
  116.  
  117. if ($validator->isValid()) {
  118. $category->update([
  119. 'name' => $name,
  120. 'updated_at' => SITETIME,
  121. ]);
  122.  
  123. setFlash('success', __('stickers.category_success_changed'));
  124.  
  125. return redirect('admin/stickers');
  126. }
  127.  
  128. setInput($request->all());
  129. setFlash('danger', $validator->getErrors());
  130. }
  131.  
  132. return view('admin/stickers/edit_category', compact('category'));
  133. }
  134.  
  135. /**
  136. * Удаление категории
  137. *
  138. * @param int $id
  139. * @param Request $request
  140. * @param Validator $validator
  141. *
  142. * @return RedirectResponse
  143. */
  144. public function delete(int $id, Request $request, Validator $validator): RedirectResponse
  145. {
  146. /** @var StickersCategory $category */
  147. $category = StickersCategory::query()->find($id);
  148.  
  149. if (! $category) {
  150. abort(404, __('stickers.category_not_exist'));
  151. }
  152.  
  153. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'));
  154.  
  155. $sticker = Sticker::query()->where('category_id', $category->id)->first();
  156. if ($sticker) {
  157. $validator->addError(__('stickers.category_has_stickers'));
  158. }
  159.  
  160. if ($validator->isValid()) {
  161. $category->delete();
  162.  
  163. setFlash('success', __('stickers.category_success_deleted'));
  164. } else {
  165. setFlash('danger', $validator->getErrors());
  166. }
  167.  
  168. return redirect('admin/stickers');
  169. }
  170.  
  171. /**
  172. * Добавление стикера
  173. *
  174. * @param Request $request
  175. * @param Validator $validator
  176. *
  177. * @return View|RedirectResponse
  178. */
  179. public function createSticker(Request $request, Validator $validator)
  180. {
  181. $cid = int($request->input('cid'));
  182.  
  183. $categories = StickersCategory::query()->get();
  184.  
  185. if ($categories->isEmpty()) {
  186. abort(200, __('stickers.empty_categories'));
  187. }
  188.  
  189. if (! is_writable(public_path('uploads/stickers'))) {
  190. abort(200, __('main.directory_not_writable'));
  191. }
  192.  
  193. if ($request->isMethod('post')) {
  194. $code = utfLower($request->input('code'));
  195. $sticker = $request->file('sticker');
  196.  
  197. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  198. ->length($code, 2, 20, ['code' => __('stickers.sticker_length')])
  199. ->regex($code, '|^:+[a-яa-z0-9_\-/\(\)]+$|i', ['code' => __('stickers.sticker_requirements')]);
  200.  
  201. $category = StickersCategory::query()->where('id', $cid)->first();
  202. $validator->notEmpty($category, ['category' => __('stickers.category_not_exist')]);
  203.  
  204. $duplicate = Sticker::query()->where('code', $code)->first();
  205. $validator->empty($duplicate, ['code' => __('stickers.sticker_exists')]);
  206.  
  207. $rules = [
  208. 'maxsize' => setting('stickermaxsize'),
  209. 'maxweight' => setting('stickermaxweight'),
  210. 'minweight' => setting('stickerminweight'),
  211. ];
  212.  
  213. $validator->file($sticker, $rules, ['sticker' => __('validator.image_upload_failed')]);
  214.  
  215. if ($validator->isValid()) {
  216. $newName = uniqueName($sticker->getClientOriginalExtension());
  217. $path = public_path((new Sticker())->uploadPath . '/' . $newName);
  218. $sticker->move(public_path((new Sticker())->uploadPath), $newName);
  219.  
  220. Sticker::query()->create([
  221. 'category_id' => $cid,
  222. 'name' => str_replace(public_path(), '', $path),
  223. 'code' => $code,
  224. ]);
  225.  
  226. clearCache('stickers');
  227. setFlash('success', __('stickers.sticker_success_created'));
  228.  
  229. return redirect('admin/stickers/' . $cid);
  230. }
  231.  
  232. setInput($request->all());
  233. setFlash('danger', $validator->getErrors());
  234. }
  235.  
  236. return view('admin/stickers/create_sticker', compact('categories', 'cid'));
  237. }
  238.  
  239. /**
  240. * Редактирование стикера
  241. *
  242. * @param int $id
  243. * @param Request $request
  244. * @param Validator $validator
  245. *
  246. * @return View|RedirectResponse
  247. */
  248. public function editSticker(int $id, Request $request, Validator $validator)
  249. {
  250. /** @var Sticker $sticker */
  251. $sticker = Sticker::query()->find($id);
  252. $page = int($request->input('page', 1));
  253.  
  254. if (! $sticker) {
  255. abort(404, __('stickers.sticker_not_exist'));
  256. }
  257.  
  258. if ($request->isMethod('post')) {
  259. $code = utfLower($request->input('code'));
  260. $cid = int($request->input('cid'));
  261.  
  262. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  263. ->length($code, 2, 20, ['code' => __('stickers.sticker_length')])
  264. ->regex($code, '|^:+[a-яa-z0-9_\-/\(\)]+$|i', ['code' => __('stickers.sticker_requirements')]);
  265.  
  266. $duplicate = Sticker::query()->where('code', $code)->where('id', '<>', $sticker->id)->first();
  267. $validator->empty($duplicate, ['code' => __('stickers.sticker_exists')]);
  268.  
  269. $category = StickersCategory::query()->where('id', $cid)->first();
  270. $validator->notEmpty($category, ['category' => __('stickers.category_not_exist')]);
  271.  
  272. if ($validator->isValid()) {
  273. $sticker->update([
  274. 'code' => $code,
  275. 'category_id' => $cid,
  276. ]);
  277.  
  278. clearCache('stickers');
  279. setFlash('success', __('stickers.sticker_success_changed'));
  280.  
  281. return redirect('admin/stickers/' . $cid . '?page=' . $page);
  282. }
  283.  
  284. setInput($request->all());
  285. setFlash('danger', $validator->getErrors());
  286. }
  287.  
  288. $categories = StickersCategory::query()->get();
  289.  
  290. return view('admin/stickers/edit_sticker', compact('sticker', 'categories', 'page'));
  291. }
  292.  
  293. /**
  294. * Удаление стикера
  295. *
  296. * @param int $id
  297. * @param Request $request
  298. * @param Validator $validator
  299. *
  300. * @return RedirectResponse
  301. s*/
  302. public function deleteSticker(int $id, Request $request, Validator $validator): RedirectResponse
  303. {
  304. if (! is_writable(public_path('uploads/stickers'))) {
  305. abort(200, __('main.directory_not_writable'));
  306. }
  307.  
  308. $sticker = Sticker::query()->where('id', $id)->first();
  309.  
  310. if (! $sticker) {
  311. abort(404, __('stickers.sticker_not_exist'));
  312. }
  313.  
  314. $page = int($request->input('page', 1));
  315. $category = $sticker->category->id;
  316.  
  317. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'));
  318.  
  319. if ($validator->isValid()) {
  320. deleteFile(public_path($sticker->name));
  321. $sticker->delete();
  322.  
  323. clearCache('stickers');
  324. setFlash('success', __('stickers.sticker_success_deleted'));
  325. } else {
  326. setFlash('danger', $validator->getErrors());
  327. }
  328.  
  329. return redirect('admin/stickers/' . $category . '?page=' . $page);
  330. }
  331. }