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

/modules/lists/lib/livefeed.php

https://gitlab.com/alexprowars/bitrix
PHP | 903 lines | 788 code | 99 blank | 16 comment | 102 complexity | 8b8f7f926dd9589eedfd7588c3f195db MD5 | raw file
  1. <?php
  2. use Bitrix\Main\Loader;
  3. use Bitrix\Main\Localization\Loc;
  4. Loc::loadMessages(__FILE__);
  5. class CListsLiveFeed
  6. {
  7. public static function setMessageLiveFeed($users, $elementId, $workflowId, $flagCompleteProcess)
  8. {
  9. $elementId = intval($elementId);
  10. $elementObject = CIBlockElement::getList(
  11. array(),
  12. array('ID' => $elementId),
  13. false,
  14. false,
  15. array('ID', 'CREATED_BY', 'IBLOCK_NAME', 'NAME', 'IBLOCK_ID', 'LANG_DIR', 'IBLOCK_CODE')
  16. );
  17. $element = $elementObject->fetch();
  18. if(!CLists::getLiveFeed($element["IBLOCK_ID"]))
  19. return false;
  20. $listSystemIblockCode = array(
  21. 'bitrix_holiday',
  22. 'bitrix_invoice',
  23. 'bitrix_trip',
  24. 'bitrix_cash',
  25. 'bitrix_incoming_doc',
  26. 'bitrix_outgoing_doc'
  27. );
  28. $params = serialize(array("ELEMENT_NAME" => $element['NAME']));
  29. if(in_array($element['IBLOCK_CODE'], $listSystemIblockCode))
  30. {
  31. $element['NAME'] = preg_replace_callback(
  32. '#^[^\[\]]+?\[(\d+)\]#i',
  33. function ($matches)
  34. {
  35. $userId = $matches[1];
  36. $db = CUser::GetByID($userId);
  37. if ($ar = $db->GetNext())
  38. {
  39. $ix = randString(5);
  40. return '<a class="feed-post-user-name" href="/company/personal/user/'.$userId.'/"
  41. bx-post-author-id="'.$userId.'" bx-post-author-gender="'.$ar['PERSONAL_GENDER'].'" bx-tooltip-user-id="'.$userId.'">'.CUser::FormatName(CSite::GetNameFormat(false), $ar, true, false).'</a>';
  42. }
  43. return $matches[0];
  44. },
  45. htmlspecialcharsbx($element['NAME'])
  46. );
  47. }
  48. $path = rtrim($element['LANG_DIR'], '/');
  49. $urlElement = $path.COption::GetOptionString('lists', 'livefeed_url').'?livefeed=y&list_id='.$element["IBLOCK_ID"].'&element_id='.$elementId;
  50. $createdBy = $element['CREATED_BY'];
  51. if(!Loader::includeModule('socialnetwork') || $createdBy <= 0)
  52. return false;
  53. $sourceId = CBPStateService::getWorkflowIntegerId($workflowId);
  54. $logId = 0;
  55. $userObject = CUser::getByID($createdBy);
  56. $siteId = array();
  57. $siteObject = CSite::getList($by="sort", $order="desc", array("ACTIVE" => "Y"));
  58. while ($site = $siteObject->fetch())
  59. $siteId[] = $site['LID'];
  60. if ($userObject->fetch())
  61. {
  62. global $DB;
  63. $soFields = Array(
  64. 'ENTITY_TYPE' => SONET_LISTS_NEW_POST_ENTITY,
  65. 'EVENT_ID' => 'lists_new_element',
  66. 'ENTITY_ID' => 1,
  67. '=LOG_UPDATE' => $DB->currentTimeFunction(),
  68. 'SOURCE_ID' => $sourceId,
  69. 'USER_ID' => $createdBy,
  70. 'MODULE_ID' => 'lists',
  71. 'TITLE_TEMPLATE' => $urlElement,
  72. 'TITLE' => $element['IBLOCK_NAME'],
  73. 'PARAMS' => $params,
  74. 'MESSAGE' => $workflowId,
  75. 'CALLBACK_FUNC' => false,
  76. 'SITE_ID' => $siteId,
  77. 'ENABLE_COMMENTS' => 'Y',
  78. 'RATING_TYPE_ID' => 'LISTS_NEW_ELEMENT',
  79. 'RATING_ENTITY_ID' => $sourceId,
  80. 'URL' => '#SITE_DIR#'.COption::GetOptionString('socialnetwork', 'user_page', false, SITE_ID).'log/'
  81. );
  82. $logObject = CSocNetLog::getList(array(), array(
  83. 'ENTITY_TYPE' => $soFields['ENTITY_TYPE'],
  84. 'ENTITY_ID' => $soFields['ENTITY_ID'],
  85. 'EVENT_ID' => $soFields['EVENT_ID'],
  86. 'SOURCE_ID' => $soFields['SOURCE_ID'],
  87. ));
  88. $iblockPicture = CIBlock::getArrayByID($element['IBLOCK_ID'], 'PICTURE');
  89. $imageFile = CFile::getFileArray($iblockPicture);
  90. if($imageFile !== false)
  91. {
  92. $imageFile = CFile::ResizeImageGet(
  93. $imageFile,
  94. array("width" => 36, "height" => 30),
  95. BX_RESIZE_IMAGE_PROPORTIONAL,
  96. false
  97. );
  98. }
  99. if(empty($imageFile['src']))
  100. $imageFile['src'] = '/bitrix/images/lists/default.png';
  101. $soFields['TEXT_MESSAGE'] = '
  102. <span class="bp-title-desc">
  103. <span class="bp-title-desc-icon">
  104. <img src="'.$imageFile['src'].'" width="36" height="30" border="0" />
  105. </span>
  106. '.in_array($element['IBLOCK_CODE'], $listSystemIblockCode) ? $element['NAME'] : htmlspecialcharsbx($element['NAME']).'
  107. </span>
  108. ';
  109. if($log = $logObject->fetch())
  110. {
  111. if (intval($log['ID']) > 0)
  112. {
  113. if(empty($users))
  114. {
  115. CSocNetLog::update($log['ID'], $soFields);
  116. }
  117. else
  118. {
  119. $activeUsers = CBPTaskService::getWorkflowParticipants($workflowId);
  120. $rights = self::getRights($activeUsers, $log['ID'], $createdBy, 'post');
  121. $usersRight = self::getUserIdForRight($rights);
  122. self::setSocnetFollow($usersRight, $log['ID'], 'Y', true);
  123. /* Recipients tasks bp */
  124. CSocNetLog::update($log['ID'], $soFields);
  125. /* Increment the counter for participants */
  126. CSocNetLogRights::deleteByLogID($log['ID']);
  127. $rightsCounter = self::getRights($users, $log['ID'], $createdBy, 'counter');
  128. CSocNetLogRights::add($log['ID'], $rightsCounter, false, false);
  129. CSocNetLog::counterIncrement($log['ID'], $soFields['EVENT_ID'], false, 'L', false);
  130. /* Return previous state rights */
  131. CSocNetLogRights::deleteByLogID($log['ID']);
  132. CSocNetLogRights::add($log['ID'], $rights, false, false);
  133. self::setSocnetFollow($users, $log['ID'], 'Y');
  134. self::setSocnetFollow($users, $log['ID'], 'N');
  135. }
  136. /* Completion of the process for the author */
  137. if ($flagCompleteProcess)
  138. {
  139. $activeUsers = CBPTaskService::getWorkflowParticipants($workflowId);
  140. $rights = self::getRights($activeUsers, $log['ID'], $createdBy, 'post');
  141. $usersRight = self::getUserIdForRight($rights);
  142. /* Increment the counter for author */
  143. $users[] = $createdBy;
  144. CSocNetLogRights::deleteByLogID($log['ID']);
  145. $rightsCounter = self::getRights($users, $log['ID'], $createdBy, 'counter');
  146. CSocNetLogRights::add($log['ID'], $rightsCounter, false, false);
  147. CSocNetLog::counterIncrement($log['ID'], $soFields['EVENT_ID'], false, 'L', false);
  148. /* Return previous state rights */
  149. CSocNetLogRights::deleteByLogID($log['ID']);
  150. CSocNetLogRights::add($log['ID'], $rights, false, false);
  151. self::setSocnetFollow($users, $log['ID'], 'Y');
  152. self::setSocnetFollow($usersRight, $log['ID'], 'N');
  153. }
  154. }
  155. }
  156. else
  157. {
  158. $activeUsers = CBPTaskService::getWorkflowParticipants($workflowId);
  159. $soFields['=LOG_DATE'] = $DB->currentTimeFunction();
  160. $logId = CSocNetLog::add($soFields, false);
  161. if (intval($logId) > 0)
  162. {
  163. $rights = self::getRights($activeUsers, $logId, $createdBy, 'post');
  164. CSocNetLogRights::add($logId, $rights, false, false);
  165. $usersRight = self::getUserIdForRight($rights);
  166. self::setSocnetFollow($usersRight, $logId, 'N');
  167. }
  168. CSocNetLog::counterIncrement($logId, $soFields['EVENT_ID'], false, 'L', false);
  169. }
  170. }
  171. return $logId;
  172. }
  173. public static function onFillSocNetAllowedSubscribeEntityTypes(&$socnetEntityTypes)
  174. {
  175. $socnetEntityTypes[] = SONET_LISTS_NEW_POST_ENTITY;
  176. global $arSocNetAllowedSubscribeEntityTypesDesc;
  177. $arSocNetAllowedSubscribeEntityTypesDesc[SONET_LISTS_NEW_POST_ENTITY] = array(
  178. 'TITLE_LIST' => '',
  179. 'TITLE_ENTITY' => ''
  180. );
  181. }
  182. public static function onFillSocNetLogEvents(&$socnetLogEvents)
  183. {
  184. $socnetLogEvents['lists_new_element'] = array(
  185. 'ENTITIES' => array(
  186. SONET_LISTS_NEW_POST_ENTITY => array(),
  187. ),
  188. 'FORUM_COMMENT_ENTITY' => 'WF',
  189. 'CLASS_FORMAT' => 'CListsLiveFeed',
  190. 'METHOD_FORMAT' => 'formatListsElement',
  191. 'HAS_CB' => 'Y',
  192. 'FULL_SET' => array('lists_new_element', 'lists_new_element_comment'),
  193. 'COMMENT_EVENT' => array(
  194. 'MODULE_ID' => 'lists_new_element',
  195. 'EVENT_ID' => 'lists_new_element_comment',
  196. 'OPERATION' => 'view',
  197. 'OPERATION_ADD' => 'log_rights',
  198. 'ADD_CALLBACK' => array('CListsLiveFeed', 'addCommentLists'),
  199. 'UPDATE_CALLBACK' => array('CSocNetLogTools', 'UpdateComment_Forum'),
  200. 'DELETE_CALLBACK' => array('CSocNetLogTools', 'DeleteComment_Forum'),
  201. 'CLASS_FORMAT' => 'CSocNetLogTools',
  202. 'METHOD_FORMAT' => 'FormatComment_Forum',
  203. "RATING_TYPE_ID" => "FORUM_POST"
  204. )
  205. );
  206. }
  207. public static function formatListsElement($fields, $params, $mail = false)
  208. {
  209. global $CACHE_MANAGER;
  210. $element = array(
  211. 'EVENT' => $fields,
  212. 'CREATED_BY' => array(),
  213. 'ENTITY' => array(),
  214. 'EVENT_FORMATTED' => array(),
  215. );
  216. $userObject = CUser::getByID($fields['ENTITY_ID']);
  217. $user = $userObject->fetch();
  218. if ($user)
  219. {
  220. if(!$mail)
  221. {
  222. global $APPLICATION;
  223. $rights = array();
  224. $rightsQuery = CSocNetLogRights::getList(array(), array('LOG_ID' => $fields['ID']));
  225. while ($right = $rightsQuery->fetch())
  226. {
  227. $rights[] = $right['GROUP_CODE'];
  228. }
  229. if(defined('BX_COMP_MANAGED_CACHE'))
  230. {
  231. $CACHE_MANAGER->registerTag('LISTS_ELEMENT_LIVE_FEED');
  232. }
  233. $componentResult = $APPLICATION->includeComponent(
  234. 'bitrix:bizproc.workflow.livefeed',
  235. '',
  236. Array(
  237. 'WORKFLOW_ID' => $fields['MESSAGE'],
  238. ),
  239. null,
  240. array('HIDE_ICONS' => 'Y')
  241. );
  242. $siteDir = rtrim(SITE_DIR, '/');
  243. $url = CSocNetLogTools::formatEvent_GetURL($fields, true);
  244. $url = str_replace('#SITE_DIR#', $siteDir, $url);
  245. $url .= ''.$fields['ID'].'/';
  246. $element = array(
  247. 'EVENT' => $fields,
  248. 'EVENT_FORMATTED' => array(
  249. 'TITLE_24' => '<a href="'.$fields['TITLE_TEMPLATE'].'" class="bx-lists-live-feed-title-link">'.$fields['TITLE'].'</a>',
  250. 'MESSAGE' => $fields['TEXT_MESSAGE'].$componentResult['MESSAGE'],
  251. 'IS_IMPORTANT' => false,
  252. 'STYLE' => 'new-employee',
  253. 'AVATAR_STYLE' => 'avatar-info',
  254. 'DESTINATION' => CSocNetLogTools::formatDestinationFromRights($rights, array_merge($params, array('CREATED_BY' => $fields['USER_ID']))),
  255. 'URL' => $url
  256. ),
  257. 'CREATED_BY' => CSocNetLogTools::formatEvent_GetCreatedBy($fields, $params, $mail),
  258. 'AVATAR_SRC' => CSocNetLog::formatEvent_CreateAvatar($fields, $params),
  259. 'CACHED_JS_PATH' => $componentResult['CACHED_JS_PATH'],
  260. 'CACHED_CSS_PATH' => $componentResult['CACHED_CSS_PATH']
  261. );
  262. if ($params['MOBILE'] == 'Y')
  263. {
  264. $element['EVENT_FORMATTED']['TITLE_24'] = Loc::getMessage('LISTS_LF_MOBILE_DESTINATION');
  265. $element['EVENT_FORMATTED']['TITLE_24_2'] = $fields['TITLE'];
  266. unset($element['CACHED_CSS_PATH']);
  267. }
  268. if (CModule::IncludeModule('bizproc'))
  269. {
  270. $workflowId = \CBPStateService::getWorkflowByIntegerId($element['EVENT']['SOURCE_ID']);
  271. }
  272. if ($workflowId)
  273. {
  274. $element['EVENT']['SOURCE_ID'] = $workflowId;
  275. }
  276. }
  277. return $element;
  278. }
  279. }
  280. public static function addCommentLists($fields)
  281. {
  282. global $DB, $USER_FIELD_MANAGER;
  283. if (!CModule::IncludeModule('forum') || !CModule::IncludeModule('bizproc'))
  284. return false;
  285. $ufFileId = array();
  286. $ufDocId = array();
  287. $fieldsMessage = array();
  288. $messageId = array();
  289. $error = array();
  290. $note = array();
  291. $sonetLogQuery = CSocNetLog::GetList(
  292. array(),
  293. array('ID' => $fields['LOG_ID']),
  294. false,
  295. false,
  296. array('ID', 'SOURCE_ID', 'SITE_ID', 'MESSAGE', 'USER_ID')
  297. );
  298. if($sonetLog = $sonetLogQuery->fetch())
  299. {
  300. $users = CBPTaskService::getWorkflowParticipants($sonetLog['MESSAGE'], CBPTaskUserStatus::Waiting);
  301. if(preg_match_all("/(?<=\[USER=)(?P<id>[0-9]+)(?=\])/", $fields['TEXT_MESSAGE'], $matches))
  302. $users = array_unique(array_merge($users, $matches['id']));
  303. $users[] = $sonetLog['USER_ID'];
  304. self::setSocnetFollow($users, $sonetLog['ID'], 'Y', false, true);
  305. $forumId = CBPHelper::getForumId();
  306. if($forumId)
  307. {
  308. $topicQuery = CForumTopic::GetList(array(), array('FORUM_ID' => $forumId, 'XML_ID' => 'WF_'.$sonetLog['MESSAGE']));
  309. if ($topicQuery && ($topic = $topicQuery->fetch()))
  310. {
  311. $topicId = $topic['ID'];
  312. }
  313. else
  314. {
  315. $dataTopic = array(
  316. 'AUTHOR_ID' => 0,
  317. 'TITLE' => 'WF_'.$sonetLog['MESSAGE'],
  318. 'TAGS' => '',
  319. 'MESSAGE' => 'WF_'.$sonetLog['MESSAGE'],
  320. 'XML_ID' => 'WF_'.$sonetLog['MESSAGE']
  321. );
  322. $userStart = array(
  323. "ID" => $dataTopic["AUTHOR_ID"],
  324. "NAME" => $GLOBALS["FORUM_STATUS_NAME"]["guest"]
  325. );
  326. $DB->StartTransaction();
  327. $topicFields = Array(
  328. "TITLE" => $dataTopic["TITLE"],
  329. "TAGS" => $dataTopic["TAGS"],
  330. "FORUM_ID" => $forumId,
  331. "USER_START_ID" => $userStart["ID"],
  332. "USER_START_NAME" => $userStart["NAME"],
  333. "LAST_POSTER_NAME" => $userStart["NAME"],
  334. "XML_ID" => $dataTopic["XML_ID"],
  335. "APPROVED" => "Y",
  336. "PERMISSION_EXTERNAL" =>'Q',
  337. "PERMISSION" => 'Y',
  338. );
  339. $topicId = CForumTopic::Add($topicFields);
  340. if (intval($topicId) > 0)
  341. {
  342. $dataTopic['MESSAGE'] = strip_tags($dataTopic['MESSAGE']);
  343. $dataFields = Array(
  344. "POST_MESSAGE" => $dataTopic['MESSAGE'],
  345. "AUTHOR_ID" => $userStart["ID"],
  346. "AUTHOR_NAME" => $userStart["NAME"],
  347. "FORUM_ID" => $forumId,
  348. "TOPIC_ID" => $topicId,
  349. "APPROVED" => "Y",
  350. "NEW_TOPIC" => "Y",
  351. "PARAM1" => "WF",
  352. "PARAM2" => 0,
  353. "PERMISSION_EXTERNAL" => 'Q',
  354. "PERMISSION" => 'Y',
  355. );
  356. $startMessageId = CForumMessage::Add($dataFields, false, array("SKIP_INDEXING" => "Y", "SKIP_STATISTIC" => "N"));
  357. if (intVal($startMessageId) <= 0)
  358. {
  359. CForumTopic::Delete($topicId);
  360. $topicId = 0;
  361. }
  362. }
  363. if (intval($topicId) <= 0)
  364. {
  365. $DB->Rollback();
  366. }
  367. else
  368. {
  369. $DB->Commit();
  370. }
  371. }
  372. if ($topicId)
  373. {
  374. $fieldsMessage = array(
  375. 'POST_MESSAGE' => $fields['TEXT_MESSAGE'],
  376. 'USE_SMILES' => 'Y',
  377. 'PERMISSION_EXTERNAL' => 'Q',
  378. 'PERMISSION' => 'Y',
  379. 'APPROVED' => 'Y'
  380. );
  381. $tmp = false;
  382. $USER_FIELD_MANAGER->editFormAddFields('SONET_COMMENT', $tmp);
  383. if (is_array($tmp))
  384. {
  385. if (array_key_exists('UF_SONET_COM_DOC', $tmp))
  386. {
  387. $GLOBALS['UF_FORUM_MESSAGE_DOC'] = $tmp['UF_SONET_COM_DOC'];
  388. }
  389. elseif (array_key_exists('UF_SONET_COM_FILE', $tmp))
  390. {
  391. $fieldsMessage['FILES'] = array();
  392. foreach($tmp['UF_SONET_COM_FILE'] as $fileId)
  393. {
  394. $fieldsMessage['FILES'][] = array('FILE_ID' => $fileId);
  395. }
  396. }
  397. if (array_key_exists("UF_SONET_COM_URL_PRV", $tmp))
  398. {
  399. $GLOBALS["UF_FORUM_MES_URL_PRV"] = $tmp["UF_SONET_COM_URL_PRV"];
  400. }
  401. }
  402. $messageId = ForumAddMessage("REPLY", $forumId, $topicId, 0, $fieldsMessage, $error, $note);
  403. if ($messageId > 0)
  404. {
  405. $addedMessageFilesQuery = CForumFiles::getList(array('ID' => 'ASC'), array('MESSAGE_ID' => $messageId));
  406. while ($addedMessageFiles = $addedMessageFilesQuery->fetch())
  407. {
  408. $ufFileId[] = $addedMessageFiles['FILE_ID'];
  409. }
  410. $ufDocId = $USER_FIELD_MANAGER->getUserFieldValue('FORUM_MESSAGE', 'UF_FORUM_MESSAGE_DOC', $messageId, LANGUAGE_ID);
  411. $ufUrlPreview = $USER_FIELD_MANAGER->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MES_URL_PRV", $messageId, LANGUAGE_ID);
  412. }
  413. }
  414. }
  415. }
  416. if (!$messageId)
  417. {
  418. $error = Loc::getMessage('LISTS_LF_ADD_COMMENT_SOURCE_ERROR');
  419. }
  420. return array(
  421. 'SOURCE_ID' => $messageId,
  422. 'MESSAGE' => ($fieldsMessage ? $fieldsMessage['POST_MESSAGE'] : false),
  423. 'RATING_TYPE_ID' => 'FORUM_POST',
  424. 'RATING_ENTITY_ID' => $messageId,
  425. 'ERROR' => $error,
  426. 'NOTES' => $note,
  427. 'UF' => array(
  428. 'FILE' => $ufFileId,
  429. 'DOC' => $ufDocId,
  430. 'URL_PREVIEW' => $ufUrlPreview
  431. )
  432. );
  433. }
  434. protected static function getRights($users, $logId, $createdBy, $method)
  435. {
  436. $rights = array();
  437. $rights[] = 'SA'; //socnet admin
  438. if(!empty($users))
  439. {
  440. if($method == 'post')
  441. $users[] = $createdBy;
  442. foreach($users as $userId)
  443. {
  444. $rights[] = 'U'.$userId;
  445. }
  446. }
  447. $rights = array_unique($rights);
  448. return $rights;
  449. }
  450. protected static function getUserIdForRight($rights)
  451. {
  452. $users = array();
  453. foreach($rights as $user)
  454. {
  455. if($user != 'SA')
  456. {
  457. $users[] = substr($user, 1);
  458. }
  459. }
  460. return $users;
  461. }
  462. protected static function setSocnetFollow($users = array(), $logId, $type, $manualMode = false, $addingComment = false)
  463. {
  464. if($manualMode)
  465. {
  466. foreach($users as $userId)
  467. {
  468. $logFollowObject = CSocNetLogFollow::getList(
  469. array('USER_ID' => $userId, 'REF_ID' => $logId), array('BY_WF', 'TYPE'));
  470. $logFollow = $logFollowObject->fetch();
  471. if(!empty($logFollow) && $logFollow['TYPE'] == 'Y' && !$logFollow['BY_WF'])
  472. {
  473. CSocNetLogFollow::delete($userId, 'L'.$logId, false);
  474. CSocNetLogFollow::set($userId, 'L'.$logId, $type,
  475. ConvertTimeStamp(time() + CTimeZone::GetOffset(), "FULL", SITE_ID), SITE_ID, true);
  476. if (
  477. $type == 'Y'
  478. && method_exists('\Bitrix\Socialnetwork\ComponentHelper','userLogSubscribe')
  479. )
  480. {
  481. \Bitrix\Socialnetwork\ComponentHelper::userLogSubscribe(array(
  482. 'logId' => $logId,
  483. 'userId' => $userId,
  484. 'typeList' => array(
  485. 'COUNTER_COMMENT_PUSH'
  486. )
  487. ));
  488. }
  489. }
  490. }
  491. }
  492. else
  493. {
  494. if($type == 'Y')
  495. {
  496. foreach($users as $userId)
  497. {
  498. $logFollowObject = CSocNetLogFollow::getList(
  499. array('USER_ID' => $userId, 'REF_ID' => $logId), array('BY_WF'));
  500. $logFollow = $logFollowObject->fetch();
  501. if(
  502. empty($logFollow)
  503. || ($logFollow['BY_WF'] == 'Y' || $addingComment)
  504. )
  505. {
  506. CSocNetLogFollow::delete($userId, 'L'.$logId, false);
  507. if (method_exists('\Bitrix\Socialnetwork\ComponentHelper','userLogSubscribe'))
  508. {
  509. \Bitrix\Socialnetwork\ComponentHelper::userLogSubscribe(array(
  510. 'logId' => $logId,
  511. 'userId' => $userId,
  512. 'typeList' => array(
  513. 'FOLLOW',
  514. 'COUNTER_COMMENT_PUSH'
  515. ),
  516. 'followDate' => 'CURRENT',
  517. 'followByWF' => true
  518. ));
  519. }
  520. else
  521. {
  522. CSocNetLogFollow::set($userId, 'L'.$logId, 'Y',
  523. ConvertTimeStamp(time() + CTimeZone::GetOffset(), "FULL", SITE_ID), SITE_ID, true);
  524. }
  525. }
  526. }
  527. }
  528. else
  529. {
  530. foreach($users as $userId)
  531. {
  532. $logFollowObject = CSocNetLogFollow::getList(
  533. array('USER_ID' => $userId, 'REF_ID' => $logId), array('BY_WF'));
  534. $logFollow = $logFollowObject->fetch();
  535. if(
  536. empty($logFollow)
  537. || $logFollow['BY_WF'] == 'Y'
  538. )
  539. {
  540. CSocNetLogFollow::set($userId, 'L'.$logId, 'N', false, SITE_ID, true);
  541. }
  542. }
  543. }
  544. }
  545. }
  546. protected static function getSiteName()
  547. {
  548. return COption::getOptionString('main', 'site_name', '');
  549. }
  550. function BeforeIndexSocNet($bxSocNetSearch, $fields)
  551. {
  552. static $bizprocForumId = false;
  553. if (!$bizprocForumId)
  554. {
  555. $bizprocForumId = intval(COption::GetOptionString('bizproc', 'forum_id'));
  556. }
  557. if(
  558. $fields['ENTITY_TYPE_ID'] == 'FORUM_POST'
  559. && intval($fields['PARAM1']) == $bizprocForumId
  560. && !empty($fields['PARAM2'])
  561. && !empty($bxSocNetSearch->_params["PATH_TO_WORKFLOW"])
  562. && CModule::IncludeModule("forum")
  563. && CModule::IncludeModule("bizproc")
  564. )
  565. {
  566. $topic = CForumTopic::GetByID($fields['PARAM2']);
  567. if (
  568. !empty($topic)
  569. && is_array($topic)
  570. && !empty($topic["XML_ID"])
  571. )
  572. {
  573. if (preg_match('/^WF_([0-9a-f\.]+)/', $topic["XML_ID"], $match))
  574. {
  575. $workflowId = $match[1];
  576. $state = CBPStateService::GetStateDocumentId($workflowId);
  577. if (
  578. $state[0] == 'lists'
  579. && $state[1] == 'BizprocDocument'
  580. && CModule::IncludeModule('iblock')
  581. && (intval($state[2]) > 0)
  582. )
  583. {
  584. $iblockElementQuery = CIBlockElement::GetList(
  585. array(),
  586. array(
  587. "ID" => intval($state[2])
  588. ),
  589. false,
  590. false,
  591. array("ID", "IBLOCK_ID")
  592. );
  593. if ($iblockElement = $iblockElementQuery->Fetch())
  594. {
  595. $listId = $iblockElement["IBLOCK_ID"];
  596. $fields["URL"] = $bxSocNetSearch->Url(
  597. str_replace(
  598. array("#list_id#", "#workflow_id#"),
  599. array($listId, urlencode($workflowId)),
  600. $bxSocNetSearch->_params["PATH_TO_WORKFLOW"]
  601. ),
  602. array(
  603. "MID" => $fields["ENTITY_ID"]
  604. ),
  605. "message".$fields["ENTITY_ID"]
  606. );
  607. if (
  608. !empty($fields["LID"])
  609. && is_array($fields["LID"])
  610. )
  611. {
  612. foreach ($fields["LID"] as $siteId => $url)
  613. {
  614. $fields["LID"][$siteId] = $fields["URL"];
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }
  621. }
  622. return $fields;
  623. }
  624. /**
  625. * Called from LiveFeed
  626. * @param array $comment
  627. */
  628. public static function OnAfterSonetLogEntryAddComment($comment)
  629. {
  630. if ($comment["EVENT_ID"] != "lists_new_element_comment")
  631. {
  632. return;
  633. }
  634. $logQuery = CSocNetLog::getList(
  635. array(),
  636. array(
  637. "ID" => $comment["LOG_ID"],
  638. "EVENT_ID" => "lists_new_element"
  639. ),
  640. false,
  641. false,
  642. array("SOURCE_ID", "URL", "TITLE", "USER_ID", "PARAMS")
  643. );
  644. if (($log = $logQuery->fetch()) && (intval($log["SOURCE_ID"]) > 0))
  645. {
  646. $params = unserialize($log["PARAMS"]);
  647. $title = $log["TITLE"]." - ".$params["ELEMENT_NAME"];
  648. CListsLiveFeed::notifyComment(
  649. array(
  650. "LOG_ID" => $comment["LOG_ID"],
  651. "MESSAGE_ID" => $comment["SOURCE_ID"],
  652. "TO_USER_ID" => $log["USER_ID"],
  653. "FROM_USER_ID" => $comment["USER_ID"],
  654. "URL" => $log["URL"],
  655. "TITLE" => $title
  656. )
  657. );
  658. }
  659. }
  660. /**
  661. * Called from popup
  662. * @param string $entityType
  663. * @param int $entityId
  664. * $param array $comment
  665. */
  666. public static function OnForumCommentIMNotify($entityType, $entityId, $comment)
  667. {
  668. if ($entityType != "WF")
  669. return;
  670. $logQuery = CSocNetLog::getList(
  671. array(),
  672. array(
  673. "ID" => $comment["LOG_ID"],
  674. "EVENT_ID" => "lists_new_element"
  675. ),
  676. false,
  677. false,
  678. array("ID", "SOURCE_ID", "URL", "TITLE", "USER_ID", "PARAMS")
  679. );
  680. if (($log = $logQuery->fetch()) && (intval($log["SOURCE_ID"]) > 0))
  681. {
  682. $params = unserialize($log["PARAMS"]);
  683. $title = $log["TITLE"]." - ".$params["ELEMENT_NAME"];
  684. CListsLiveFeed::notifyComment(
  685. array(
  686. "LOG_ID" => $log["ID"],
  687. "MESSAGE_ID" => $comment["MESSAGE_ID"],
  688. "TO_USER_ID" => $log["USER_ID"],
  689. "FROM_USER_ID" => $comment["USER_ID"],
  690. "URL" => $log["URL"],
  691. "TITLE" => $title
  692. )
  693. );
  694. }
  695. }
  696. public static function NotifyComment($comment)
  697. {
  698. if (!Loader::includeModule("im"))
  699. return;
  700. if($comment["TO_USER_ID"] == $comment["FROM_USER_ID"])
  701. return;
  702. $siteDir = rtrim(SITE_DIR, '/');
  703. $url = str_replace('#SITE_DIR#', $siteDir, $comment["URL"]);
  704. $url .= ''.$comment['LOG_ID'].'/';
  705. $messageAddComment = Loc::getMessage("LISTS_LF_COMMENT_MESSAGE_ADD",
  706. array("#PROCESS#" => '<a href="'.$url.'" class="bx-notifier-item-action">'.$comment["TITLE"].'</a>'));
  707. $userQuery = CUser::getList(
  708. $by = "id",
  709. $order = "asc",
  710. array("ID_EQUAL_EXACT" => intval($comment["FROM_USER_ID"])),
  711. array("FIELDS" => array("PERSONAL_GENDER"))
  712. );
  713. if ($user = $userQuery->fetch())
  714. {
  715. switch ($user["PERSONAL_GENDER"])
  716. {
  717. case "F":
  718. case "M":
  719. $messageAddComment = Loc::getMessage("LISTS_LF_COMMENT_MESSAGE_ADD" . '_' . $user["PERSONAL_GENDER"],
  720. array("#PROCESS#" => '<a href="'.$url.'" class="bx-notifier-item-action">'.$comment["TITLE"].'</a>'));
  721. break;
  722. default:
  723. break;
  724. }
  725. }
  726. $messageFields = array(
  727. "TO_USER_ID" => $comment["TO_USER_ID"],
  728. "FROM_USER_ID" => $comment["FROM_USER_ID"],
  729. "NOTIFY_TYPE" => IM_NOTIFY_FROM,
  730. "NOTIFY_MODULE" => "lists",
  731. "NOTIFY_TAG" => "SONET|EVENT|".$comment["LOG_ID"],
  732. "NOTIFY_SUB_TAG" => "FORUM|COMMENT|".$comment["MESSAGE_ID"]."|".$comment["TO_USER_ID"],
  733. "NOTIFY_EVENT" => "event_lists_comment_add",
  734. "NOTIFY_MESSAGE" => $messageAddComment
  735. );
  736. CIMNotify::Add($messageFields);
  737. }
  738. public static function OnSendMentionGetEntityFields($commentFields)
  739. {
  740. if (!in_array($commentFields["EVENT_ID"], array("lists_new_element_comment")))
  741. {
  742. return false;
  743. }
  744. if (!CModule::IncludeModule("socialnetwork"))
  745. {
  746. return true;
  747. }
  748. $dbLog = CSocNetLog::GetList(
  749. array(),
  750. array(
  751. "ID" => $commentFields["LOG_ID"],
  752. ),
  753. false,
  754. false,
  755. array("ID", "TITLE", "SOURCE_ID", "PARAMS")
  756. );
  757. if ($log = $dbLog->GetNext())
  758. {
  759. $genderSuffix = "";
  760. $dbUser = CUser::GetByID($commentFields["USER_ID"]);
  761. if($user = $dbUser->Fetch())
  762. {
  763. $genderSuffix = $user["PERSONAL_GENDER"];
  764. }
  765. $params = unserialize($log["~PARAMS"]);
  766. $title = $log["TITLE"]." - ".$params["ELEMENT_NAME"];
  767. $entityName = GetMessage("LISTS_LF_COMMENT_MENTION_TITLE", Array("#PROCESS#" => $title));
  768. $notifyMessage = GetMessage("LISTS_LF_COMMENT_MENTION" . (strlen($genderSuffix) > 0 ? "_" . $genderSuffix : ""), Array("#title#" => "<a href=\"#url#\" class=\"bx-notifier-item-action\">".$entityName."</a>"));
  769. $notifyMessageOut = GetMessage("LISTS_LF_COMMENT_MENTION" . (strlen($genderSuffix) > 0 ? "_" . $genderSuffix : ""), Array("#title#" => $entityName)) . " (" . "#server_name##url#)";
  770. $strPathToLogEntry = str_replace("#log_id#", $log["ID"], COption::GetOptionString("socialnetwork", "log_entry_page", "/company/personal/log/#log_id#/", SITE_ID));
  771. $strPathToLogEntryComment = $strPathToLogEntry . (strpos($strPathToLogEntry, "?") !== false ? "&" : "?") . "commentID=" . $commentFields["ID"] . "#com" . $commentFields["ID"];
  772. $return = array(
  773. "URL" => $strPathToLogEntryComment,
  774. "NOTIFY_MODULE" => "lists",
  775. "NOTIFY_TAG" => "LISTS|COMMENT_MENTION|".$commentFields["ID"],
  776. "NOTIFY_MESSAGE" => $notifyMessage,
  777. "NOTIFY_MESSAGE_OUT" => $notifyMessageOut
  778. );
  779. return $return;
  780. }
  781. else
  782. {
  783. return false;
  784. }
  785. }
  786. public static function OnSocNetGroupDelete($groupId)
  787. {
  788. $iblockIdList = array();
  789. $res = \CIBlock::getList(array(), array("SOCNET_GROUP_ID" => $groupId));
  790. while($iblock = $res->fetch())
  791. {
  792. $iblockIdList[] = $iblock["ID"];
  793. }
  794. if (empty($iblockIdList))
  795. {
  796. return true;
  797. }
  798. foreach($iblockIdList as $iblockId)
  799. {
  800. CIBlock::Delete($iblockId);
  801. }
  802. return true;
  803. }
  804. }