Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
map.php
1<?php
2
4
8
9
16class Map
17{
18 private static function getProductEntityCode($exportId)
19 {
20 return "VK_PRODUCTS_" . $exportId;
21 }
22
23 private static function getPhotoEntityCode($exportId)
24 {
25 return "VK_PHOTOS_" . $exportId;
26 }
27
28 private static function getAlbumEntityCode($exportId)
29 {
30 return "VK_ALBUMS_" . $exportId;
31 }
32
33 private static function getSectionsEntityCode($exportId)
34 {
35 return "VK_SECTIONS_" . $exportId;
36 }
37
38 private static function getGeneralCodePrefix()
39 {
40 return "VK_";
41 }
42
43
44 private static function getProductEntityId($exportId)
45 {
46 $productEntCode = self::getProductEntityCode($exportId);
47
48 return self::getMapEntityId($productEntCode);
49 }
50
51 private static function getPhotoEntityId($exportId)
52 {
53 $photoEntCode = self::getPhotoEntityCode($exportId);
54
55 return self::getMapEntityId($photoEntCode);
56 }
57
58
59 private static function getAlbumEntityId($exportId)
60 {
61 $albumEntCode = self::getAlbumEntityCode($exportId);
62
63 return self::getMapEntityId($albumEntCode);
64 }
65
66
67 private static function getSectionsEntityId($exportId)
68 {
69 $sectionsEntCode = self::getSectionsEntityCode($exportId);
70
71 return self::getMapEntityId($sectionsEntCode);
72 }
73
74// --------------------------------------------------------
75 public static function addProductMapping($values, $exportId)
76 {
77 $mapEntityCode = self::getProductEntityCode($exportId);
78 $mapEntityID = self::getMapEntityId($mapEntityCode);
79
80 return self::addEntityMapping($values, $mapEntityID);
81 }
82
83 public static function removeProductMapping($values, $exportId, $flagKeys = '')
84 {
85 $mapEntityCode = self::getProductEntityCode($exportId);
86 $mapEntityID = self::getMapEntityId($mapEntityCode);
87
88 return self::removeEntityMapping($values, $mapEntityID, $flagKeys);
89 }
90
91 public static function updateProductMapping($values, $exportId, $flagKeys = '')
92 {
93 $mapEntityCode = self::getProductEntityCode($exportId);
94 $mapEntityID = self::getMapEntityId($mapEntityCode);
95
96 return self::updateEntityMapping($values, $mapEntityID, $flagKeys);
97 }
98
99// --------------------------------------------------------
100 public static function addPhotoMapping($values, $exportId)
101 {
102 $mapEntityCode = self::getPhotoEntityCode($exportId);
103 $mapEntityID = self::getMapEntityId($mapEntityCode);
104
105 return self::addEntityMapping($values, $mapEntityID);
106 }
107
108 public static function removePhotoMapping($values, $exportId, $flagKeys = '')
109 {
110 $mapEntityCode = self::getPhotoEntityCode($exportId);
111 $mapEntityID = self::getMapEntityId($mapEntityCode);
112
113 return self::removeEntityMapping($values, $mapEntityID, $flagKeys);
114 }
115
116 public static function updatePhotoMapping($values, $exportId, $flagKeys = '')
117 {
118 $mapEntityCode = self::getPhotoEntityCode($exportId);
119 $mapEntityID = self::getMapEntityId($mapEntityCode);
120
121 return self::updateEntityMapping($values, $mapEntityID, $flagKeys);
122 }
123
124// --------------------------------------------------------
125 public static function addAlbumMapping($values, $exportId)
126 {
127 $mapEntityCode = self::getAlbumEntityCode($exportId);
128 $mapEntityID = self::getMapEntityId($mapEntityCode);
129
130 return self::addEntityMapping($values, $mapEntityID);
131 }
132
133 public static function removeAlbumMapping($values, $exportId, $flagKeys = '')
134 {
135 $mapEntityCode = self::getAlbumEntityCode($exportId);
136 $mapEntityID = self::getMapEntityId($mapEntityCode);
137
138 return self::removeEntityMapping($values, $mapEntityID, $flagKeys);
139 }
140
141 public static function updateAlbumMapping($values, $exportId, $flagKeys = '')
142 {
143 $mapEntityCode = self::getAlbumEntityCode($exportId);
144 $mapEntityID = self::getMapEntityId($mapEntityCode);
145
146 return self::updateEntityMapping($values, $mapEntityID, $flagKeys);
147 }
148
149// --------------------------------------------------------
150 public static function addSectionsMapping($values, $exportId)
151 {
152 $mapEntityCode = self::getSectionsEntityCode($exportId);
153 $mapEntityID = self::getMapEntityId($mapEntityCode);
154
155 return self::addEntityMapping($values, $mapEntityID);
156 }
157
158 public static function removeSectionsMapping($values, $exportId, $flagKeys = '')
159 {
160 $mapEntityCode = self::getSectionsEntityCode($exportId);
161 $mapEntityID = self::getMapEntityId($mapEntityCode);
162
163 return self::removeEntityMapping($values, $mapEntityID, $flagKeys);
164 }
165
166 public static function updateSectionsMapping($values, $exportId, $flagKeys = '')
167 {
168 $mapEntityCode = self::getSectionsEntityCode($exportId);
169 $mapEntityID = self::getMapEntityId($mapEntityCode);
170
171 return self::updateEntityMapping($values, $mapEntityID, $flagKeys);
172 }
173
174
182 private static function getMapEntityId($mapEntityCode)
183 {
184 $result = 0;
185 $vk = Vk::getInstance();
186
187 $fields = array(
188 "TRADING_PLATFORM_ID" => $vk->getId(),
189 "CODE" => $mapEntityCode,
190 );
191
192 $resMapEntity = MapEntityTable::getList(array(
193 "filter" => $fields,
194 ));
195
196 if ($mapEntity = $resMapEntity->fetch())
197 {
198 $result = $mapEntity["ID"];
199 }
200 else
201 {
202 $resAdd = MapEntityTable::add($fields);
203
204 if ($resAdd->isSuccess())
205 $result = $resAdd->getId();
206 }
207
208 if ($result <= 0)
209 throw new SystemException("Can' t get map entity id for code: " . $mapEntityCode . ".");
210
211 return $result;
212 }
213
214
223 private static function addEntityMapping($values, $mapEntityID)
224 {
225 $result = true;
226// todo: maybe we can use packed adding for acceleration
227 foreach ($values as $item)
228 {
229 $item = array_change_key_case($item, CASE_UPPER); // preserve low case keys
230 $fields = array(
231 "VALUE_EXTERNAL" => $item["VALUE_EXTERNAL"],
232 "VALUE_INTERNAL" => $item["VALUE_INTERNAL"],
233 "ENTITY_ID" => $mapEntityID,
234 );
235// add params if not null
236 if ($item["PARAMS"])
237 $fields["PARAMS"] = $item["PARAMS"];
238
239 $addRes = MapTable::add($fields);
240
241 if (!$addRes->isSuccess() || !$result)
242 $result = false;
243 }
244
245 return $result;
246 }
247
248
259 private static function updateEntityMapping($values, $mapEntityID, $flagKeys)
260 {
261 $result = true;
262
263 foreach ($values as $item)
264 {
265 $fields = array_change_key_case($item, CASE_UPPER); // preserve low case keys
266 $fields["ENTITY_ID"] = intval($mapEntityID);
267 $fields["VALUE_EXTERNAL"] = strval($item["VALUE_EXTERNAL"]);
268 $fields["VALUE_INTERNAL"] = strval($item["VALUE_INTERNAL"]);
269 $filterToId = $fields;
270
271 if ($flagKeys == "ONLY_INTERNAL")
272 unset($filterToId["VALUE_EXTERNAL"]);
273 if ($flagKeys == "ONLY_EXTERNAL")
274 unset($filterToId["VALUE_INTERNAL"]);
275 unset($filterToId["PARAMS"]);
276
277// get ID for current element
278 $id = MapTable::getList(
279 array(
280 "filter" => $filterToId,
281 "select" => array("ID"),
282 )
283 );
284 $id = $id->fetch();
285
286// update or create element
287 if ($id = $id["ID"])
288 {
289 $upRes = MapTable::update($id, $fields);
290 if (!$upRes->isSuccess())
291 $result = false;
292 }
293 else
294 {
295 $addRes = MapTable::add($fields);
296 if (!$addRes->isSuccess())
297 $result = false;
298 }
299
300 }
301
302// if result == false - we have problem minimum in one item, maybe in all items
303 return $result;
304 }
305
306
317 private static function removeEntityMapping($values, $mapEntityID, $flagKey = '')
318 {
319 $result = true;
320// todo: maybe we can use packed adding for acceleration
321 foreach ($values as $item)
322 {
323// break empty items
324 if(empty($item))
325 continue;
326
327// preserve lowercase $item
328 $item = array_change_key_case($item, CASE_UPPER);
329
330 if ($flagKey == "ONLY_INTERNAL")
331 unset($item["VALUE_EXTERNAL"]);
332 elseif ($flagKey == "ONLY_EXTERNAL")
333 unset($item["VALUE_INTERNAL"]);
334
335 $fields = array("ENTITY_ID" => $mapEntityID);
336 $fields = array_merge($fields, $item);
337
338 $id = MapTable::getList(array(
339 "filter" => $fields,
340 "select" => array("ID"),
341 )
342 );
343 $id = $id->fetch();
344
345 if ($id)
346 {
347 $delRes = MapTable::delete($id["ID"]);
348 if (!$delRes->isSuccess() || !$result)
349 $result = false;
350 }
351 else
352 $result = false;
353
354 }
355
356// if result == false - we have problem minimum in one item, maybe in all items
357 return $result;
358 }
359
360
368 public static function getMappedAlbums($exportId)
369 {
370 $result = array();
371 $albumEntityId = self::getAlbumEntityId($exportId);
372
373 $catRes = MapTable::getList(array(
374// 'select' => array('VALUE_INTERNAL'),
375 'filter' => array('=ENTITY_ID' => $albumEntityId),
376 ));
377
378 while ($album = $catRes->fetch())
379 $result[$album["VALUE_INTERNAL"]] = array(
380 "SECTION_ID" => $album["VALUE_INTERNAL"],
381 "ALBUM_VK_ID" => $album["VALUE_EXTERNAL"],
382 );
383
384 return $result;
385 }
386
394 public static function getMappedProducts($exportId)
395 {
396 $result = array();
397 $productEntId = self::getProductEntityId($exportId);
398
399 $catRes = MapTable::getList(array(
400// 'select' => array('VALUE_INTERNAL'),
401 'filter' => array('=ENTITY_ID' => $productEntId),
402 ));
403
404 while ($product = $catRes->fetch())
405 $result[$product["VALUE_INTERNAL"]] = array(
406 "BX_ID" => $product["VALUE_INTERNAL"],
407 "VK_ID" => $product["VALUE_EXTERNAL"],
408 );
409
410 return $result;
411 }
412
421 public static function getMappedSections($exportId, $sectionId = NULL)
422 {
423 $result = array();
424 $catEntId = self::getSectionsEntityId($exportId);
425
426 $filter = array('=ENTITY_ID' => $catEntId);
427 if ($sectionId)
428 $filter['=VALUE_INTERNAL'] = $sectionId;
429
430// todo: we can cached map. Clear cache if set setting in section
431 $catRes = MapTable::getList(array(
432 'filter' => $filter,
433 ));
434
435 while ($product = $catRes->fetch())
436 $result[$product["VALUE_INTERNAL"]] = array(
437 "BX_ID" => $product["VALUE_INTERNAL"],
438 "VK_ID" => $product["VALUE_EXTERNAL"],
439 "PARAMS" => $product["PARAMS"],
440 );
441
442 return $result;
443 }
444
445
459 public static function checkMappingMatches(array $data, array $dataFromVk, $exportId, $type, $isAgressive)
460 {
461// todo: diff methods for albums and product, wrap over this method
462 switch ($type)
463 {
464 case 'ALBUMS':
465 $bxKey = "SECTION_ID";
466 $vkKey = "ALBUM_VK_ID";
467 $deleteMapMethod = "removeAlbumMapping";
468 $dataFromMapping = self::getMappedAlbums($exportId);
469 break;
470
471 case 'PRODUCTS':
472 $bxKey = "BX_ID";
473 $vkKey = "VK_ID";
474 $deleteMapMethod = "removeProductMapping";
475 $dataFromMapping = self::getMappedProducts($exportId);
476 break;
477
478 default:
479 throw new SystemException("Wrong VK-mapping type");
480 }
481
482// FIND items, which exist in VK and map, but not exist on site (was be deleted, changed settings etc)
483// todo: now we can check only current data-chunk (about 25 items) and can't find element,
484// todo: which exist in VK and MAP, but not added in VK. Must do this function for preserve duplication
485// todo: old code see in the repo
486
487
488// MATCH items between VK and mapping
489 $dataMappedToRemove = array();
490 foreach ($data as &$item)
491 {
492 $itemMapped = $dataFromMapping[$item[$bxKey]];
493 if (isset($itemMapped))
494 {
495 if (isset($dataFromVk[$itemMapped[$vkKey]]))
496 {
497 if ($isAgressive)
498 {
499// editing albums/products which exists in VK
500 $item[$vkKey] = $dataFromMapping[$item[$bxKey]][$vkKey];
501 $item["FLAG_EDIT"] = true;
502 }
503 else
504 {
505// if not agressive export we not editing products/albums
506 unset($data[$item[$bxKey]]);
507 }
508 }
509
510 else
511 {
512// delete from map albums which not exist in vk
513 $item["FLAG_EDIT"] = false;
514 $dataMappedToRemove[] = array("VALUE_EXTERNAL" => $itemMapped[$vkKey]);
515 }
516 }
517// other albums not delete from map and not edit - only adding
518 else
519 $item["FLAG_EDIT"] = false;
520 }
521
522// DELETE from mapping items which not exist in VK
523 if (!empty($dataMappedToRemove))
524 self::$deleteMapMethod($dataMappedToRemove, $exportId);
525
526 return $data;
527 }
528
529
537 public static function deleteAllMapping()
538 {
539// GET all entity IDs
540 $resEntityIds = MapEntityTable::getList(array(
541 "filter" => array('%=CODE' => self::getGeneralCodePrefix() . '%'),
542 ));
543 $entityIds = array();
544 while ($entityId = $resEntityIds->fetch())
545 $entityIds[] = $entityId['ID'];
546
547// DELETE all MAP ENTITY
548 foreach ($entityIds as $entityId)
549 MapEntityTable::delete($entityId);
550
551
552// GET all map items IDs
553 $resMapIds = MapTable::getList(array(
554 "filter" => array('=ENTITY_ID' => $entityIds),
555 ));
556 $mapIds = array();
557 while ($mapId = $resMapIds->fetch())
558 $mapIds[] = $mapId['ID'];
559
560// DELETE all from MAP
561 foreach ($mapIds as $mapId)
562 MapTable::delete($mapId);
563 }
564}
static addPhotoMapping($values, $exportId)
Definition map.php:100
static addAlbumMapping($values, $exportId)
Definition map.php:125
static updateAlbumMapping($values, $exportId, $flagKeys='')
Definition map.php:141
static getMappedSections($exportId, $sectionId=NULL)
Definition map.php:421
static removeAlbumMapping($values, $exportId, $flagKeys='')
Definition map.php:133
static removePhotoMapping($values, $exportId, $flagKeys='')
Definition map.php:108
static removeProductMapping($values, $exportId, $flagKeys='')
Definition map.php:83
static updateSectionsMapping($values, $exportId, $flagKeys='')
Definition map.php:166
static addSectionsMapping($values, $exportId)
Definition map.php:150
static getMappedProducts($exportId)
Definition map.php:394
static addProductMapping($values, $exportId)
Definition map.php:75
static getMappedAlbums($exportId)
Definition map.php:368
static updateProductMapping($values, $exportId, $flagKeys='')
Definition map.php:91
static removeSectionsMapping($values, $exportId, $flagKeys='')
Definition map.php:158
static checkMappingMatches(array $data, array $dataFromVk, $exportId, $type, $isAgressive)
Definition map.php:459
static updatePhotoMapping($values, $exportId, $flagKeys='')
Definition map.php:116
if(!function_exists(__NAMESPACE__.'\\___1034172934'))
Definition license.php:1