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()
68 $options[
'save'] =
true;
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);
205 public static function deleteOld($days)
208 $expired->add(
'-'.$days.
' days');
209 $expiredValue = $expired->format(
'Y-m-d H:i:s');
212 $connection = Main\Application::getConnection();
213 $sqlHelper = $connection->getSqlHelper();
215 $query =
"DELETE FROM b_sale_fuser WHERE
216 b_sale_fuser.DATE_UPDATE < ".$sqlHelper->getDateToCharFunction(
"'".$expiredValue.
"'").
"
217 AND b_sale_fuser.USER_ID IS NULL
218 AND b_sale_fuser.id NOT IN (select FUSER_ID from b_sale_basket)";
219 $connection->queryExecute($query);
228 protected static function createForUserId(
int $userId): ORM\Data\AddResult
233 $result = static::save(
236 'DATE_INSERT' => $currentTime,
237 'DATE_UPDATE' => $currentTime,
239 'CODE' => static::generateCode(),
253 protected static function getSession(): ?Session
257 if (!$session->isAccessible())
267 return Option::get(
'sale',
'encode_fuser_id') ===
'Y';
273 Option::get(
'sale',
'use_secure_cookies') ===
'Y'
274 && Main\Context::getCurrent()->getRequest()->isHttps()
280 return Option::get(
'sale',
'save_anonymous_fuser_cookie') ===
'Y';
285 $session = static::getSession();
286 if ($session ===
null)
290 if (!$session->has(self::SESSION_USER_ID))
294 $rawValue = $session->get(self::SESSION_USER_ID);
297 $value = (int)$rawValue;
298 $rawValue = (string)$rawValue;
299 if ((
string)$value !== $rawValue)
304 return ($value > 0 ? $value :
null);
309 $session = static::getSession();
310 if ($session ===
null)
318 $session->set(self::SESSION_USER_ID, $id);
323 $session = static::getSession();
324 if ($session ===
null)
328 $session->remove(self::SESSION_USER_ID);
333 $request = Main\Context::getCurrent()->getRequest();
335 return $request->getCookie(self::COOKIE_USER_ID);
342 ->setSecure(static::isSecureCookie())
347 $response = Main\Context::getCurrent()->getResponse();
349 $response->addCookie($cookie);
357 static::setIdToCookie(
'0');
362 if ($cookie ===
null || $cookie ===
'')
368 if (static::isEncodeCookie())
376 $cookie = (int)$cookie;
385 return (!empty($filter) ? $filter :
null);
390 $row = Internals\FuserTable::getRow([
399 $result = (int)($row[
'ID'] ?? 0);
401 return $result > 0 ? $result:
null;
406 $userId = self::getCurrentUserId();
412 return static::getIdByFilter([
417 private static function getCurrentUserId(): ?int
423 && $USER instanceof \CUser
436 return md5(time() . Main\Security\Random::getString(10,
true));
439 public static function add(array $options = []):
Result
444 $userCode = static::generateCode();
445 $currentUserId = self::getCurrentUserId();
447 $options[
'update'] ??=
true;
448 if (!is_bool($options[
'update']))
450 $options[
'update'] =
true;
452 $options[
'save'] ??=
false;
453 if (!is_bool($options[
'save']))
455 $options[
'save'] =
false;
459 $internalResult = static::save(
462 'DATE_INSERT' => $currentTime,
463 'DATE_UPDATE' => $currentTime,
464 'USER_ID' => $currentUserId,
468 if (!$internalResult->isSuccess())
470 $result->addErrors($internalResult->getErrors());
475 $id = (int)$internalResult->getId();
478 && $options[
'update']
479 && ($currentUserId !==
null || self::isSaveAnonymousUserCookie())
482 $cookieValue = (static::isEncodeCookie() ? $userCode : (string)$id);
483 static::setIdToCookie($cookieValue);
485 static::setIdToSession($id);
491 public static function update(
int $id, array $options = []): Result
493 $result =
new Result();
500 $options[
'update'] ??=
true;
501 if (!is_bool($options[
'update']))
503 $options[
'update'] =
true;
505 $options[
'save'] ??=
false;
506 if (!is_bool($options[
'save']))
508 $options[
'save'] =
false;
511 $fuser = Internals\FuserTable::getRow([
522 $databaseUpdate = $options[
'update'] && $fuser !==
null;
523 $encodeCookie = static::isEncodeCookie();
525 $userCode = trim((
string)($fuser[
'CODE'] ??
null));
526 $currentUserId = self::getCurrentUserId();
531 'DATE_UPDATE' =>
new Main\Type\DateTime(),
533 if ($currentUserId !==
null)
535 $userId = (int)$fuser[
'USER_ID'];
538 $fields[
'USER_ID'] = $currentUserId;
541 if ($encodeCookie && $userCode ===
'')
543 $userCode = static::generateCode();
544 $fields[
'CODE'] = $userCode;
548 $internalResult = static::save($id, $fields);
549 if (!$internalResult->isSuccess())
551 $result->addErrors($internalResult->getErrors());
558 if ($encodeCookie && $userCode ===
'')
560 $userCode = static::generateCode();
564 if ($options[
'save'] && $options[
'update'] && $currentUserId !==
null)
566 $cookieValue = (static::isEncodeCookie() ? $userCode : (string)$id);
567 static::setIdToCookie($cookieValue);
569 static::setIdToSession($id);
574 protected static function save(?
int $id, array $fields): ORM\Data\
Result
577 $pool->useMasterOnly(
true);
580 $internalResult = Internals\FuserTable::add($fields);
584 $internalResult = Internals\FuserTable::update($id, $fields);
586 $pool->useMasterOnly(
false);
589 return $internalResult;
600 'update' => ($params[
'update'] ??
true) ===
true,
601 'save' => ($params[
'save'] ??
false) ===
true,
604 $id = static::getIdFromSession();
605 if ($id ===
null && $options[
'update'])
607 $filter = static::getFilterFromCookie(static::getIdFromCookie());
608 if ($filter !==
null)
610 $id = static::getIdByFilter($filter);
619 $filter[
'!=ID'] = $id;
621 $row = Internals\FuserTable::getRow([
632 $newId = (int)$row[
'ID'];
633 if ($id !==
null && $options[
'update'])
635 if (\CSaleBasket::TransferBasket($id, $newId))
637 \CSaleUser::Delete($id);
645 static::update($id, $options);
647 elseif ($options[
'update'])
649 unset($options[
'update']);
650 static::add($options);
656 static::clearSession();
657 static::clearCookie();