PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/socialnetwork/lib/item/helper.php

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