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

Размер файла: 4.87Kb
  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\Notice;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\View\View;
  12.  
  13. class NoticeController extends AdminController
  14. {
  15. /**
  16. * Главная страница
  17. *
  18. * @return View
  19. */
  20. public function index(): View
  21. {
  22. $notices = Notice::query()
  23. ->orderBy('id')
  24. ->with('user')
  25. ->get();
  26.  
  27. return view('admin/notices/index', compact('notices'));
  28. }
  29.  
  30. /**
  31. * Создание шаблона
  32. *
  33. * @param Request $request
  34. * @param Validator $validator
  35. *
  36. * @return View|RedirectResponse
  37. */
  38. public function create(Request $request, Validator $validator)
  39. {
  40. if ($request->isMethod('post')) {
  41. $type = $request->input('type');
  42. $name = $request->input('name');
  43. $text = $request->input('text');
  44. $protect = empty($request->input('protect')) ? 0 : 1;
  45.  
  46. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  47. ->regex($type, '|^[a-z0-9_\-]+$|i', ['type' => 'Недопустимое название типа шаблона!'])
  48. ->length($type, 3, 20, ['type' => __('admin.notices.notice_length')])
  49. ->length($name, 5, 100, ['name' => __('validator.text')])
  50. ->length($text, 10, 65000, ['text' => __('validator.text')]);
  51.  
  52. $duplicate = Notice::query()->where('type', $type)->first();
  53. $validator->empty($duplicate, ['type' => __('admin.notices.notice_exists')]);
  54.  
  55. if ($validator->isValid()) {
  56. /** @var Notice $notice */
  57. $notice = Notice::query()->create([
  58. 'type' => $type,
  59. 'name' => $name,
  60. 'text' => $text,
  61. 'user_id' => getUser('id'),
  62. 'protect' => $protect,
  63. 'created_at' => SITETIME,
  64. 'updated_at' => SITETIME,
  65. ]);
  66.  
  67. setFlash('success', __('admin.notices.notice_success_saved'));
  68.  
  69. return redirect('admin/notices/edit/' . $notice->id);
  70. }
  71.  
  72. setInput($request->all());
  73. setFlash('danger', $validator->getErrors());
  74. }
  75.  
  76. return view('admin/notices/create');
  77. }
  78.  
  79. /**
  80. * Редактирование шаблона
  81. *
  82. * @param int $id
  83. * @param Request $request
  84. * @param Validator $validator
  85. *
  86. * @return View|RedirectResponse
  87. */
  88. public function edit(int $id, Request $request, Validator $validator)
  89. {
  90. /** @var Notice $notice */
  91. $notice = Notice::query()->find($id);
  92.  
  93. if (! $notice) {
  94. abort(404, __('admin.notices.notice_not_found'));
  95. }
  96.  
  97. if ($request->isMethod('post')) {
  98. $name = $request->input('name');
  99. $text = $request->input('text');
  100. $protect = empty($request->input('protect')) ? 0 : 1;
  101.  
  102. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  103. ->length($name, 5, 100, ['name' => __('validator.text')])
  104. ->length($text, 10, 65000, ['text' => __('validator.text')]);
  105.  
  106. if ($validator->isValid()) {
  107. $notice->update([
  108. 'name' => $name,
  109. 'text' => $text,
  110. 'user_id' => getUser('id'),
  111. 'protect' => $protect,
  112. 'updated_at' => SITETIME,
  113. ]);
  114.  
  115. setFlash('success', __('admin.notices.notice_success_saved'));
  116.  
  117. return redirect('admin/notices/edit/' . $notice->id);
  118. }
  119.  
  120. setInput($request->all());
  121. setFlash('danger', $validator->getErrors());
  122. }
  123.  
  124. return view('admin/notices/edit', compact('notice'));
  125. }
  126.  
  127. /**
  128. * Удаление шаблона
  129. *
  130. * @param int $id
  131. * @param Request $request
  132. * @param Validator $validator
  133. *
  134. * @return RedirectResponse
  135. */
  136. public function delete(int $id, Request $request, Validator $validator): RedirectResponse
  137. {
  138. /** @var Notice $notice */
  139. $notice = Notice::query()->find($id);
  140.  
  141. $validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
  142. ->notEmpty($notice, __('admin.notices.notice_not_found'))
  143. ->empty($notice->protect, __('admin.notices.notice_protect'));
  144.  
  145. if ($validator->isValid()) {
  146. $notice->delete();
  147.  
  148. setFlash('success', __('admin.notices.notice_success_deleted'));
  149. } else {
  150. setFlash('danger', $validator->getErrors());
  151. }
  152.  
  153. return redirect('admin/notices');
  154. }
  155. }