Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vkcategories.php
1<?php
2
4
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// CREATE agent if not exist
41 if (!$agent = $this->getAgentId())
42 {
43 $ttl = self::CACHE_TTL;
44 $timeToStart = ConvertTimeStamp(strtotime(date('Y-m-d H:i:s', time() + $ttl)), 'FULL');
45
46 $resultAgentAdd = \CAgent::AddAgent(
47 self::createAgentName($this->exportId),
48 'sale',
49 "N",
50 $ttl,
51 $timeToStart,
52 "Y",
53 $timeToStart
54 );
55
56 return $resultAgentAdd;
57 }
58
59 else
60 {
61 return $agent;
62 }
63 }
64
65
71 private function getAgentId()
72 {
73 $dbRes = \CAgent::GetList(
74 array(),
75 array(
76 'NAME' => self::createAgentName($this->exportId),
77 )
78 );
79
80 if ($agent = $dbRes->Fetch())
81 return $agent;
82 else
83 return false;
84 }
85
86
91 public function deleteAgent()
92 {
93// not change cache - they will self dropped after ttl
94// dropped agent
95 $dbRes = \CAgent::GetList(
96 array(),
97 array(
98 'NAME' => self::createAgentName($this->exportId),
99 )
100 );
101
102 if ($agent = $dbRes->Fetch())
103 \CAgent::Delete($agent["ID"]);
104 }
105
106
110 public function deleteAllAgents()
111 {
112 $vk = Vk::getInstance();
113 $settings = $vk->getSettings();
114
115 foreach ($settings as $id => $value)
116 {
117 $this->deleteAgent();
118 }
119 }
120
121
126 private static function createCacheId()
127 {
128// we need only one cache for all exports => no needed export ID for cache ID
130 }
131
137 private static function createAgentName(int $exportId): string
138 {
139 return 'Bitrix\Sale\TradingPlatform\Vk\VkCategories::updateVkCategoriesAgent(' . $exportId . ');';
140 }
141
149 public function getList($isTree = true)
150 {
151 $cacheManager = Application::getInstance()->getManagedCache();
152 $result = NULL;
153
154 if ($cacheManager->read(self::CACHE_TTL, self::createCacheId()))
155 {
156 $result = $cacheManager->get(self::createCacheId());
157 }
158 else
159 {
160 $result = self::updateDataToCache($this->exportId);
161 }
162
163 if ($isTree)
164 {
165 $result = self::convertVkCategoriesToTree($result);
166 }
167 else
168 {
169 $result = self::convertVkCategoriesToList($result);
170 }
171
172 return $result;
173 }
174
175
182 private static function updateDataToCache($exportId)
183 {
184 $vkCategories = self::getDataFromVk($exportId);
185
186 if (is_array($vkCategories))
187 {
188 $cacheManager = Application::getInstance()->getManagedCache();
189 $cacheManager->set(self::createCacheId(), $vkCategories);
190
191 return $vkCategories;
192 }
193 else
194 {
195 return null;
196 }
197 }
198
199
206 private static function getDataFromVk($exportId)
207 {
208 $apiHelper = new ApiHelper($exportId);
209
210 return $apiHelper->getVkCategories();
211 }
212
213
220 private static function convertVkCategoriesToTree($categoriesList)
221 {
222 $categoriesTree = array();
223 foreach ($categoriesList as $category)
224 {
225 if (!isset($categoriesTree[$category["section"]["id"]]))
226 {
227// create NEW tree-item
228 $categoriesTree[$category["section"]["id"]] = array(
229 "ID" => $category["section"]["id"],
230 "NAME" => $category["section"]["name"],
231 "ITEMS" => array(),
232 );
233 }
234
235// put data in exist tree item
236 $categoriesTree[$category["section"]["id"]]["ITEMS"][$category["id"]] = array(
237 "ID" => $category["id"],
238 "NAME" => $category["name"],
239 );
240 }
241
242 return $categoriesTree;
243 }
244
245
252 private static function convertVkCategoriesToList($categoriesList)
253 {
254 $categoriesListFormatted = array();
255 foreach ($categoriesList as $category)
256 {
257 $categoriesListFormatted[$category["id"]]= array(
258 "ID" => $category["id"],
259 "NAME" => $category["name"],
260 );
261 }
262
263 return $categoriesListFormatted;
264 }
265
266
274 public function getVkCategorySelector($catVkSelected = NULL, $defaultItemText = '')
275 {
276 $vkCategory = $this->getList();
277
278// todo: why upper case dont work?
279 $defaultItemText = $defaultItemText <> '' ? $defaultItemText : Loc::getMessage("SALE_CATALOG_CHANGE_VK_CATEGORY");
280 $strSelect = '<option value="-1">[' . $defaultItemText . ']</option>';
281
282 foreach ($vkCategory as $vkTreeItem)
283 {
284 $strSelect .= '<option disabled value="0">'.mb_strtoupper($vkTreeItem["NAME"]) . '</option>';
285
286 foreach ($vkTreeItem["ITEMS"] as $sectionItem)
287 {
288 $selected = '';
289 if ($catVkSelected && ($sectionItem["ID"] == $catVkSelected))
290 $selected = " selected";
291
292 $strSelect .= '<option' . $selected . ' value="' . $sectionItem["ID"] . '">- ' . $sectionItem["NAME"] . '</option>';
293 }
294 }
295
296 return $strSelect;
297 }
298
299
306 public static function updateVkCategoriesAgent(int $exportId): string
307 {
308 if (self::updateDataToCache($exportId))
309 {
310 return self::createAgentName($exportId);
311 }
312 else return '';
313 }
314}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getVkCategorySelector($catVkSelected=NULL, $defaultItemText='')