21 private int $exportId;
29 $this->exportId = $exportId;
41 if (!$agent = $this->getAgentId())
44 $timeToStart = ConvertTimeStamp(strtotime(date(
'Y-m-d H:i:s', time() + $ttl)),
'FULL');
46 $resultAgentAdd = \CAgent::AddAgent(
47 self::createAgentName($this->exportId),
56 return $resultAgentAdd;
71 private function getAgentId()
73 $dbRes = \CAgent::GetList(
76 'NAME' => self::createAgentName($this->exportId),
80 if ($agent = $dbRes->Fetch())
95 $dbRes = \CAgent::GetList(
98 'NAME' => self::createAgentName($this->exportId),
102 if ($agent = $dbRes->Fetch())
103 \CAgent::Delete($agent[
"ID"]);
113 $settings = $vk->getSettings();
115 foreach ($settings as $id => $value)
126 private static function createCacheId()
137 private static function createAgentName(
int $exportId): string
139 return 'Bitrix\Sale\TradingPlatform\Vk\VkCategories::updateVkCategoriesAgent(' . $exportId .
');';
154 if ($cacheManager->read(self::CACHE_TTL, self::createCacheId()))
156 $result = $cacheManager->get(self::createCacheId());
160 $result = self::updateDataToCache($this->exportId);
165 $result = self::convertVkCategoriesToTree($result);
169 $result = self::convertVkCategoriesToList($result);
182 private static function updateDataToCache($exportId)
184 $vkCategories = self::getDataFromVk($exportId);
186 if (is_array($vkCategories))
189 $cacheManager->set(self::createCacheId(), $vkCategories);
191 return $vkCategories;
206 private static function getDataFromVk($exportId)
208 $apiHelper =
new ApiHelper($exportId);
210 return $apiHelper->getVkCategories();
220 private static function convertVkCategoriesToTree($categoriesList)
222 $categoriesTree = array();
223 foreach ($categoriesList as $category)
225 if (!isset($categoriesTree[$category[
"section"][
"id"]]))
228 $categoriesTree[$category[
"section"][
"id"]] = array(
229 "ID" => $category[
"section"][
"id"],
230 "NAME" => $category[
"section"][
"name"],
236 $categoriesTree[$category[
"section"][
"id"]][
"ITEMS"][$category[
"id"]] = array(
237 "ID" => $category[
"id"],
238 "NAME" => $category[
"name"],
242 return $categoriesTree;
252 private static function convertVkCategoriesToList($categoriesList)
254 $categoriesListFormatted = array();
255 foreach ($categoriesList as $category)
257 $categoriesListFormatted[$category[
"id"]]= array(
258 "ID" => $category[
"id"],
259 "NAME" => $category[
"name"],
263 return $categoriesListFormatted;
276 $vkCategory = $this->
getList();
279 $defaultItemText = $defaultItemText <>
'' ? $defaultItemText :
Loc::getMessage(
"SALE_CATALOG_CHANGE_VK_CATEGORY");
280 $strSelect =
'<option value="-1">[' . $defaultItemText .
']</option>';
282 foreach ($vkCategory as $vkTreeItem)
284 $strSelect .=
'<option disabled value="0">'.mb_strtoupper($vkTreeItem[
"NAME"]) .
'</option>';
286 foreach ($vkTreeItem[
"ITEMS"] as $sectionItem)
289 if ($catVkSelected && ($sectionItem[
"ID"] == $catVkSelected))
290 $selected =
" selected";
292 $strSelect .=
'<option' . $selected .
' value="' . $sectionItem[
"ID"] .
'">- ' . $sectionItem[
"NAME"] .
'</option>';
308 if (self::updateDataToCache($exportId))
310 return self::createAgentName($exportId);
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)