1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
client.php
См. документацию.
1<?php
2namespace Bitrix\Rest\Marketplace;
3
4use Bitrix\Main\Application;
5use Bitrix\Main\Config\Option;
6use Bitrix\Main\Event;
7use Bitrix\Main\EventManager;
8use Bitrix\Main\Loader;
9use Bitrix\Main\Localization\Loc;
10use Bitrix\Main\ModuleManager;
11use Bitrix\Main\SystemException;
12use Bitrix\Main\Type\Date;
13use Bitrix\Rest\AppTable;
14use Bitrix\Rest\Engine\Access;
15use Bitrix\Bitrix24\Feature;
16
17if(!defined('REST_MP_CATEGORIES_CACHE_TTL'))
18{
19 define('REST_MP_CATEGORIES_CACHE_TTL', 86400);
20}
21
22class Client
23{
24 const CATEGORIES_CACHE_TTL = REST_MP_CATEGORIES_CACHE_TTL;
25 private const SUBSCRIPTION_REGION = [
26 'ru',
27 'ua',
28 'by',
29 ];
30 private const SUBSCRIPTION_DEFAULT_START_TIME = [
31 'ua' => 1625090400,
32 'by' => 1660514400,
33 ];
34
35 protected static $buyLinkList = array(
36 'bitrix24' => '/settings/order/make.php?limit=#NUM#&module=#CODE#',
37 'ru' => 'https://marketplace.1c-bitrix.ru/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
38 'en' => 'https://store.bitrix24.com/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
39 'de' => 'https://store.bitrix24.de/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
40 );
41
42 private static $appTop = null;
43 private static $isPayApplicationAvailable;
44
45 public static function getTop($action, $fields = array())
46 {
47 $allowedActions = array(
52 );
53
54 if(in_array($action, $allowedActions))
55 {
56 if(!is_array(self::$appTop))
57 {
58 $batch = array();
59 foreach($allowedActions as $method)
60 {
61 $batch[$method] = array($method, $fields);
62 }
63
64 self::$appTop = Transport::instance()->batch($batch);
65 }
66
67 return self::$appTop[$action];
68 }
69 else
70 {
71 return Transport::instance()->call($action);
72 }
73 }
74
75 public static function getBuy($codeList)
76 {
77 return Transport::instance()->call(
79 array(
80 "code" => implode(",", $codeList)
81 )
82 );
83 }
84
85 public static function getImmuneApp(): array
86 {
87 $immuneAppList = Transport::instance()->getDictionary(
89 );
90
91 if (!is_array($immuneAppList))
92 {
93 if ($immuneAppList !== false) {
95 'message' => 'Wrong answer from service',
96 'data' => print_r($immuneAppList, true),
97 ], 'rest');
98 }
99 return [];
100 }
101
102 return $immuneAppList;
103 }
104
105 public static function getUpdates($codeList)
106 {
107 $updatesList = Transport::instance()->call(
109 array(
110 "code" => serialize($codeList)
111 )
112 );
113
114 return $updatesList;
115 }
116
117 public static function setAvailableUpdate($updateList = array())
118 {
119 if(!is_array($updateList) || count($updateList) <= 0)
120 {
121 $cnt = 0;
122 $optionValue = "";
123 }
124 else
125 {
126 $cnt = count($updateList);
128
129 foreach($updateList as $update)
130 {
131 if(is_array($update['VERSIONS']) && count($update['VERSIONS']) > 0)
132 {
133 $optionValue[$update["CODE"]] = max(array_keys($update["VERSIONS"]));
134 }
135 }
136
137 $optionValue = serialize($optionValue);
138 }
139
140 Option::set("rest", "mp_num_updates", $cnt);
141 Option::set("rest", "mp_updates", $optionValue);
142 }
143
144 public static function getAvailableUpdate($code = false)
145 {
146 $updates = Option::get("rest", "mp_updates", "");
147 $updates = $updates == "" ? array() : unserialize($updates, ['allowed_classes' => false]);
148
149 if($code !== false)
150 {
151 return array_key_exists($code, $updates) ? $updates[$code] : false;
152 }
153 else
154 {
155 return $updates;
156 }
157 }
158
159 public static function getAvailableUpdateNum()
160 {
161 return intval(Option::get("rest", "mp_num_updates", 0));
162 }
163
170 public static function getCategoriesFull($forceReload = false)
171 {
172 $managedCache = Application::getInstance()->getManagedCache();
173
174 $cacheId = 'rest|marketplace|categories|full|'.LANGUAGE_ID;
175
176 $requestNeeded = true;
177
178 if (
179 $forceReload === false
180 && static::CATEGORIES_CACHE_TTL > 0
181 && $managedCache->read(static::CATEGORIES_CACHE_TTL, $cacheId)
182 )
183 {
184 $result = $managedCache->get($cacheId);
185 if (is_array($result))
186 {
187 $requestNeeded = false;
188 }
189 elseif (intval($result) > time())
190 {
191 $requestNeeded = false;
192 $result = [];
193 }
194 }
195
196 if ($requestNeeded)
197 {
199 if (!is_array($result))
200 {
201 $result = time() + 300;
202 }
203
204 if (static::CATEGORIES_CACHE_TTL > 0)
205 {
206 $managedCache->set($cacheId, $result);
207 }
208 }
209
210 return (is_array($result) ? $result : []);
211 }
212
219 public static function getCategories($forceReload = false)
220 {
221 $categories = static::getCategoriesFull($forceReload);
222 return (is_array($categories['ITEMS']) ? $categories['ITEMS'] : []);
223 }
224
225 public static function getCategory($code, $page = false, $pageSize = false, bool $isMarket = false)
226 {
227 $queryFields = [];
228 $page = (int)$page;
229 $pageSize = (int)$pageSize;
230
231 if ($page > 0)
232 {
233 $queryFields["page"] = $page;
234 }
235 if ($pageSize > 0)
236 {
237 $queryFields["onPageSize"] = $pageSize;
238 }
239
240 if ($isMarket)
241 {
242 $queryFields['_market_'] = 'Y';
243 if (is_string($code))
244 {
245 $queryFields['category'] = $code;
246 }
247 }
248 else
249 {
250 $queryFields['code'] = $code;
251 }
252
253 return Transport::instance()->call(
255 $queryFields
256 );
257 }
258
259 public static function getByTag($tag, $page = false, $pageSize = false)
260 {
261 $queryFields = Array(
262 "tag" => $tag
263 );
264 $page = intval($page);
265 if($page > 0)
266 {
267 $queryFields["page"] = $page;
268 }
269
270 if($pageSize > 0)
271 {
272 $queryFields["onPageSize"] = $pageSize;
273 }
274
275 return Transport::instance()->call(
277 $queryFields
278 );
279 }
280
281 public static function getLastByTag($tag, $page = false, $pageSize = false)
282 {
283 $queryFields = Array(
284 "tag" => $tag,
285 "sort" => "date_public"
286 );
287
288 $page = intval($page);
289 if($page > 0)
290 {
291 $queryFields["page"] = $page;
292 }
293
294 if($pageSize > 0)
295 {
296 $queryFields["onPageSize"] = $pageSize;
297 }
298
299 return Transport::instance()->call(Transport::METHOD_GET_TAG, $queryFields);
300 }
301
302 public static function getApp($code, $version = false, $checkHash = false, $installHash = false)
303 {
304 $queryFields = Array(
305 "code" => $code
306 );
307
308 $version = intval($version);
309 if($version > 0)
310 {
311 $queryFields["ver"] = $version;
312 }
313
314 if($checkHash !== false)
315 {
316 $queryFields["check_hash"] = $checkHash;
317 $queryFields["install_hash"] = $installHash;
318 }
319
320 return Transport::instance()->call(
322 $queryFields
323 );
324 }
325
332 public static function getSite($id)
333 {
334 $query = [
335 'site_id' => $id
336 ];
337
338 return Transport::instance()->call(
340 $query
341 );
342 }
343
351 public static function getSiteList(array $query = [])
352 {
353 $query['onPageSize'] = (int)($query['pageSize'] ?? 50);
354 $query['page'] = (int)($query['page'] ?? 1);
355
356 return Transport::instance()->call(
358 $query
359 );
360 }
361
362 public static function getAppPublic($code, $version = false, $checkHash = false, $installHash = false)
363 {
364 $queryFields = [
365 "code" => $code
366 ];
367
368 $version = intval($version);
369 if($version > 0)
370 {
371 $queryFields["ver"] = $version;
372 }
373
374 if($checkHash !== false)
375 {
376 $queryFields["check_hash"] = $checkHash;
377 $queryFields["install_hash"] = $installHash;
378 }
379
380 return Transport::instance()->call(
382 $queryFields
383 );
384 }
385
386 public static function filterApp($fields, $page = false)
387 {
388 if (!is_array($fields))
390
391 $queryFields = $fields;
392
393 $page = intval($page);
394 if($page > 0)
395 {
396 $queryFields["page"] = $page;
397 }
398
399 return Transport::instance()->call(
401 $queryFields
402 );
403 }
404
405 public static function searchApp($q, $page = false)
406 {
407 $q = trim($q);
408
409 $queryFields = Array(
410 "q" => $q
411 );
412
413 $page = intval($page);
414 if($page > 0)
415 {
416 $queryFields["page"] = $page;
417 }
418
419 return Transport::instance()->call(
421 $queryFields
422 );
423 }
424
425 public static function getInstall($code, $version = false, $checkHash = false, $installHash = false)
426 {
427 $queryFields = Array(
428 "code" => $code,
429 "encode_key" => "Y",
430 "member_id" => \CRestUtil::getMemberId(),
431 );
432
433 $version = intval($version);
434 if($version > 0)
435 {
436 $queryFields["ver"] = $version;
437 }
438
439 if($checkHash !== false)
440 {
441 $queryFields["check_hash"] = $checkHash;
442 $queryFields["install_hash"] = $installHash;
443 }
444
445 return Transport::instance()->call(Transport::METHOD_GET_INSTALL, $queryFields);
446 }
447
448 public static function getBuyLink($num, $appCode)
449 {
450 $linkTpl = static::$buyLinkList['en'];
451
453 {
454 $linkTpl = static::$buyLinkList['bitrix24'];
455 }
456 else
457 {
458 if(array_key_exists(LANGUAGE_ID, static::$buyLinkList))
459 {
460 $linkTpl = static::$buyLinkList[LANGUAGE_ID];
461 }
462 elseif(array_key_exists(Loc::getDefaultLang(LANGUAGE_ID), static::$buyLinkList))
463 {
464 $linkTpl = static::$buyLinkList[Loc::getDefaultLang(LANGUAGE_ID)];
465 }
466 }
467
468 return str_replace(
469 array('#NUM#', '#CODE#'),
470 array(intval($num), urlencode($appCode)),
471 $linkTpl
472 );
473 }
474
475 public static function getNumUpdates()
476 {
477 $appCodes = array();
478 $dbApps = AppTable::getList(array(
479 'filter' => array(
480 "=ACTIVE" => AppTable::ACTIVE,
481 "!=STATUS" => AppTable::STATUS_LOCAL,
482 ),
483 'select' => array('CODE', 'VERSION')
484 ));
485 while($app = $dbApps->fetch())
486 {
487 $appCodes[$app["CODE"]] = $app["VERSION"];
488 }
489
490 if(!empty($appCodes))
491 {
492 $updateList = static::getUpdates($appCodes);
493
494 if (is_array($updateList) && isset($updateList['ITEMS']))
495 {
496 self::setAvailableUpdate($updateList['ITEMS']);
497 }
498 else
499 {
501 }
502 }
503
504 return __CLASS__."::getNumUpdates();";
505 }
506
507 public static function getTagByPlacement($placement)
508 {
509 $tag = array();
510 if($placement <> '')
511 {
512 if(mb_substr($placement, 0, 4) === 'CRM_' || $placement === \Bitrix\Rest\Api\UserFieldType::PLACEMENT_UF_TYPE)
513 {
514 if($placement !== 'CRM_ROBOT_TRIGGERS')
515 {
516 $tag[] = 'placement';
517 }
518 else
519 {
520 $tag[] = 'automation';
521 }
522
523 $tag[] = 'crm';
524 }
525 elseif(mb_substr($placement, 0, 5) === 'CALL_')
526 {
527 $tag[] = 'placement';
528 $tag[] = 'telephony';
529 }
530 }
531
532 $tag[] = $placement;
533
534 return $tag;
535 }
536
540 public static function isSubscriptionAvailable()
541 {
542 if (ModuleManager::isModuleInstalled('bitrix24'))
543 {
544 $status = Option::get('bitrix24', '~mp24_paid', 'N');
545 }
546 else
547 {
548 $status = Option::get('main', '~mp24_paid', 'N');
549 if ($status === 'T' && Option::get('main', '~mp24_used_trial', 'N') !== 'Y')
550 {
551 Option::set('main', '~mp24_used_trial', 'Y');
552 }
553 }
554
555 $result = ($status === 'Y' || $status === 'T');
556
557 if (
558 $status === 'Y'
560 && Loader::includeModule('bitrix24')
561 && \CBitrix24::getLicenseFamily() === 'project'
562 && Option::get('rest', 'can_use_subscription_project', 'N') === 'N'
563 )
564 {
565 $result = false;
566 }
568 {
569 $date = static::getSubscriptionFinalDate();
570 if ($date)
571 {
572 $now = new \Bitrix\Main\Type\Date();
573 if ($date < $now)
574 {
575 $result = false;
576 }
577 }
578 }
579
580 return $result;
581 }
582
583 public static function isStartDemoSubscription(): bool
584 {
585 if (ModuleManager::isModuleInstalled('bitrix24'))
586 {
587 return Option::get('bitrix24', '~mp24_paid', 'N') === 'T'
588 && Option::get('bitrix24', '~mp24_used_trial', 'N') === 'Y';
589 }
590
591 return false;
592 }
593
594 public static function getSubscriptionFinalDate(): ?Date
595 {
596 $result = null;
597
598 if (ModuleManager::isModuleInstalled('bitrix24'))
599 {
600 $timestamp = (int)Option::get('bitrix24', '~mp24_paid_date');
601 }
602 else
603 {
604 $timestamp = (int)Option::get('main', '~mp24_paid_date');
605 }
606
607 if ($timestamp > 0)
608 {
609 $result = Date::createFromTimestamp($timestamp);
610 }
611
612 return $result;
613 }
614
620 public static function isSubscriptionDemo(): bool
621 {
622 if (ModuleManager::isModuleInstalled('bitrix24'))
623 {
624 $status = Option::get('bitrix24', '~mp24_paid', 'N');
625 }
626 else
627 {
628 $status = Option::get('main', '~mp24_paid', 'N');
629 }
630
631 return $status === 'T';
632 }
633
634 private static function checkSubscriptionAccessStart($region): bool
635 {
636 $canStart = true;
637 if (!empty(static::SUBSCRIPTION_DEFAULT_START_TIME[$region]))
638 {
639 $time = Option::get(
640 'rest',
641 'subscription_region_start_time_' . $region,
642 static::SUBSCRIPTION_DEFAULT_START_TIME[$region]
643 );
644 $canStart = $time < time();
645 }
646
647 return $canStart && in_array($region, static::SUBSCRIPTION_REGION, true);
648 }
649
650 public static function isSubscriptionAccess()
651 {
652 if (ModuleManager::isModuleInstalled('bitrix24') && Loader::includeModule('bitrix24'))
653 {
654 $result = static::checkSubscriptionAccessStart(\CBitrix24::getLicensePrefix());
655 }
656 else
657 {
658 $result = Option::get(Access::MODULE_ID, Access::OPTION_SUBSCRIPTION_AVAILABLE, 'N') === 'Y';
659 }
660
661 return $result;
662 }
663
664 public static function canBuySubscription()
665 {
666 $result = false;
667 if (
668 static::isSubscriptionAccess()
669 && Access::isFeatureEnabled()
670 && !(
672 && Loader::includeModule('bitrix24')
673 && !Feature::isFeatureEnabled('rest_can_buy_subscription')
674 )
675 )
676 {
677 $result = true;
678 }
679
680 return $result;
681 }
682
683 public static function isSubscriptionDemoAvailable()
684 {
685 if (ModuleManager::isModuleInstalled('bitrix24'))
686 {
687 $used = Option::get('bitrix24', '~mp24_used_trial', 'N') === 'Y';
688 }
689 else
690 {
691 $used = Option::get('main', '~mp24_used_trial', 'N') === 'Y';
692 }
693
694 return !$used && static::isSubscriptionAccess();
695 }
696
701 public static function isPayApplicationAvailable() : bool
702 {
703 if (is_null(static::$isPayApplicationAvailable))
704 {
705 static::$isPayApplicationAvailable = true;
706 $time = (int) Option::get('rest', 'time_pay_application_off', 1621029600);
707 if (time() > $time)
708 {
709 if (Loader::includeModule('bitrix24'))
710 {
711 $region = \CBitrix24::getLicensePrefix();
712 }
713 else
714 {
715 $region = Option::get('main', '~PARAM_CLIENT_LANG', '');
716 }
717
718 if ($region === 'ru')
719 {
720 static::$isPayApplicationAvailable = false;
721 }
722 }
723 }
724
725 return static::$isPayApplicationAvailable;
726 }
727
731 public static function onChangeSubscriptionDate(Event $event): void
732 {
733 if (static::isSubscriptionAvailable())
734 {
735 $event = new Event(
736 'rest',
737 'onSubscriptionRenew',
738 );
739
740 EventManager::getInstance()->send($event);
741 }
742 }
743}
Определения event.php:5
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
Определения date.php:9
const STATUS_LOCAL
Определения app.php:84
const ACTIVE
Определения app.php:69
static searchApp($q, $page=false)
Определения client.php:405
static getNumUpdates()
Определения client.php:475
static getTop($action, $fields=array())
Определения client.php:45
static getImmuneApp()
Определения client.php:85
static getAvailableUpdate($code=false)
Определения client.php:144
static setAvailableUpdate($updateList=array())
Определения client.php:117
static getBuyLink($num, $appCode)
Определения client.php:448
static getCategories($forceReload=false)
Определения client.php:219
static getUpdates($codeList)
Определения client.php:105
static canBuySubscription()
Определения client.php:664
static isSubscriptionDemoAvailable()
Определения client.php:683
static getBuy($codeList)
Определения client.php:75
static getApp($code, $version=false, $checkHash=false, $installHash=false)
Определения client.php:302
static getCategory($code, $page=false, $pageSize=false, bool $isMarket=false)
Определения client.php:225
static isStartDemoSubscription()
Определения client.php:583
static getByTag($tag, $page=false, $pageSize=false)
Определения client.php:259
static getAppPublic($code, $version=false, $checkHash=false, $installHash=false)
Определения client.php:362
static filterApp($fields, $page=false)
Определения client.php:386
static getTagByPlacement($placement)
Определения client.php:507
static getSite($id)
Определения client.php:332
static isSubscriptionDemo()
Определения client.php:620
static $buyLinkList
Определения client.php:35
static isSubscriptionAvailable()
Определения client.php:540
static getAvailableUpdateNum()
Определения client.php:159
static getSubscriptionFinalDate()
Определения client.php:594
static isPayApplicationAvailable()
Определения client.php:701
static onChangeSubscriptionDate(Event $event)
Определения client.php:731
static getSiteList(array $query=[])
Определения client.php:351
static getInstall($code, $version=false, $checkHash=false, $installHash=false)
Определения client.php:425
const CATEGORIES_CACHE_TTL
Определения client.php:24
static isSubscriptionAccess()
Определения client.php:650
static getLastByTag($tag, $page=false, $pageSize=false)
Определения client.php:281
static getCategoriesFull($forceReload=false)
Определения client.php:170
const METHOD_GET_BUY
Определения transport.php:55
static instance()
Определения transport.php:87
const METHOD_GET_INSTALL
Определения transport.php:67
const METHOD_GET_LAST
Определения transport.php:51
const METHOD_GET_DEV
Определения transport.php:52
const DICTIONARY_IMMUNE_LIST
Определения transport.php:74
const METHOD_SEARCH_APP
Определения transport.php:69
const METHOD_GET_APP
Определения transport.php:65
const METHOD_GET_SITE_LIST
Определения transport.php:71
const METHOD_GET_BEST
Определения transport.php:53
const METHOD_GET_SALE_OUT
Определения transport.php:54
const METHOD_FILTER_APP
Определения transport.php:70
const METHOD_GET_CATEGORY
Определения transport.php:63
const METHOD_GET_APP_PUBLIC
Определения transport.php:66
const METHOD_GET_UPDATES
Определения transport.php:56
const METHOD_GET_SITE_ITEM
Определения transport.php:72
const METHOD_GET_TAG
Определения transport.php:64
const METHOD_GET_CATEGORIES
Определения transport.php:62
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$region
Определения .description.php:13
$app
Определения proxy.php:8
$pageSize
Определения csv_new_run.php:34
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$status
Определения session.php:10
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)
Определения tools.php:3941
Определения handlers.php:8
Определения event.php:2
$time
Определения payment.php:61
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$page
Определения order_form.php:33
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$optionValue
Определения options.php:3512
$method
Определения index.php:27
$action
Определения file_dialog.php:21
$fields
Определения yandex_run.php:501