Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StoreV3Master.php
1<?php
2
4
5use Bitrix\Crm\Order\TradeBindingCollection;
9use Bitrix\Main\Entity\ExpressionField;
13
14final class StoreV3Master
15{
16 private const STORE_ID = 'store_v3';
17 private const MINIMAL_ORDERS_LIMIT = 5;
18
19 private static $storeId;
20
21 public static function hasStore(): bool
22 {
23 return self::getStoreId() !== null;
24 }
25
26 public static function getStoreId(): ?int
27 {
28 if (self::$storeId === null)
29 {
30 if (!Loader::includeModule('landing'))
31 {
32 return null;
33 }
34
36
37 $result = Site::getList([
38 'select' => ['ID'],
39 'filter' => ['=TPL_CODE' => self::STORE_ID],
40 'order' => ['ID' => 'desc'],
41 'limit' => 1,
42 ]);
43 if ($store = $result->fetch())
44 {
45 self::$storeId = (int)$store['ID'];
46 }
47
49 }
50
51 return self::$storeId;
52 }
53
54 public static function getStoreUrl(): ?string
55 {
56 if (!self::hasStore())
57 {
58 return null;
59 }
60
62 $storeUrl = Site::getPublicUrl(self::getStoreId());
64
65 return $storeUrl;
66 }
67
68 public static function addNewStore(): Result
69 {
70 $result = new Result();
71
72 if (!Loader::includeModule('landing'))
73 {
74 return $result->addError(new Error('Failed to load module \'landing\'.'));
75 }
76
77 if (self::limitExceeded())
78 {
79 if (!self::hasUnusedStores())
80 {
81 return $result->addError(new Error('Store limit exceeded.'));
82 }
83
84 $res = self::deactivateUnusedStore();
85 if (!$res->isSuccess())
86 {
87 return $result->addErrors($res->getErrors());
88 }
89
90 $result->setData($res->getData());
91 }
92
93 self::createStore();
94
95 if (!self::hasStore())
96 {
97 $result->addError(new Error('Failed to create a new store.'));
98 }
99
100 return $result;
101 }
102
103 public static function canCreate(): bool
104 {
105 if (!Loader::includeModule('landing'))
106 {
107 return false;
108 }
109
110 if (!self::limitExceeded())
111 {
112 return true;
113 }
114
115 return self::hasUnusedStores();
116 }
117
118 private static function limitExceeded(): bool
119 {
120 return !Manager::checkFeature(
121 Manager::FEATURE_PUBLICATION_SITE,
122 ['type' => 'STORE']
123 );
124 }
125
126 private static function hasUnusedStores(): bool
127 {
128 // return !empty(self::getUnusedStoresList());
129 return false;
130 }
131
132 private static function deactivateUnusedStore(): Result
133 {
134 $result = new Result();
135
136 if (!self::hasUnusedStores())
137 {
138 return $result->addError(
139 new Error('There are no unused stores on the portal.')
140 );
141 }
142
144 $deactivatedStore = null;
145
146 foreach (self::getUnusedStoresList() as $storeId)
147 {
148 $result = Site::update($storeId, ['ACTIVE' => 'N']);
149 if ($result->isSuccess())
150 {
151 $deactivatedStore = Site::getList([
152 'select' => ['ID', 'TITLE'],
153 'filter' => ['=ID' => $storeId],
154 ])
155 ->fetch()
156 ;
157 if ($deactivatedStore)
158 {
159 $result->setData(compact('deactivatedStore'));
160 }
161
162 break;
163 }
164 }
165
167 if (!$deactivatedStore)
168 {
169 $result->addError(new Error('Failed to deactivate unused stores.'));
170 }
171
172 return $result;
173 }
174
175 private static function getUnusedStoresList(): array
176 {
177 static $result = null;
178
179 if ($result === null)
180 {
181 if (!Loader::includeModule('landing') || !Loader::includeModule('crm'))
182 {
183 return [];
184 }
185
187
188 $result = [];
189 $activeStoreIds = [];
190 $res = Site::getList([
191 'select' => ['ID'],
192 'filter' => [
193 '=TYPE' => 'STORE',
194 '=ACTIVE' => 'Y',
195 ],
196 ]);
197 while ($row = $res->fetch())
198 {
199 $activeStoreIds[] = $row['ID'];
200 }
201
202 if (!empty($activeStoreIds))
203 {
204 $storesInUse = [];
205 $filter = [
206 '>CNT' => self::MINIMAL_ORDERS_LIMIT,
207 '=TRADING_PLATFORM.CODE' => [],
208 ];
209 foreach ($activeStoreIds as $siteId)
210 {
211 $filter['=TRADING_PLATFORM.CODE'][] = 'landing_' . $siteId;
212 }
214 'select' => [
215 'CNT',
216 'TRADING_PLATFORM_CODE' => 'TRADING_PLATFORM.CODE',
217 ],
218 'filter' => $filter,
219 'group' => 'TRADING_PLATFORM.CODE',
220 'runtime' => [new ExpressionField('CNT', 'COUNT(*)')],
221 ]);
222 while ($row = $res->fetch())
223 {
224 if ($row['TRADING_PLATFORM_CODE'])
225 {
226 [, $siteId] = explode('_', $row['TRADING_PLATFORM_CODE']);
227 $storesInUse[] = $siteId;
228 }
229 }
230
231 $result = array_diff($activeStoreIds, $storesInUse);
232 }
233
235 }
236
237 return $result;
238 }
239
240 private static function createStore(): void
241 {
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);
248
249 self::clearCache();
250 }
251
252 private static function clearCache(): void
253 {
254 self::$storeId = null;
255 }
256}
static setGlobalOff()
Definition rights.php:105
static getPublicUrl($id, bool $full=true, bool $hostInclude=true, bool $previewForNotActive=false)
Definition site.php:77
static getList(array $parameters=array())