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
|
|
|
$chatId = (int) $chatId; |
15
|
|
|
$result = new Server\Result(); |
16
|
|
View Code Duplication |
if (!$chatId || !($chat = \Chats\Chat::get($chatId))) { |
|
|
|
|
17
|
|
|
$result->success = false; |
18
|
|
|
$result->content = 'Такого чата не существует'; |
19
|
|
|
$result->send(); |
20
|
|
|
} |
21
|
|
|
$where = [ |
22
|
|
|
['chat_id', $chatId], |
23
|
|
|
['delete', 0] |
24
|
|
|
]; |
25
|
|
|
if (!empty($_GET['lastEventDate'])) { |
26
|
|
|
$where[] = ['date_create', $_GET['lastEventDate'], '>']; |
27
|
|
|
} |
28
|
|
|
$result->content = [ |
29
|
|
|
'members' => [], |
30
|
|
|
'messages' => [] |
31
|
|
|
]; |
32
|
|
|
if (Users\User::$cur->id) { |
33
|
|
|
$member = \Chats\Chat\Member::get([['chat_id', $chatId], ['user_id', Users\User::$cur->id]]); |
34
|
|
|
if (!$member) { |
35
|
|
|
$member = new \Chats\Chat\Member(); |
36
|
|
|
$member->user_id = Users\User::$cur->id; |
37
|
|
|
$member->chat_id = $chatId; |
38
|
|
|
} |
39
|
|
|
$member->date_last_active = date('Y-m-d H:i:s'); |
40
|
|
|
$member->save(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$messages = \Chats\Chat\Message::getList(['where' => $where, 'limit' => 20, 'order' => ['date_create', 'DESC']]); |
44
|
|
|
if ($messages) { |
|
|
|
|
45
|
|
|
$messages = array_reverse($messages); |
46
|
|
|
foreach ($messages as $message) { |
47
|
|
|
$msg = [ |
48
|
|
|
'message' => $message->_params, |
49
|
|
|
'fullUserName' => $message->user->name(), |
50
|
|
|
'userFirstName' => $message->user->info->first_name, |
51
|
|
|
'userPhoto' => $message->user->info->photo ? $message->user->info->photo->path : '/static/system/images/no-image.png' |
52
|
|
|
]; |
53
|
|
|
$result->content['messages'][] = $msg; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
$result->send(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function deleteMsgAction($messageId) { |
60
|
|
|
$result = new Server\Result(); |
61
|
|
View Code Duplication |
if (!\Users\User::$cur->isAdmin()) { |
|
|
|
|
62
|
|
|
$result->success = false; |
63
|
|
|
$result->content = 'Вы не админ'; |
64
|
|
|
$result->send(); |
65
|
|
|
} |
66
|
|
|
$msg = Chats\Chat\Message::get((int) $messageId); |
67
|
|
|
if (!$msg) { |
68
|
|
|
$result->success = false; |
69
|
|
|
$result->content = 'Сообщение не найдено'; |
70
|
|
|
$result->send(); |
71
|
|
|
} |
72
|
|
|
$msg->delete = 1; |
73
|
|
|
$msg->save(); |
74
|
|
|
$result->successMsg = 'Сообщение удалено'; |
75
|
|
|
$result->send(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function banUserAction($messageId) { |
79
|
|
|
$result = new Server\Result(); |
80
|
|
View Code Duplication |
if (!\Users\User::$cur->isAdmin()) { |
|
|
|
|
81
|
|
|
$result->success = false; |
82
|
|
|
$result->content = 'Вы не админ'; |
83
|
|
|
$result->send(); |
84
|
|
|
} |
85
|
|
|
$msg = Chats\Chat\Message::get((int) $messageId); |
86
|
|
|
if (!$msg) { |
87
|
|
|
$result->success = false; |
88
|
|
|
$result->content = 'Сообщение не найдено'; |
89
|
|
|
$result->send(); |
90
|
|
|
} |
91
|
|
|
$ban = new Chats\Chat\Ban(); |
92
|
|
|
$ban->user_id = $msg->user_id; |
93
|
|
|
$ban->chat_id = $msg->chat_id; |
94
|
|
|
$ban->chat_message_id = $msg->id; |
95
|
|
|
$ban->save(); |
96
|
|
|
$msgs = Chats\Chat\Message::getList(['where' => ['user_id', $ban->user_id]]); |
97
|
|
|
foreach ($msgs as $msg) { |
98
|
|
|
$msg->delete = 1; |
99
|
|
|
$msg->save(); |
100
|
|
|
} |
101
|
|
|
$result->content = array_flip(array_keys($msgs)); |
102
|
|
|
$result->successMsg = 'Сообщение удалено'; |
103
|
|
|
$result->send(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function sendFormAction($chatId = 0) { |
107
|
|
|
$chatId = (int) $chatId; |
108
|
|
|
$result = new Server\Result(); |
109
|
|
View Code Duplication |
if (!$chatId || !($chat = \Chats\Chat::get($chatId))) { |
|
|
|
|
110
|
|
|
$result->success = false; |
111
|
|
|
$result->content = 'Такого чата не существует'; |
112
|
|
|
$result->send(); |
113
|
|
|
} |
114
|
|
View Code Duplication |
if (!Users\User::$cur->id) { |
|
|
|
|
115
|
|
|
$result->success = false; |
116
|
|
|
$result->content = 'Вы не авторизованы'; |
117
|
|
|
$result->send(); |
118
|
|
|
} |
119
|
|
|
if (empty($_POST['chat-message']) || !trim($_POST['chat-message'])) { |
120
|
|
|
$result->success = false; |
121
|
|
|
$result->content = 'Сообщение не может быть пустым'; |
122
|
|
|
$result->send(); |
123
|
|
|
} |
124
|
|
|
if (Chats\Chat\Ban::get(['user_id', \Users\User::$cur->id])) { |
125
|
|
|
$result->success = false; |
126
|
|
|
$result->content = 'Вы не можете писать в чат'; |
127
|
|
|
$result->send(); |
128
|
|
|
} |
129
|
|
|
$message = new \Chats\Chat\Message(); |
130
|
|
|
$message->user_id = Users\User::$cur->id; |
131
|
|
|
$message->chat_id = $chatId; |
132
|
|
|
$message->text = htmlspecialchars(trim($_POST['chat-message'])); |
133
|
|
|
$message->save(); |
134
|
|
|
$result->successMsg = 'Ваше сообщение было отправлено'; |
135
|
|
|
$result->send(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.