33 public static function getId($skipCreate =
false): ?int
35 $id = static::getIdFromSession();
41 $filter = static::getFilterFromCookie(static::getIdFromCookie());
44 $id = static::getIdByFilter(
$filter);
48 $id = static::getIdByCurrentUser();
52 $internalResult = static::update($id);
53 if (!$internalResult->isSuccess())
57 unset($internalResult);
60 if ($id ===
null && !$skipCreate)
64 self::getCurrentUserId() ===
null
65 && self::isSaveAnonymousUserCookie()
70 $internalResult = static::add(
$options);
71 if ($internalResult->isSuccess())
73 $id = $internalResult->getId();
75 unset($internalResult);
83 $id = static::getIdFromSession();
89 return static::getId(
true);
99 $result = static::getRegeneratedId();
109 public static function getRegeneratedId(): ?int
111 $userId = static::getCurrentUserId();
116 $id = static::getIdByFilter([
124 $userCode = static::generateCode();
127 $internalResult = static::save(
133 if (!$internalResult->isSuccess())
138 $cookieValue = (static::isEncodeCookie() ? $userCode : (string)$id);
139 static::setIdToCookie($cookieValue);
140 static::setIdToSession($id);
154 $id = static::getIdByFilter([
159 $internalResult = static::createForUserId(
$userId);
160 if ($internalResult->isSuccess())
162 $id = (int)$internalResult->getId();
164 unset($internalResult);
167 return $id ===
null ?
false : $id;
184 $row = Internals\FuserTable::getRow([
196 return (
int)($row[
'USER_ID'] ?? 0);
213 DELETE FROM b_sale_fuser
215 b_sale_fuser.DATE_UPDATE < " . $helper->addDaysToDateTime(-$days) .
"
216 AND b_sale_fuser.USER_ID IS NULL
217 AND b_sale_fuser.ID NOT IN (select FUSER_ID from b_sale_basket)
237 'DATE_INSERT' => $currentTime,
238 'DATE_UPDATE' => $currentTime,
240 'CODE' => static::generateCode(),
254 protected static function getSession(): ?Session
257 $session = Application::getInstance()->getSession();
258 if (!$session->isAccessible())
268 return Option::get(
'sale',
'encode_fuser_id') ===
'Y';
274 Option::get(
'sale',
'use_secure_cookies') ===
'Y'
275 && Main\Context::getCurrent()->getRequest()->isHttps()
281 return Option::get(
'sale',
'save_anonymous_fuser_cookie') ===
'Y';
286 $session = static::getSession();
287 if ($session ===
null)
291 if (!$session->has(self::SESSION_USER_ID))
295 $rawValue = $session->get(self::SESSION_USER_ID);
298 $value = (int)$rawValue;
299 $rawValue = (string)$rawValue;
300 if ((
string)$value !== $rawValue)
305 return ($value > 0 ? $value :
null);
310 $session = static::getSession();
311 if ($session ===
null)
319 $session->set(self::SESSION_USER_ID, $id);
324 $session = static::getSession();
325 if ($session ===
null)
329 $session->remove(self::SESSION_USER_ID);
334 $request = Main\Context::getCurrent()->getRequest();
336 return $request->getCookie(self::COOKIE_USER_ID);
343 ->setSecure(static::isSecureCookie())
348 $response = Main\Context::getCurrent()->getResponse();
358 static::setIdToCookie(
'0');
363 if ($cookie ===
null || $cookie ===
'')
369 if (static::isEncodeCookie())
377 $cookie = (int)$cookie;
391 $row = Internals\FuserTable::getRow([
400 $result = (int)($row[
'ID'] ?? 0);
407 $userId = self::getCurrentUserId();
413 return static::getIdByFilter([
418 private static function getCurrentUserId(): ?int
424 &&
$USER instanceof \CUser
437 return md5(time() .
Main\
Security\Random::getString(10,
true));
445 $userCode = static::generateCode();
446 $currentUserId = self::getCurrentUserId();
455 $internalResult = static::save(
458 'DATE_INSERT' => $currentTime,
459 'DATE_UPDATE' => $currentTime,
460 'USER_ID' => $currentUserId,
464 if (!$internalResult->isSuccess())
466 $result->addErrors($internalResult->getErrors());
471 $id = (int)$internalResult->getId();
474 && ($currentUserId !==
null || self::isSaveAnonymousUserCookie())
477 $cookieValue = (static::isEncodeCookie() ? $userCode : (string)$id);
478 static::setIdToCookie($cookieValue);
480 static::setIdToSession($id);
486 public static function update(
int $id,
array $options = []): Result
506 $fuser = Internals\FuserTable::getRow([
517 $databaseUpdate =
$options[
'update'] && $fuser !==
null;
518 $encodeCookie = static::isEncodeCookie();
520 $userCode = trim((
string)($fuser[
'CODE'] ??
null));
521 $currentUserId = self::getCurrentUserId();
526 'DATE_UPDATE' =>
new Main\Type\DateTime(),
528 if ($currentUserId !==
null)
530 $userId = (int)$fuser[
'USER_ID'];
533 $fields[
'USER_ID'] = $currentUserId;
536 if ($encodeCookie && $userCode ===
'')
538 $userCode = static::generateCode();
543 $internalResult = static::save($id,
$fields);
544 if (!$internalResult->isSuccess())
546 $result->addErrors($internalResult->getErrors());
553 if ($encodeCookie && $userCode ===
'')
555 $userCode = static::generateCode();
559 if (
$options[
'save'] && $currentUserId !==
null)
561 $cookieValue = (static::isEncodeCookie() ? $userCode : (string)$id);
562 static::setIdToCookie($cookieValue);
564 static::setIdToSession($id);
571 $pool = Application::getInstance()->getConnectionPool();
572 $pool->useMasterOnly(
true);
575 $internalResult = Internals\FuserTable::add(
$fields);
579 $internalResult = Internals\FuserTable::update($id,
$fields);
581 $pool->useMasterOnly(
false);
584 return $internalResult;
595 'update' => (
$params[
'update'] ??
true) ===
true,
596 'save' => (
$params[
'save'] ??
false) ===
true,
603 $id = static::getIdFromSession();
606 $filter = static::getFilterFromCookie(static::getIdFromCookie());
609 $id = static::getIdByFilter(
$filter);
620 $row = Internals\FuserTable::getRow([
631 $newId = (int)$row[
'ID'];
634 if (\CSaleBasket::TransferBasket($id, $newId))
655 static::clearSession();
656 static::clearCookie();
static getConnection($name="")