|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
/**
|
|
4
|
|
|
* Chat Controller
|
|
5
|
|
|
*
|
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]>
|
|
7
|
|
|
* @link http://inji.ru/
|
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy
|
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
|
|
10
|
|
|
*/
|
|
11
|
|
|
class ChatsController extends Controller
|
|
12
|
|
|
{
|
|
13
|
|
|
public function eventsAction($chatId = 0)
|
|
|
|
|
|
|
14
|
|
|
{
|
|
15
|
|
|
$chatId = (int) $chatId;
|
|
16
|
|
|
$result = new Server\Result();
|
|
17
|
|
View Code Duplication |
if (!$chatId || !($chat = \Chats\Chat::get($chatId))) {
|
|
|
|
|
|
|
18
|
|
|
$result->success = false;
|
|
19
|
|
|
$result->content = 'Такого чата не существует';
|
|
20
|
|
|
$result->send();
|
|
21
|
|
|
}
|
|
22
|
|
|
$where = [
|
|
23
|
|
|
['chat_id', $chatId],
|
|
24
|
|
|
['delete', 0]
|
|
25
|
|
|
];
|
|
26
|
|
|
if (!empty($_GET['lastEventDate'])) {
|
|
27
|
|
|
$where[] = ['date_create', $_GET['lastEventDate'], '>'];
|
|
28
|
|
|
}
|
|
29
|
|
|
$result->content = [
|
|
30
|
|
|
'members' => [],
|
|
31
|
|
|
'messages' => []
|
|
32
|
|
|
];
|
|
33
|
|
|
if (Users\User::$cur->id) {
|
|
34
|
|
|
$member = \Chats\Chat\Member::get([['chat_id', $chatId], ['user_id', Users\User::$cur->id]]);
|
|
35
|
|
|
if (!$member) {
|
|
36
|
|
|
$member = new \Chats\Chat\Member();
|
|
37
|
|
|
$member->user_id = Users\User::$cur->id;
|
|
38
|
|
|
$member->chat_id = $chatId;
|
|
39
|
|
|
}
|
|
40
|
|
|
$member->date_last_active = date('Y-m-d H:i:s');
|
|
41
|
|
|
$member->save();
|
|
42
|
|
|
}
|
|
43
|
|
|
$members = $this->module->getMembers($chatId);
|
|
44
|
|
View Code Duplication |
foreach ($members as $member) {
|
|
|
|
|
|
|
45
|
|
|
$result->content['members'][$member->user_id] = [
|
|
46
|
|
|
'fullUserName' => $member->user->name(),
|
|
47
|
|
|
'userFirstName' => $member->user->info->first_name,
|
|
48
|
|
|
'userPhoto' => $member->user->info->photo ? $member->user->info->photo->path : '/static/system/images/no-image.png'
|
|
49
|
|
|
];
|
|
50
|
|
|
}
|
|
51
|
|
|
|
|
52
|
|
|
$messages = \Chats\Chat\Message::getList(['where' => $where, 'limit' => 20, 'order' => ['date_create', 'DESC']]);
|
|
53
|
|
|
$messages = array_reverse($messages);
|
|
54
|
|
View Code Duplication |
foreach ($messages as $message) {
|
|
|
|
|
|
|
55
|
|
|
$msg = [
|
|
56
|
|
|
'message' => $message->_params,
|
|
57
|
|
|
'fullUserName' => $message->user->name(),
|
|
58
|
|
|
'userFirstName' => $message->user->info->first_name,
|
|
59
|
|
|
'userPhoto' => $message->user->info->photo ? $message->user->info->photo->path : '/static/system/images/no-image.png'
|
|
60
|
|
|
];
|
|
61
|
|
|
$result->content['messages'][] = $msg;
|
|
62
|
|
|
}
|
|
63
|
|
|
$result->send();
|
|
64
|
|
|
}
|
|
65
|
|
|
|
|
66
|
|
|
function deleteMsgAction($messageId)
|
|
|
|
|
|
|
67
|
|
|
{
|
|
68
|
|
|
$result = new Server\Result();
|
|
69
|
|
View Code Duplication |
if (!\Users\User::$cur->isAdmin()) {
|
|
|
|
|
|
|
70
|
|
|
$result->success = false;
|
|
71
|
|
|
$result->content = 'Вы не админ';
|
|
72
|
|
|
$result->send();
|
|
73
|
|
|
}
|
|
74
|
|
|
$msg = Chats\Chat\Message::get((int) $messageId);
|
|
75
|
|
|
if (!$msg) {
|
|
76
|
|
|
$result->success = false;
|
|
77
|
|
|
$result->content = 'Сообщение не найдено';
|
|
78
|
|
|
$result->send();
|
|
79
|
|
|
}
|
|
80
|
|
|
$msg->delete = 1;
|
|
81
|
|
|
$msg->save();
|
|
82
|
|
|
$result->successMsg = 'Сообщение удалено';
|
|
83
|
|
|
$result->send();
|
|
84
|
|
|
}
|
|
85
|
|
|
|
|
86
|
|
|
function banUserAction($messageId)
|
|
|
|
|
|
|
87
|
|
|
{
|
|
88
|
|
|
$result = new Server\Result();
|
|
89
|
|
View Code Duplication |
if (!\Users\User::$cur->isAdmin()) {
|
|
|
|
|
|
|
90
|
|
|
$result->success = false;
|
|
91
|
|
|
$result->content = 'Вы не админ';
|
|
92
|
|
|
$result->send();
|
|
93
|
|
|
}
|
|
94
|
|
|
$msg = Chats\Chat\Message::get((int) $messageId);
|
|
95
|
|
|
if (!$msg) {
|
|
96
|
|
|
$result->success = false;
|
|
97
|
|
|
$result->content = 'Сообщение не найдено';
|
|
98
|
|
|
$result->send();
|
|
99
|
|
|
}
|
|
100
|
|
|
$ban = new Chats\Chat\Ban();
|
|
101
|
|
|
$ban->user_id = $msg->user_id;
|
|
102
|
|
|
$ban->chat_id = $msg->chat_id;
|
|
103
|
|
|
$ban->chat_message_id = $msg->id;
|
|
104
|
|
|
$ban->save();
|
|
105
|
|
|
$msgs = Chats\Chat\Message::getList(['where' => ['user_id', $ban->user_id]]);
|
|
106
|
|
|
foreach ($msgs as $msg) {
|
|
107
|
|
|
$msg->delete = 1;
|
|
108
|
|
|
$msg->save();
|
|
109
|
|
|
}
|
|
110
|
|
|
$result->content = array_flip(array_keys($msgs));
|
|
111
|
|
|
$result->successMsg = 'Сообщение удалено';
|
|
112
|
|
|
$result->send();
|
|
113
|
|
|
}
|
|
114
|
|
|
|
|
115
|
|
|
public function sendFormAction($chatId = 0)
|
|
|
|
|
|
|
116
|
|
|
{
|
|
117
|
|
|
$chatId = (int) $chatId;
|
|
118
|
|
|
$result = new Server\Result();
|
|
119
|
|
View Code Duplication |
if (!$chatId || !($chat = \Chats\Chat::get($chatId))) {
|
|
|
|
|
|
|
120
|
|
|
$result->success = false;
|
|
121
|
|
|
$result->content = 'Такого чата не существует';
|
|
122
|
|
|
$result->send();
|
|
123
|
|
|
}
|
|
124
|
|
View Code Duplication |
if (!Users\User::$cur->id) {
|
|
|
|
|
|
|
125
|
|
|
$result->success = false;
|
|
126
|
|
|
$result->content = 'Вы не авторизованы';
|
|
127
|
|
|
$result->send();
|
|
128
|
|
|
}
|
|
129
|
|
|
if (empty($_POST['chat-message']) || !trim($_POST['chat-message'])) {
|
|
130
|
|
|
$result->success = false;
|
|
131
|
|
|
$result->content = 'Сообщение не может быть пустым';
|
|
132
|
|
|
$result->send();
|
|
133
|
|
|
}
|
|
134
|
|
|
if (Chats\Chat\Ban::get(['user_id', \Users\User::$cur->id])) {
|
|
135
|
|
|
$result->success = false;
|
|
136
|
|
|
$result->content = 'Вы не можете писать в чат';
|
|
137
|
|
|
$result->send();
|
|
138
|
|
|
}
|
|
139
|
|
|
$message = new \Chats\Chat\Message();
|
|
140
|
|
|
$message->user_id = Users\User::$cur->id;
|
|
141
|
|
|
$message->chat_id = $chatId;
|
|
142
|
|
|
$message->text = htmlspecialchars(trim($_POST['chat-message']));
|
|
143
|
|
|
$message->save();
|
|
144
|
|
|
$result->successMsg = 'Ваше сообщение было отправлено';
|
|
145
|
|
|
$result->send();
|
|
146
|
|
|
}
|
|
147
|
|
|
|
|
148
|
|
|
}
|
|
149
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: