12use Bitrix\Market\Subscription;
15use Bitrix\Bitrix24\Feature;
17if(!defined(
'REST_MP_CATEGORIES_CACHE_TTL'))
19 define(
'REST_MP_CATEGORIES_CACHE_TTL', 86400);
25 private const SUBSCRIPTION_REGION = [
30 private const SUBSCRIPTION_DEFAULT_START_TIME = [
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 'ua' =>
'https://marketplace.1c-bitrix.ua/tobasket.php?ID=#CODE#&limit=#NUM#&b24=y',
43 private static $appTop =
null;
44 private static $isPayApplicationAvailable;
46 public static function getTop($action, $fields = array())
48 $allowedActions = array(
55 if(in_array($action, $allowedActions))
57 if(!is_array(self::$appTop))
60 foreach($allowedActions as $method)
62 $batch[$method] = array($method, $fields);
68 return self::$appTop[$action];
76 public static function getBuy($codeList)
81 "code" => implode(
",", $codeList)
98 "code" => serialize($codeList)
107 if(!is_array($updateList) || count($updateList) <= 0)
114 $cnt = count($updateList);
115 $optionValue = array();
117 foreach($updateList as $update)
119 if(is_array($update[
'VERSIONS']) && count($update[
'VERSIONS']) > 0)
121 $optionValue[$update[
"CODE"]] = max(array_keys($update[
"VERSIONS"]));
125 $optionValue = serialize($optionValue);
128 Option::set(
"rest",
"mp_num_updates", $cnt);
129 Option::set(
"rest",
"mp_updates", $optionValue);
134 $updates = Option::get(
"rest",
"mp_updates",
"");
135 $updates = $updates ==
"" ? array() : unserialize($updates, [
'allowed_classes' =>
false]);
139 return array_key_exists($code, $updates) ? $updates[$code] :
false;
149 return intval(Option::get(
"rest",
"mp_num_updates", 0));
162 $cacheId =
'rest|marketplace|categories|full|'.LANGUAGE_ID;
164 $requestNeeded =
true;
167 $forceReload ===
false
168 && static::CATEGORIES_CACHE_TTL > 0
169 && $managedCache->read(static::CATEGORIES_CACHE_TTL, $cacheId)
172 $result = $managedCache->get($cacheId);
173 if (is_array($result))
175 $requestNeeded =
false;
177 elseif (intval($result) > time())
179 $requestNeeded =
false;
187 if (!is_array($result))
189 $result = time() + 300;
192 if (static::CATEGORIES_CACHE_TTL > 0)
194 $managedCache->set($cacheId, $result);
198 return (is_array($result) ? $result : []);
209 $categories = static::getCategoriesFull($forceReload);
210 return (is_array($categories[
'ITEMS']) ? $categories[
'ITEMS'] : []);
213 public static function getCategory($code, $page =
false, $pageSize =
false)
215 $queryFields = Array(
218 $page = intval($page);
219 $pageSize = intval($pageSize);
222 $queryFields[
"page"] = $page;
226 $queryFields[
"onPageSize"] = $pageSize;
235 public static function getByTag($tag, $page =
false, $pageSize =
false)
237 $queryFields = Array(
240 $page = intval($page);
243 $queryFields[
"page"] = $page;
248 $queryFields[
"onPageSize"] = $pageSize;
257 public static function getLastByTag($tag, $page =
false, $pageSize =
false)
259 $queryFields = Array(
261 "sort" =>
"date_public"
264 $page = intval($page);
267 $queryFields[
"page"] = $page;
272 $queryFields[
"onPageSize"] = $pageSize;
278 public static function getApp($code, $version =
false, $checkHash =
false, $installHash =
false)
280 $queryFields = Array(
284 $version = intval($version);
287 $queryFields[
"ver"] = $version;
290 if($checkHash !==
false)
292 $queryFields[
"check_hash"] = $checkHash;
293 $queryFields[
"install_hash"] = $installHash;
329 $query[
'onPageSize'] = (int)($query[
'pageSize'] ?? 50);
330 $query[
'page'] = (int)($query[
'page'] ?? 1);
338 public static function getAppPublic($code, $version =
false, $checkHash =
false, $installHash =
false)
344 $version = intval($version);
347 $queryFields[
"ver"] = $version;
350 if($checkHash !==
false)
352 $queryFields[
"check_hash"] = $checkHash;
353 $queryFields[
"install_hash"] = $installHash;
362 public static function filterApp($fields, $page =
false)
364 if (!is_array($fields))
365 $fields = array($fields);
367 $queryFields = $fields;
369 $page = intval($page);
372 $queryFields[
"page"] = $page;
385 $queryFields = Array(
389 $page = intval($page);
392 $queryFields[
"page"] = $page;
401 public static function getInstall($code, $version =
false, $checkHash =
false, $installHash =
false)
403 $queryFields = Array(
406 "member_id" => \CRestUtil::getMemberId(),
409 $version = intval($version);
412 $queryFields[
"ver"] = $version;
415 if($checkHash !==
false)
417 $queryFields[
"check_hash"] = $checkHash;
418 $queryFields[
"install_hash"] = $installHash;
426 $linkTpl = static::$buyLinkList[
'en'];
430 $linkTpl = static::$buyLinkList[
'bitrix24'];
434 if(array_key_exists(LANGUAGE_ID, static::$buyLinkList))
436 $linkTpl = static::$buyLinkList[LANGUAGE_ID];
445 array(
'#NUM#',
'#CODE#'),
446 array(intval($num), urlencode($appCode)),
454 $dbApps = AppTable::getList(array(
459 'select' => array(
'CODE',
'VERSION')
461 while($app = $dbApps->fetch())
463 $appCodes[$app[
"CODE"]] = $app[
"VERSION"];
466 if(!empty($appCodes))
468 $updateList = static::getUpdates($appCodes);
470 if (is_array($updateList) && isset($updateList[
'ITEMS']))
480 return __CLASS__.
"::getNumUpdates();";
488 if(mb_substr($placement, 0, 4) ===
'CRM_' || $placement === \
Bitrix\Rest\Api\UserFieldType::PLACEMENT_UF_TYPE)
490 if($placement !==
'CRM_ROBOT_TRIGGERS')
492 $tag[] =
'placement';
496 $tag[] =
'automation';
501 elseif(mb_substr($placement, 0, 5) ===
'CALL_')
503 $tag[] =
'placement';
504 $tag[] =
'telephony';
520 $status = Option::get(
'bitrix24',
'~mp24_paid',
'N');
524 $status = Option::get(
'main',
'~mp24_paid',
'N');
525 if ($status ===
'T' && Option::get(
'main',
'~mp24_used_trial',
'N') !==
'Y')
527 Option::set(
'main',
'~mp24_used_trial',
'Y');
531 $result = ($status ===
'Y' || $status ===
'T');
536 && Loader::includeModule(
'bitrix24')
537 && \CBitrix24::getLicenseFamily() ===
'project'
538 && Option::get(
'rest',
'can_use_subscription_project',
'N') ===
'N'
545 $date = static::getSubscriptionFinalDate();
548 $now = new \Bitrix\Main\Type\Date();
563 return Option::get(
'bitrix24',
'~mp24_paid',
'N') ===
'T'
564 && Option::get(
'bitrix24',
'~mp24_used_trial',
'N') ===
'Y';
576 $timestamp = (int)Option::get(
'bitrix24',
'~mp24_paid_date');
580 $timestamp = (int)Option::get(
'main',
'~mp24_paid_date');
585 $result = Date::createFromTimestamp($timestamp);
600 $status = Option::get(
'bitrix24',
'~mp24_paid',
'N');
604 $status = Option::get(
'main',
'~mp24_paid',
'N');
607 return $status ===
'T';
610 private static function checkSubscriptionAccessStart($region): bool
613 if (!empty(static::SUBSCRIPTION_DEFAULT_START_TIME[$region]))
617 'subscription_region_start_time_' . $region,
618 static::SUBSCRIPTION_DEFAULT_START_TIME[$region]
620 $canStart = $time < time();
623 return $canStart && in_array($region, static::SUBSCRIPTION_REGION,
true);
630 $result = static::checkSubscriptionAccessStart(\CBitrix24::getLicensePrefix());
644 static::isSubscriptionAccess()
648 && Loader::includeModule(
'bitrix24')
649 && !Feature::isFeatureEnabled(
'rest_can_buy_subscription')
663 $used = Option::get(
'bitrix24',
'~mp24_used_trial',
'N') ===
'Y';
667 $used = Option::get(
'main',
'~mp24_used_trial',
'N') ===
'Y';
670 return !$used && static::isSubscriptionAccess();
679 if (is_null(static::$isPayApplicationAvailable))
681 static::$isPayApplicationAvailable =
true;
682 $time = (int) Option::get(
'rest',
'time_pay_application_off', 1621029600);
685 if (Loader::includeModule(
'bitrix24'))
687 $region = \CBitrix24::getLicensePrefix();
691 $region = Option::get(
'main',
'~PARAM_CLIENT_LANG',
'');
694 if ($region ===
'ru')
696 static::$isPayApplicationAvailable =
false;
701 return static::$isPayApplicationAvailable;
709 if (static::isSubscriptionAvailable())
711 if (self::isStartDemoSubscription())
713 $eventDemo =
new Event(
715 'onSubscriptionIsDemo',
723 'onSubscriptionRenew',
726 EventManager::getInstance()->send($event);
static getDefaultLang($lang)
static isModuleInstalled($moduleName)
static isFeatureEnabled()
const OPTION_SUBSCRIPTION_AVAILABLE
static searchApp($q, $page=false)
static getTop($action, $fields=array())
static getAvailableUpdate($code=false)
static setAvailableUpdate($updateList=array())
static getBuyLink($num, $appCode)
static getCategories($forceReload=false)
static getUpdates($codeList)
static getCategory($code, $page=false, $pageSize=false)
static canBuySubscription()
static isSubscriptionDemoAvailable()
static getApp($code, $version=false, $checkHash=false, $installHash=false)
static isStartDemoSubscription()
static getByTag($tag, $page=false, $pageSize=false)
static getAppPublic($code, $version=false, $checkHash=false, $installHash=false)
static filterApp($fields, $page=false)
static getTagByPlacement($placement)
static isSubscriptionDemo()
static isSubscriptionAvailable()
static getAvailableUpdateNum()
static getSubscriptionFinalDate()
static isPayApplicationAvailable()
static onChangeSubscriptionDate(Event $event)
static getSiteList(array $query=[])
static getInstall($code, $version=false, $checkHash=false, $installHash=false)
const CATEGORIES_CACHE_TTL
static isSubscriptionAccess()
static getLastByTag($tag, $page=false, $pageSize=false)
static getCategoriesFull($forceReload=false)
const METHOD_GET_SITE_LIST
const METHOD_GET_SALE_OUT
const METHOD_GET_CATEGORY
const METHOD_GET_APP_PUBLIC
const METHOD_GET_SITE_ITEM
const METHOD_GET_CATEGORIES