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'))
37 $result = Site::getList([
39 'filter' => [
'=TPL_CODE' => self::STORE_ID],
40 'order' => [
'ID' =>
'desc'],
43 if ($store = $result->fetch())
45 self::$storeId = (int)$store[
'ID'];
51 return self::$storeId;
56 if (!self::hasStore())
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())
87 return $result->addErrors($res->getErrors());
90 $result->setData($res->getData());
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())
138 return $result->addError(
139 new Error(
'There are no unused stores on the portal.')
144 $deactivatedStore =
null;
146 foreach (self::getUnusedStoresList() as $storeId)
148 $result = Site::update($storeId, [
'ACTIVE' =>
'N']);
149 if ($result->isSuccess())
151 $deactivatedStore = Site::getList([
152 'select' => [
'ID',
'TITLE'],
153 'filter' => [
'=ID' => $storeId],
157 if ($deactivatedStore)
159 $result->setData(compact(
'deactivatedStore'));
167 if (!$deactivatedStore)
169 $result->addError(
new Error(
'Failed to deactivate unused stores.'));
175 private static function getUnusedStoresList(): array
177 static $result =
null;
179 if ($result ===
null)
181 if (!Loader::includeModule(
'landing') || !Loader::includeModule(
'crm'))
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)
211 $filter[
'=TRADING_PLATFORM.CODE'][] =
'landing_' . $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']);
227 $storesInUse[] = $siteId;
231 $result = array_diff($activeStoreIds, $storesInUse);
240 private static function createStore(): void
242 $componentName =
'bitrix:landing.site_master';
243 $className = \CBitrixComponent::includeComponentClass($componentName);
244 $siteMaster =
new $className;
246 $siteMaster->initComponent($componentName);
247 $siteMaster->actionCreate(self::STORE_ID);
252 private static function clearCache(): void
254 self::$storeId =
null;