1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
vkcategories.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\TradingPlatform\Vk;
4
5use Bitrix\Sale\TradingPlatform\Vk\Api\ApiHelper;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Main\Application;
8
9Loc::loadMessages(__FILE__);
10
17{
18 const CACHE_DIR = '/sale/vkexport/';
19 const CACHE_TTL = 86400;
20 const CACHE_ID_PREFIX = "vkcategory_cache";
21 private int $exportId;
22
27 public function __construct(int $exportId)
28 {
29 $this->exportId = $exportId;
30 }
31
32
38 public function createAgent()
39 {
40 if (empty($this->exportId))
41 {
42 return null;
43 }
44
45 $agent = $this->getAgentId();
46 if (!$agent)
47 {
48 // CREATE agent if not exist
49 $ttl = self::CACHE_TTL;
50 $timeToStart = ConvertTimeStamp(strtotime(date('Y-m-d H:i:s', time() + $ttl)), 'FULL');
51
52 return \CAgent::AddAgent(
53 self::createAgentName($this->exportId),
54 'sale',
55 "N",
56 $ttl,
57 $timeToStart,
58 "Y",
59 $timeToStart
60 );
61 }
62 else
63 {
64 return $agent;
65 }
66 }
67
68
74 private function getAgentId()
75 {
76 $dbRes = \CAgent::GetList(
77 array(),
78 array(
79 'NAME' => self::createAgentName($this->exportId),
80 )
81 );
82
83 if ($agent = $dbRes->Fetch())
84 return $agent;
85 else
86 return false;
87 }
88
89
94 public function deleteAgent()
95 {
96// not change cache - they will self dropped after ttl
97// dropped agent
98 $dbRes = \CAgent::GetList(
99 array(),
100 array(
101 'NAME' => self::createAgentName($this->exportId),
102 )
103 );
104
105 if ($agent = $dbRes->Fetch())
106 \CAgent::Delete($agent["ID"]);
107 }
108
109
113 public function deleteAllAgents()
114 {
115 $vk = Vk::getInstance();
116 $settings = $vk->getSettings();
117
118 foreach ($settings as $id => $value)
119 {
120 $this->deleteAgent();
121 }
122 }
123
124
129 private static function createCacheId()
130 {
131// we need only one cache for all exports => no needed export ID for cache ID
132 return self::CACHE_ID_PREFIX;
133 }
134
140 private static function createAgentName(int $exportId): string
141 {
142 return 'Bitrix\Sale\TradingPlatform\Vk\VkCategories::updateVkCategoriesAgent(' . $exportId . ');';
143 }
144
152 public function getList($isTree = true)
153 {
154 $cacheManager = Application::getInstance()->getManagedCache();
155 $result = NULL;
156
157 if ($cacheManager->read(self::CACHE_TTL, self::createCacheId()))
158 {
159 $result = $cacheManager->get(self::createCacheId());
160 }
161 else
162 {
163 $result = self::updateDataToCache($this->exportId);
164 }
165
166 if ($isTree)
167 {
168 $result = self::convertVkCategoriesToTree($result);
169 }
170 else
171 {
172 $result = self::convertVkCategoriesToList($result);
173 }
174
175 return $result;
176 }
177
178
185 private static function updateDataToCache($exportId)
186 {
187 if (empty($exportId))
188 {
189 return null;
190 }
191
192 $vkCategories = self::getDataFromVk($exportId);
193
194 if (is_array($vkCategories))
195 {
196 $cacheManager = Application::getInstance()->getManagedCache();
197 $cacheManager->set(self::createCacheId(), $vkCategories);
198
199 return $vkCategories;
200 }
201 else
202 {
203 return null;
204 }
205 }
206
207
214 private static function getDataFromVk($exportId)
215 {
216 $apiHelper = new ApiHelper($exportId);
217
218 return $apiHelper->getVkCategories();
219 }
220
221
228 private static function convertVkCategoriesToTree($categoriesList)
229 {
230 $categoriesTree = array();
231 foreach ($categoriesList as $category)
232 {
233 if (!isset($categoriesTree[$category["section"]["id"]]))
234 {
235// create NEW tree-item
236 $categoriesTree[$category["section"]["id"]] = array(
237 "ID" => $category["section"]["id"],
238 "NAME" => $category["section"]["name"],
239 "ITEMS" => array(),
240 );
241 }
242
243// put data in exist tree item
244 $categoriesTree[$category["section"]["id"]]["ITEMS"][$category["id"]] = array(
245 "ID" => $category["id"],
246 "NAME" => $category["name"],
247 );
248 }
249
250 return $categoriesTree;
251 }
252
253
260 private static function convertVkCategoriesToList($categoriesList)
261 {
262 $categoriesListFormatted = array();
263 foreach ($categoriesList as $category)
264 {
265 $categoriesListFormatted[$category["id"]]= array(
266 "ID" => $category["id"],
267 "NAME" => $category["name"],
268 );
269 }
270
271 return $categoriesListFormatted;
272 }
273
274
282 public function getVkCategorySelector($catVkSelected = NULL, $defaultItemText = '')
283 {
284 $vkCategory = $this->getList();
285
286// todo: why upper case dont work?
287 $defaultItemText = $defaultItemText <> '' ? $defaultItemText : Loc::getMessage("SALE_CATALOG_CHANGE_VK_CATEGORY");
288 $strSelect = '<option value="-1">[' . $defaultItemText . ']</option>';
289
290 foreach ($vkCategory as $vkTreeItem)
291 {
292 $strSelect .= '<option disabled value="0">'.mb_strtoupper($vkTreeItem["NAME"]) . '</option>';
293
294 foreach ($vkTreeItem["ITEMS"] as $sectionItem)
295 {
296 $selected = '';
297 if ($catVkSelected && ($sectionItem["ID"] == $catVkSelected))
298 $selected = " selected";
299
300 $strSelect .= '<option' . $selected . ' value="' . $sectionItem["ID"] . '">- ' . $sectionItem["NAME"] . '</option>';
301 }
302 }
303
304 return $strSelect;
305 }
306
307
314 public static function updateVkCategoriesAgent(int $exportId): string
315 {
316 if (self::updateDataToCache($exportId))
317 {
318 return self::createAgentName($exportId);
319 }
320 else return '';
321 }
322}
static updateVkCategoriesAgent(int $exportId)
Определения vkcategories.php:314
getVkCategorySelector($catVkSelected=NULL, $defaultItemText='')
Определения vkcategories.php:282
static getInstance()
Определения vk.php:68
</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
$settings
Определения product_settings.php:43
$dbRes
Определения yandex_detail.php:168