Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
site.php
1<?php
3
4use \Bitrix\Bitrix24\Feature;
5use \Bitrix\Bitrix24\PhoneVerify;
6use \Bitrix\Landing\Manager;
7use \Bitrix\Main\Entity;
8use \Bitrix\Landing\Domain;
9use \Bitrix\Landing\Rights;
10use \Bitrix\Landing\Site as SiteCore;
12use \Bitrix\Main\Type\DateTime;
13
14class Site
15{
20
24 private const LIMIT_BY_TEMPLATES_MINIMAL = [
25 'store-chats' => 1,
26 ];
27
31 private const OVER_LIMIT_TEMPLATES = [
32 'store-chats'
33 ];
34
38 private const NEW_STORE_CODE = 'store_v3';
39
46 private static function checkLimitByTemplates(array $filter, int $limit): bool
47 {
48 $sites = [];
49 $currentSiteId = null;
50 $currentSiteTemplate = null;
51
52 if (isset($filter['!ID']))
53 {
54 $currentSiteId = $filter['!ID'];
55 }
56
57 // get all sites (active) and group by templates
58 $res = \Bitrix\Landing\Site::getList([
59 'select' => [
60 'ID', 'XML_ID', 'TPL_CODE'
61 ],
62 'filter' => $filter
63 ]);
64 while ($row = $res->fetch())
65 {
66 $sites[] = $row;
67 }
68
69 // current site
70 if ($currentSiteId)
71 {
72 $res = \Bitrix\Landing\Site::getList([
73 'select' => [
74 'ID', 'XML_ID', 'TPL_CODE'
75 ],
76 'filter' => [
77 'CHECK_PERMISSIONS' => 'N',
78 'ID' => $currentSiteId
79 ]
80 ]);
81 if ($row = $res->fetch())
82 {
83 $sites[] = $row;
84 }
85 }
86
87 // calc templates
88 $templates = [];
89 $templatesCount = 0;
90 $templatesLimits = self::LIMIT_BY_TEMPLATES_MINIMAL;
91 // $templatesLimits['%'] = max($limit, 1);
92 $limit = max($limit, 1);
93 foreach ($sites as $row)
94 {
95 if (!$row['TPL_CODE'])
96 {
97 if (mb_strpos($row['XML_ID'], '|') !== false)
98 {
99 [, $row['TPL_CODE']] = explode('|', $row['XML_ID']);
100 }
101 }
102
103 // store-chat-dark === store-chat-light === store-chat
104 foreach ($templatesLimits as $code => $cnt)
105 {
106 if (strpos($row['TPL_CODE'], $code) === 0)
107 {
108 $row['TPL_CODE'] = $code;
109 break;
110 }
111 }
112
113 if ($currentSiteId && $currentSiteId === $row['ID'])
114 {
115 $currentSiteTemplate = $row['TPL_CODE'];
116 }
117
118 if (!($templates[$row['TPL_CODE']] ?? null))
119 {
120 $templates[$row['TPL_CODE']] = 0;
121 }
122 $templates[$row['TPL_CODE']]++;
123
124 if (!in_array($row['TPL_CODE'], self::OVER_LIMIT_TEMPLATES, true))
125 {
126 $templatesCount++;
127 }
128 }
129
130 // special limit for store v3
131 if (
132 $currentSiteTemplate
133 && $currentSiteTemplate === self::NEW_STORE_CODE
134 && self::isNew2021Tariff()
135 )
136 {
137 \CBitrixComponent::includeComponentClass('bitrix:landing.site_master');
138 $optionName = \LandingSiteMasterComponent::getShopInstallCountOptionName($currentSiteTemplate);
139 if ((int)Manager::getOption($optionName, 0) <= 1)
140 {
141 $limit++;
142 }
143 }
144
145 // calc limits
146 if (
147 $currentSiteTemplate
148 && in_array($row['TPL_CODE'], self::OVER_LIMIT_TEMPLATES, true)
149 )
150 {
151 return true;
152 }
153
154 if ($templates)
155 {
156 foreach ($templatesLimits as $code => $templateLimit)
157 {
158 if (
159 isset($templates[$code])
160 && $templates[$code] > $templateLimit
161 )
162 {
163 return false;
164 }
165 }
166 }
167
168 if ($limit < $templatesCount)
169 {
170 return false;
171 }
172
173 return true;
174 }
175
176 protected static function isNew2021Tariff(): bool
177 {
178 if (!Loader::includeModule('bitrix24'))
179 {
180 return false;
181 }
182
183 return in_array(
184 \CBitrix24::getLicenseType(),
185 ['basic', 'std', 'pro']
186 );
187 }
188
195 public static function isCreatingAllowed(string $code, array $params): bool
196 {
197 if (!Loader::includeModule('bitrix24'))
198 {
199 return true;
200 }
201
202 if (
203 $params['action_type'] === 'publication'
204 && Manager::licenseIsFreeSite($params['type'])
206 )
207 {
208 if (!isset($params['filter']['!ID']))
209 {
210 return false;
211 }
212
213 $siteId = $params['filter']['!ID'];
214 $site = SiteCore::getList([
215 'select' => ['ID' , 'DATE_CREATE'],
216 'filter' => ['ID' => $siteId],
217 ])->fetch();
218 $dateWhenClosedFree = DateTime::createFromTimestamp('1646238000'); //02.03.2022 16:20:00
219 if ($site['DATE_CREATE']->getTimestamp() >= $dateWhenClosedFree->getTimestamp())
220 {
221 return false;
222 }
223 }
224
225 $optPrefix = 'landing_site_';
226 $optSuffix = ($params['action_type'] == 'publication') ? '_publication' : '';
227 $variableCode = $optPrefix . strtolower($params['type']) . $optSuffix;
228 $limit = (int) Feature::getVariable($variableCode);
229
230 if ($limit)
231 {
232 $filter = [
233 'CHECK_PERMISSIONS' => 'N',
234 '=TYPE' => $params['type'],
235 '=SPECIAL' => 'N'
236 ];
237
238 if ($params['action_type'] == 'publication')
239 {
240 $filter['=ACTIVE'] = 'Y';
241 }
242
243 if (
244 isset($params['filter']) &&
245 is_array($params['filter'])
246 )
247 {
248 $filter = array_merge(
249 $filter,
250 $params['filter']
251 );
252 }
253
254 if ($params['action_type'] === 'publication' && $params['type'] === 'STORE')
255 {
256 return self::checkLimitByTemplates($filter, $limit);
257 }
258
259 $check = SiteCore::getList([
260 'select' => [
261 'CNT' => new Entity\ExpressionField('CNT', 'COUNT(*)')
262 ],
263 'filter' => $filter,
264 'group' => []
265 ])->fetch();
266 if ($check && $check['CNT'] >= $limit)
267 {
268 return false;
269 }
270 }
271
272 return true;
273 }
274
281 public static function isFreeDomainAllowed(string $code, array $params): bool
282 {
283 // free domain is available in cloud version only
284 if (!Loader::includeModule('bitrix24'))
285 {
286 return false;
287 }
288
289 $availableCount = Feature::getVariable(
290 'landing_free_domain'
291 );
292 if ($availableCount === null)
293 {
294 return false;
295 }
296 if (($params['trueOnNotNull'] ?? false))
297 {
298 return true;
299 }
300 if ($availableCount > 0)
301 {
302 $check = Domain::getList([
303 'select' => [
304 'CNT' => new Entity\ExpressionField('CNT', 'COUNT(*)')
305 ],
306 'filter' => [
307 '!PROVIDER' => null
308 ],
309 'group' => []
310 ])->fetch();
311 if ($check && $check['CNT'] >= $availableCount)
312 {
313 return false;
314 }
315 }
316 return true;
317 }
318
325 public static function manageFreeDomains(bool $setActive, int $executeAfterSeconds = 0): void
326 {
327 $methodName = __CLASS__ . '::' . __FUNCTION__ . '(' . ($setActive ? 'true' : 'false') . ');';
328
329 if ($executeAfterSeconds > 0)
330 {
331 $dateTime = new DateTime();
332 \CAgent::addAgent(
333 $methodName,
334 'landing', 'N', 0, '', 'Y',
335 $dateTime->add('+' . $executeAfterSeconds . ' seconds')
336 );
337 return;
338 }
339 if ($setActive)
340 {
341 \CAgent::removeAgent($methodName, 'landing');
342 }
343
345 $res = SiteCore::getList([
346 'select' => [
347 'ID',
348 'ACTIVE',
349 'DOMAIN_ID'
350 ],
351 'filter' => [
352 '=DOMAIN.ACTIVE' => $setActive ? 'N' : 'Y',
353 '!DOMAIN.PROVIDER' => null
354 ]
355 ]);
356 while ($site = $res->fetch())
357 {
358 if ($site['ACTIVE'] === ($setActive ? 'N' : 'Y'))
359 {
360 SiteCore::update($site['ID'], [
361 'ACTIVE' => $setActive ? 'Y' : 'N'
362 ])->isSuccess();
363 }
364 Domain::update($site['DOMAIN_ID'], [
365 'ACTIVE' => $setActive ? 'Y' : 'N'
366 ])->isSuccess();
367 }
369 }
370
375 public static function getFreeDomainSuspendedTime(): int
376 {
377 $tariffTtl = 0;
378 $resetFreeTime = Manager::getOption('reset_to_free_time');
379 if ($resetFreeTime)
380 {
381 $tariffTtl = $resetFreeTime + self::FREE_DOMAIN_GRACE_DAYS * 86400;
382 }
383
384 return $tariffTtl;
385 }
386
391 public static function isExportAllowed(): bool
392 {
393 if (Loader::includeModule('bitrix24'))
394 {
395 return Feature::isFeatureEnabled('landing_allow_export');
396 }
397
398 return true;
399 }
400
405 public static function isTermsFooterShow(): bool
406 {
407 if (Loader::includeModule('bitrix24'))
408 {
409 return !Feature::isFeatureEnabled('landing_hide_terms_footer');
410 }
411
412 return false;
413 }
414
420 public static function isEmailConfirmed(int $siteId): bool
421 {
422 static $checkedSites = [];
423
424 if (array_key_exists($siteId, $checkedSites))
425 {
426 return $checkedSites[$siteId];
427 }
428
429 $checkedSites[$siteId] = true;
430
431 if (Loader::includeModule('bitrix24'))
432 {
433 if (!\CBitrix24::isEmailConfirmed())
434 {
435 $checkedSites[$siteId] = false;
436 }
437 }
438
439 return $checkedSites[$siteId];
440 }
441
447 public static function isPhoneConfirmed(int $siteId): bool
448 {
449 static $checkedSites = [];
450
451 if (array_key_exists($siteId, $checkedSites))
452 {
453 return $checkedSites[$siteId];
454 }
455
456 $checkedSites[$siteId] = true;
457
458 if (Loader::includeModule('bitrix24'))
459 {
460 $checkedSites[$siteId] = (new PhoneVerify('landing_site', $siteId))->isVerified();
461 }
462
463 return $checkedSites[$siteId];
464 }
465}
static getOption($code, $default=null)
Definition manager.php:160
static isFreePublicAllowed()
Definition manager.php:1243
static licenseIsFreeSite(string $type)
Definition manager.php:1233
static isCreatingAllowed(string $code, array $params)
Definition site.php:195
static isEmailConfirmed(int $siteId)
Definition site.php:420
static manageFreeDomains(bool $setActive, int $executeAfterSeconds=0)
Definition site.php:325
static isFreeDomainAllowed(string $code, array $params)
Definition site.php:281
static isPhoneConfirmed(int $siteId)
Definition site.php:447
static setGlobalOff()
Definition rights.php:105
static createFromTimestamp($timestamp)
Definition datetime.php:246