3namespace Bitrix\Sale\TradingPlatform\Vk\Api;
5use Bitrix\Main\ArgumentNullException;
6use Bitrix\Sale\TradingPlatform\TimeIsOverException;
7use Bitrix\Sale\TradingPlatform\Vk\Vk;
8use Bitrix\Main\Web\HttpClient;
9use Bitrix\Main\Localization\Loc;
10use Bitrix\Main\Web\Json;
12use Bitrix\Sale\TradingPlatform\Timer;
13use Bitrix\Sale\TradingPlatform\Vk\Logger;
15Loc::loadMessages(__FILE__);
44 if (empty($photoType))
49 $this->exportId = $exportId;
50 $this->type = $photoType;
51 $this->timer = $timer;
53 $this->vkGroupId = $this->vk->getGroupId($this->exportId);
54 $this->api = $this->vk->getApi($exportId);
55 $this->logger =
new Logger($this->exportId);
71 case self::TYPE_PRODUCT_MAIN_PHOTO:
72 $this->params[
'uploadServerMethod']=
'photos.getMarketUploadServer';
73 $this->params[
'saveMethod'] =
'photos.saveMarketPhoto';
74 $this->params[
'keyReference'] =
'BX_ID';
75 $this->params[
'keyPhotoVk'] =
'PHOTO_MAIN_VK_ID';
76 $this->params[
'keyPhotoBx'] =
'PHOTO_MAIN_BX_ID';
77 $this->params[
'keyPhotoUrl']=
'PHOTO_MAIN_URL';
80 case self::TYPE_PRODUCT_PHOTOS:
81 $this->params[
'uploadServerMethod']=
'photos.getMarketUploadServer';
82 $this->params[
'saveMethod'] =
'photos.saveMarketPhoto';
83 $this->params[
'keyReference'] =
'PHOTO_BX_ID';
84 $this->params[
'keyPhotoVk'] =
'PHOTO_VK_ID';
85 $this->params[
'keyPhotoBx'] =
'PHOTO_BX_ID';
86 $this->params[
'keyPhotoUrl']=
'PHOTO_URL';
89 case self::TYPE_ALBUM_PHOTO:
90 $this->params[
'uploadServerMethod']=
'photos.getMarketAlbumUploadServer';
91 $this->params[
'saveMethod'] =
'photos.saveMarketAlbumPhoto';
92 $this->params[
'keyReference'] =
'SECTION_ID';
93 $this->params[
'keyPhotoVk'] =
'PHOTO_VK_ID';
94 $this->params[
'keyPhotoBx'] =
'PHOTO_BX_ID';
95 $this->params[
'keyPhotoUrl']=
'PHOTO_URL';
118 $photoSaveResults =
array();
122 foreach (
$data as $item)
125 if (!array_key_exists($this->params[
'keyPhotoBx'], $item) || empty($item[$this->params[
'keyPhotoBx']]))
131 $getServerParams =
array(
"group_id" => str_replace(
"-",
"", $this->vkGroupId));
132 if ($this->type ==
'PRODUCT_MAIN_PHOTO')
134 $getServerParams += self::setUploadServerMainPhotoParams($item[$this->params[
'keyPhotoBx']]);
137 $uploadServer = $this->api->run($this->params[
'uploadServerMethod'], $getServerParams);
139 $this->logger->addLog(
"Get photo upload server", [
140 'PARAMS' => $getServerParams,
141 'RESULT' => $uploadServer,
143 $uploadServer = $uploadServer[
"upload_url"];
147 $this->logger->addLog(
"Upload photo HTTP before",
array(
148 "UPLOAD_TYPE" => $this->type,
149 "ITEM" => array_key_exists(
"BX_ID", $item) ?
150 $item[
"BX_ID"] .
': ' . $item[
"NAME"] :
151 $item[
"SECTION_ID"] .
': ' . $item[
"TITLE"],
152 "PHOTO_BX_ID" => array_key_exists(
"PHOTO_MAIN_BX_ID",
153 $item) ? $item[
"PHOTO_MAIN_BX_ID"] : $item[
"PHOTO_BX_ID"],
154 "PHOTO_URL" => array_key_exists(
"PHOTO_MAIN_URL", $item) ? $item[
"PHOTO_MAIN_URL"] : $item[
"PHOTO_URL"],
155 "PHOTOS" => $item[
"PHOTOS"]
157 $uploadHttpResult = $this->uploadHttp($item, $uploadServer);
159 if($uploadHttpResult ===
false)
161 if(!$photoSaveResults[
'errors'])
163 $photoSaveResults[
'errors'] = [];
165 $photoSaveResults[
'errors'][] = $item[$this->params[
'keyReference']];
169 $savePhotoResult = $this->savePhoto($uploadHttpResult);
171 $photoSaveResults[$item[$this->params[
'keyReference']]] =
array(
172 $this->params[
'keyReference'] => $item[$this->params[
'keyReference']],
173 $this->params[
'keyPhotoVk'] => $savePhotoResult[0][
"id"],
178 return $photoSaveResults;
190 private function uploadHttp(
$data, $uploadServer)
193 "url" =>
$data[$this->params[
'keyPhotoUrl']],
194 "filename" =>
IO\Path::getName(
$data[$this->params[
'keyPhotoUrl']]),
195 "param_name" =>
'file',
200 $boundary = md5(rand() . time());
203 $request =
'--' . $boundary .
"\r\n";
205 $request .=
'Content-Type: application/octet-stream' .
"\r\n\r\n";
207 $request .=
'--' . $boundary .
"--\r\n";
209 $http->setHeader(
'Content-type',
'multipart/form-data; boundary=' . $boundary);
210 $http->setHeader(
'Content-length', strlen(
$request));
212 $this->logger->addLog(
"Upload photo HTTP params", [
213 'SERVER' => $uploadServer,
215 'FILE_OK' => $file ?
'Y' :
'N',
220 $this->logger->addLog(
"Upload photo HTTP response",
$result);
226 $this->logger->addError(
227 explode(
':',
$result[
'error'])[0] .
'_' . $this->type,
228 $data[$this->params[
'keyReference']]
235 if($this->timer !==
null && !$this->timer->check())
243 private function getFile(
string $url)
247 return $http->get(
$url);
256 private function savePhoto($uploadResult)
258 $photoSaveParams =
array(
259 "group_id" => str_replace(
'-',
'', $this->vkGroupId),
260 "photo" => $uploadResult[
"photo"],
261 "server" => $uploadResult[
"server"],
262 "hash" => $uploadResult[
"hash"],
266 if ($this->params[
'saveMethod'] ==
"photos.saveMarketPhoto")
268 if (isset($uploadResult[
"crop_hash"]) && $uploadResult[
"crop_hash"])
269 $photoSaveParams[
"crop_hash"] = $uploadResult[
"crop_hash"];
270 if (isset($uploadResult[
"crop_data"]) && $uploadResult[
"crop_data"])
271 $photoSaveParams[
"crop_data"] = $uploadResult[
"crop_data"];
274 return $this->api->run($this->params[
'saveMethod'], $photoSaveParams);
284 private static function setUploadServerMainPhotoParams($photoId)
289 $photoParams = \CFile::GetFileArray($photoId);
290 $w = $photoParams[
"WIDTH"];
291 $h = $photoParams[
"HEIGHT"];
295 $result[
"crop_x"] = ceil(($w + $h) / 2);
302 $result[
"crop_y"] = ceil(($w + $h) / 2);
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)