40 $logger->addError(
'TOO_MANY_PRODUCTS_TO_EXPORT');
45 $startPosition = current($data);
46 $startPosition = $startPosition[
"BX_ID"];
49 if (self::$firstRunning)
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;
60 $productsFromVk = $vkExportedData->getData();
61 $data = Vk\Map::checkMappingMatches($data, $productsFromVk, $this->exportId,
'PRODUCTS', self::$isAgressive);
66 foreach ($data as $item)
68 if (!isset($item[
"PHOTO_MAIN_BX_ID"]) || !$item[
"PHOTO_MAIN_BX_ID"])
70 $logger->addError(
"PRODUCT_WRONG_PHOTOS", $item[
"BX_ID"]);
71 unset($data[$item[
"BX_ID"]]);
79 $logger->addLog(
"Upload main photo");
80 $photoUploader =
new PhotoUploader($this->exportId, PhotoUploader::TYPE_PRODUCT_MAIN_PHOTO, $timer);
81 $mainPhotoSaveResults = $photoUploader->upload($data);
84 if(array_key_exists(
'errors', $mainPhotoSaveResults))
86 foreach($mainPhotoSaveResults[
'errors'] as $errorId)
88 unset($data[$errorId]);
90 unset($mainPhotoSaveResults[
'errors']);
93 $data = Vk\Api\ApiHelper::addResultToData($data, $mainPhotoSaveResults,
"BX_ID");
98 foreach ($data as &$product)
101 if ($product[
"PHOTOS"])
104 $logger->addLog(
"Upload product photos");
105 $photoUploader =
new PhotoUploader($this->exportId, PhotoUploader::TYPE_PRODUCT_PHOTOS, $timer);
106 $photosSaveResults = $photoUploader->upload($product[
"PHOTOS"]);
109 if(array_key_exists(
'errors', $photosSaveResults))
111 foreach($photosSaveResults[
'errors'] as $errorId)
113 unset($product[
"PHOTOS"][$errorId]);
115 unset($photosSaveResults[
'errors']);
118 $product[
"PHOTOS"] = Vk\Api\ApiHelper::addResultToData(
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),
135 $data = Vk\Api\ApiHelper::addResultToData($data, $productsAddEditResults,
"BX_ID");
136 unset($productsAddEditResults, $productsData);
140 $dataToMapping = array();
141 foreach ($data as $product)
143 if (isset($product[
"FLAG_PRODUCT_ADD_RESULT"]) && $product[
"FLAG_PRODUCT_ADD_RESULT"])
145 $dataToMapping[] = array(
146 "value_external" => $product[
"VK_ID"],
147 "value_internal" => $product[
"BX_ID"],
153 $productsToAlbums = array();
157 $productsIds = array_keys($data);
158 $productsMultiSections = $sectionsList->getMultiSectionsToProduct($productsIds);
160 foreach ($productsMultiSections as $productId => $product)
162 foreach ($product as $sectionId)
165 $toAlbumSectionId = $sectionsList->getToAlbumBySection($sectionId);
168 if (isset(self::$albumsMapped[$toAlbumSectionId]))
170 $productsToAlbums[] = array(
171 "BX_ID" => $productId,
172 "VK_ID" => $data[$productId][
"VK_ID"],
173 "ALBUM_VK_ID" => self::$albumsMapped[$toAlbumSectionId][
"ALBUM_VK_ID"],
178 $this->addProductsToAlbums($logger, $productsToAlbums);
179 $productsToAlbums = [];
184 $this->addProductsToAlbums($logger, $productsToAlbums);
188 if (!empty($dataToMapping))
190 Vk\Map::addProductMapping($dataToMapping, $this->exportId);
192 unset($dataToMapping, $product);
198 $dataToCache = Vk\Api\ApiHelper::extractItemsFromArray($data, array(
'VK_ID'));
199 $dataToCache = Vk\Api\ApiHelper::changeArrayMainKey($dataToCache,
'VK_ID');
200 $vkExportedData->addData($dataToCache);
203 $logger->addLog(
"Finish product add chunk");
206 if ($timer !==
null && !$timer->check())