Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
2
10
26use Bitrix\Disk\Uf\FileUserType;
27
28class Helper
29{
30 public static function addBlogPost($params, $scope = Controller::SCOPE_AJAX, &$resultFields = [])
31 {
32 global $USER, $CACHE_MANAGER, $APPLICATION;
33
34 $siteId = (
35 is_set($params, 'SITE_ID')
36 && !empty($params['SITE_ID'])
37 ? $params['SITE_ID']
38 : SITE_ID
39 );
40
41 $authorId = (
42 isset($params['USER_ID'])
43 && (int)$params['USER_ID'] > 0
45 ? $params['USER_ID']
46 : $USER->getId()
47 );
48
49 if (!Loader::includeModule('blog'))
50 {
51 $APPLICATION->throwException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_BLOG_MODULE_NOT_INSTALLED'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_MODULE_BLOG_NOT_INSTALLED');
52 return false;
53 }
54
55 $blogGroupId = Option::get('socialnetwork', 'userbloggroup_id', false, $siteId);
56 if (empty($blogGroupId))
57 {
59 'SITE_ID' => $siteId
60 ]);
61 if (!empty($blogGroupIdList))
62 {
63 $blogGroupId = array_shift($blogGroupIdList);
64 }
65 }
66
67 $blog = Blog::getByUser([
68 'GROUP_ID' => $blogGroupId,
69 'SITE_ID' => $siteId,
70 'USER_ID' => $authorId,
71 'CREATE' => 'Y'
72 ]);
73
74 if (!$blog)
75 {
76 $APPLICATION->throwException('Blog not found', 'SONET_CONTROLLER_LIVEFEED_BLOG_NOT_FOUND');
77 return false;
78 }
79
80 $connection = \Bitrix\Main\Application::getConnection();
81 $helper = $connection->getSqlHelper();
82
83 $postFields = [
84 'BLOG_ID' => $blog['ID'],
85 'AUTHOR_ID' => $authorId,
86 '=DATE_CREATE' => $helper->getCurrentDateTimeFunction(),
87 '=DATE_PUBLISH' => $helper->getCurrentDateTimeFunction(),
88 'MICRO' => 'N',
89 'TITLE' => (($params['POST_TITLE'] ?? '') <> '' ? $params['POST_TITLE'] : ''),
90 'DETAIL_TEXT' => $params['POST_MESSAGE'],
91 'DETAIL_TEXT_TYPE' => 'text',
92 'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH,
93 'HAS_IMAGES' => 'N',
94 'HAS_TAGS' => 'N',
95 'HAS_SOCNET_ALL' => 'N'
96 ];
97
98 $emailUserAllowed = (
99 ModuleManager::isModuleInstalled('mail')
100 && ModuleManager::isModuleInstalled('intranet')
101 && (
102 !Loader::includeModule('bitrix24')
103 || \CBitrix24::isEmailConfirmed()
104 )
105 );
106
107 if (
108 !empty($params['DEST'])
109 && is_array($params['DEST'])
110 )
111 {
112 $resultFields = [
113 'ERROR_MESSAGE' => false,
114 'PUBLISH_STATUS' => $postFields['PUBLISH_STATUS']
115 ];
116
117 if ($emailUserAllowed)
118 {
119 $destinationList = $params['DEST'];
121 $params['DEST'] = array_unique($destinationList);
122 }
123
124 $postFields['SOCNET_RIGHTS'] = ComponentHelper::checkBlogPostDestinationList([
125 'DEST' => $params['DEST'],
126 'SITE_ID' => $siteId,
127 'AUTHOR_ID' => $authorId,
128 'MOBILE' => ($params['MOBILE'] ?? 'N'),
129 ], $resultFields);
130
131 if ($resultFields['ERROR_MESSAGE_PUBLIC'] ?? null)
132 {
133 return false;
134 }
135
136 if ($resultFields['ERROR_MESSAGE'])
137 {
138 $APPLICATION->throwException($resultFields['ERROR_MESSAGE']);
139 return false;
140 }
141
142 $postFields['PUBLISH_STATUS'] = $resultFields['PUBLISH_STATUS'];
143 }
144 elseif (
145 !empty($params['SPERM'])
146 && $scope === Controller::SCOPE_REST
147 )
148 {
149 if ($emailUserAllowed)
150 {
151 $pseudoHttpPostFields = [
152 'SPERM' => $params['SPERM'],
153 'INVITED_USER_NAME' => (!empty($params['INVITED_USER_NAME']) && is_array($params['INVITED_USER_NAME']) ? $params['INVITED_USER_NAME'] : []),
154 'INVITED_USER_LAST_NAME' => (!empty($params['INVITED_USER_NAME']) && is_array($params['INVITED_USER_LAST_NAME']) ? $params['INVITED_USER_LAST_NAME'] : []),
155 'INVITED_USER_CRM_ENTITY' => (!empty($params['INVITED_USER_CRM_ENTITY']) && is_array($params['INVITED_USER_CRM_ENTITY']) ? $params['INVITED_USER_CRM_ENTITY'] : []),
156 'INVITED_USER_CREATE_CRM_CONTACT' => (!empty($params['INVITED_USER_CREATE_CRM_CONTACT']) && is_array($params['INVITED_USER_CREATE_CRM_CONTACT']) ? $params['INVITED_USER_CREATE_CRM_CONTACT'] : []),
157 ];
158 $temporaryParams = [
159 'ALLOW_EMAIL_INVITATION' => true
160 ];
161 ComponentHelper::processBlogPostNewMailUser($pseudoHttpPostFields, $temporaryParams);
162 if (!empty($temporaryParams['ERROR_MESSAGE']))
163 {
164 $APPLICATION->throwException($temporaryParams['ERROR_MESSAGE'], 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_ADD_ERROR');
165 return false;
166 }
167
168 $params['SPERM'] = $pseudoHttpPostFields['SPERM'];
169 }
170
171 $resultFields = [
172 'ERROR_MESSAGE' => false,
173 'PUBLISH_STATUS' => $postFields['PUBLISH_STATUS'],
174 ];
175
176 $postFields['SOCNET_RIGHTS'] = ComponentHelper::convertBlogPostPermToDestinationList([
177 'PERM' => $params['SPERM'],
178 'IS_REST' => true,
179 'AUTHOR_ID' => $authorId,
180 'SITE_ID' => $siteId
181 ], $resultFields);
182
183 $postFields['PUBLISH_STATUS'] = $resultFields['PUBLISH_STATUS'];
184 if (!empty($resultFields['ERROR_MESSAGE']))
185 {
186 $APPLICATION->throwException($resultFields['ERROR_MESSAGE'], 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_ADD_ERROR');
187 return false;
188 }
189 }
190 elseif (
191 !Loader::includeModule('extranet')
192 || \CExtranet::isIntranetUser()
193 )
194 {
195 $postFields['SOCNET_RIGHTS'] = [ 'UA' ];
196 }
197
198 if (empty($postFields['SOCNET_RIGHTS']))
199 {
200 $APPLICATION->throwException('No destination specified', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_ADD_ERROR');
201 return false;
202 }
203
204 if ((string)$postFields['TITLE'] === '')
205 {
206 $postFields['MICRO'] = 'Y';
207 $postFields['TITLE'] = preg_replace([ "/\n+/is" . BX_UTF_PCRE_MODIFIER, "/\s+/is" . BX_UTF_PCRE_MODIFIER ], ' ', \blogTextParser::killAllTags($postFields['DETAIL_TEXT']));
208 $postFields['TITLE'] = trim($postFields['TITLE'], " \t\n\r\0\x0B\xA0");
209 }
210
211 if (
212 isset($params['IMPORTANT'])
213 && $params['IMPORTANT'] === 'Y'
214 )
215 {
216 $postFields['UF_BLOG_POST_IMPRTNT'] = true;
217
218 if (!empty($params['IMPORTANT_DATE_END']))
219 {
220 $endDate = \CRestUtil::unConvertDate($params['IMPORTANT_DATE_END']);
221 if ($endDate)
222 {
223 $postFields['UF_IMPRTANT_DATE_END'] = \Bitrix\Main\Type\DateTime::createFromUserTime($endDate);
224 }
225 }
226 }
227
228 if (isset($params['GRATITUDE_MEDAL'], $params['GRATITUDE_EMPLOYEES']))
229 {
230 $gratitudeElementId = \Bitrix\Socialnetwork\Helper\Gratitude::create([
231 'medal' => $params['GRATITUDE_MEDAL'],
232 'employees' => $params['GRATITUDE_EMPLOYEES']
233 ]);
234 if ($gratitudeElementId)
235 {
236 $postFields['UF_GRATITUDE'] = $gratitudeElementId;
237 }
238 }
239
240 if (
241 !empty($params['UF_BLOG_POST_VOTE'])
242 && !empty($params['UF_BLOG_POST_VOTE_' . $params['UF_BLOG_POST_VOTE'] . '_DATA'])
243 )
244 {
245 $postFields['UF_BLOG_POST_VOTE'] = $params['UF_BLOG_POST_VOTE'];
246 $GLOBALS['UF_BLOG_POST_VOTE_' . $params['UF_BLOG_POST_VOTE'] . '_DATA'] = $params['UF_BLOG_POST_VOTE_' . $params['UF_BLOG_POST_VOTE'] . '_DATA'];
247 }
248
249 if (!empty($params['BACKGROUND_CODE']))
250 {
251 $postFields['BACKGROUND_CODE'] = $params['BACKGROUND_CODE'];
252 }
253
254 if (
255 isset($params['PARSE_PREVIEW'])
256 && $params['PARSE_PREVIEW'] === 'Y'
257 && !empty($postFields['DETAIL_TEXT'])
258 && ($urlPreviewValue = ComponentHelper::getUrlPreviewValue($postFields['DETAIL_TEXT']))
259 )
260 {
261 $postFields['UF_BLOG_POST_URL_PRV'] = $urlPreviewValue;
262 }
263
264 $result = \CBlogPost::add($postFields);
265
266 if (!$result)
267 {
268 $APPLICATION->throwException('Blog post hasn\'t been added', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_ADD_ERROR');
269 return false;
270 }
271
273 'postId' => $result,
274 'authorId' => $postFields['AUTHOR_ID']
275 ]);
276
277 \Bitrix\Main\FinderDestTable::merge([
278 'CONTEXT' => 'blog_post',
279 'CODE' => \Bitrix\Main\FinderDestTable::convertRights($socnetPerms, [ 'U' . $postFields['AUTHOR_ID'] ])
280 ]);
281
282 if (
283 isset($params['IMPORTANT'])
284 && $params['IMPORTANT'] === 'Y'
285 )
286 {
287 \CBlogUserOptions::setOption($result, 'BLOG_POST_IMPRTNT', 'Y', $authorId);
288
289 if (defined('BX_COMP_MANAGED_CACHE'))
290 {
291 $CACHE_MANAGER->clearByTag('blogpost_important_all');
292 }
293 }
294
295 $categoryIdListFromPostData = BlogPostEdit\Tag::getTagsFromPostData([
296 'blogId' => $blog['ID'],
297 'tags' => $params['TAGS'] ?? '',
298 ]);
299 $newCategoryIdList = BlogPostEdit\Tag::parseTagsFromFields([
300 'blogCategoryIdList' => $categoryIdListFromPostData,
301 'postFields' => $postFields,
302 'blogId' => $blog['ID'],
303 ]);
304 $categoryIdList = array_merge($categoryIdListFromPostData, $newCategoryIdList);
305 if (!empty($categoryIdList))
306 {
307 \CBlogPostCategory::deleteByPostID($result);
308 foreach ($categoryIdList as $categoryId)
309 {
310 \CBlogPostCategory::add([
311 'BLOG_ID' => $postFields['BLOG_ID'],
312 'POST_ID' => $result,
313 'CATEGORY_ID' => $categoryId
314 ]);
315 }
316
317 \CBlogPost::update(
318 $result,
319 [
320 'CATEGORY_ID' => implode(',', $categoryIdList),
321 'HAS_TAGS' => 'Y'
322 ]
323 );
324 }
325
326 if (
327 Option::get('disk', 'successfully_converted', false)
328 && Loader::includeModule('disk')
329 && ($storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId($authorId))
330 && ($folder = $storage->getFolderForUploadedFiles())
331 )
332 {
333 $filesList = [];
334
335 if (
336 isset($params['FILES'])
337 && is_array($params['FILES'])
338 && $scope === Controller::SCOPE_REST
339 )
340 {
341 foreach ($params['FILES'] as $fileData)
342 {
343 $fileFields = \CRestUtil::saveFile($fileData);
344
345 if (is_array($fileFields))
346 {
347 $file = $folder->uploadFile(
348 $fileFields, // file array
349 [
350 'NAME' => $fileFields['name'],
351 'CREATED_BY' => $authorId
352 ],
353 [],
354 true
355 );
356
357 if ($file)
358 {
359 $filesList[] = FileUserType::NEW_FILE_PREFIX . $file->getId();
360 }
361 }
362 }
363 }
364 elseif (
365 isset($params['UF_BLOG_POST_FILE'])
366 && is_array($params['UF_BLOG_POST_FILE'])
367 )
368 {
369 $filesList = $params['UF_BLOG_POST_FILE'];
370 }
371
372 if (!empty($filesList)) // update post
373 {
374 \CBlogPost::update(
375 $result,
376 [
377 'HAS_PROPS' => 'Y',
378 'UF_BLOG_POST_FILE' => $filesList
379 ]
380 );
381 }
382 }
383
384 $pathToPost = \Bitrix\Socialnetwork\Helper\Path::get('userblogpost_page', $siteId);
385
386 $postFields['ID'] = $result;
387
388 $postUrl = \CComponentEngine::makePathFromTemplate(htmlspecialcharsBack($pathToPost), [
389 'post_id' => $result,
390 'user_id' => $blog['OWNER_ID']
391 ]);
392
393 if ($postFields['PUBLISH_STATUS'] === BLOG_PUBLISH_STATUS_PUBLISH)
394 {
395 $paramsNotify = [
396 'bSoNet' => true,
397 'allowVideo' => Option::get('blog', 'allow_video', 'Y'),
398 'PATH_TO_POST' => $pathToPost,
399 'user_id' => $authorId,
400 'NAME_TEMPLATE' => \CSite::getNameFormat(null, $siteId),
401 ];
402
403 $logId = \CBlogPost::notify($postFields, $blog, $paramsNotify);
404 if (
405 $logId
406 && ($post = Post::getById($result))
407 )
408 {
409 \CSocNetLog::update($logId, [
410 'EVENT_ID' => self::getBlogPostEventId([
411 'postId' => $post->getId()
412 ]),
413 'SOURCE_ID' => $result, // table column field
414 'TAG' => $post->getTags(),
415 ]);
416 }
417
418 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
419 'TYPE' => 'posts_last',
420 'SITE_ID' => $siteId
421 ]));
422
423 $mentionList = \Bitrix\Socialnetwork\Helper\Mention::getUserIds($postFields['DETAIL_TEXT']);
424
426 'post' => [
427 'ID' => $result,
428 'TITLE' => $postFields['TITLE'],
429 'AUTHOR_ID' => $authorId
430 ],
431 'siteId' => $siteId,
432 'postUrl' => $postUrl,
433 'socnetRights' => ($logId ? LogRight::get($logId) : $postFields['SOCNET_RIGHTS']),
434 'socnetRightsOld' => [],
435 'mentionListOld' => [],
436 'mentionList' => $mentionList
437 ]);
438 }
439 elseif (
440 $postFields['PUBLISH_STATUS'] === BLOG_PUBLISH_STATUS_READY
441 && !empty($postFields['SOCNET_RIGHTS'])
442 )
443 {
444 \CBlogPost::notifyImReady([
445 'TYPE' => 'POST',
446 'POST_ID' => $result,
447 'TITLE' => $postFields['TITLE'],
448 'POST_URL' => $postUrl,
449 'FROM_USER_ID' => $authorId,
450 'TO_SOCNET_RIGHTS' => $postFields['SOCNET_RIGHTS']
451 ]);
452
453 $resultFields['WARNING_MESSAGE_PUBLIC'] = Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_MODERATION_WARNING');
454 }
455
456 foreach ($postFields['SOCNET_RIGHTS'] as $destination)
457 {
458 if (preg_match('/^SG(\d+)/i', $destination, $matches))
459 {
460 \CSocNetGroup::setLastActivity($matches[1]);
461 }
462 }
463
464 return $result;
465 }
466
467 public static function updateBlogPost($params = [], $scope = Controller::SCOPE_AJAX, &$resultFields = [])
468 {
469 global $USER, $USER_FIELD_MANAGER, $APPLICATION, $CACHE_MANAGER;
470
471 $postId = (int) ($params['POST_ID'] ?? null);
472
473 if ($postId <= 0)
474 {
475 $APPLICATION->throwException('Wrong post ID', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_UPDATE_ERROR');
476 return false;
477 }
478
479 if (!Loader::includeModule('blog'))
480 {
481 $APPLICATION->throwException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_BLOG_MODULE_NOT_INSTALLED'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_UPDATE_ERROR');
482 return false;
483 }
484
485 $currentUserId = (
486 isset($params['USER_ID'])
487 && (int)$params['USER_ID'] > 0
489 ? $params['USER_ID']
490 : $USER->getId()
491 );
492
493 $siteId = (
494 is_set($params, 'SITE_ID')
495 && !empty($params['SITE_ID'])
496 ? $params['SITE_ID']
497 : SITE_ID
498 );
499
500 $currentUserPerm = self::getBlogPostPerm([
501 'USER_ID' => $currentUserId,
502 'POST_ID' => $postId
503 ]);
504
505 if ($currentUserPerm <= Permissions::WRITE)
506 {
507 $APPLICATION->throwException('No write perms', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_UPDATE_ERROR');
508 return false;
509 }
510
511 $postFields = Post::getById($postId)->getFields();
512 if (empty($postFields))
513 {
514 $APPLICATION->throwException('No post found', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_UPDATE_ERROR');
515 return false;
516 }
517
518 $blog = Blog::getByUser([
519 'GROUP_ID' => Option::get('socialnetwork', 'userbloggroup_id', false, $siteId),
520 'SITE_ID' => $siteId,
521 'USER_ID' => $postFields['AUTHOR_ID']
522 ]);
523
524 if (!$blog)
525 {
526 $APPLICATION->throwException('No blog found', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_UPDATE_ERROR');
527 return false;
528 }
529
530 $updateFields = [
531 'PUBLISH_STATUS' => $postFields['PUBLISH_STATUS']
532 ];
533
534 $updateFields['TITLE'] = '';
535 $updateFields['MICRO'] = 'N';
536
537 if (isset($params['POST_TITLE']))
538 {
539 $updateFields['TITLE'] = $params['POST_TITLE'];
540 }
541
542 if (
543 (string)$updateFields['TITLE'] === ''
544 && isset($params['POST_MESSAGE'])
545 )
546 {
547 $updateFields['MICRO'] = 'Y';
548 $updateFields['TITLE'] = preg_replace([ "/\n+/is" . BX_UTF_PCRE_MODIFIER, "/\s+/is" . BX_UTF_PCRE_MODIFIER ], ' ', \blogTextParser::killAllTags($params['POST_MESSAGE']));
549 $updateFields['TITLE'] = trim($updateFields['TITLE'], " \t\n\r\0\x0B\xA0");
550 }
551
552 if (($params['POST_MESSAGE'] ?? '') <> '')
553 {
554 $updateFields['DETAIL_TEXT'] = $params['POST_MESSAGE'];
555 }
556
557 if (!empty($params['DEST']))
558 {
559 if (
560 ModuleManager::isModuleInstalled('mail')
561 && ModuleManager::isModuleInstalled('intranet')
562 && (
563 !Loader::includeModule('bitrix24')
564 || \CBitrix24::isEmailConfirmed()
565 )
566 )
567 {
568 $destinationList = $params['DEST'];
570 $params['DEST'] = array_unique($destinationList);
571 }
572
573 $resultFields = [
574 'ERROR_MESSAGE' => false,
575 'PUBLISH_STATUS' => $updateFields['PUBLISH_STATUS']
576 ];
577
578 $updateFields['SOCNET_RIGHTS'] = ComponentHelper::checkBlogPostDestinationList([
579 'DEST' => $params['DEST'],
580 'SITE_ID' => $siteId,
581 'AUTHOR_ID' => $postFields['AUTHOR_ID'],
582 'MOBILE' => ($params['MOBILE'] ?? 'N'),
583 ], $resultFields);
584
585 if ($resultFields['ERROR_MESSAGE_PUBLIC'] ?? null)
586 {
587 return false;
588 }
589
590 if ($resultFields['ERROR_MESSAGE'])
591 {
592 $APPLICATION->throwException($resultFields['ERROR_MESSAGE'], 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_UPDATE_ERROR');
593 return false;
594 }
595
596 $updateFields['PUBLISH_STATUS'] = $resultFields['PUBLISH_STATUS'];
597 }
598
599 if (isset($params['IMPORTANT']))
600 {
601 if ($params['IMPORTANT'] === 'Y')
602 {
603 $updateFields['UF_BLOG_POST_IMPRTNT'] = true;
604
605 if (!empty($params['IMPORTANT_DATE_END']))
606 {
607 $endDate = \CRestUtil::unConvertDate($params['IMPORTANT_DATE_END']);
608 if ($endDate)
609 {
610 $updateFields['UF_IMPRTANT_DATE_END'] = \Bitrix\Main\Type\DateTime::createFromUserTime($endDate);
611 }
612 }
613 }
614 else
615 {
616 $updateFields['UF_BLOG_POST_IMPRTNT'] = false;
617 $updateFields['UF_IMPRTANT_DATE_END'] = false;
618 }
619 }
620
621 $categoryIdListFromPostData = BlogPostEdit\Tag::getTagsFromPostData([
622 'blogId' => $blog['ID'],
623 'tags' => $params['TAGS'] ?? '',
624 ]);
625
626 $newCategoryIdList = BlogPostEdit\Tag::parseTagsFromFields([
627 'blogCategoryIdList' => $categoryIdListFromPostData,
628 'postFields' => $postFields,
629 'blogId' => $blog['ID'],
630 ]);
631 $categoryIdList = array_merge($categoryIdListFromPostData, $newCategoryIdList);
632 if (!empty($categoryIdList))
633 {
634 \CBlogPostCategory::deleteByPostID($postId);
635 foreach ($categoryIdList as $categoryId)
636 {
637 \CBlogPostCategory::add([
638 'BLOG_ID' => $postFields['BLOG_ID'],
639 'POST_ID' => $postId,
640 'CATEGORY_ID' => $categoryId
641 ]);
642 }
643
644 $updateFields['CATEGORY_ID'] = implode(',', $categoryIdList);
645 $updateFields['HAS_TAGS'] = 'Y';
646 }
647
648 if (isset($params['GRATITUDE_MEDAL']))
649 {
650 if (
651 !empty($params['GRATITUDE_MEDAL'])
652 && isset($params['GRATITUDE_EMPLOYEES'])
653 )
654 {
655 $gratitudeElementId = \Bitrix\Socialnetwork\Helper\Gratitude::create([
656 'medal' => $params['GRATITUDE_MEDAL'],
657 'employees' => $params['GRATITUDE_EMPLOYEES']
658 ]);
659 if ($gratitudeElementId)
660 {
661 $updateFields['UF_GRATITUDE'] = $gratitudeElementId;
662 $updateFields['HAS_PROPS'] = 'Y';
663 }
664 }
665 else
666 {
667 $updateFields['UF_GRATITUDE'] = false;
668 }
669 }
670
671 if (isset($params['UF_BLOG_POST_VOTE']))
672 {
673 if (
674 !empty($params['UF_BLOG_POST_VOTE'])
675 && !empty($params['UF_BLOG_POST_VOTE_' . $params['UF_BLOG_POST_VOTE'] . '_DATA'])
676 )
677 {
678 $updateFields['UF_BLOG_POST_VOTE'] = $params['UF_BLOG_POST_VOTE'];
679 $GLOBALS['UF_BLOG_POST_VOTE_' . $params['UF_BLOG_POST_VOTE'] . '_DATA'] = $params['UF_BLOG_POST_VOTE_' . $params['UF_BLOG_POST_VOTE'] . '_DATA'];
680 $updateFields['HAS_PROPS'] = 'Y';
681 }
682 else
683 {
684 $updateFields['UF_BLOG_POST_VOTE'] = false;
685 }
686 }
687
688 if (isset($params['BACKGROUND_CODE']))
689 {
690 $updateFields['BACKGROUND_CODE'] = (!empty($params['BACKGROUND_CODE']) ? $params['BACKGROUND_CODE'] : false);
691 }
692
693 if (
694 isset($params['PARSE_PREVIEW'])
695 && $params['PARSE_PREVIEW'] === 'Y'
696 && !empty($updateFields['DETAIL_TEXT'])
697 && ($urlPreviewValue = ComponentHelper::getUrlPreviewValue($updateFields['DETAIL_TEXT']))
698 )
699 {
700 $updateFields['UF_BLOG_POST_URL_PRV'] = $urlPreviewValue;
701 $updateFields['HAS_PROPS'] = 'Y';
702 }
703
704 if ($result = \CBlogPost::update($postId, $updateFields))
705 {
706 if (
707 Option::get('disk', 'successfully_converted', false)
708 && Loader::includeModule('disk')
709 && ($storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId($postFields['AUTHOR_ID']))
710 && ($folder = $storage->getFolderForUploadedFiles())
711 )
712 {
713 $filesList = [];
714
715 $needToDelete = false;
716
717 if (
718 (
719 !empty($params['FILES'])
720 || !empty($params['UF_BLOG_POST_FILE'])
721 )
722 && $scope === Controller::SCOPE_REST
723 )
724 {
725 $postUF = $USER_FIELD_MANAGER->getUserFields('BLOG_POST', $postId, LANGUAGE_ID);
726 if (
727 !empty($postUF['UF_BLOG_POST_FILE'])
728 && !empty($postUF['UF_BLOG_POST_FILE']['VALUE'])
729 )
730 {
731 $filesList = array_merge($filesList, $postUF['UF_BLOG_POST_FILE']['VALUE']);
732 }
733
734 if (!empty($params['FILES']))
735 {
736 foreach ($params['FILES'] as $key => $fileData)
737 {
738 if (
739 $fileData === 'del'
740 && in_array($key, $filesList)
741 )
742 {
743 foreach ($filesList as $i => $v)
744 {
745 if ($v == $key)
746 {
747 unset($filesList[$i]);
748 $needToDelete = true;
749 }
750 }
751 }
752 else
753 {
754 $fileFields = \CRestUtil::saveFile($fileData);
755
756 if (is_array($fileFields))
757 {
758 $file = $folder->uploadFile(
759 $fileFields,
760 [
761 'NAME' => $fileFields['name'],
762 'CREATED_BY' => $postFields['AUTHOR_ID']
763 ],
764 [],
765 true
766 );
767
768 if ($file)
769 {
770 $filesList[] = FileUserType::NEW_FILE_PREFIX . $file->getId();
771 }
772 }
773 }
774 }
775 }
776 elseif (
777 isset($params['UF_BLOG_POST_FILE'])
778 && is_array($params['UF_BLOG_POST_FILE'])
779 )
780 {
781 if (
782 count($params['UF_BLOG_POST_FILE']) === 1
783 && $params['UF_BLOG_POST_FILE'][0] === 'empty'
784 )
785 {
786 $filesList = [];
787 $needToDelete = true;
788 }
789 else
790 {
791 $filesList = array_unique(array_merge($filesList, array_map(static function($value) {
792 return (
793 preg_match('/^' . FileUserType::NEW_FILE_PREFIX . '(\d+)$/i', $value)
794 ? $value
795 : (int)$value
796 );
797 }, $params['UF_BLOG_POST_FILE'])));
798 }
799 }
800 }
801 elseif (
802 !empty($params['UF_BLOG_POST_FILE'])
803 && $scope === Controller::SCOPE_AJAX
804 )
805 {
806 $filesList = array_unique(array_merge($filesList, array_map(static function($value) {
807 return (
808 preg_match('/^' . FileUserType::NEW_FILE_PREFIX . '(\d+)$/i', $value)
809 ? $value
810 : (int)$value
811 );
812 }, $params['UF_BLOG_POST_FILE'])));
813 }
814
815 if (
816 !empty($filesList)
817 || $needToDelete
818 )
819 {
820 \CBlogPost::update($postId, [
821 'HAS_PROPS' => 'Y',
822 'UF_BLOG_POST_FILE' => $filesList
823 ]);
824 }
825 }
826
827 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
828 'TYPE' => 'post',
829 'POST_ID' => $postId
830 ]));
831 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
832 'TYPE' => 'post_general',
833 'POST_ID' => $postId
834 ]));
835 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
836 'TYPE' => 'posts_popular',
837 'SITE_ID' => $siteId
838 ]));
839
840 $updateFields['AUTHOR_ID'] = $postFields['AUTHOR_ID'];
841
842 if ($postFields['PUBLISH_STATUS'] === BLOG_PUBLISH_STATUS_PUBLISH)
843 {
844 if ($updateFields['PUBLISH_STATUS'] === BLOG_PUBLISH_STATUS_DRAFT)
845 {
846 \CBlogPost::deleteLog($postId);
847 }
848 elseif ($updateFields['PUBLISH_STATUS'] === BLOG_PUBLISH_STATUS_PUBLISH)
849 {
850 \CBlogPost::updateLog($postId, $updateFields, $blog, [
851 'allowVideo' => Option::get('blog', 'allow_video', 'Y'),
852 'PATH_TO_SMILE' => false
853 ]);
854 }
855 }
856
857 if (
858 isset($params['IMPORTANT'])
859 && $params['IMPORTANT'] === 'Y'
860 )
861 {
862 \CBlogUserOptions::setOption($result, 'BLOG_POST_IMPRTNT', 'Y', $currentUserId);
863
864 if (defined('BX_COMP_MANAGED_CACHE'))
865 {
866 $CACHE_MANAGER->clearByTag('blogpost_important_all');
867 }
868 }
869 }
870
871 return $result;
872 }
873
883 public static function deleteBlogPost(array $params = []): bool
884 {
885 global $USER;
886
887 $postId = (int)$params['POST_ID'];
888
889 if ($postId <= 0)
890 {
891 throw new ArgumentException('Wrong post ID');
892 }
893
894 if (!Loader::includeModule('blog'))
895 {
896 throw new LoaderException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_BLOG_MODULE_NOT_INSTALLED'));
897 }
898
899 $currentUserId = (
900 isset($params['USER_ID'])
901 && (int)$params['USER_ID'] > 0
903 ? $params['USER_ID']
904 : $USER->getId()
905 );
906
907 $siteId = (
908 is_set($params, 'SITE_ID')
909 && !empty($params['SITE_ID'])
910 ? $params['SITE_ID']
911 : SITE_ID
912 );
913
914 $currentUserPerm = self::getBlogPostPerm([
915 'USER_ID' => $currentUserId,
916 'POST_ID' => $postId
917 ]);
918
919 if ($currentUserPerm < Permissions::FULL)
920 {
921 throw new AccessDeniedException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_DELETE_NO_RIGHTS'));
922 }
923
924 \CBlogPost::DeleteLog($postId);
925
926 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
927 'TYPE' => 'posts_popular',
928 'SITE_ID' => $siteId,
929 ]));
930 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
931 'TYPE' => 'post',
932 'POST_ID' => $postId,
933 ]));
934 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
935 'TYPE' => 'post_general',
936 'POST_ID' => $postId,
937 ]));
938 BXClearCache(true, ComponentHelper::getBlogPostCacheDir([
939 'TYPE' => 'posts_last_blog',
940 'SITE_ID' => $siteId,
941 ]));
942 BXClearCache(true, \CComponentEngine::makeComponentPath('bitrix:socialnetwork.blog.blog'));
943
944 if (!\CBlogPost::delete($postId))
945 {
946 throw new SystemException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_DELETE_ERROR'));
947 }
948
949 $sonetGroupId = (int)($params['ACTIVITY_SONET_GROUP_ID'] ?? 0);
950 if ($sonetGroupId > 0)
951 {
952 \CSocNetGroup::setLastActivity($sonetGroupId);
953 }
954
955 return true;
956 }
957
958 public static function getBlogPostPerm(array $params = [])
959 {
960 global $USER, $APPLICATION;
961
962 if (!Loader::includeModule('blog'))
963 {
964 $APPLICATION->throwException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_BLOG_MODULE_NOT_INSTALLED'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_ERROR');
965 return false;
966 }
967
968 $currentUserId = (
969 isset($params['USER_ID'])
970 && (int)$params['USER_ID'] > 0
972 ? (int)$params['USER_ID']
973 : (int)$USER->getId()
974 );
975
976 $postId = (int)($params['POST_ID'] ?? 0);
977 if ($postId <= 0)
978 {
979 $APPLICATION->throwException('Wrong post ID', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_ERROR');
980 return false;
981 }
982
983 if (
984 \CSocNetUser::isUserModuleAdmin($currentUserId, SITE_ID)
985 || \CMain::getGroupRight('blog') >= 'W'
986 )
987 {
988 return Permissions::FULL;
989 }
990
991 $postItem = Post::getById($postId);
992 $postFields = $postItem->getFields();
993
994 if ((int)$postFields['AUTHOR_ID'] === $currentUserId)
995 {
996 $result = Permissions::FULL;
997 }
998 else
999 {
1000 $permsResult = $postItem->getSonetPerms([
1001 'CHECK_FULL_PERMS' => true
1002 ]);
1003 $result = $permsResult['PERM'];
1004 if (
1005 $result <= Permissions::READ
1006 && $permsResult['READ_BY_OSG']
1007 )
1008 {
1009 $result = Permissions::READ;
1010 }
1011 }
1012
1013 return $result;
1014 }
1015
1016 public static function getBlogPostFields($postId)
1017 {
1018 global $APPLICATION;
1019 $tzOffset = \CTimeZone::getOffset();
1020
1021 $cacheTtl = 2592000;
1022 $cacheId = 'blog_post_socnet_general_' . $postId . '_' . LANGUAGE_ID.($tzOffset <> 0 ? '_' . $tzOffset : '') . '_' . \Bitrix\Main\Context::getCurrent()->getCulture()->getDateTimeFormat() . '_rest';
1024 'TYPE' => 'post_general',
1025 'POST_ID' => $postId
1026 ]);
1027
1028 if (!Loader::includeModule('blog'))
1029 {
1030 $APPLICATION->throwException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_BLOG_MODULE_NOT_INSTALLED'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_MODULE_ERROR');
1031 return false;
1032 }
1033
1034 $cache = new \CPHPCache;
1035 if ($cache->initCache($cacheTtl, $cacheId, $cacheDir))
1036 {
1037 $postFields = $cache->getVars();
1038 $postItem = new Post;
1039 $postItem->setFields($postFields);
1040 }
1041 else
1042 {
1043 $cache->startDataCache();
1044 $postItem = Post::getById($postId);
1045 $postFields = $postItem->getFields();
1046 $cache->endDataCache($postFields);
1047 }
1048
1049 return $postFields;
1050 }
1051
1052 public static function getBlogPostEventId(array $params = []): string
1053 {
1054 global $USER_FIELD_MANAGER;
1055
1056 if (!Loader::includeModule('blog'))
1057 {
1058 throw new SystemException(Loc::getMessage('SOCIALNETWORK_ITEM_HELPER_BLOG_MODULE_NOT_INSTALLED'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_MODULE_BLOG_NOT_INSTALLED');
1059 }
1060
1061 $postId = (isset($params['postId']) && (int)$params['postId'] > 0 ? (int)$params['postId'] : 0);
1062 if ($postId <= 0)
1063 {
1064 throw new SystemException('Empty post ID', 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_EMPTY_POST_ID');
1065 }
1066
1067 $eventId = \Bitrix\Blog\Integration\Socialnetwork\Log::EVENT_ID_POST;
1068 $postUserFields = $USER_FIELD_MANAGER->getUserFields('BLOG_POST', $postId, LANGUAGE_ID);
1069
1070 if (
1071 isset($postUserFields['UF_BLOG_POST_IMPRTNT']['VALUE'])
1072 && (int)$postUserFields['UF_BLOG_POST_IMPRTNT']['VALUE'] > 0
1073 )
1074 {
1075 $eventId = \Bitrix\Blog\Integration\Socialnetwork\Log::EVENT_ID_POST_IMPORTANT;
1076 }
1077 elseif (
1078 isset($postUserFields['UF_BLOG_POST_VOTE']['VALUE'])
1079 && (int)$postUserFields['UF_BLOG_POST_VOTE']['VALUE'] > 0
1080 )
1081 {
1082 $eventId = \Bitrix\Blog\Integration\Socialnetwork\Log::EVENT_ID_POST_VOTE;
1083 }
1084 elseif (
1085 isset($postUserFields['UF_GRATITUDE']['VALUE'])
1086 && (int)$postUserFields['UF_GRATITUDE']['VALUE'] > 0
1087 )
1088 {
1089 $eventId = \Bitrix\Blog\Integration\Socialnetwork\Log::EVENT_ID_POST_GRAT;
1090 }
1091
1092 return $eventId;
1093 }
1094}
Definition post.php:20
setFields($fields=array())
Definition post.php:113
static getCurrent()
Definition context.php:241
static convertRights($rights, $excludeCodes=[])
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static convertBlogPostPermToDestinationList($params, &$resultFields)
static notifyBlogPostCreated($params=array())
static getBlogPostCacheDir($params=array())
static checkBlogPostDestinationList($params, &$resultFields)
static getUrlPreviewValue($text, $html=true)
static processBlogPostNewMailUser(&$HTTPPost, &$componentResult)
static getBlogPostSocNetPerms($params=array())
static processBlogPostNewMailUserDestinations(&$destinationList)
static updateBlogPost($params=[], $scope=Controller::SCOPE_AJAX, &$resultFields=[])
Definition helper.php:467
static addBlogPost($params, $scope=Controller::SCOPE_AJAX, &$resultFields=[])
Definition helper.php:30
static getBlogPostEventId(array $params=[])
Definition helper.php:1052
static getBlogPostFields($postId)
Definition helper.php:1016
static deleteBlogPost(array $params=[])
Definition helper.php:883
static getBlogPostPerm(array $params=[])
Definition helper.php:958
$GLOBALS['____1444769544']
Definition license.php:1
Definition tag.php:3