38 protected static function init()
40 if(static::$classNames !=
null)
45 $classes = static::getBuildInRestrictions();
47 Loader::registerAutoLoadClasses(
'sale', $classes);
53 foreach ($classes as $class => $path)
55 if (!$class::isAvailable())
57 unset($classes[$class]);
61 $event =
new Event(
'sale', static::getEventName());
63 $resultList = $event->getResults();
65 if (is_array($resultList) && !empty($resultList))
67 $customClasses = array();
69 foreach ($resultList as $eventResult)
72 if ($eventResult->getType() != EventResult::SUCCESS)
73 throw new SystemException(
"Can't add custom restriction class successfully");
75 $params = $eventResult->getParameters();
77 if(!empty($params) && is_array($params))
78 $customClasses = array_merge($customClasses, $params);
81 if(!empty($customClasses))
83 Loader::registerAutoLoadClasses(
null, $customClasses);
84 $classes = array_merge($customClasses, $classes);
88 static::$classNames = array_keys($classes);
106 if (static::$classNames ===
null)
109 return static::$classNames;
121 if(intval($serviceId) <= 0)
126 $restrictions = static::getRestrictionsList($serviceId);
128 foreach($restrictions as $rstrParams)
130 if(!$rstrParams[
'PARAMS'])
131 $rstrParams[
'PARAMS'] = array();
133 $res = $rstrParams[
'CLASS_NAME']::checkByEntity($entity, $rstrParams[
'PARAMS'], $mode, $serviceId);
135 if($res == self::SEVERITY_STRICT)
138 if($res == self::SEVERITY_SOFT && $result != self::SEVERITY_SOFT)
160 if ((
int)$serviceId <= 0)
163 $serviceType = static::getServiceType();
165 if (!isset(static::$cachedFields[$serviceType]))
168 $dbRes = ServiceRestrictionTable::getList(array(
170 '=SERVICE_TYPE' => $serviceType,
172 'order' => array(
'SORT' =>
'ASC'),
175 while($restriction = $dbRes->fetch())
177 if (!isset($result[$restriction[
'SERVICE_ID']]))
178 $result[$restriction[
'SERVICE_ID']] = array();
180 $result[$restriction[
'SERVICE_ID']][$restriction[
"ID"]] = $restriction;
183 static::$cachedFields[$serviceType] = $result;
186 if (!isset(static::$cachedFields[$serviceType][$serviceId]))
189 return static::$cachedFields[$serviceType][$serviceId];
203 foreach(static::getRestrictionsList($id) as $fields)
205 if($fields[
'CLASS_NAME'] ==
'\Bitrix\Sale\Delivery\Restrictions\BySite')
207 if(!empty($fields[
"PARAMS"][
"SITE_ID"]))
209 if(is_array($fields[
"PARAMS"][
"SITE_ID"]))
210 $result = $fields[
"PARAMS"][
"SITE_ID"];
212 $result = array($fields[
"PARAMS"][
"SITE_ID"]);
228 public static function prepareData(array $servicesIds, array $fields = array())
230 if(empty($servicesIds))
233 $serviceType = static::getServiceType();
235 isset(static::$cachedFields[$serviceType]) && is_array(static::$cachedFields[$serviceType])
236 ? array_keys(static::$cachedFields[$serviceType])
239 $ids = array_diff($servicesIds, $cachedServices);
240 $idsForDb = array_diff($ids, array_keys($fields));
242 if(!empty($idsForDb))
244 $dbRes = ServiceRestrictionTable::getList(array(
246 '=SERVICE_ID' => $idsForDb,
247 '=SERVICE_TYPE' => $serviceType,
249 'order' => array(
'SORT' =>
'ASC'),
252 while($restriction = $dbRes->fetch())
253 self::setCache($restriction[
"SERVICE_ID"], $serviceType, $restriction);
256 foreach($fields as $serviceId => $serviceRestrictions)
258 if(is_array($serviceRestrictions))
260 foreach($serviceRestrictions as $restrId => $restrFields)
265 foreach($ids as $serviceId)
269 foreach(static::getClassesList() as $className)
270 $className::prepareData($ids);
279 protected static function setCache($serviceId, $serviceType, array $fields = array())
281 if(intval($serviceId) <= 0)
284 if(!isset(static::$cachedFields[$serviceType]))
285 static::$cachedFields[$serviceType] = array();
287 if(!isset(static::$cachedFields[$serviceType][$serviceId]))
288 static::$cachedFields[$serviceType][$serviceId] = array();
291 static::$cachedFields[$serviceType][$serviceId][$fields[
"ID"]] = $fields;
300 protected static function getCache($serviceId, $serviceType)
304 if(intval($serviceId) > 0)
306 if(isset(static::$cachedFields[$serviceType][$serviceId]))
307 $result = static::$cachedFields[$serviceType][$serviceId];
311 if(isset(static::$cachedFields[$serviceType]))
312 $result = static::$cachedFields[$serviceType];
334 if (!$params[
'filter'])
335 $params[
'filter'] = array();
337 $params[
'filter'][
'SERVICE_TYPE'] = static::getServiceType();
339 return ServiceRestrictionTable::getList($params);
348 return ServiceRestrictionTable::getById($id);
358 foreach (static::getClassesList() as $className)
361 self::isRestrictionClassname($className)
362 && $className::isMyCode($restrictionCode)
372 private static function isRestrictionClassname(
string $className): bool
376 $restrictionClass = new \ReflectionClass($className);
378 catch (\ReflectionException $e)
383 return $restrictionClass->isSubclassOf(Restriction::class);
394 public static function applyRestriction(
int $serviceId, RestrictionInfo $restrictionInfo): Result
396 $result =
new Result();
398 $restriction = static::getRestriction($restrictionInfo->getType());
400 $reflectionClass = new \ReflectionClass(static::class);
401 $methodPath = $reflectionClass->getName() .
"::applyRestriction";
405 Logger::addError(
"[{$methodPath}] restriction '{$restrictionInfo->getType()}' not found.");
407 $publicErrorMessage =
Loc::getMessage(
'SALE_BASE_RSTR_MANAGER_FIND_RSTR_ERROR', [
408 '#RSTR_CLASSNAME#' => htmlspecialcharsbx($restrictionInfo->getType()),
410 $result->addError(
new \
Bitrix\Main\Error($publicErrorMessage));
419 $restrictionApplyResult = $restriction::save([
420 'SERVICE_ID' => $serviceId,
421 'SERVICE_TYPE' => static::getServiceType(),
422 'PARAMS' => $restrictionInfo->getOptions(),
425 if (!$restrictionApplyResult->isSuccess())
427 foreach ($restrictionApplyResult->getErrors() as $error)
432 $publicErrorMessage = $restriction::getOnApplyErrorMessage();
433 $result->addError(
new Error($publicErrorMessage));
446 public static function setupDefaultRestrictions(RestrictableService $service): Result
448 $result =
new Result();
450 $startupRestrictions = $service->getStartupRestrictions();
454 static::ON_STARTUP_SERVICE_RESTRICTIONS_EVENT_NAME,
456 'STARTUP_RESTRICTIONS_COLLECTION' => $startupRestrictions,
457 'SERVICE_ID' => $service->getServiceId(),
461 self::clearAlreadyUsedByServiceRestrictions($service->getServiceId(), $startupRestrictions);
464 foreach ($startupRestrictions as $restrictionInfo)
466 $applyResult = static::applyRestriction($service->getServiceId(), $restrictionInfo);
467 $result->addErrors($applyResult->getErrors());
473 private static function clearAlreadyUsedByServiceRestrictions(
int $serviceId, RestrictionInfoCollection $collection): void
475 $serviceRestrictions = array_column(static::getRestrictionsList($serviceId),
'CLASS_NAME');
477 foreach ($serviceRestrictions as $restrictionClassName)
479 if (self::isRestrictionClassname($restrictionClassName))
482 $collection->delete($restrictionClassName::getCode());
static getMessage($code, $replace=null, $language=null)