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

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