Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productadd.php
1<?php
2
4
10
16{
17 private static $firstRunning = true;
18 private static $albumsMapped;
19 private static $apiHelper;
20 private static $isAgressive;
21
32 public function process($data, Timer $timer = null)
33 {
34// logger use always, but rich log need only if set this option
35 $logger = new Vk\Logger($this->exportId);
36
37 if (count($data) > Vk\Vk::MAX_EXECUTION_ITEMS)
38 {
39 $data = array_slice($data, 0, Vk\Vk::MAX_ALBUMS);
40 $logger->addError('TOO_MANY_PRODUCTS_TO_EXPORT');
41 }
42
43// get STARTPOSITION for create next process step
44 reset($data);
45 $startPosition = current($data);
46 $startPosition = $startPosition["BX_ID"];
47
48// set STATIC variables for several cycles
49 if (self::$firstRunning)
50 {
51 self::$apiHelper = new Vk\Api\ApiHelper($this->exportId);
52 self::$albumsMapped = Vk\Map::getMappedAlbums($this->exportId);
53 self::$albumsMapped = Vk\Api\ApiHelper::changeArrayMainKey(self::$albumsMapped, 'SECTION_ID');
54 self::$isAgressive = self::$vk->isAgressiveExport($this->exportId);
55 self::$firstRunning = false;
56 }
57
58// CHECK existing products
59 $vkExportedData = new Vk\VkExportedData($this->exportId, 'PRODUCTS');
60 $productsFromVk = $vkExportedData->getData();
61 $data = Vk\Map::checkMappingMatches($data, $productsFromVk, $this->exportId, 'PRODUCTS', self::$isAgressive);
62
63 try
64 {
65// check MAIN PHOTO and delete items. NO photo = NO product!
66 foreach ($data as $item)
67 {
68 if (!isset($item["PHOTO_MAIN_BX_ID"]) || !$item["PHOTO_MAIN_BX_ID"])
69 {
70 $logger->addError("PRODUCT_WRONG_PHOTOS", $item["BX_ID"]);
71 unset($data[$item["BX_ID"]]);
72 }
73 }
74
75// UPLOAD main photo
76// todo: need a photo mapping check before upload.
77 if (!empty($data))
78 {
79 $logger->addLog("Upload main photo");
80 $photoUploader = new PhotoUploader($this->exportId, PhotoUploader::TYPE_PRODUCT_MAIN_PHOTO, $timer);
81 $mainPhotoSaveResults = $photoUploader->upload($data);
82
83// photos UPLOAD may be FAILED on VK side. SKIP product
84 if(array_key_exists('errors', $mainPhotoSaveResults))
85 {
86 foreach($mainPhotoSaveResults['errors'] as $errorId)
87 {
88 unset($data[$errorId]);
89 }
90 unset($mainPhotoSaveResults['errors']);
91 }
92
93 $data = Vk\Api\ApiHelper::addResultToData($data, $mainPhotoSaveResults, "BX_ID");
94 }
95
96
97// UPLOAD photoS
98 foreach ($data as &$product)
99 {
100// todo: if fail load photo - just log
101 if ($product["PHOTOS"])
102 {
103// if error in some photo - just skip them
104 $logger->addLog("Upload product photos");
105 $photoUploader = new PhotoUploader($this->exportId, PhotoUploader::TYPE_PRODUCT_PHOTOS, $timer);
106 $photosSaveResults = $photoUploader->upload($product["PHOTOS"]);
107
108// photos UPLOAD may be FAILED on VK side. SKIP photo
109 if(array_key_exists('errors', $photosSaveResults))
110 {
111 foreach($photosSaveResults['errors'] as $errorId)
112 {
113 unset($product["PHOTOS"][$errorId]);
114 }
115 unset($photosSaveResults['errors']);
116 }
117
118 $product["PHOTOS"] = Vk\Api\ApiHelper::addResultToData(
119 $product["PHOTOS"],
120 $photosSaveResults,
121 "PHOTO_BX_ID"
122 );
123 }
124 }
125 unset($product);
126
127// ADD or EDIT products
128 $logger->addLog("Add or edit products", $data);
129 $productsData = Vk\Api\ApiHelper::prepareProductsDataToVk($data);
130 $productsAddEditResults = $this->executer->executeMarketProductAddEdit(array(
131 "owner_id" => $this->vkGroupId,
132 "data" => $productsData,
133 "count" => count($productsData),
134 ));
135 $data = Vk\Api\ApiHelper::addResultToData($data, $productsAddEditResults, "BX_ID");
136 unset($productsAddEditResults, $productsData);
137
138
139// construct MAPPING data for success results
140 $dataToMapping = array();
141 foreach ($data as $product)
142 {
143 if (isset($product["FLAG_PRODUCT_ADD_RESULT"]) && $product["FLAG_PRODUCT_ADD_RESULT"])
144 {
145 $dataToMapping[] = array(
146 "value_external" => $product["VK_ID"],
147 "value_internal" => $product["BX_ID"],
148 );
149 }
150 }
151
152// adding to ALBUMS
153 $productsToAlbums = array();
154 $sectionsList = new Vk\SectionsList($this->exportId);
155
156// product may have multisections - find all them
157 $productsIds = array_keys($data);
158 $productsMultiSections = $sectionsList->getMultiSectionsToProduct($productsIds);
159
160 foreach ($productsMultiSections as $productId => $product)
161 {
162 foreach ($product as $sectionId)
163 {
164// find album to adding current product
165 $toAlbumSectionId = $sectionsList->getToAlbumBySection($sectionId);
166
167// prepare array to ADDING products TO ALBUMS
168 if (isset(self::$albumsMapped[$toAlbumSectionId]))
169 {
170 $productsToAlbums[] = array(
171 "BX_ID" => $productId,
172 "VK_ID" => $data[$productId]["VK_ID"],
173 "ALBUM_VK_ID" => self::$albumsMapped[$toAlbumSectionId]["ALBUM_VK_ID"],
174 );
175
176 if (count($productsToAlbums) === Vk\Vk::MAX_EXECUTION_ITEMS)
177 {
178 $this->addProductsToAlbums($logger, $productsToAlbums);
179 $productsToAlbums = [];
180 }
181 }
182 }
183 }
184 $this->addProductsToAlbums($logger, $productsToAlbums);
185
186// WRITE successful results TO MAP
187// we don't need use timer in last operation . Timer will be checked in feed cycle.
188 if (!empty($dataToMapping))
189 {
190 Vk\Map::addProductMapping($dataToMapping, $this->exportId);
191 }
192 unset($dataToMapping, $product);
193
194
195// add saved data to CACHE to accelereate export process. Cache updated every hour (for long exports)
196 if (!empty($data))
197 {
198 $dataToCache = Vk\Api\ApiHelper::extractItemsFromArray($data, array('VK_ID'));
199 $dataToCache = Vk\Api\ApiHelper::changeArrayMainKey($dataToCache, 'VK_ID');
200 $vkExportedData->addData($dataToCache);
201 }
202
203 $logger->addLog("Finish product add chunk");
204
205// check timer before next step, because not-agressive export can be run very long time
206 if ($timer !== null && !$timer->check())
207 {
208 throw new TimeIsOverException();
209 }
210 }
211
212 catch (TimeIsOverException $e)
213 {
214 throw new TimeIsOverException("Timelimit for export is over", $startPosition);
215 }
216
217 return true;
218 }
219
220 private function addProductsToAlbums($logger, $productsToAlbums): void
221 {
222 $logger->addLog("Add products to albums", $productsToAlbums);
223 $this->executer->executeMarketProductAddToAlbums(array(
224 "owner_id" => $this->vkGroupId,
225 "data" => $productsToAlbums,
226 "count" => count($productsToAlbums),
227 ));
228 }
229}