forked from vgrish/NewsModxBox
/
create.class.php
365 lines (290 loc) · 14.3 KB
/
create.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
require_once MODX_CORE_PATH . 'components/modsociety/processors/society/web/threads/comments/create.class.php';
class modWebSocietyTopicsCommentsCreateProcessor extends modSocietyWebThreadsCommentsCreateProcessor{
public function checkPermissions(){
if(!$this->modx->user->id){
return false;
}
return parent::checkPermissions();
}
public function getLanguageTopics() {
return array_merge((array)parent::getLanguageTopics(), array(
"modsociety:default",
));
}
public function initialize(){
$this->setProperties(array(
"createdby" => $this->modx->user->id,
"createdon" => time(),
"published" => $this->modx->hasPermission("modsociety.publish_comment"),
));
$this->unsetProperty('thread_id');
$this->unsetProperty('ip');
$this->unsetProperty('editedon');
$this->unsetProperty('editedby');
$this->unsetProperty('deleted');
$this->unsetProperty('deletedon');
$this->unsetProperty('deletedby');
$this->unsetProperty('comments_count');
$this->unsetProperty('properties');
return parent::initialize();
}
public function beforeSet(){
// Режем контент
$content = $this->getProperty('text');
$this->setProperty('raw_text', $content);
# $content = str_replace(array(
# "<?"
# ), array(
# "<"
# ), $content);
#
# $content = strip_tags($content, '<strong><composite><composite><model><object><field><code><pre><cut><p><a><h4><h5><h6><img><b><em><i><s><u><hr><blockquote><table><tr><th><td><ul><li><ol>');
#
# // Реплейсим переносы
# $content = preg_replace("/[\r\n]{3,}/", "<br /><br />", $content);
# $content = preg_replace("/\r/", "<br />", $content);
#
# $content = preg_replace('/<code>(.+?)<\/code>/sim', "<pre class=\"prettyprint\"><code>$1</code></pre>", $content);
#
# $this->setProperty('text', $content);
$jevix = $this->modx->getService('modJevix','modJevix', MODX_CORE_PATH . 'components/modjevix/model/modJevix/');
if($this->modx->hasPermission('modxclub.post_indexed_links')){
$rel = "follow";
}
else{
$rel = "nofollow";
}
$jevix->cfgSetTagParamDefault('a','rel',$rel,true);
$errors = '';
$content = $jevix->parse($content, $errors);
$this->setProperty('text', $content);
return parent::beforeSet();
}
public function afterSave(){
$use_delayed_emails = $this->modx->getOption('modsociety.use_delayed_emails', null, false);
# ini_set('display_errors', 1);
$comment = $this->modx->getObject('SocietyComment', $this->object->comment_id);
$topic = $this->modx->getObject($this->object->target_class, $this->object->target_id);
if(empty($this->modx->smarty)){
$this->modx->invokeEvent('OnHandleRequest');
}
$this->modx->smarty->assign('topic', $topic->toArray());
$this->modx->smarty->assign('comment', $comment->toArray());
if($author = $this->modx->user){
$this->modx->smarty->assign('author', $author->toArray());
if($author_profile = $author->Profile){
$this->modx->smarty->assign('author_profile', $author_profile->toArray());
}
}
# $this->modx->smarty->assign('topic_url', $this->modx->makeUrl($topic->id, '', '', 'full'));
/*
Рассылаем емейл-уведомления.
Схема: https://www.lucidchart.com/documents/view/4f91f952-779b-4c62-beeb-ee6210a22ad6
*/
// Кому уже отправлялось, чтобы не отправлять повторно
// Сразу добавляем и того, кто пишет
$sended_to = array(
$this->modx->user->id,
);
// Не используем этот объект запроса, а только его клоны
$users_query = $this->modx->newQuery('modUser');
$users_query->innerJoin('modUserProfile', 'Profile');
$users_query->where(array(
"active" => 1,
"Profile.blocked" => 0,
"id:not in" => $sended_to,
));
$users_query->leftJoin('SocietyNoticeUser', 'Notices');
$users_query->leftJoin('SocietyNoticeType', 'NoticeType', "NoticeType.id = Notices.notice_id");
$users_query->where(array(
"Profile.blockeduntil" => 0,
"OR:Profile.blockeduntil:<" => time(),
));
// Комментарий в ответ на другой комментарий?
if($parent = $comment->Parent){
// Автор комментария - автор родительского комментария?
if($parent->createdby != $this->modx->user->id){
// Если текущий пользователь не автор родительского комментария,
// то отслыаем уведомление об ответе на комментарий
$sended_to = array_unique($sended_to);
$q = clone($users_query);
if($sended_to){
$q->where(array(
"id:not in" => $sended_to,
));
}
$q->where(array(
"id" => $parent->createdby,
"NoticeType.type" => "new_reply",
));
$q->limit(1);
if(
$user = $this->modx->getObject('modUser', $q)
AND $topic->checkPolicy('view', null, $user)
){
// Проверяем настройку уведомлений о новых комментариях
$this->modx->smarty->assign('auth_user_id', $user->id);
$message = $this->modx->smarty->fetch('messages/society/new_comment/comment_reply.tpl');
$this->modx->smarty->assign('auth_user_id', false);
# $this->modx->log(1, $message);
# print $message;
#
# exit;
$user->sendEmail($message, array(
"subject" => "Новый ответ в топике «{$topic->pagetitle}»",
));
$this->modx->mail->reset();
$sended_to[] = $parent->createdby;
}
// Если текущий пользователь не автор родительского комментария,
// то отслыаем уведомление об ответе на комментарий
# if(
# $ParentAuthor = $parent->Author
# AND $ParentAuthor->active
# AND !$ParentAuthor->Profile->blocked
# ){
# // Проверяем настройку уведомлений о новых комментариях
#
# $message = $this->modx->smarty->fetch('messages/society/new_comment/comment_reply.tpl');
# $ParentAuthor->sendEmail($message, array(
# "subject" => "Новый ответ на ваш комментарий",
# ));
# $sended_to[] = $ParentAuthor->id;
# $this->modx->mail->reset();
# }
}
}
# else{
#
# }
// Отправляем уведомление автору топика о новом комментарии
$sended_to = array_unique($sended_to);
$q = clone($users_query);
$q->where(array(
"id" => $topic->createdby,
"NoticeType.type" => "new_comments_in_my_topics",
));
if($sended_to){
$q->where(array(
"id:not in" => $sended_to,
));
}
if(
$user = $this->modx->getObject('modUser', $q)
AND $topic->checkPolicy('view', null, $user)
){
$this->modx->smarty->assign('auth_user_id', $user->id);
$message = $this->modx->smarty->fetch('messages/society/new_comment/topic_author.tpl');
$this->modx->smarty->assign('auth_user_id', false);
$user->sendEmail($message, array(
"subject" => "Новый комментарий в вашем топике «{$topic->pagetitle}»",
));
$sended_to[] = $user->id;
$this->modx->mail->reset();
}
// Отсылаем комментарии всем, кто в этом топике писал
// Получаем всех пользователей всех комментариев данной ветки
$sended_to = array_unique($sended_to);
$q = clone($users_query);
$q->innerJoin('SocietyComment', "Comments", "Comments.thread_id = {$comment->thread_id} AND Comments.id != {$comment->id} AND Comments.createdby = modUser.id");
if($sended_to){
$q->where(array(
"id:not in" => $sended_to,
));
}
$q->where(array(
"NoticeType.type" => "new_comment",
));
if($users = $this->modx->getCollection('modUser', $q)){
foreach($users as $user){
if($topic->checkPolicy('view', null, $user)){
$this->modx->smarty->assign('auth_user_id', $user->id);
$message = $this->modx->smarty->fetch('messages/society/new_comment/participants.tpl');
# $subject = "В топике добавлен новый комментарий";
$subject = "Новый комментарий в топике «{$topic->pagetitle}»";
/*
Пытаемся записать в отложенную рассылку
*/
if(
!$use_delayed_emails
OR !$emailmessage = $this->modx->newObject('SocietyEmailMessage', array(
"user_id" => $user->id,
"subject" => $subject,
"message" => $message,
))
OR !$emailmessage->save()
){
$user->sendEmail($message, array(
"subject" => $subject,
));
$this->modx->mail->reset();
}
$sended_to[] = $user->id;
}
$this->modx->smarty->assign('auth_user_id', false);
# else{
# // Если нет права на топик, сразу добавляем в исключения
# $sended_to[] = $user->id;
# }
}
}
// Отсылаем уведомление администрации
$sended_to = array_unique($sended_to);
$q = clone($users_query);
$q->innerJoin('modUserGroupMember', 'UserGroupMembers');
$q->where(array(
"UserGroupMembers.user_group" => 20,
));
if($sended_to){
$q->where(array(
"id:not in" => $sended_to,
));
}
if($users = $this->modx->getCollection('modUser', $q)){
foreach($users as $user){
$this->modx->smarty->assign('auth_user_id', $user->id);
$message = $this->modx->smarty->fetch('messages/society/new_comment/administration.tpl');
$user->sendEmail($message, array(
"subject" => "Новый комментарий в топике «{$topic->pagetitle}»",
));
$sended_to[] = $user->id;
$this->modx->mail->reset();
}
$this->modx->smarty->assign('auth_user_id', false);
}
$this->modx->runProcessor('system/clearcache');
return parent::afterSave();
}
public function cleanup(){
$comment_id = $this->object->comment_id;
$comment_html = '';
// Пытаемся получить данные комментария в шаблоне
// if(!$response = $modx->runProcessor('web/society/topics/comments/create',
if($response = $this->modx->runProcessor('web/society/threads/comments/getdata',
array(
"comment_id" => $comment_id,
"listType" => false,
), array(
'processors_path' => MODX_CORE_PATH .'components/modxsite/processors/',
))){
if(
!$response->isError()
AND $object = $response->getObject()
){
$comment = current($object);
if(empty($this->modx->smarty)){
$this->modx->invokeEvent('OnHandleRequest');
}
$this->modx->smarty->assign('comment', $comment);
$comment_html = $this->modx->smarty->fetch('society/threads/comments/inner.tpl');
}
}
# return $this->success('Комментарий успешно опубликован', array(
return $this->success($this->modx->lexicon('comment_post.success'), array(
"comment_id" => $comment_id,
"comment_html" => $comment_html,
));
}
}
return 'modWebSocietyTopicsCommentsCreateProcessor';