Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
createentity.php
1<?php
2
4
11
12Loc::loadMessages(__FILE__);
13
14class CreateEntity extends Base
15{
16 public const TYPE = 'CREATEENTITY';
17 public const POST_TEXT = 'commentAuxCreateEntity';
18
19 public const SOURCE_TYPE_BLOG_POST = 'BLOG_POST';
20 public const SOURCE_TYPE_TASK = 'TASK';
21 public const SOURCE_TYPE_FORUM_TOPIC = 'FORUM_TOPIC';
22 public const SOURCE_TYPE_CALENDAR_EVENT = 'CALENDAR_EVENT';
23 public const SOURCE_TYPE_TIMEMAN_ENTRY = 'TIMEMAN_ENTRY';
24 public const SOURCE_TYPE_TIMEMAN_REPORT = 'TIMEMAN_REPORT';
25 public const SOURCE_TYPE_LOG_ENTRY = 'LOG_ENTRY';
26 public const SOURCE_TYPE_PHOTO_ALBUM = 'PHOTO_ALBUM';
27 public const SOURCE_TYPE_PHOTO_PHOTO = 'PHOTO_PHOTO';
28 public const SOURCE_TYPE_WIKI = 'WIKI';
29 public const SOURCE_TYPE_LISTS_NEW_ELEMENT = 'LISTS_NEW_ELEMENT';
30 public const SOURCE_TYPE_INTRANET_NEW_USER = 'INTRANET_NEW_USER';
31 public const SOURCE_TYPE_BITRIX24_NEW_USER = 'BITRIX24_NEW_USER';
32
33 public const SOURCE_TYPE_BLOG_COMMENT = 'BLOG_COMMENT';
34 public const SOURCE_TYPE_FORUM_POST = 'FORUM_POST';
35 public const SOURCE_TYPE_LOG_COMMENT = 'LOG_COMMENT';
36
37 public const ENTITY_TYPE_TASK = 'TASK';
38 public const ENTITY_TYPE_BLOG_POST = 'BLOG_POST';
39 public const ENTITY_TYPE_CALENDAR_EVENT = 'CALENDAR_EVENT';
40
41 protected $postTypeList = [
55 ];
56 protected $commentTypeList = [
60 ];
61
62 protected $entityTypeList = [
66 ];
67
68 protected $postTypeListInited = false;
69 protected $commentTypeListInited = false;
70
71 protected static $blogPostClass = \CBlogPost::class;
72 protected static $blogCommentClass = \CBlogComment::class;
73
74 public function getPostTypeList(): array
75 {
76 if ($this->postTypeListInited === false)
77 {
78 $moduleEvent = new \Bitrix\Main\Event(
79 'socialnetwork',
80 'onCommentAuxGetPostTypeList',
81 []
82 );
83 $moduleEvent->send();
84
85 foreach ($moduleEvent->getResults() as $moduleEventResult)
86 {
87 if ($moduleEventResult->getType() === \Bitrix\Main\EventResult::SUCCESS)
88 {
89 $moduleEventParams = $moduleEventResult->getParameters();
90
91 if (
92 is_array($moduleEventParams)
93 && !empty($moduleEventParams['typeList'])
94 && is_array($moduleEventParams['typeList'])
95 )
96 {
97 foreach ($moduleEventParams['typeList'] as $type)
98 {
99 $this->addPostTypeList($type);
100 }
101 }
102 }
103 }
104
105 $this->postTypeListInited = true;
106 }
107
108 return $this->postTypeList;
109 }
110
111 public function getCommentTypeList(): array
112 {
113 if ($this->commentTypeListInited === false)
114 {
115 $moduleEvent = new \Bitrix\Main\Event(
116 'socialnetwork',
117 'onCommentAuxGetCommentTypeList',
118 []
119 );
120 $moduleEvent->send();
121
122 foreach ($moduleEvent->getResults() as $moduleEventResult)
123 {
124 if ($moduleEventResult->getType() === \Bitrix\Main\EventResult::SUCCESS)
125 {
126 $moduleEventParams = $moduleEventResult->getParameters();
127
128 if (
129 is_array($moduleEventParams)
130 && !empty($moduleEventParams['typeList'])
131 && is_array($moduleEventParams['typeList'])
132 )
133 {
134 foreach($moduleEventParams['typeList'] as $type)
135 {
136 $this->addCommentTypeList($type);
137 }
138 }
139 }
140 }
141
142 $this->commentTypeListInited = true;
143 }
144
146 }
147
148 public function addPostTypeList($type): void
149 {
150 $this->postTypeList[] = $type;
151 }
152
153 public function addCommentTypeList($type): void
154 {
155 $this->commentTypeList[] = $type;
156 }
157
158 public function getSourceTypeList(): array
159 {
160 return array_merge($this->getPostTypeList(), $this->getCommentTypeList());
161 }
162
163 public function getEntityTypeList(): array
164 {
166 }
167
168 public function getParamsFromFields($fields = []): array
169 {
170 $params = [];
171
172 if (!empty($fields['SHARE_DEST']))
173 {
174 $params = $this->getSocNetData($fields['SHARE_DEST']);
175 }
176 elseif (
177 isset($fields['RATING_TYPE_ID'], $fields['SOURCE_ID'])
178 && (int)$fields['SOURCE_ID'] > 0
179 && in_array($fields['RATING_TYPE_ID'], ['FORUM_POST', 'CRM_ENTITY_COMMENT' ])
180 && Loader::includeModule('forum')
181 )
182 {
183 $messageId = (int)$fields['SOURCE_ID'];
184
185 $forumPostLivefeedProvider = new \Bitrix\Socialnetwork\Livefeed\ForumPost();
186 $commentData = $forumPostLivefeedProvider->getAuxCommentCachedData($messageId);
187
188 $serviceData = $this->getForumServiceData($commentData);
189
190 if (
191 !empty($commentData)
192 && !empty($serviceData)
193 && isset($commentData['SERVICE_TYPE'])
194 && $commentData['SERVICE_TYPE'] === $this->getForumType()
195 )
196 {
197 try
198 {
199 $messageParams = Json::decode($serviceData);
200 }
201 catch (ArgumentException $e)
202 {
203 $messageParams = [];
204 }
205
206 $params = $messageParams;
207 }
208 else
209 {
210 $res = MessageTable::getList([
211 'filter' => [
212 '=ID' => (int)$fields['SOURCE_ID']
213 ],
214 'select' => $this->getForumMessageFields(),
215 ]);
216
217 if ($forumMessageFields = $res->fetch())
218 {
219 $serviceData = $this->getForumServiceData($forumMessageFields);
220 if (!empty($serviceData))
221 {
222 try
223 {
224 $messageParams = Json::decode($serviceData);
225 }
226 catch (ArgumentException $e)
227 {
228 $messageParams = [];
229 }
230
231 $params = $messageParams;
232 }
233 }
234 }
235 }
236
237 return $params;
238 }
239
240 public function getText(): string
241 {
242 static $userPage = null;
243 static $parser = null;
244
245 $result = '';
248
249 $siteId = (!empty($options['siteId']) ? $options['siteId'] : SITE_ID);
250
251 if (
252 !isset($params['sourceType'], $params['sourceId'], $params['entityId'], $params['entityType'])
253 || (int)$params['sourceId'] <= 0
254 || (int)$params['entityId'] <= 0
255 || !in_array($params['sourceType'], $this->getSourceTypeList(), true)
256 || !in_array($params['entityType'], $this->getEntityTypeList(), true)
257 )
258 {
259 return $result;
260 }
261
262 if ($provider = $this->getLivefeedProvider())
263 {
264 $options['suffix'] = $provider->getSuffix($options['suffix'] ?? null);
265 $this->setOptions($options);
266 }
267
268 if ($userPage === null)
269 {
270 $userPage = Option::get(
271 'socialnetwork',
272 'user_page',
273 SITE_DIR.'company/personal/',
274 $siteId
275 ).'user/#user_id#/';
276 }
277
278 if (in_array($params['sourceType'], $this->getCommentTypeList(), true))
279 {
280 $sourceData = $this->getSourceCommentData([
281 'userPage' => $userPage,
282 ]);
283
284 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_COMMENT_' . $params['sourceType'] . (!empty($sourceData['suffix']) ? '_' . $sourceData['suffix'] : ''), [
285 '#ENTITY_CREATED#' => $this->getEntityCreatedMessage(),
286 '#ENTITY_NAME#' => $this->getEntityName(),
287 '#A_BEGIN#' => (!empty($sourceData['path']) ? '[URL=' . $sourceData['path'] . ']' : ''),
288 '#A_END#' => (!empty($sourceData['path']) ? '[/URL]' : '')
289 ]);
290 }
291 elseif (in_array($params['sourceType'], $this->getPostTypeList(), true))
292 {
293 $suffix = ($options['suffix'] ?? ($params['sourceType'] === static::SOURCE_TYPE_BLOG_POST ? '2' : ''));
294
295 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_POST_' . $params['sourceType'] . (!empty($suffix) ? '_' . $suffix : ''), [
296 '#ENTITY_CREATED#' => $this->getEntityCreatedMessage(),
297 '#ENTITY_NAME#' => $this->getEntityName(),
298 ]);
299 }
300
301 if (!empty($result))
302 {
303 if ($parser === null)
304 {
305 $parser = new \CTextParser();
306 $parser->allow = [ 'HTML' => 'N', 'ANCHOR' => 'Y' ];
307 }
308 $result = $parser->convertText($result);
309 }
310
311 return (string)$result;
312 }
313
314 protected function getNotFoundMessage(): string
315 {
316 $result = '';
317
319 if (
320 !isset($params['entityType'])
321 || !in_array($params['entityType'], $this->getEntityTypeList(), true)
322 )
323 {
324 return $result;
325 }
326
327 $entityType = $params['entityType'];
328
329 switch ($entityType)
330 {
331 case static::ENTITY_TYPE_TASK:
332 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_TASK_NOT_FOUND');
333 break;
334 case static::ENTITY_TYPE_BLOG_POST:
335 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_BLOG_POST_NOT_FOUND');
336 break;
337 case static::ENTITY_TYPE_CALENDAR_EVENT:
338 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_CALENDAR_EVENT_NOT_FOUND');
339 break;
340 default:
341 }
342
343 return (string)$result;
344 }
345
346 protected function getEntityCreatedMessage(): string
347 {
348 $result = '';
349
351
352 if (
353 !isset($params['entityType'])
354 || !in_array($params['entityType'], $this->getEntityTypeList(), true)
355 )
356 {
357 return $result;
358 }
359
360 switch ($params['entityType'])
361 {
362 case static::ENTITY_TYPE_TASK:
363 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_ENTITY_CREATED_TASK');
364 break;
365 case static::ENTITY_TYPE_BLOG_POST:
366 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_ENTITY_CREATED_BLOG_POST');
367 break;
368 case static::ENTITY_TYPE_CALENDAR_EVENT:
369 $result = Loc::getMessage('SONET_COMMENTAUX_CREATEENTITY_ENTITY_CREATED_CALENDAR_EVENT');
370 break;
371 default:
372 $result = '';
373 }
374
375 return (string)$result;
376 }
377
378 protected function getEntityName(): string
379 {
380 if ($entity = $this->getEntity(false))
381 {
382 $entityPath = $entity['url'];
383 $entityTitle = $entity['title'];
384 }
385 else
386 {
387 $entityPath = '';
388 $entityTitle = $this->getNotFoundMessage();
389 }
390
391 if (mb_strlen($entityTitle) <= 0)
392 {
393 return '';
394 }
395
396 return (!empty($entityPath) ? '[URL=' . $entityPath . ']' . $entityTitle . '[/URL]' : $entityTitle);
397 }
398
399 public function checkRecalcNeeded($fields, $params): bool
400 {
401 $result = false;
402
403 if (
404 !empty($params['bPublicPage'])
405 && $params['bPublicPage']
406 )
407 {
408 $result = true;
409 }
410 else
411 {
412 $handlerParams = $this->getParamsFromFields($fields);
413
414 if (
415 !empty($handlerParams)
416 && !empty($handlerParams['entityType'])
417 && !empty($handlerParams['entityId'])
418 && (int)$handlerParams['entityId'] > 0
419 && ($this->getEntity())
420 )
421 {
422 $result = true;
423 }
424 }
425
426 return $result;
427 }
428
429 protected function getEntity($checkPermissions = true)
430 {
431 static $cache = [
432 'Y' => [],
433 'N' => [],
434 ];
435
437 $entityType = $params['entityType'] ?? null;
438 $entityId = (int) ($params['entityId'] ?? null);
439
440 $result = false;
441 $permissionCacheKey = ($checkPermissions ? 'Y' : 'N');
442 $entityKey = $entityType . '_' . $entityId;
443
444 if (isset($cache[$permissionCacheKey][$entityKey]))
445 {
446 $result = $cache[$permissionCacheKey][$entityKey];
447 }
448 else
449 {
450 $entity = false;
451
452 switch ($entityType)
453 {
454 case static::ENTITY_TYPE_TASK:
455 $provider = new \Bitrix\Socialnetwork\Livefeed\TasksTask();
456 break;
457 case static::ENTITY_TYPE_BLOG_POST:
458 $provider = new \Bitrix\Socialnetwork\Livefeed\BlogPost();
459 break;
460 case static::ENTITY_TYPE_CALENDAR_EVENT:
461 $provider = new \Bitrix\Socialnetwork\Livefeed\CalendarEvent();
462 break;
463 default:
464 $provider = false;
465 }
466
467 if ($provider)
468 {
469 $provider->setEntityId($entityId);
470 $provider->setOption('checkAccess', $checkPermissions);
471
472 $entity = [
473 'title' => $provider->getSourceTitle(),
474 'url' => $provider->getLiveFeedUrl(),
475 ];
476 }
477
478 if ($entity)
479 {
480 $result = $cache[$permissionCacheKey][$entityKey] = $entity;
481 }
482 elseif(!$checkPermissions)
483 {
484 $result = $cache[$permissionCacheKey][$entityKey] = false;
485 }
486 }
487
488 return $result;
489 }
490
491 protected function getRatingNotificationNotigyTag(array $ratingVoteParams = [], array $fields = []): string
492 {
493 return 'RATING|' . ($ratingVoteParams['VALUE'] >= 0 ? '' : 'DL|') . 'BLOG_COMMENT|' . $fields['ID'];
494 }
495
496 protected function getForumType(): string
497 {
498 return \Bitrix\Forum\Comments\Service\Manager::TYPE_ENTITY_CREATED;
499 }
500
501 protected function getForumServiceData(array $commentData = [])
502 {
503 return $commentData['SERVICE_DATA'];
504 }
505
506 protected function getForumMessageFields(): array
507 {
508 return [ 'SERVICE_DATA' ];
509 }
510
511 protected function getSocNetData($data = ''): array
512 {
513 try
514 {
515 $result = Json::decode($data);
516 }
517 catch (ArgumentException $e)
518 {
519 $result = [];
520 }
521
522 return $result;
523 }
524
525 public function getLivefeedProvider()
526 {
529
530 return \Bitrix\Socialnetwork\Livefeed\Provider::init([
531 'ENTITY_TYPE' => ($params['sourceType'] ?? $params['sourcetype']),
532 'ENTITY_ID' => (int)($params['sourceId'] ?? $params['sourceid']),
533 'LOG_ID' => (int)($options['logId'] ?? 0)
534 ]);
535 }
536
537 protected function getSourceCommentData(array $additionalParams = []): array
538 {
539 $result = [
540 'path' => '',
541 'suffix' => '',
542 ];
543
546
547 $userPage = ($additionalParams['userPage'] ?? '');
548 $params['sourceType'] = ($params['sourceType'] ?? $params['sourcetype']);
549 $params['sourceId'] = (int)($params['sourceId'] ?? $params['sourceid']);
550
551 if (
552 $params['sourceType'] === static::SOURCE_TYPE_BLOG_COMMENT
553 && Loader::includeModule('blog')
554 && ($comment = static::$blogCommentClass::getById($params['sourceId']))
555 && ($post = static::$blogPostClass::getById($comment['POST_ID']))
556 )
557 {
558 $result['path'] = (
559 (!isset($options['im']) || !$options['im'])
560 && (!isset($options['bPublicPage']) || !$options['bPublicPage'])
561 && (!isset($options['mail']) || !$options['mail'])
562 ? str_replace([ '#user_id#', '#USER_ID#' ], $post['AUTHOR_ID'], $userPage) . 'blog/' . $post['ID'] . '/?commentId=' . $params['sourceId'] . '#com' . $params['sourceId']
563 : ''
564 );
565 }
566 else
567 {
568 $commentProvider = \Bitrix\Socialnetwork\Livefeed\Provider::getProvider($params['sourceType']);
569
570 if (
571 $commentProvider
572 && (!isset($options['im']) || !$options['im'])
573 && (!isset($options['bPublicPage']) || !$options['bPublicPage'])
574 && (!isset($options['mail']) || !$options['mail'])
575 && isset($options['logId'])
576 && (int)$options['logId'] > 0
577 )
578 {
579 $commentProvider->setEntityId((int)$params['sourceId']);
580 $commentProvider->setLogId($options['logId']);
581 $commentProvider->initSourceFields();
582
583 $result['path'] = $commentProvider->getLiveFeedUrl();
584 }
585 }
586
587 $result['suffix'] = ($options['suffix'] ?? ($params['sourceType'] === static::SOURCE_TYPE_BLOG_COMMENT ? '2' : ''));
588
589 return $result;
590 }
591}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getRatingNotificationNotigyTag(array $ratingVoteParams=[], array $fields=[])
getSourceCommentData(array $additionalParams=[])