- Модуль: sale
- Путь к файлу: ~/bitrix/modules/sale/lib/tradingplatform/vk/api/apihelper.php
- Класс: BitrixSaleTradingPlatformVkApiApiHelper
- Вызов: ApiHelper::uploadPhotos
public function uploadPhotos($data, $vkGroupId, $uploadType, Timer $timer = NULL)
{
// todo: this is a little kostyl. In cool variant we must separately do http-upload,
// todo: and photo save run through execute method
// todo: but now VK can't run savePhotoMethod through execute. Sadness ((
// PARAMS set
$photoSaveResults = array();
switch ($uploadType)
{
case 'PRODUCT_MAIN_PHOTO':
$uploadServerMethod = 'photos.getMarketUploadServer';
$saveMethod = 'photos.saveMarketPhoto';
$keyReference = 'BX_ID';
$keyPhotoVk = 'PHOTO_MAIN_VK_ID';
$keyPhotoBx = 'PHOTO_MAIN_BX_ID';
break;
case 'PRODUCT_PHOTOS':
$uploadServerMethod = 'photos.getMarketUploadServer';
$saveMethod = 'photos.saveMarketPhoto';
$keyReference = 'PHOTO_BX_ID';
$keyPhotoVk = 'PHOTO_VK_ID';
$keyPhotoBx = 'PHOTO_BX_ID';
break;
case 'ALBUM_PHOTO':
$uploadServerMethod = 'photos.getMarketAlbumUploadServer';
$saveMethod = 'photos.saveMarketAlbumPhoto';
$keyReference = 'SECTION_ID';
$keyPhotoVk = 'PHOTO_VK_ID';
$keyPhotoBx = 'PHOTO_BX_ID';
break;
default:
throw new SystemException("Wrong photo upload type");
break;
}
// PROCESSED
foreach ($data as $item)
{
// check EXISTING photo
if (!array_key_exists($keyPhotoBx, $item) || empty($item[$keyPhotoBx]))
continue;
// GET upload server by type
$getServerParams = array("group_id" => str_replace("-", "", $vkGroupId));
if ($uploadType == 'PRODUCT_MAIN_PHOTO')
$getServerParams += self::setUploadServerMainPhotoParams($item[$keyPhotoBx]);
$uploadServer = $this->api->run($uploadServerMethod, $getServerParams);
// todo: may be this error in upload server response
$this->logger->addLog("Get photo upload server", [
'PARAMS' => $getServerParams,
'RESULT' => $uploadServer,
]);
$uploadServer = $uploadServer["upload_url"];
// UPLOAD photo by http
$this->logger->addLog("Upload photo HTTP before", array(
"UPLOAD_TYPE" => $uploadType,
"ITEM" => array_key_exists("BX_ID", $item) ?
$item["BX_ID"].': '.$item["NAME"] :
$item["SECTION_ID"].': '.$item["TITLE"],
"PHOTO_BX_ID" => array_key_exists("PHOTO_MAIN_BX_ID", $item) ? $item["PHOTO_MAIN_BX_ID"] : $item["PHOTO_BX_ID"],
"PHOTO_URL" => array_key_exists("PHOTO_MAIN_URL", $item) ? $item["PHOTO_MAIN_URL"] : $item["PHOTO_URL"],
"PHOTOS" => $item["PHOTOS"] //only for products
));
$responseHttp = $this->uploadPhotoHttp($item, $uploadServer, $uploadType, $timer);
// SAVE upload result
$photoSaveParams = array(
"group_id" => str_replace('-', '', $vkGroupId),
"photo" => $responseHttp["photo"],
"server" => $responseHttp["server"],
"hash" => $responseHttp["hash"],
);
// for product photo we need more params
if ($saveMethod == "photos.saveMarketPhoto")
{
if (isset($responseHttp["crop_hash"]) && $responseHttp["crop_hash"])
$photoSaveParams["crop_hash"] = $responseHttp["crop_hash"];
if (isset($responseHttp["crop_data"]) && $responseHttp["crop_data"])
$photoSaveParams["crop_data"] = $responseHttp["crop_data"];
}
$responsePhotoSave = $this->api->run($saveMethod, $photoSaveParams);
// RESULT
$photoSaveResults[] = array(
$keyReference => $item[$keyReference],
$keyPhotoVk => $responsePhotoSave[0]["id"],
);
// todo: photo mapping. po odnomu, navernoe, ved timer
}
return $photoSaveResults;
}