use \Bitrix\Main;
use Bitrix\Main\Config;
try
{
if ( !Main\Loader::IncludeModule('socialnetwork') )
{
throw new \Exception("Socialnetwork module not installed");
}
if ( !Main\Loader::IncludeModule('blog') )
{
throw new \Exception("Blog module not installed");
}
/**
* @var integer Author id
*/
$ownerId = 1;
/**
* @var string Blog post title
* can be empty
*/
$title = 'Заголовок новости!';
/**
* @var string Blog message
*/
$message = 'Текст какой-то новости';
$siteId = SITE_ID;
$pathToPost = Config\Option::get("socialnetwork", "userblogpost_page", false, $siteId);
$pathToSmile = Config\Option::get("socialnetwork", "smile_page", false, $siteId);
$blogGroupID = Config\Option::get("socialnetwork", "userbloggroup_id", false, $siteId);
$micro = 'N';
if ( empty($title) )
{
$micro = 'Y';
$title = trim(preg_replace(
["/\n+/is".BX_UTF_PCRE_MODIFIER, '/\s+/is'.BX_UTF_PCRE_MODIFIER],
" ",
\blogTextParser::killAllTags($title)
));
}
/**
* @var array List of socnet destination
*/
$socnetRights = [
'SG1'
];
$blog = \Bitrix\Blog\Item\Blog::getByUser(array(
"GROUP_ID" => Config\Option::get("socialnetwork", "userbloggroup_id", false, $siteId),
"SITE_ID" => $siteId,
"USER_ID" => $ownerId,
"CREATE" => "Y",
));
if ( !$blog )
{
throw new Exception('User blog not found');
}
$postFields = array(
'TITLE' => $title,
'DETAIL_TEXT' => HTMLToTxt(nl2br($message), '', array(), 0),
'DETAIL_TEXT_TYPE' => 'text',
'=DATE_PUBLISH' => $DB->CurrentTimeFunction(),
'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH,
'CATEGORY_ID' => '',
'PATH' => CComponentEngine::MakePathFromTemplate($pathToPost, array("post_id" => "#post_id#", "user_id" => $ownerId)),
'URL' => $blog['URL'],
'PERMS_POST' => array(),
'PERMS_COMMENT' => array(),
'MICRO' => $micro,
'SOCNET_RIGHTS' => $socnetRights,
'=DATE_CREATE' => $DB->CurrentTimeFunction(),
'AUTHOR_ID' => $ownerId,
'BLOG_ID' => $blog['ID'],
"HAS_IMAGES" => "N",
"HAS_TAGS" => "N",
"HAS_PROPS" => "Y",
"HAS_SOCNET_ALL" => "N",
"SEARCH_GROUP_ID" => $blogGroupID,
'UF_BLOG_POST_FILE' => [],
'UF_BLOG_POST_IMPRTNT' => 1,
'UF_IMPRTANT_DATE_END' => '',
);
if(!empty($postFields["SOCNET_RIGHTS"]) && count($postFields["SOCNET_RIGHTS"]) == 1 && in_array("UA", $postFields["SOCNET_RIGHTS"]))
$postFields['HAS_SOCNET_ALL'] = 'Y';
$newId = \CBlogPost::add($postFields);
$postFields["ID"] = $newId;
global $USER;
\CBlogUserOptions::SetOption($newId, "BLOG_POST_IMPRTNT", "Y", $USER->GetID());
$arParamsNotify = Array(
"bSoNet" => true,
"UserID" => $ownerId,
"allowVideo" => Config\Option::get("blog", "allow_video", "Y", $siteId),
"PATH_TO_SMILE" => $pathToSmile,
"PATH_TO_POST" => $pathToPost,
"user_id" => $ownerId,
"NAME_TEMPLATE" => CSite::GetNameFormat(false),
"SITE_ID" => $siteId,
'BLOG_POST_IMPRTNT' => 'Y',
);
\CBlogPost::Notify($postFields, $blog, $arParamsNotify);
\BXClearCache(true, "/".$siteId."/blog/last_messages_list/");
$arFieldsIM = Array(
"TYPE" => "POST",
"TITLE" => $postFields["TITLE"],
"URL" => \CComponentEngine::MakePathFromTemplate($pathToPost, array("post_id" => $newId, "user_id" => $ownerId)),
"ID" => $newId,
"FROM_USER_ID" => $ownerId,
"TO_USER_ID" => array(),
"TO_SOCNET_RIGHTS" => $postFields["SOCNET_RIGHTS"],
"TO_SOCNET_RIGHTS_OLD" => array()
);
\CBlogPost::NotifyIm($arFieldsIM);
}
catch( \Exception $e )
{
var_dump($e->getMessage());
} |