PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/socialnetwork/lib/controller/livefeed/blogpost.php

https://gitlab.com/alexprowars/bitrix
PHP | 516 lines | 460 code | 56 blank | 0 comment | 51 complexity | 4d0f7b19e6e99c8687aa17107f91805a MD5 | raw file
  1. <?php
  2. namespace Bitrix\Socialnetwork\Controller\Livefeed;
  3. use Bitrix\Main\Loader;
  4. use Bitrix\Main\Error;
  5. use Bitrix\Main\Localization\Loc;
  6. use Bitrix\Blog\Item\Permissions;
  7. use Bitrix\Main\ModuleManager;
  8. use Bitrix\Main\Web\Json;
  9. use Bitrix\Socialnetwork\ComponentHelper;
  10. use Bitrix\Main\ArgumentException;
  11. use Bitrix\Socialnetwork\Controller\Base;
  12. use Bitrix\Socialnetwork\Item\Helper;
  13. class BlogPost extends Base
  14. {
  15. public function getDataAction(array $params = []): ?array
  16. {
  17. $postId = (int)($params['postId'] ?? 0);
  18. $public = ($params['public'] ?? 'N');
  19. $groupReadOnly = ($params['groupReadOnly'] ?? 'N');
  20. $pathToPost = ($params['pathToPost'] ?? '');
  21. $voteId = (int)($params['voteId'] ?? 0);
  22. $checkModeration = ($params['checkModeration'] ?? 'N');
  23. $currentUserId = (int)$this->getCurrentUser()->getId();
  24. $currentModuleAdmin = \CSocNetUser::isCurrentUserModuleAdmin(SITE_ID, false);
  25. if ($postId <= 0)
  26. {
  27. $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_LIVEFEED_BLOGPOST_EMPTY'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_EMPTY'));
  28. return null;
  29. }
  30. if (
  31. !Loader::includeModule('blog')
  32. || !Loader::includeModule('socialnetwork')
  33. || !($postItem = \Bitrix\Blog\Item\Post::getById($postId))
  34. )
  35. {
  36. $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_LIVEFEED_BLOGPOST_NOT_FOUND'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_NOT_FOUND'));
  37. return null;
  38. }
  39. $postFields = $postItem->getFields();
  40. $logId = 0;
  41. $logFavoritesUserId = 0;
  42. $allowModerate = false;
  43. if (
  44. $postFields['PUBLISH_STATUS'] === BLOG_PUBLISH_STATUS_READY
  45. && $checkModeration === 'Y'
  46. )
  47. {
  48. $postSocnetPermsList = \CBlogPost::getSocNetPerms($postId);
  49. if (
  50. !empty($postSocnetPermsList['SG'])
  51. && is_array($postSocnetPermsList['SG'])
  52. )
  53. {
  54. $groupIdList = array_keys($postSocnetPermsList['SG']);
  55. foreach($groupIdList as $groupId)
  56. {
  57. if (
  58. \CSocNetFeaturesPerms::canPerformOperation($currentUserId, SONET_ENTITY_GROUP, $groupId, 'blog', 'full_post', $currentModuleAdmin)
  59. || \CSocNetFeaturesPerms::canPerformOperation($currentUserId, SONET_ENTITY_GROUP, $groupId, 'blog', 'write_post')
  60. || \CSocNetFeaturesPerms::canPerformOperation($currentUserId, SONET_ENTITY_GROUP, $groupId, 'blog', 'moderate_post')
  61. )
  62. {
  63. $allowModerate = true;
  64. break;
  65. }
  66. }
  67. }
  68. elseif(
  69. (int)$postFields['AUTHOR_ID'] === $currentUserId
  70. || $currentModuleAdmin
  71. )
  72. {
  73. $allowModerate = true;
  74. }
  75. }
  76. $blogPostLivefeedProvider = new \Bitrix\Socialnetwork\Livefeed\BlogPost;
  77. $filter = array(
  78. "EVENT_ID" => $blogPostLivefeedProvider->getEventId(),
  79. "SOURCE_ID" => $postId,
  80. );
  81. if (
  82. Loader::includeModule('extranet')
  83. && \CExtranet::isExtranetSite(SITE_ID)
  84. )
  85. {
  86. $filter["SITE_ID"] = SITE_ID;
  87. }
  88. elseif ($public !== 'Y')
  89. {
  90. $filter["SITE_ID"] = [ SITE_ID, false ];
  91. }
  92. $res = \CSocNetLog::getList(
  93. [],
  94. $filter,
  95. false,
  96. false,
  97. [ 'ID', 'FAVORITES_USER_ID', 'PINNED_USER_ID' ],
  98. [ 'USE_PINNED' => 'Y' ]
  99. );
  100. if ($logEntry = $res->fetch())
  101. {
  102. $logId = (int)$logEntry['ID'];
  103. $logFavoritesUserId = (int)$logEntry['FAVORITES_USER_ID'];
  104. $logPinnedUserId = (int)$logEntry['PINNED_USER_ID'];
  105. }
  106. if ((int)$postFields["AUTHOR_ID"] === $currentUserId)
  107. {
  108. $perms = Permissions::FULL;
  109. }
  110. elseif (
  111. $currentModuleAdmin
  112. || \CMain::getGroupRight('blog') >= 'W'
  113. )
  114. {
  115. $perms = Permissions::FULL;
  116. }
  117. elseif (!$logId)
  118. {
  119. $perms = Permissions::DENY;
  120. }
  121. else
  122. {
  123. $permsResult = $postItem->getSonetPerms([
  124. 'PUBLIC' => ($public === 'Y'),
  125. 'CHECK_FULL_PERMS' => true,
  126. 'LOG_ID' => $logId
  127. ]);
  128. $perms = $permsResult['PERM'];
  129. $groupReadOnly = (
  130. $permsResult['PERM'] <= \Bitrix\Blog\Item\Permissions::READ
  131. && $permsResult['READ_BY_OSG']
  132. ? 'Y'
  133. : 'N'
  134. );
  135. }
  136. $shareForbidden = ComponentHelper::getBlogPostLimitedViewStatus(array(
  137. 'logId' => $logId,
  138. 'postId' => $postId,
  139. 'authorId' => $postFields['AUTHOR_ID']
  140. ));
  141. $postUrl = \CComponentEngine::makePathFromTemplate(
  142. $pathToPost,
  143. [
  144. 'post_id' => $postFields['ID'],
  145. 'user_id' => $postFields['AUTHOR_ID']
  146. ]
  147. );
  148. $voteExportUrl = '';
  149. if ($voteId > 0)
  150. {
  151. $voteExportUrl = \CHTTP::urlAddParams(
  152. \CHTTP::urlDeleteParams(
  153. $postUrl,
  154. [ 'exportVoting ' ]
  155. ),
  156. [ 'exportVoting' => $voteId ]
  157. );
  158. }
  159. return [
  160. 'perms' => $perms,
  161. 'isGroupReadOnly' => $groupReadOnly,
  162. 'isShareForbidden' => ($shareForbidden ? 'Y' : 'N'),
  163. 'logId' => $logId,
  164. 'logFavoritesUserId' => $logFavoritesUserId,
  165. 'logPinnedUserId' => $logPinnedUserId,
  166. 'authorId' => (int)$postFields['AUTHOR_ID'],
  167. 'urlToPost' => $postUrl,
  168. 'urlToVoteExport' => $voteExportUrl,
  169. 'allowModerate' => ($allowModerate ? 'Y' : 'N'),
  170. 'backgroundCode' => $postFields['BACKGROUND_CODE']
  171. ];
  172. }
  173. public function shareAction(array $params = [])
  174. {
  175. $postId = (int)($params['postId'] ?? 0);
  176. $destCodesList = ($params['DEST_CODES'] ?? []);
  177. $destData = ($params['DEST_DATA'] ?? []);
  178. $invitedUserName = ($params['INVITED_USER_NAME'] ?? []);
  179. $invitedUserLastName = ($params['INVITED_USER_LAST_NAME'] ?? []);
  180. $invitedUserCrmEntity = ($params['INVITED_USER_CRM_ENTITY'] ?? []);
  181. $invitedUserCreateCrmContact = ($params['INVITED_USER_CREATE_CRM_CONTACT'] ?? []);
  182. $readOnly = (isset($params['readOnly']) && $params['readOnly'] === 'Y');
  183. $pathToUser = ($params['pathToUser'] ?? '');
  184. $pathToPost = ($params['pathToPost'] ?? '');
  185. $currentUserId = $this->getCurrentUser()->getId();
  186. $data = [
  187. 'ALLOW_EMAIL_INVITATION' => (
  188. ModuleManager::isModuleInstalled('mail')
  189. && ModuleManager::isModuleInstalled('intranet')
  190. && (
  191. !Loader::includeModule('bitrix24')
  192. || \CBitrix24::isEmailConfirmed()
  193. )
  194. )
  195. ];
  196. if ($postId <= 0)
  197. {
  198. $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_LIVEFEED_BLOGPOST_EMPTY'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_EMPTY'));
  199. return null;
  200. }
  201. if (
  202. !Loader::includeModule('blog')
  203. || !($postItem = \Bitrix\Blog\Item\Post::getById($postId))
  204. )
  205. {
  206. $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_LIVEFEED_BLOGPOST_NOT_FOUND'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_NOT_FOUND'));
  207. return null;
  208. }
  209. $currentUserPerm = Helper::getBlogPostPerm([
  210. 'USER_ID' => $currentUserId,
  211. 'POST_ID' => $postId
  212. ]);
  213. if ($currentUserPerm <= Permissions::DENY)
  214. {
  215. $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_LIVEFEED_BLOGPOST_NOT_FOUND'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_NOT_FOUND'));
  216. return null;
  217. }
  218. $postFields = $postItem->getFields();
  219. $perms2update = [];
  220. $sonetPermsListOld = \CBlogPost::getSocNetPerms($postId);
  221. foreach($sonetPermsListOld as $type => $val)
  222. {
  223. foreach($val as $id => $values)
  224. {
  225. if($type !== 'U')
  226. {
  227. $perms2update[] = $type . $id;
  228. }
  229. else
  230. {
  231. $perms2update[] = (
  232. in_array('US' . $id, $values, true)
  233. ? 'UA'
  234. : $type.$id
  235. );
  236. }
  237. }
  238. }
  239. $newRightsList = [];
  240. $sonetPermsListNew = [
  241. 'UA' => [],
  242. 'U' => [],
  243. 'UE' => [],
  244. 'SG' => [],
  245. 'DR' => []
  246. ];
  247. if (!empty($destData))
  248. {
  249. try
  250. {
  251. $entitites = Json::decode($destData);
  252. if (!empty($entitites))
  253. {
  254. $destCodesList = \Bitrix\Main\UI\EntitySelector\Converter::convertToFinderCodes($entitites);
  255. }
  256. }
  257. catch(ArgumentException $e)
  258. {
  259. }
  260. }
  261. foreach($destCodesList as $destCode)
  262. {
  263. if ($destCode === 'UA')
  264. {
  265. $sonetPermsListNew['UA'][] = 'UA';
  266. }
  267. elseif (preg_match('/^UE(.+)$/i', $destCode, $matches))
  268. {
  269. $sonetPermsListNew['UE'][] = $matches[1];
  270. }
  271. elseif (preg_match('/^U(\d+)$/i', $destCode, $matches))
  272. {
  273. $sonetPermsListNew['U'][] = 'U'.$matches[1];
  274. }
  275. elseif (preg_match('/^SG(\d+)$/i', $destCode, $matches))
  276. {
  277. $sonetPermsListNew['SG'][] = 'SG'.$matches[1];
  278. }
  279. elseif (preg_match('/^DR(\d+)$/i', $destCode, $matches))
  280. {
  281. $sonetPermsListNew['DR'][] = 'DR'.$matches[1];
  282. }
  283. }
  284. $HTTPPost = [
  285. 'SONET_PERMS' => $sonetPermsListNew,
  286. 'INVITED_USER_NAME' => $invitedUserName,
  287. 'INVITED_USER_LAST_NAME' => $invitedUserLastName,
  288. 'INVITED_USER_CRM_ENTITY' => $invitedUserCrmEntity,
  289. 'INVITED_USER_CREATE_CRM_CONTACT' => $invitedUserCreateCrmContact
  290. ];
  291. ComponentHelper::processBlogPostNewMailUser($HTTPPost, $data);
  292. $sonetPermsListNew = $HTTPPost['SONET_PERMS'];
  293. $currentAdmin = \CSocNetUser::isCurrentUserModuleAdmin();
  294. $canPublish = true;
  295. foreach($sonetPermsListNew as $type => $val)
  296. {
  297. foreach($val as $code)
  298. {
  299. if(in_array($type, [ 'U', 'SG', 'DR', 'CRMCONTACT' ]))
  300. {
  301. if (!in_array($code, $perms2update))
  302. {
  303. if ($type === 'SG')
  304. {
  305. $sonetGroupId = (int)str_replace('SG', '', $code);
  306. $canPublish = (
  307. $currentAdmin
  308. || \CSocNetFeaturesPerms::canPerformOperation($currentUserId, SONET_ENTITY_GROUP, $sonetGroupId, 'blog', 'write_post')
  309. || \CSocNetFeaturesPerms::canPerformOperation($currentUserId, SONET_ENTITY_GROUP, $sonetGroupId, 'blog', 'moderate_post')
  310. || \CSocNetFeaturesPerms::canPerformOperation($currentUserId, SONET_ENTITY_GROUP, $sonetGroupId, 'blog', 'full_post')
  311. );
  312. if (!$canPublish)
  313. {
  314. break;
  315. }
  316. }
  317. $perms2update[] = $code;
  318. $newRightsList[] = $code;
  319. }
  320. }
  321. elseif ($type === 'UA')
  322. {
  323. if (!in_array('UA', $perms2update, true))
  324. {
  325. $perms2update[] = 'UA';
  326. $newRightsList[] = 'UA';
  327. }
  328. }
  329. }
  330. if (!$canPublish)
  331. {
  332. break;
  333. }
  334. }
  335. if (
  336. !empty($newRightsList)
  337. && $canPublish
  338. )
  339. {
  340. ComponentHelper::processBlogPostShare(
  341. [
  342. 'POST_ID' => $postId,
  343. 'BLOG_ID' => $postFields['BLOG_ID'],
  344. 'SITE_ID' => SITE_ID,
  345. 'SONET_RIGHTS' => $perms2update,
  346. 'NEW_RIGHTS' => $newRightsList,
  347. 'USER_ID' => $currentUserId
  348. ],
  349. [
  350. 'MENTION' => 'N',
  351. 'LIVE' => 'Y',
  352. 'CAN_USER_COMMENT' => (!$readOnly ? 'Y' : 'N'),
  353. 'PATH_TO_USER' => $pathToUser,
  354. 'PATH_TO_POST' => $pathToPost,
  355. ]
  356. );
  357. }
  358. elseif (!$canPublish)
  359. {
  360. $this->addError(new Error(Loc::getMessage('SONET_CONTROLLER_LIVEFEED_BLOGPOST_SHARE_PREMODERATION'), 'SONET_CONTROLLER_LIVEFEED_BLOGPOST_SHARE_PREMODERATION'));
  361. return null;
  362. }
  363. }
  364. public function addAction(array $params = []): ?array
  365. {
  366. global $APPLICATION;
  367. $warnings = [];
  368. try
  369. {
  370. $postId = Helper::addBlogPost($params, $this->getScope(), $resultFields);
  371. if ($postId <= 0)
  372. {
  373. if (
  374. is_array($resultFields)
  375. && !empty($resultFields['ERROR_MESSAGE_PUBLIC'])
  376. )
  377. {
  378. $this->addError(new Error($resultFields['ERROR_MESSAGE_PUBLIC'], 0, [
  379. 'public' => 'Y'
  380. ]));
  381. return null;
  382. }
  383. $e = $APPLICATION->getException();
  384. throw new \Exception($e ? $e->getString() : 'Cannot add blog post');
  385. }
  386. if (
  387. is_array($resultFields)
  388. && !empty($resultFields['WARNING_MESSAGE_PUBLIC'])
  389. )
  390. {
  391. $warnings[] = $resultFields['WARNING_MESSAGE_PUBLIC'];
  392. }
  393. }
  394. catch (\Exception $e)
  395. {
  396. $this->addError(new Error($e->getMessage(), $e->getCode()));
  397. return null;
  398. }
  399. return [
  400. 'id' => $postId,
  401. 'warnings' => $warnings
  402. ];
  403. }
  404. public function updateAction($id = 0, array $params = []): ?array
  405. {
  406. global $APPLICATION;
  407. try
  408. {
  409. $params['POST_ID'] = $id;
  410. $postId = Helper::updateBlogPost($params, $this->getScope(), $resultFields);
  411. if ($postId <= 0)
  412. {
  413. if (
  414. is_array($resultFields)
  415. && !empty($resultFields['ERROR_MESSAGE_PUBLIC'])
  416. )
  417. {
  418. $this->addError(new Error($resultFields['ERROR_MESSAGE_PUBLIC'], 0, [
  419. 'public' => 'Y'
  420. ]));
  421. return null;
  422. }
  423. $e = $APPLICATION->getException();
  424. throw new \Exception($e ? $e->getString() : 'Cannot update blog post');
  425. }
  426. }
  427. catch (\Exception $e)
  428. {
  429. $this->addError(new Error($e->getMessage(), $e->getCode()));
  430. return null;
  431. }
  432. return [
  433. 'id' => $postId
  434. ];
  435. }
  436. public function getBlogPostMobileFullDataAction(array $params = []): ?array
  437. {
  438. if (!Loader::includeModule('mobile'))
  439. {
  440. $this->addError(new Error('Mobile module not installed', 'SONET_CONTROLLER_LIVEFEED_MOBILE_MODULE_NOT_INSTALLED'));
  441. return null;
  442. }
  443. return \Bitrix\Mobile\Livefeed\Helper::getBlogPostFullData($params);
  444. }
  445. public function deleteAction($id = 0): ?bool
  446. {
  447. try
  448. {
  449. $result = Helper::deleteBlogPost([
  450. 'POST_ID' => (int)$id,
  451. ]);
  452. }
  453. catch (\Exception $e)
  454. {
  455. $this->addError(new Error($e->getMessage(), $e->getCode()));
  456. return null;
  457. }
  458. return $result;
  459. }
  460. }