View file app/Controllers/ContactController.php

File size: 4.52Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use App\Classes\Validator;
  8. use App\Models\Contact;
  9. use Illuminate\Http\Request;
  10.  
  11. class ContactController extends BaseController
  12. {
  13. /**
  14. * Конструктор
  15. */
  16. public function __construct()
  17. {
  18. parent::__construct();
  19.  
  20. if (! getUser()) {
  21. abort(403, __('main.not_authorized'));
  22. }
  23. }
  24.  
  25. /**
  26. * Главная страница
  27. *
  28. * @param Request $request
  29. * @param Validator $validator
  30. *
  31. * @return string
  32. */
  33. public function index(Request $request, Validator $validator): string
  34. {
  35. $login = $request->input('user');
  36.  
  37. if ($request->isMethod('post')) {
  38. $page = int($request->input('page', 1));
  39.  
  40. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'));
  41.  
  42. $user = getUserByLogin($login);
  43. $validator->notEmpty($user, ['user' => __('validator.user')]);
  44.  
  45. if ($user) {
  46. $validator->notEqual($user->login, getUser('login'), ['user' => __('contacts.forbidden_yourself')]);
  47.  
  48. $totalContact = Contact::query()->where('user_id', getUser('id'))->count();
  49. $validator->lte($totalContact, setting('limitcontact'), __('contacts.contact_full', ['max' => setting('limitcontact')]));
  50.  
  51. $validator->false(getUser()->isContact($user), ['user' => __('contacts.already_contacts')]);
  52. }
  53.  
  54. if ($validator->isValid()) {
  55. Contact::query()->create([
  56. 'user_id' => getUser('id'),
  57. 'contact_id' => $user->id,
  58. 'created_at' => SITETIME,
  59. ]);
  60.  
  61. if (! $user->isIgnore(getUser())) {
  62. $text = textNotice('contact', ['login' => getUser('login')]);
  63. $user->sendMessage(null, $text);
  64. }
  65.  
  66. setFlash('success', __('contacts.success_added'));
  67. redirect('/contacts?page='.$page);
  68. } else {
  69. setInput($request->all());
  70. setFlash('danger', $validator->getErrors());
  71. }
  72. }
  73.  
  74. $contacts = Contact::query()
  75. ->where('user_id', getUser('id'))
  76. ->orderByDesc('created_at')
  77. ->with('contactor')
  78. ->paginate(setting('contactlist'));
  79.  
  80. return view('contacts/index', compact('contacts', 'login'));
  81. }
  82.  
  83. /**
  84. * Заметка для пользователя
  85. *
  86. * @param int $id
  87. * @param Request $request
  88. * @param Validator $validator
  89. *
  90. * @return string
  91. */
  92. public function note(int $id, Request $request, Validator $validator): string
  93. {
  94. $contact = Contact::query()
  95. ->where('user_id', getUser('id'))
  96. ->where('id', $id)
  97. ->first();
  98.  
  99. if (! $contact) {
  100. abort(404, __('main.record_not_found'));
  101. }
  102.  
  103. if ($request->isMethod('post')) {
  104. $msg = $request->input('msg');
  105.  
  106. $validator->equal($request->input('token'), $_SESSION['token'], ['msg' => __('validator.token')])
  107. ->length($msg, 0, 1000, ['msg' => __('users.note_to_big')]);
  108.  
  109. if ($validator->isValid()) {
  110. $contact->update([
  111. 'text' => $msg,
  112. ]);
  113.  
  114. setFlash('success', __('users.note_saved_success'));
  115. redirect('/contacts');
  116. } else {
  117. setInput($request->all());
  118. setFlash('danger', $validator->getErrors());
  119. }
  120. }
  121.  
  122. return view('contacts/note', compact('contact'));
  123. }
  124.  
  125. /**
  126. * Удаление контактов
  127. *
  128. * @param Request $request
  129. * @param Validator $validator
  130. *
  131. * @return void
  132. */
  133. public function delete(Request $request, Validator $validator): void
  134. {
  135. $page = int($request->input('page', 1));
  136. $del = intar($request->input('del'));
  137.  
  138. $validator->equal($request->input('token'), $_SESSION['token'], __('validator.token'))
  139. ->true($del, __('validator.deletion'));
  140.  
  141. if ($validator->isValid()) {
  142. Contact::query()
  143. ->where('user_id', getUser('id'))
  144. ->whereIn('id', $del)
  145. ->delete();
  146.  
  147. setFlash('success', __('main.records_deleted_success'));
  148. } else {
  149. setFlash('danger', $validator->getErrors());
  150. }
  151.  
  152. redirect('/contacts?page='.$page);
  153. }
  154. }