Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
try {
$objCookieFav = new Cookies();
if ($objCookieFav->get() !== null) {
$arrCookies = json_decode($objCookieFav->get(), true);
} else {
$arrCookies = [];
}
$userId = (int) $arUser['user_fields']['ID'];
$siteId = Application::getInstance()->getContext()->getSite();
$elementsTable = FavoritesTable::getList([
'select' => ['UF_IBLOCK', 'UF_ENTITY'],
'filter' => ['UF_OWNER' => $userId]
])->fetchAll();
$helper = new Helper();
$tableFormat = $helper->transformCookieFormat($elementsTable);
$dif = [];
$arr1keys = array_keys($arrCookies);
$arr2keys = array_keys($tableFormat);
$arrayValues = array_unique(array_merge(array_values($arr1keys), array_values($arr2keys)));
foreach ($arrayValues as $k) {
if (empty($tableFormat)) {
$dif = $arrCookies;
break;
}
$dif[$k] = array_diff($tableFormat[$k], $arrCookies[$k]);
if (empty($dif[$k])) {
unset($dif[$k]);
}
}
if ($dif !== []) {
foreach ($dif as $iblockId => $elementsId) {
foreach ($elementsId as $elementId) {
$favoriteObj = new FavoritesTable($iblockId, $elementId);
$favoriteObj->addElement();
}
}
}
if (!empty($arrCookies)) {
$newCookie = [];
$arrayKeys = array_unique(array_merge(array_keys($arrCookies), array_keys($tableFormat)));
foreach ($arrayKeys as $k) {
$newCookie[$k] = array_unique(array_merge(
$arrCookies[$k] ?? [],
$tableFormat[$k] ?? []
));
}
}
$cookie = $newCookie ?? $tableFormat;
if (!empty($arrCookies)) {
$objCookieFav->clear($arrCookies);
}
$objCookieFav->set($cookie);
} catch (\Exception $e) {
AddMessage2Log($e->getMessage(), 'neti.favorite');
}