static function Set($user_id, $code = "**", $type = "Y", $follow_date = false, $site_id = SITE_ID, $bByWF = false)
{
global $USER;
static $LOG_CACHE;
static $runCache = []; // to prevent double run
if ($code == '')
{
$code = "**";
}
if ($type != "Y")
{
$type = "N";
}
$user_id = intval($user_id);
if ($user_id <= 0)
{
$user_id = $USER->GetID();
}
$runCacheKey = [
'userId' => $user_id,
'code' => $code,
'type' => $type,
'date' => $follow_date,
'siteId' => $site_id,
'byWF' => $bByWF
];
$runCacheKey = md5(serialize($runCacheKey));
if (array_key_exists($runCacheKey, $runCache))
{
return true;
}
$runCache[$runCacheKey] = true;
$arFollows = array();
$rsFollow = CSocNetLogFollow::GetList(
array(
"USER_ID" => $user_id,
"CODE" => array_unique(array("**", $code))
)
);
while($arFollow = $rsFollow->Fetch())
{
$arFollows[$arFollow["CODE"]] = array(
"TYPE" => $arFollow["TYPE"],
"FOLLOW_DATE" => $arFollow["FOLLOW_DATE"]
);
}
$default_type = (
array_key_exists("**", $arFollows)
? $arFollows["**"]["TYPE"]
: COption::GetOptionString("socialnetwork", "follow_default_type", "Y")
);
$res = false;
if (preg_match('/^L(d+)$/', $code, $matches))
{
$log_id = intval($matches[1]);
if ($log_id > 0)
{
if (isset($LOG_CACHE[$log_id]))
{
$arLog = $LOG_CACHE[$log_id];
}
else
{
$rsLog = CSocNetLog::GetList(
array("ID" => "DESC"),
array("ID" => $log_id),
false,
false,
array("ID", "LOG_UPDATE", "LOG_DATE"),
array(
"CHECK_RIGHTS" => "N",
"USE_SUBSCRIBE" => "N",
"USE_FOLLOW" => "N"
)
);
if ($arLog = $rsLog->Fetch())
{
$LOG_CACHE[$log_id] = $arLog;
}
}
if ($arLog)
{
$log_date = ($arLog["LOG_DATE"] <> '' ? $arLog["LOG_DATE"] : false);
$log_update = ($arLog["LOG_UPDATE"] <> '' ? $arLog["LOG_UPDATE"] : false);
if (array_key_exists($code, $arFollows)) // already in the follows table
{
$res = CSocNetLogFollow::Update(
$user_id,
$code,
$type,
(
$arFollows[$code]["FOLLOW_DATE"] <> ''
? $arFollows[$code]["FOLLOW_DATE"] // existing value
: (
$type == "N"
? $log_update
: ($code == "**" ? $log_date : false)
)
),
$bByWF
);
}
elseif ($type != $default_type) // new record in the follow table only if not equal to default type
{
$res = CSocNetLogFollow::Add(
$user_id,
$code,
$type,
(
$follow_date
? $follow_date
: (
$type == "N"
? $log_update
: $log_date
)
),
$bByWF
);
}
if ($res)
{
$events = getModuleEvents('socialnetwork', 'onAfterLogFollowSet');
while ($eventFields = $events->fetch())
{
executeModuleEventEx($eventFields, [ $log_id, $type, $user_id ]);
}
}
}
}
}
else // **, change of default type
{
$res = (
array_key_exists($code, $arFollows)
? CSocNetLogFollow::Update($user_id, $code, $type, false)
: CSocNetLogFollow::Add($user_id, $code, $type, false)
);
}
return $res;
}