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-05-14 17:22:42 +02:00
|
|
|
use Content, Validation, Html;
|
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
|
|
|
{
|
2021-08-18 20:47:18 +02:00
|
|
|
$post_id = Request::getPostInt('post_id');
|
2022-01-17 19:17:58 +01:00
|
|
|
$post = PostModel::getPost($post_id, 'id', $this->user);
|
2022-03-23 15:54:41 +01:00
|
|
|
Html::pageError404($post);
|
2021-08-01 20:43:50 +02:00
|
|
|
|
2022-03-05 06:33:11 +01:00
|
|
|
$content = $_POST['content']; // для Markdown
|
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-18 23:54:22 +02:00
|
|
|
|
|
|
|
if (!Validation::length($content, 6, 5000)) {
|
|
|
|
Html::addMsg(__('msg.string_length', ['name' => '«' . __('msg.content') . '»']), 'error');
|
|
|
|
redirect($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
|
|
|
|
// Проверим стоп слова, url
|
|
|
|
$trigger = (new \App\Controllers\AuditController())->prohibitedContent($content, 'answer');
|
2021-07-17 17:03:39 +02:00
|
|
|
|
2022-01-17 19:17:58 +01:00
|
|
|
$last_id = AnswerModel::add(
|
2022-01-12 17:49:35 +01:00
|
|
|
[
|
|
|
|
'answer_post_id' => $post_id,
|
2022-05-09 07:40:51 +02:00
|
|
|
'answer_content' => $content,
|
2022-01-13 17:59:40 +01:00
|
|
|
'answer_published' => ($trigger === false) ? 0 : 1,
|
2022-01-12 17:49:35 +01:00
|
|
|
'answer_ip' => Request::getRemoteAddress(),
|
2022-01-17 19:17:58 +01:00
|
|
|
'answer_user_id' => $this->user['id'],
|
2022-01-12 17:49:35 +01:00
|
|
|
]
|
|
|
|
);
|
2021-08-06 16:46:45 +02:00
|
|
|
|
2022-01-13 17:59:40 +01:00
|
|
|
// Recalculating the number of responses for the post + 1
|
2022-01-12 17:49:35 +01:00
|
|
|
// Пересчитываем количество ответов для поста + 1
|
|
|
|
PostModel::updateCount($post_id, 'answers');
|
2021-08-01 20:43:50 +02:00
|
|
|
|
2022-01-14 12:55:57 +01:00
|
|
|
$url = $url_post . '#answer_' . $last_id;
|
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-01-13 17:59:40 +01:00
|
|
|
if ($trigger === false) {
|
2022-05-03 23:05:50 +02:00
|
|
|
(new \App\Controllers\AuditController())->create('answer', $last_id, url('admin.audits'));
|
2021-08-01 20:43:50 +02:00
|
|
|
}
|
|
|
|
|
2022-01-14 12:55:57 +01:00
|
|
|
// Notification (@login). 11 - mentions in answers
|
2022-03-05 06:33:11 +01:00
|
|
|
if ($message = 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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-01 20:43:50 +02:00
|
|
|
|
2022-01-12 17:49:35 +01:00
|
|
|
ActionModel::addLogs(
|
|
|
|
[
|
2022-03-05 06:33:11 +01:00
|
|
|
'user_id' => $this->user['id'],
|
|
|
|
'user_login' => $this->user['login'],
|
|
|
|
'id_content' => $last_id,
|
2022-03-12 16:17:52 +01:00
|
|
|
'action_type' => 'answer',
|
2022-05-03 23:05:50 +02:00
|
|
|
'action_name' => 'content_added',
|
2022-03-05 06:33:11 +01:00
|
|
|
'url_content' => $url,
|
2022-01-12 17:49:35 +01:00
|
|
|
]
|
|
|
|
);
|
2021-07-17 17:03:39 +02:00
|
|
|
|
2022-01-14 12:55:57 +01:00
|
|
|
redirect($url);
|
2021-08-01 20:43:50 +02:00
|
|
|
}
|
|
|
|
}
|