16 private const STORE_ID =
'store_v3';
17 private const MINIMAL_ORDERS_LIMIT = 5;
19 private static $storeId;
28 if (self::$storeId ===
null)
30 if (!Loader::includeModule(
'landing'))
35 Rights::setGlobalOff();
39 'filter' => [
'=TPL_CODE' => self::STORE_ID],
40 'order' => [
'ID' =>
'desc'],
45 self::$storeId = (int)$store[
'ID'];
48 Rights::setGlobalOn();
51 return self::$storeId;
56 if (!self::hasStore())
61 Rights::setGlobalOff();
62 $storeUrl = Site::getPublicUrl(self::getStoreId());
63 Rights::setGlobalOn();
72 if (!Loader::includeModule(
'landing'))
74 return $result->addError(
new Error(
'Failed to load module \'landing\'.'));
77 if (self::limitExceeded())
79 if (!self::hasUnusedStores())
81 return $result->addError(
new Error(
'Store limit exceeded.'));
84 $res = self::deactivateUnusedStore();
85 if (!
$res->isSuccess())
95 if (!self::hasStore())
97 $result->addError(
new Error(
'Failed to create a new store.'));
105 if (!Loader::includeModule(
'landing'))
110 if (!self::limitExceeded())
115 return self::hasUnusedStores();
118 private static function limitExceeded(): bool
120 return !Manager::checkFeature(
121 Manager::FEATURE_PUBLICATION_SITE,
126 private static function hasUnusedStores(): bool
132 private static function deactivateUnusedStore():
Result
136 if (!self::hasUnusedStores())
139 new Error(
'There are no unused stores on the portal.')
143 Rights::setGlobalOff();
144 $deactivatedStore =
null;
146 foreach (self::getUnusedStoresList() as $storeId)
148 $result = Site::update($storeId, [
'ACTIVE' =>
'N']);
151 $deactivatedStore = Site::getList([
152 'select' => [
'ID',
'TITLE'],
153 'filter' => [
'=ID' => $storeId],
157 if ($deactivatedStore)
159 $result->setData(compact(
'deactivatedStore'));
166 Rights::setGlobalOn();
167 if (!$deactivatedStore)
169 $result->addError(
new Error(
'Failed to deactivate unused stores.'));
175 private static function getUnusedStoresList():
array
181 if (!Loader::includeModule(
'landing') || !Loader::includeModule(
'crm'))
186 Rights::setGlobalOff();
189 $activeStoreIds = [];
190 $res = Site::getList([
197 while ($row =
$res->fetch())
199 $activeStoreIds[] = $row[
'ID'];
202 if (!empty($activeStoreIds))
206 '>CNT' => self::MINIMAL_ORDERS_LIMIT,
207 '=TRADING_PLATFORM.CODE' => [],
209 foreach ($activeStoreIds as
$siteId)
216 'TRADING_PLATFORM_CODE' =>
'TRADING_PLATFORM.CODE',
219 'group' =>
'TRADING_PLATFORM.CODE',
220 'runtime' => [
new ExpressionField(
'CNT',
'COUNT(*)')],
222 while ($row =
$res->fetch())
224 if ($row[
'TRADING_PLATFORM_CODE'])
226 [,
$siteId] = explode(
'_', $row[
'TRADING_PLATFORM_CODE']);
231 $result = array_diff($activeStoreIds, $storesInUse);
234 Rights::setGlobalOn();
240 private static function createStore(): void
243 $className = \CBitrixComponent::includeComponentClass(
$componentName);
244 $siteMaster =
new $className;
247 $siteMaster->actionCreate(self::STORE_ID);
252 private static function clearCache(): void
254 self::$storeId =
null;