3namespace Bitrix\Sale\TradingPlatform\Vk\Api;
5use Bitrix\Main\ArgumentNullException;
6use Bitrix\Main\SystemException;
7use Bitrix\Main\Web\HttpClient;
8use Bitrix\Main\Web\Json;
10use Bitrix\Sale\TradingPlatform\Timer;
11use Bitrix\Sale\TradingPlatform\Vk\Logger;
12use Bitrix\Sale\TradingPlatform\Vk\Vk;
13use Bitrix\Sale\TradingPlatform\TimeIsOverException;
14use Bitrix\Main\Localization\Loc;
16Loc::loadMessages(__FILE__);
41 $this->exportId = $exportId;
43 $this->api = $this->vk->getApi($exportId);
44 $this->executer = $this->vk->getExecuter($exportId);
45 $this->logger =
new Logger($this->exportId);
58 if (!isset($keys) || empty($keys))
62 foreach (
$data as $value)
64 if (!is_array($value))
73 $currArr[
$k] = $value[
$k];
93 if (empty(
$result) || !isset($referenceKey))
100 if (isset(
$data[$item[$referenceKey]]))
102 $data[$item[$referenceKey]] += $item;
120 if (!isset($mainKey))
124 foreach (
$data as $item)
126 $result[$item[$mainKey]] = $item;
129 $result[$item[$mainKey]][$keyRename] =
$result[$item[$mainKey]][$mainKey];
130 unset(
$result[$item[$mainKey]][$mainKey]);
156 $photoSaveResults =
array();
159 case 'PRODUCT_MAIN_PHOTO':
160 $uploadServerMethod =
'photos.getMarketUploadServer';
161 $saveMethod =
'photos.saveMarketPhoto';
162 $keyReference =
'BX_ID';
163 $keyPhotoVk =
'PHOTO_MAIN_VK_ID';
164 $keyPhotoBx =
'PHOTO_MAIN_BX_ID';
167 case 'PRODUCT_PHOTOS':
168 $uploadServerMethod =
'photos.getMarketUploadServer';
169 $saveMethod =
'photos.saveMarketPhoto';
170 $keyReference =
'PHOTO_BX_ID';
171 $keyPhotoVk =
'PHOTO_VK_ID';
172 $keyPhotoBx =
'PHOTO_BX_ID';
176 $uploadServerMethod =
'photos.getMarketAlbumUploadServer';
177 $saveMethod =
'photos.saveMarketAlbumPhoto';
178 $keyReference =
'SECTION_ID';
179 $keyPhotoVk =
'PHOTO_VK_ID';
180 $keyPhotoBx =
'PHOTO_BX_ID';
189 foreach (
$data as $item)
192 if (!array_key_exists($keyPhotoBx, $item) || empty($item[$keyPhotoBx]))
196 $getServerParams =
array(
"group_id" => str_replace(
"-",
"", $vkGroupId));
197 if ($uploadType ==
'PRODUCT_MAIN_PHOTO')
198 $getServerParams += self::setUploadServerMainPhotoParams($item[$keyPhotoBx]);
200 $uploadServer = $this->api->run($uploadServerMethod, $getServerParams);
202 $this->logger->addLog(
"Get photo upload server", [
203 'PARAMS' => $getServerParams,
204 'RESULT' => $uploadServer,
206 $uploadServer = $uploadServer[
"upload_url"];
210 $this->logger->addLog(
"Upload photo HTTP before",
array(
211 "UPLOAD_TYPE" => $uploadType,
212 "ITEM" => array_key_exists(
"BX_ID", $item) ?
213 $item[
"BX_ID"].
': '.$item[
"NAME"] :
214 $item[
"SECTION_ID"].
': '.$item[
"TITLE"],
215 "PHOTO_BX_ID" => array_key_exists(
"PHOTO_MAIN_BX_ID", $item) ? $item[
"PHOTO_MAIN_BX_ID"] : $item[
"PHOTO_BX_ID"],
216 "PHOTO_URL" => array_key_exists(
"PHOTO_MAIN_URL", $item) ? $item[
"PHOTO_MAIN_URL"] : $item[
"PHOTO_URL"],
217 "PHOTOS" => $item[
"PHOTOS"]
219 $responseHttp = $this->uploadPhotoHttp($item, $uploadServer, $uploadType, $timer);
222 $photoSaveParams =
array(
223 "group_id" => str_replace(
'-',
'', $vkGroupId),
224 "photo" => $responseHttp[
"photo"],
225 "server" => $responseHttp[
"server"],
226 "hash" => $responseHttp[
"hash"],
230 if ($saveMethod ==
"photos.saveMarketPhoto")
232 if (isset($responseHttp[
"crop_hash"]) && $responseHttp[
"crop_hash"])
233 $photoSaveParams[
"crop_hash"] = $responseHttp[
"crop_hash"];
234 if (isset($responseHttp[
"crop_data"]) && $responseHttp[
"crop_data"])
235 $photoSaveParams[
"crop_data"] = $responseHttp[
"crop_data"];
238 $responsePhotoSave = $this->api->run($saveMethod, $photoSaveParams);
241 $photoSaveResults[] =
array(
242 $keyReference => $item[$keyReference],
243 $keyPhotoVk => $responsePhotoSave[0][
"id"],
249 return $photoSaveResults;
265 private function uploadPhotoHttp(
$data, $uploadServer, $uploadType,
Timer $timer = NULL)
271 "url" =>
$data[
"PHOTO_URL"],
272 "filename" =>
IO\Path::getName(
$data[
"PHOTO_URL"]),
273 "param_name" =>
'file',
278 case 'PRODUCT_MAIN_PHOTO':
280 "url" =>
$data[
"PHOTO_MAIN_URL"],
281 "filename" =>
IO\Path::getName(
$data[
"PHOTO_MAIN_URL"]),
282 "param_name" =>
'file',
287 case 'PRODUCT_PHOTOS':
289 "url" =>
$data[
"PHOTO_URL"],
290 "filename" =>
IO\Path::getName(
$data[
"PHOTO_URL"]),
291 "param_name" =>
'file',
302 return $this->uploadHttp($uploadServer,
$postParams);
313 private static function setUploadServerMainPhotoParams($photoId)
318 $photoParams = \CFile::GetFileArray($photoId);
319 $w = $photoParams[
"WIDTH"];
320 $h = $photoParams[
"HEIGHT"];
324 $result[
"crop_x"] = ceil(($w + $h) / 2);
331 $result[
"crop_y"] = ceil(($w + $h) / 2);
346 private function uploadHttp($uploadServer,
$params)
348 $http =
new HttpClient();
349 $boundary = md5(rand() . time());
351 $file = $this->getFile(
$params[
"url"]);
353 $data =
'--' . $boundary .
"\r\n";
354 $data .=
'Content-Disposition: form-data; name="' .
$params[
"param_name"] .
'"; filename="' .
$params[
"filename"] .
'"' .
"\r\n";
355 $data .=
'Content-Type: application/octet-stream' .
"\r\n\r\n";
356 $data .= $file .
"\r\n";
357 $data .=
'--' . $boundary .
"--\r\n";
359 $http->setHeader(
'Content-type',
'multipart/form-data; boundary=' . $boundary);
360 $http->setHeader(
'Content-length', strlen(
$data));
362 $this->logger->addLog(
"Upload photo HTTP params", [
363 'SERVER' => $uploadServer,
365 'FILE_OK' => $file ?
'Y' :
'N',
370 $this->logger->addLog(
"Upload photo HTTP response",
$result);
373 if (array_key_exists(
"timer",
$params))
376 if ($timer !== NULL && !$timer->check())
377 throw new TimeIsOverException();
383 private function getFile(
string $url)
385 $http =
new HttpClient();
387 return $http->get(
$url);
393 $groupsSelector =
false;
395 $gpoups = $this->getUserGroups();
396 if(is_array($gpoups) && !empty($gpoups))
398 $groupsSelector =
'<option value="-1">['.Loc::getMessage(
'SALE_VK_CHANGE_GROUP').
']</option>';
399 $selectedValue = str_replace(
'-',
'', $selectedValue);
401 $id = $id ?
' id="' . $id .
'"' :
'';
403 foreach ($gpoups as $group)
405 $selected = $selectedValue == $group[
"id"] ?
' selected' :
'';
407 '<option' . $selected .
' value="' . $group[
'id'] .
'">' . $group[
'name'] .
'</option>';
411 '<select id="vk_export_groupselector" onchange="BX.Sale.VkAdmin.changeVkGroupLink();"' . $id .
$name .
'>' .
415 '<span style="padding-left:10px">
416 <a href="https://vk.com/club'. $selectedValue .
'" id="vk_export_groupselector__link">
417 <img src="/bitrix/images/sale/vk/vk_icon.png">
422 return $groupsSelector;
426 private function getUserGroups($offset =
null)
428 $userGroups =
array();
436 'filter' =>
'editor',
437 'offset' => $stepCount,
440 $apiResult = $this->api->run(
'groups.get',
$params);
441 foreach($apiResult[
'items'] as $group)
443 $userGroups[$group[
'id']] =
array(
444 'id' => $group[
'id'],
445 'name' => $group[
'name']
469 $albumsFromVk = $this->executer->executeMarketAlbumsGet(
array(
470 "owner_id" => $vkGroupId,
474 $albumsFromVk = $albumsFromVk[
"items"];
475 foreach ($albumsFromVk as &$item)
480 $albumsFromVk = array_flip($albumsFromVk);
482 return $albumsFromVk;
494 $productsFromVk =
array();
498 $productsFromVk += $this->executer->executeMarketProductsGet(
array(
499 "owner_id" => $vkGroupId,
500 "offset" => $prodGetStep,
505 if ($productsFromVk[
"end_products"])
507 unset($productsFromVk[
"end_products"]);
513 foreach($productsFromVk as $productFromVk)
514 $result[$productFromVk] =
array(
"VK_ID" => $productFromVk);
530 foreach (
$data as $item)
533 if (isset($item[
"PHOTOS"]) && is_array($item[
"PHOTOS"]))
535 $photosIds =
array();
536 foreach ($item[
"PHOTOS"] as $photo)
538 if (is_numeric($photo[
"PHOTO_VK_ID"]))
539 $photosIds[] = $photo[
"PHOTO_VK_ID"];
542 if (!empty($photosIds))
543 $item[
"PHOTOS"] = implode(
",", $photosIds);
545 unset($item[
"PHOTOS"]);
549 if (!(isset($item[
"CATEGORY_VK"]) && intval($item[
"CATEGORY_VK"]) > 0))
573 $vkCats = $this->api->run(
'market.getCategories',
array(
"count" =>
$count,
"offset" => $offset));
577 return $vkCats[
"items"];
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']