2021-07-17 17:03:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controllers\Answer;
|
|
|
|
|
|
|
|
use Hleb\Constructor\Handlers\Request;
|
2022-05-14 17:22:42 +02:00
|
|
|
use App\Controllers\Controller;
|
2022-03-05 06:33:11 +01:00
|
|
|
use App\Models\{NotificationModel, ActionModel, AnswerModel, PostModel};
|
2022-09-02 21:18:40 +02:00
|
|
|
use App\Validate\Validator;
|
|
|
|
use Content;
|
2021-07-17 17:03:39 +02:00
|
|
|
|
2022-05-14 17:22:42 +02:00
|
|
|
class AddAnswerController extends Controller
|
2021-07-17 17:03:39 +02:00
|
|
|
{
|
2021-10-21 19:55:00 +02:00
|
|
|
public function create()
|
2021-12-19 15:10:26 +01:00
|
|
|
{
|
2022-09-12 07:03:07 +02:00
|
|
|
$post = self::getPost($this->user);
|
2021-08-01 20:43:50 +02:00
|
|
|
|
2022-05-03 23:05:50 +02:00
|
|
|
$url_post = url('post', ['id' => $post['post_id'], 'slug' => $post['post_slug']]);
|
2022-05-27 20:35:41 +02:00
|
|
|
|
2022-09-12 07:03:07 +02:00
|
|
|
Validator::Length($content = $_POST['content'], 6, 5000, 'content', $url_post);
|
2022-01-12 17:49:35 +01:00
|
|
|
|
2022-05-18 23:54:22 +02:00
|
|
|
// Let's check the stop words, url
|
2022-09-13 06:24:48 +02:00
|
|
|
// Проверим стоп слова и url
|
|
|
|
$trigger = (new \App\Services\Audit())->prohibitedContent($content);
|
2021-07-17 17:03:39 +02:00
|
|
|
|
2022-09-12 07:03:07 +02:00
|
|
|
$last_id = AnswerModel::add($post['post_id'], $content, $trigger);
|
2021-08-01 20:43:50 +02:00
|
|
|
|
2022-01-13 23:02:45 +01:00
|
|
|
// Add an audit entry and an alert to the admin
|
2022-09-12 07:03:07 +02:00
|
|
|
// Аудит и оповещение персоналу
|
2022-01-13 17:59:40 +01:00
|
|
|
if ($trigger === false) {
|
2022-09-13 06:24:48 +02:00
|
|
|
(new \App\Services\Audit())->create('answer', $last_id, url('admin.audits'));
|
2021-08-01 20:43:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-12 07:03:07 +02:00
|
|
|
$url = $url_post . '#answer_' . $last_id;
|
|
|
|
|
|
|
|
$this->notif($content, $post, $url);
|
|
|
|
|
|
|
|
ActionModel::addLogs(
|
|
|
|
[
|
|
|
|
'id_content' => $last_id,
|
|
|
|
'action_type' => 'answer',
|
|
|
|
'action_name' => 'added',
|
|
|
|
'url_content' => $url,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
redirect($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notifications when adding a answer
|
|
|
|
// Уведомления при добавлении ответа
|
|
|
|
public function notif($content, $post, $url)
|
|
|
|
{
|
|
|
|
// Contact via @
|
|
|
|
// Обращение через @
|
2022-09-21 08:35:10 +02:00
|
|
|
if ($message = \App\Services\Parser\Content::parseUser($content, true, true)) {
|
2022-04-06 23:00:26 +02:00
|
|
|
(new \App\Controllers\NotificationController())->mention(NotificationModel::TYPE_ADDRESSED_ANSWER, $message, $url, $post['post_user_id']);
|
2021-07-17 17:03:39 +02:00
|
|
|
}
|
2022-01-14 13:59:57 +01:00
|
|
|
|
2022-09-12 07:03:07 +02:00
|
|
|
// Who is following this question/post
|
2021-08-03 22:23:02 +02:00
|
|
|
// Кто подписан на данный вопрос / пост
|
2022-03-05 06:33:11 +01:00
|
|
|
if ($focus_all = PostModel::getFocusUsersPost($post['post_id'])) {
|
2021-08-03 22:23:02 +02:00
|
|
|
foreach ($focus_all as $focus_user) {
|
2022-01-17 19:17:58 +01:00
|
|
|
if ($focus_user['signed_user_id'] != $this->user['id']) {
|
2022-03-05 06:33:11 +01:00
|
|
|
NotificationModel::send(
|
2022-01-12 17:49:35 +01:00
|
|
|
[
|
2022-03-05 06:33:11 +01:00
|
|
|
'sender_id' => $this->user['id'],
|
|
|
|
'recipient_id' => $focus_user['signed_user_id'],
|
|
|
|
'action_type' => NotificationModel::TYPE_AMSWER_POST,
|
|
|
|
'url' => $url,
|
2022-01-12 17:49:35 +01:00
|
|
|
]
|
|
|
|
);
|
2021-08-03 22:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 07:03:07 +02:00
|
|
|
}
|
2021-08-01 20:43:50 +02:00
|
|
|
|
2022-09-12 07:03:07 +02:00
|
|
|
public static function getPost($user)
|
|
|
|
{
|
|
|
|
$post_id = Request::getPostInt('post_id');
|
2021-07-17 17:03:39 +02:00
|
|
|
|
2022-09-12 07:03:07 +02:00
|
|
|
$post = PostModel::getPost($post_id, 'id', $user);
|
|
|
|
self::error404($post);
|
|
|
|
|
|
|
|
return $post;
|
2021-08-01 20:43:50 +02:00
|
|
|
}
|
|
|
|
}
|