• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/integration/forum/task/comment.php
  • Класс: BitrixTasksIntegrationForumTaskComment
  • Вызов: Comment::onAfterUpdate
static function onAfterUpdate($entityType, $taskID, $arData)
{
	// 'TK' is our entity type
	if ($entityType !== 'TK')
	{
		return;
	}

	if (empty($arData["MESSAGE_ID"]))
	{
		return;
	}

	if ($arData['ACTION'] === 'EDIT')
	{
		(new ResultManager(User::getId()))->updateFromComment((int) $taskID, (int) $arData["MESSAGE_ID"]);
	}

	$arMessage = false;

	if (BitrixTasksIntegrationSocialnetwork::includeModule() && BitrixTasksIntegrationSocialNetwork::isEnabled())
	{
		$parser = new CTextParser();
		$parser->allow = array("HTML" => 'Y',"ANCHOR" => 'Y',"BIU" => 'Y',"IMG" => "Y","VIDEO" => "Y","LIST" => 'N',"QUOTE" => 'Y',"CODE" => 'Y',"FONT" => 'Y',"SMILES" => "N","UPLOAD" => 'N',"NL2BR" => 'N',"TABLE" => "Y");

		$oTask = CTaskItem::getInstance($taskID, User::getAdminId());

		try
		{
			$arTask = $oTask->getData();
		}
		catch (TasksException $e)
		{
			return;
		}

		$bCrmTask = (
			isset($arTask["UF_CRM_TASK"])
			&& (
				(
					is_array($arTask["UF_CRM_TASK"])
					&& (
						isset($arTask["UF_CRM_TASK"][0])
						&& $arTask["UF_CRM_TASK"][0] <> ''
					)
				)
				||
				(
					!is_array($arTask["UF_CRM_TASK"])
					&& $arTask["UF_CRM_TASK"] <> ''
				)
			)
		);

		switch ($arData["ACTION"])
		{
			case "DEL":
			case "HIDE":
				$dbLogComment = CSocNetLogComments::GetList(
					array("ID" => "DESC"),
					array(
						"EVENT_ID"	=> ($bCrmTask ? array('crm_activity_add_comment') : array('tasks_comment')),
						"SOURCE_ID" => intval($arData["MESSAGE_ID"])
					),
					false,
					false,
					array("ID")
				);
				while ($arLogComment = $dbLogComment->Fetch())
				{
					CSocNetLogComments::Delete($arLogComment["ID"]);
				}
				break;
			case "SHOW":
				$dbLogComment = CSocNetLogComments::GetList(
					array("ID" => "DESC"),
					array(
						"EVENT_ID"	=> ($bCrmTask ? array('crm_activity_add_comment') : array('tasks_comment')),
						"SOURCE_ID" => intval($arData["MESSAGE_ID"])
					),
					false,
					false,
					array("ID")
				);
				$arLogComment = $dbLogComment->Fetch();
				if (!$arLogComment)
				{
					$arMessage = CForumMessage::GetByID(intval($arData["MESSAGE_ID"]));
					if ($arMessage)
					{
						$arFilter = false;
						if (!$bCrmTask)
						{
							$arFilter = array(
								"EVENT_ID" => "tasks",
								"SOURCE_ID" => $taskID
							);
						}
						elseif (BitrixTasksIntegrationCRM::includeModule())
						{
							$dbCrmActivity = CCrmActivity::GetList(
								array(),
								array(
									'TYPE_ID' => CCrmActivityType::Task,
									'ASSOCIATED_ENTITY_ID' => $taskID,
									'CHECK_PERMISSIONS' => 'N'
								),
								false,
								false,
								array('ID')
							);

							if ($arCrmActivity = $dbCrmActivity->Fetch())
							{
								$arFilter = array(
									"EVENT_ID" => "crm_activity_add",
									"ENTITY_ID" => $arCrmActivity["ID"]
								);
							}
						}

						if ($arFilter)
						{
							$dbLog = CSocNetLog::GetList(
								array("ID" => "DESC"),
								$arFilter,
								false,
								false,
								array("ID", "ENTITY_TYPE", "ENTITY_ID")
							);
							if ($arLog = $dbLog->Fetch())
							{
								$log_id = $arLog["ID"];
								$entity_type = $arLog["ENTITY_TYPE"];
								$entity_id = $arLog["ENTITY_ID"];
							}
							else
							{
								$entity_type = ($arTask["GROUP_ID"] ? SONET_ENTITY_GROUP : SONET_ENTITY_USER);
								$entity_id = ($arTask["GROUP_ID"] ? $arTask["GROUP_ID"] : $arTask["CREATED_BY"]);

								$rsUser = CUser::GetByID($arTask["CREATED_BY"]);
								if ($arUser = $rsUser->Fetch())
								{
									$arSoFields = array(
										"ENTITY_TYPE" => $entity_type,
										"ENTITY_ID" => $entity_id,
										"EVENT_ID" => "tasks",
										"LOG_DATE" => $arTask["CREATED_DATE"],
										"TITLE_TEMPLATE" => "#TITLE#",
										"TITLE" => $arTask["TITLE"],
										"MESSAGE" => "",
										"TEXT_MESSAGE" => '', //$strMsgNewTask,
										"MODULE_ID" => "tasks",
										"CALLBACK_FUNC" => false,
										"SOURCE_ID" => $taskID,
										"ENABLE_COMMENTS" => "Y",
										"USER_ID" => $arTask["CREATED_BY"],
										"URL" => CTaskNotifications::getNotificationPath($arUser, $taskID),
										"PARAMS" => serialize(array("TYPE" => "create"))
									);
									$log_id = CSocNetLog::Add($arSoFields, false);
									if (intval($log_id) > 0)
									{
										$arRights = CTaskNotifications::__UserIDs2Rights(self::getTaskMembersByTaskId($taskID));
										if($arTask["GROUP_ID"])
											$arRights[] = "S".SONET_ENTITY_GROUP.$arTask["GROUP_ID"];
										CSocNetLogRights::Add($log_id, $arRights);
									}
								}
							}
						}

						if ($log_id > 0)
						{
							$sText = (COption::GetOptionString("forum", "FILTER", "Y") == "Y" ? $arMessage["POST_MESSAGE_FILTER"] : $arMessage["POST_MESSAGE"]);
							$strURL = $GLOBALS['APPLICATION']->GetCurPageParam("", array("IFRAME", "IFRAME_TYPE", "MID", "SEF_APPLICATION_CUR_PAGE_URL", BX_AJAX_PARAM_ID, "result"));
							$strURL = ForumAddPageParams(
								$strURL,
								array(
									"MID" => intval($arData["MESSAGE_ID"]),
									"result" => "reply"
								),
								false,
								false
							);

							$arFieldsForSocnet = array(
								"ENTITY_TYPE" => $entity_type,
								"ENTITY_ID" => $entity_id,
								"EVENT_ID" => ($bCrmTask ? 'crm_activity_add_comment' : 'tasks_comment'),
								"MESSAGE" => $sText,
								"TEXT_MESSAGE" => $parser->convert4mail($sText),
//									"URL" => str_replace("?IFRAME=Y", "", str_replace("&IFRAME=Y", "", str_replace("IFRAME=Y&", "", $strURL))),
								"MODULE_ID" => "tasks",
								"SOURCE_ID" => intval($arData["MESSAGE_ID"]),
								"LOG_ID" => $log_id,
								"RATING_TYPE_ID" => "FORUM_POST",
								"RATING_ENTITY_ID" => intval($arData["MESSAGE_ID"])
							);

							$arFieldsForSocnet["USER_ID"] = $arMessage["AUTHOR_ID"];
							$arFieldsForSocnet["=LOG_DATE"] = $GLOBALS["DB"]->currentTimeFunction();

							$ufFileID = array();
							$dbAddedMessageFiles = CForumFiles::GetList(array("ID" => "ASC"), array("MESSAGE_ID" => intval($arData["MESSAGE_ID"])));
							while ($arAddedMessageFiles = $dbAddedMessageFiles->Fetch())
								$ufFileID[] = $arAddedMessageFiles["FILE_ID"];

							if (count($ufFileID) > 0)
								$arFieldsForSocnet["UF_SONET_COM_FILE"] = $ufFileID;

							$ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_DOC", intval($arData["MESSAGE_ID"]), LANGUAGE_ID);
							if ($ufDocID)
								$arFieldsForSocnet["UF_SONET_COM_DOC"] = $ufDocID;

							$comment_id = CSocNetLogComments::Add($arFieldsForSocnet, false, false);
							CSocNetLog::CounterIncrement($comment_id, false, false, "LC");
						}
					}
				}
				break;
			case "EDIT":
				$occurAsUserId = (int) static::getOccurAsId($arData['MESSAGE']['AUTHOR_ID']);

				$arMessage = CForumMessage::GetByID(intval($arData["MESSAGE_ID"]));
				if ($arMessage)
				{
					$dbLogComment = CSocNetLogComments::GetList(
						array("ID" => "DESC"),
						array(
							"EVENT_ID" => ($bCrmTask ? 'crm_activity_add_comment' : 'tasks_comment'),
							"SOURCE_ID" => intval($arData["MESSAGE_ID"])
						),
						false,
						false,
						array("ID", "LOG_ID")
					);
					$arLogComment = $dbLogComment->fetch();
					if ($arLogComment)
					{
						$sText = (COption::GetOptionString("forum", "FILTER", "Y") == "Y" ? $arMessage["POST_MESSAGE_FILTER"] : $arMessage["POST_MESSAGE"]);
						$arFieldsForSocnet = array(
							"LOG_ID" => intval($arLogComment["LOG_ID"]),
							"MESSAGE" => $sText,
							"TEXT_MESSAGE" => $parser->convert4mail($sText),
						);

						$ufFileID = array();
						$arFilesIds = array();

						$taskId = null;
						if (
							isset($arData['PARAMS']['PARAM2'])
							&& !empty($arData['PARAMS']['PARAM2'])
						)
						{
							$taskId = (int) $arData['PARAMS']['PARAM2'];
						}

						$dbAddedMessageFiles = CForumFiles::GetList(array("ID" => "ASC"), array("MESSAGE_ID" => intval($arData["MESSAGE_ID"])));
						while ($arAddedMessageFiles = $dbAddedMessageFiles->Fetch())
						{
							$ufFileID[] = $arAddedMessageFiles["FILE_ID"];
							$arFilesIds[] = $arAddedMessageFiles["FILE_ID"];
						}

						if (count($ufFileID) > 0)
						{
							$arFieldsForSocnet["UF_SONET_COM_FILE"] = $ufFileID;
						}

						$ufDocID = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MESSAGE_DOC", intval($arData["MESSAGE_ID"]), LANGUAGE_ID);
						if ($ufDocID)
						{
							$arFieldsForSocnet["UF_SONET_COM_DOC"] = $ufDocID;

							if (is_array($ufDocID))
							{
								$arFilesIds = array_merge($arFilesIds, $ufDocID);
							}
						}

						$ufUrlPreview = $GLOBALS["USER_FIELD_MANAGER"]->GetUserFieldValue("FORUM_MESSAGE", "UF_FORUM_MES_URL_PRV", intval($arData["MESSAGE_ID"]), LANGUAGE_ID);
						if ($ufUrlPreview)
						{
							$arFieldsForSocnet["UF_SONET_COM_URL_PRV"] = $ufUrlPreview;
						}

						if ($taskId && ! empty($arFilesIds))
						{
							static::addWebDavFileRights($taskId, $arFilesIds);
						}
						CSocNetLogComments::Update($arLogComment["ID"], $arFieldsForSocnet);
					}
				}
				break;
			default:
		}
	}

	// add records to log. only EDIT and DEL actions handled
	// here may be edit or delete

	if ($arData['ACTION'] === 'EDIT' || $arData['ACTION'] === 'DEL')
	{
		$messageId = (int)$arData['MESSAGE_ID'];
		if (!is_array($arData['MESSAGE']) || !array_key_exists('AUTHOR_ID', $arData['MESSAGE']))
		{
			$arData['MESSAGE'] = ($arMessage === false ? CForumMessage::getByID($messageId) : $arMessage);
		}
		$messageAuthorId = $arData['MESSAGE']['AUTHOR_ID'];

		if ($arData['ACTION'] === 'EDIT')
		{
			$messageText = $arData['PARAMS']['POST_MESSAGE'];
			SearchIndex::setCommentSearchIndex($taskID, $messageId, $messageText);
		}
		else
		{
			SearchIndexTable::deleteByTaskAndMessageIds($taskID, $messageId);

			CounterCounterService::addEvent(
				CounterEventEventDictionary::EVENT_AFTER_COMMENT_DELETE,
				[
					'TASK_ID' => (int) $taskID,
					'USER_ID' => (int) $messageAuthorId,
					'MESSAGE_ID' => (int) $messageId
				]
			);
		}

		// no instant notification on comment update

		if (!isset($arData['replica']))
		{
			static::addLogItem(array(
				"TASK_ID" => (int)$taskID,
				"USER_ID" => static::getOccurAsId($messageAuthorId),
				"CREATED_DATE" => null,
				"FIELD" => "COMMENT_".$arData['ACTION'],
				"TO_VALUE" => $arData['MESSAGE_ID']
			));

			static::fireEvent($arData['ACTION'] == 'EDIT'? 'Update': 'Delete', $taskID, $arData);
		}
	}
}