static function handleForwardReceivedBlogPost(BitrixMainEvent $event)
{
$userId = intval($event->getParameter('from'));
$message = trim($event->getParameter('content'));
$subject = trim($event->getParameter('subject'));
$attachments = $event->getParameter('attachments');
$siteId = $event->getParameter('site_id');
if (
$message == ''
&& count($attachments) > 0
)
{
$message = Loc::getMessage('BLOG_MAILHANDLER_ATTACHMENTS');
}
if (
$userId <= 0
|| $message == ''
|| $siteId == ''
)
{
return false;
}
if (!Loader::includeModule('socialnetwork'))
{
throw new MainSystemException("Could not load 'socialnetwork' module.");
}
$pathToPost = ConfigOption::get("socialnetwork", "userblogpost_page", '', $siteId);
$postId = false;
$blog = Blog::getByUser(array(
"GROUP_ID" => ConfigOption::get("socialnetwork", "userbloggroup_id", false, $siteId),
"SITE_ID" => $siteId,
"USER_ID" => $userId,
"CREATE" => "Y",
));
if (!$blog)
{
return $postId;
}
$connection = BitrixMainApplication::getConnection();
$helper = $connection->getSqlHelper();
$fields = Array(
"BLOG_ID" => $blog["ID"],
"AUTHOR_ID" => $userId,
"=DATE_CREATE" => $helper->getCurrentDateTimeFunction(),
"=DATE_PUBLISH" => $helper->getCurrentDateTimeFunction(),
"MICRO" => "N",
"TITLE" => $subject,
"DETAIL_TEXT" => $message,
"DETAIL_TEXT_TYPE" => "text",
"PUBLISH_STATUS" => BLOG_PUBLISH_STATUS_PUBLISH,
"HAS_IMAGES" => "N",
"HAS_TAGS" => "N",
"HAS_SOCNET_ALL" => "N",
"SOCNET_RIGHTS" => array("U".$userId)
);
if ($fields["TITLE"] == '')
{
$fields["MICRO"] = "Y";
$fields["TITLE"] = preg_replace("/[ATTACHMENTs*=s*[^]]*]/is".BX_UTF_PCRE_MODIFIER, "", blogTextParser::killAllTags($fields["DETAIL_TEXT"]));
$fields["TITLE"] = TruncateText(trim(preg_replace(array("/n+/is".BX_UTF_PCRE_MODIFIER, "/s+/is".BX_UTF_PCRE_MODIFIER), " ", $fields["TITLE"])), 100);
if($fields["TITLE"] == '')
{
$fields["TITLE"] = Loc::getMessage("BLOG_MAILHANDLER_EMPTY_TITLE_PLACEHOLDER");
}
}
$ufCode = (
isModuleInstalled("webdav")
|| isModuleInstalled("disk")
? "UF_BLOG_POST_FILE"
: "UF_BLOG_POST_DOC"
);
$fields[$ufCode] = array();
$type = false;
$attachmentRelations = array();
foreach ($attachments as $key => $attachedFile)
{
$resultId = CSocNetLogComponent::saveFileToUF($attachedFile, $type, $userId);
if ($resultId)
{
$fields[$ufCode][] = $resultId;
$attachmentRelations[$key] = $resultId;
}
}
$fields["HAS_PROPS"] = (!empty($attachmentRelations) ? "Y" :"N");
$fields["DETAIL_TEXT"] = preg_replace_callback(
"/[ATTACHMENTs*=s*([^]]*)]/is".BX_UTF_PCRE_MODIFIER,
function ($matches) use ($attachmentRelations)
{
return (
isset($attachmentRelations[$matches[1]])
? "[DISK FILE ID=".$attachmentRelations[$matches[1]]."]"
: ""
);
},
$fields["DETAIL_TEXT"]
);
if (Loader::includeModule('disk'))
{
BitrixDiskUfFileUserType::setValueForAllowEdit("BLOG_POST", true);
}
$postId = CBlogPost::add($fields);
if (!$postId)
{
return $postId;
}
BXClearCache(true, "/".$siteId."/blog/last_messages_list/");
$fields["ID"] = $postId;
$paramsNotify = array(
"bSoNet" => true,
"UserID" => $userId,
"allowVideo" => "N",
"PATH_TO_SMILE" => ConfigOption::get("socialnetwork", "smile_page", '', $siteId),
"PATH_TO_POST" => $pathToPost,
"user_id" => $userId,
"NAME_TEMPLATE" => CSite::getNameFormat(null, $siteId),
"SHOW_LOGIN" => "Y",
"SEND_COUNTER_TO_AUTHOR" => "Y"
);
CBlogPost::notify($fields, $blog, $paramsNotify);
if (Loader::includeModule('im'))
{
$postUrl = CComponentEngine::makePathFromTemplate($pathToPost, array(
"post_id" => $postId,
"user_id" => $userId
));
$processedPathData = CSocNetLogTools::processPath(array("POST_URL" => $postUrl), $userId, $siteId);
$serverName = $processedPathData["SERVER_NAME"];
$postUrl = $processedPathData["URLS"]["POST_URL"];
CIMNotify::add(array(
"MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
"NOTIFY_TYPE" => IM_NOTIFY_SYSTEM,
"NOTIFY_MODULE" => "blog",
"NOTIFY_EVENT" => "post_mail",
"NOTIFY_TAG" => "BLOG|POST|".$postId,
"TO_USER_ID" => $userId,
"NOTIFY_MESSAGE" => Loc::getMessage("BLOG_MAILHANDLER_NEW_POST", array(
"#TITLE#" => "".$fields["TITLE"].""
)),
"NOTIFY_MESSAGE_OUT" => Loc::getMessage("BLOG_MAILHANDLER_NEW_POST", array(
"#TITLE#" => $fields["TITLE"]
)).' '.$serverName.$postUrl
));
}
$categoryIdList = BitrixSocialnetworkComponentBlogPostEditTag::parseTagsFromFields([
'postFields' => $fields,
'blogId' => $blog['ID'],
]);
if (!empty($categoryIdList))
{
foreach ($categoryIdList as $categoryId)
{
CBlogPostCategory::add([
'BLOG_ID' => $fields['BLOG_ID'],
'POST_ID' => $postId,
'CATEGORY_ID' => $categoryId
]);
}
CBlogPost::update(
$postId,
[
'CATEGORY_ID' => implode(',', $categoryIdList),
'HAS_TAGS' => 'Y',
]
);
}
return $postId;
}