23 private $api = array();
24 private $executer = array();
27 const TOKEN_URL =
"https://oauth.vk.com/access_token";
70 return parent::getInstanceByCode(self::TRADING_PLATFORM_CODE);
84 $filter[
"=ID"] = $exportId;
87 $profiles = ExportProfileTable::getList(array(
'filter' => $filter));
88 while ($profile = $profiles->fetch())
91 'DESCRIPTION' => $profile[
'DESCRIPTION'],
92 'VK_SETTINGS' => $profile[
'VK_SETTINGS'],
93 'EXPORT_SETTINGS' => $profile[
'EXPORT_SETTINGS'],
94 'OAUTH' => $profile[
'OAUTH'],
95 'PROCESS' => $profile[
'PROCESS'],
116 $exportId = isset($settings[
'EXPORT_ID']) ? $settings[
'EXPORT_ID'] : NULL;
117 $settings = $settings[
'SETTINGS'];
119 $settingsToSave = array();
120 if (isset($settings[
"DESCRIPTION"]))
121 $settingsToSave[
"DESCRIPTION"] = $settings[
"DESCRIPTION"];
123 if (isset($settings[
"VK_SETTINGS"]))
124 $settingsToSave[
"VK_SETTINGS"] = $settings[
"VK_SETTINGS"];
126 if (isset($settings[
"EXPORT_SETTINGS"]))
127 $settingsToSave[
"EXPORT_SETTINGS"] = $settings[
"EXPORT_SETTINGS"];
129 if (isset($settings[
"OAUTH"]))
130 $settingsToSave[
"OAUTH"] = $settings[
"OAUTH"];
132 if (isset($settings[
"PROCESS"]))
133 $settingsToSave[
"PROCESS"] = $settings[
"PROCESS"];
137 if ($exportId && array_key_exists($exportId, $settingsExists))
139 $resUpdate = ExportProfileTable::update($exportId, $settingsToSave);
141 return $resUpdate->isSuccess() && $resUpdate->getAffectedRowsCount();
145 $settingsToSave[
"PLATFORM_ID"] = $this->id;
146 $resAdd = ExportProfileTable::add($settingsToSave);
147 if ($resAdd->isSuccess())
148 return $resAdd->getId();
167 return parent::setActive();
179 return parent::unsetActive();
188 RegisterModuleDependences(
'main',
'OnEventLogGetAuditTypes',
'sale',
'\Bitrix\Sale\TradingPlatform\Vk\Vk',
'OnEventLogGetAuditTypes');
191 $tptAddRes = TradingPlatformTable::add(array(
196 "CATALOG_SECTION_TAB_CLASS_NAME" =>
'\Bitrix\Sale\TradingPlatform\Vk\CatalogSectionTabHandler',
197 "CLASS" =>
'\Bitrix\Sale\TradingPlatform\Vk\Vk',
201 if ($tptAddRes->isSuccess())
204 $this->
id = $tptAddRes->getId();
208 $iblockIds = TradingPlatform\Helper::getIblocksIds(
true);
209 foreach ($iblockIds as $iblock)
211 self::createSectionFieldVkImage($iblock[
"IBLOCK_ID"]);
214 return $tptAddRes->isSuccess();
225 $iblockIds = TradingPlatform\Helper::getIblocksIds(
true);
226 foreach ($iblockIds as $iblock)
228 self::deleteSectionFieldVkImage($iblock[
"IBLOCK_ID"]);
248 $resDel = ExportProfileTable::delete($exportId);
252 $vkCategories->deleteAgent();
275 private function createCatalogField($props)
277 $result = array(
'RESULT' =>
true,
'VALUE' =>
'');
280 if ($existPropId = self::checkExistingCatalogField(
282 self::createCodeForSectionFieldVkImage($props[
'IBLOCK_ID'])
286 $result[
'VALUE'] = $existPropId;
292 $ibp = new \CIBlockProperty();
293 $result[
'VALUE'] = (int)$ibp->Add($props);
294 if ($result[
'VALUE'] <= 0)
296 $result[
'RESULT'] =
false;
297 $result[
'VALUE'] = $ibp->LAST_ERROR;
311 private function checkExistingCatalogField($iblockId, $code)
313 $existingProps = \CIBlockProperty::GetList(
316 'IBLOCK_ID' => $iblockId,
321 if ($existPropId = $existingProps->Fetch())
322 return $existPropId[
'ID'];
334 private function deleteSectionFieldVkImage($iblockId)
337 if ($propertyId = self::checkExistingCatalogField($iblockId, self::createCodeForSectionFieldVkImage($iblockId)))
338 return \CIBlockProperty::Delete($propertyId);
351 private function createSectionFieldVkImage($iblockId)
356 'CODE' => self::createCodeForSectionFieldVkImage($iblockId),
357 'IBLOCK_ID' => $iblockId,
359 'PROPERTY_TYPE' =>
'F',
362 'FILE_TYPE' =>
'jpg, jpeg, bmp, gif, png',
365 return self::createCatalogField($properties);
375 private function createCodeForSectionFieldVkImage($iblockId)
377 return 'PHOTOS_FOR_VK_' . $iblockId;
392 if (isset(
$settings[
"VK_SETTINGS"][
"GROUP_ID"]))
394 $groupId =
$settings[
"VK_SETTINGS"][
"GROUP_ID"];
395 $groupId = mb_substr($groupId, 0, 1) ==
'-' ? $groupId :
'-'.$groupId;
412 if (isset(
$settings[
"EXPORT_SETTINGS"][
"AGRESSIVE"]) &&
$settings[
"EXPORT_SETTINGS"][
"AGRESSIVE"])
428 if (!isset($this->api[$exportId]))
429 $this->api[$exportId] =
new Api($this->getAccessToken($exportId), $exportId);
431 return $this->api[$exportId];
443 if (!isset($this->executer[$exportId]))
444 $this->executer[$exportId] =
new Executer($this->
getApi($exportId));
446 return $this->executer[$exportId];
456 private function getAccessToken($exportId)
458 if (!isset($this->accessToken[$exportId]))
460 $settings = $this->getSettings();
462 if (isset($settings[$exportId][
"OAUTH"][
"ACCESS_TOKEN"]))
463 $this->accessToken[$exportId] = $settings[$exportId][
"OAUTH"][
"ACCESS_TOKEN"];
468 return $this->accessToken[$exportId];
482 'PRODUCTS_DELETE_ALL',
500 public function log($level, $type, $itemId, $description)
503 $logLevel = TradingPlatform\Logger::LOG_LEVEL_DEBUG;
508 return $this->
addLogRecord($level, $type, $itemId, $description);
522 (isset(
$settings[
"VK_SETTINGS"][
"GROUP_ID"]) && !empty(
$settings[
"VK_SETTINGS"][
"GROUP_ID"])) &&
524 (isset(
$settings[
"VK_SETTINGS"][
"SECRET"]) && !empty(
$settings[
"VK_SETTINGS"][
"SECRET"])) &&
526 (isset(
$settings[
"EXPORT_SETTINGS"][
"CATEGORY_DEFAULT"]) &&
$settings[
"EXPORT_SETTINGS"][
"CATEGORY_DEFAULT"] > 0)
528 $this->setActiveById($exportId);
541 private function setActiveById($exportId)
543 $settings = $this->getSettings($exportId);
544 if (isset($settings) && is_array($settings))
545 $settings[
"EXPORT_SETTINGS"][
"ACTIVE"] =
"Y";
548 if (!parent::isActive())
551 return $this->saveSettings(array(
'SETTINGS' => $settings,
'EXPORT_ID' => $exportId));
565 $settings[$exportId][
"EXPORT_SETTINGS"][
"ACTIVE"] =
"N";
597 if (isset(
$settings[
"EXPORT_SETTINGS"][
"ACTIVE"]) &&
$settings[
"EXPORT_SETTINGS"][
"ACTIVE"] ==
"Y")
614 $urlParams = array();
616 if (isset(
$settings[$exportId][
"VK_SETTINGS"][
"APP_ID"]) && !empty(
$settings[$exportId][
"VK_SETTINGS"][
"APP_ID"]))
617 $urlParams[
'client_id'] =
$settings[$exportId][
"VK_SETTINGS"][
"APP_ID"];
620 if (!empty($redirectUrl))
621 $urlParams[
'redirect_uri'] = self::formatRedirectUrl($redirectUrl);
624 $urlParams[
"display"] =
"page";
625 $urlParams[
"scope"] = self::getScope(array(
"market",
"photos",
"offline",
"wall",
"docs",
"groups"));
626 $urlParams[
"response_type"] =
"code";
627 $urlParams[
"v"] = Api::$apiVersion;
629 return self::OAUTH_URL .
"?" . http_build_query($urlParams);
644 $urlParams = array();
646 if (isset(
$settings[
"VK_SETTINGS"][
"APP_ID"]) && !empty(
$settings[
"VK_SETTINGS"][
"APP_ID"]))
647 $urlParams[
'client_id'] =
$settings[
"VK_SETTINGS"][
"APP_ID"];
650 if (isset(
$settings[
"VK_SETTINGS"][
"SECRET"]) && !empty(
$settings[
"VK_SETTINGS"][
"SECRET"]))
651 $urlParams[
'client_secret'] =
$settings[
"VK_SETTINGS"][
"SECRET"];
654 if (!empty($redirectUrl))
655 $urlParams[
'redirect_uri'] = self::formatRedirectUrl($redirectUrl);
659 $urlParams[
'code'] =
$code;
662 return self::TOKEN_URL .
"?" . http_build_query($urlParams);
672 private static function formatRedirectUrl($redirectUrl)
674 $protocol = \CMain::IsHTTPS() ?
"https://" :
"http://";
675 $redirectUrl = $protocol . urldecode($redirectUrl);
705 "notifications" => 524288,
708 "market" => 134217728,
713 foreach ($params as $param)
715 if (isset($scopes[$param]))
716 $scope += $scopes[$param];
731 if (isset(
$settings[
"EXPORT_SETTINGS"][
"TIMELIMIT"]))
732 return $settings[
"EXPORT_SETTINGS"][
"TIMELIMIT"];
747 if (isset(
$settings[
"EXPORT_SETTINGS"][
"COUNT_ITEMS"]))
748 return $settings[
"EXPORT_SETTINGS"][
"COUNT_ITEMS"];
764 if (isset(
$settings[
"EXPORT_SETTINGS"][
"RICH_LOG"]) &&
$settings[
"EXPORT_SETTINGS"][
"RICH_LOG"])
775 if (isset(
$settings[
"EXPORT_SETTINGS"][
"ONLY_AVAILABLE_FLAG"]) && !
$settings[
"EXPORT_SETTINGS"][
"ONLY_AVAILABLE_FLAG"])
791 $exportIds = array();
792 $resExports = ExportProfileTable::getList(array(
793 'filter' => array(
'PLATFORM_ID' => $this->
getId()),
794 'select' => array(
'ID',
'DESCRIPTION',
'EXPORT_SETTINGS'),
798 while ($export = $resExports->fetch())
800 if ($onlyActive && $export[
"EXPORT_SETTINGS"][
"ACTIVE"] !=
"Y")
803 $exportIds[$export[
"ID"]] = array(
804 'ID' => $export[
"ID"],
805 'DESC' => $export[
'DESCRIPTION'],
824 "VK_PROCESS__TIMELIMIT" =>
Loc::getMessage(
"SALE_VK_PROCESS__TIMELIMIT"),
829 "VK_FEED__FEED_TIMELIMIT" =>
Loc::getMessage(
"SALE_VK_FEED_TIMELIMIT"),
831 "VK_FEED__FEED_FINISH_OK" =>
Loc::getMessage(
"SALE_VK_FEED_FINISH_OK"),
832 "VK_FEED__FEED_ALBUM_PART_FINISH" =>
Loc::getMessage(
"SALE_VK_FEED_ALBUM_PART_FINISH"),
835 array_walk($result,
function (&$value, $key, $prefix)
837 $value = $prefix . $value;
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)