Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
product.php
1<?php
2
4
9
10Loc::loadMessages(__FILE__);
11
13{
15 protected $sectionsList;
16 private $result;
17
19 const DESCRIPTION_LENGHT_MAX = 3300; // it is not entirely accurate value, but in doc i can't find true info
20 const NAME_LENGHT_MIN = 4;
21 const NAME_LENGHT_MAX = 100;
22
27 public function __construct($exportId)
28 {
29 if (!isset($exportId) || $exportId == '')
30 throw new ArgumentNullException("EXPORT_ID");
31
32 $this->exportId = $exportId;
33 $this->sectionsList = new Vk\SectionsList($this->exportId);
34 }
35
36
43 public function convert($data)
44 {
45 $logger = new Vk\Logger($this->exportId);
46 $this->result = array();
47
48// get common SKU and notSKU data
49 $this->result = $this->getNotSkuItemData($data);
50
51// product WITH SKU
52 $offersDescription = '';
53 if (isset($data["OFFERS"]) && is_array($data["OFFERS"]) && !empty($data["OFFERS"]))
54 {
55 //adding desc and additional photos from SKU
56 $this->selectOfferProps = $data["SELECT_OFFER_PROPS"];
57 $this->result["PHOTOS_OFFERS"] = array();
58 $this->result["PHOTOS_OFFERS_FOR_VK"] = array();
59
60 $offersConverted = array();
61 foreach ($data["OFFERS"] as $offer)
62 {
63 $resultOffer = $this->getItemDataOffersOffer($offer);
64
65 if (!empty($resultOffer["PHOTOS"]))
66 $this->result["PHOTOS_OFFERS"] += $resultOffer["PHOTOS"];
67
68 if (!empty($resultOffer["PHOTOS_FOR_VK"]))
69 $this->result["PHOTOS_OFFERS_FOR_VK"] += $resultOffer["PHOTOS_FOR_VK"];
70
71 $offersConverted[] = $resultOffer;
72 }
73
74 $offersDescription = $this->createOffersDescriptionByPrices($offersConverted);
75 }
76
77// check price. After offers convertions price may be changed
78 if (!$this->result["PRICE"])
79 {
80 $logger->addError('PRODUCT_EMPTY_PRICE', $data["ID"]);
81
82 return NULL;
83 }
84
85// if exist offers descriptions - add title for them
86 if ($offersDescription <> '')
87 $this->result["description"] .= "\n\n" . Loc::getMessage("SALE_VK_PRODUCT_VARIANTS") . "\n" . $offersDescription;
88
89// sorted photos array in right order
90// todo: move this operation in Photoresizer
91 $photosSorted = $this->sortPhotosArray();
92
93// CHECK photo sizes and count
94 $photosChecked = Vk\PhotoResizer::checkPhotos($photosSorted, 'PRODUCT');
95 if (empty($photosChecked))
96 {
97 $logger->addError("PRODUCT_WRONG_PHOTOS", $data["ID"]);
98
99 return NULL;
100 }
101
102 $this->result["PHOTO_MAIN_BX_ID"] = $photosChecked["PHOTO_MAIN_BX_ID"];
103 $this->result["PHOTO_MAIN_URL"] = $photosChecked["PHOTO_MAIN_URL"];
104 $this->result["PHOTOS"] = $photosChecked["PHOTOS"];
105
106// add item to log, if image was be resized
107 if ($photosChecked['RESIZE_UP'])
108 $logger->addError('PRODUCT_PHOTOS_RESIZE_UP', $data["ID"]);
109 if ($photosChecked['RESIZE_DOWN'])
110 $logger->addError('PRODUCT_PHOTOS_RESIZE_DOWN', $data["ID"]);
111
112
113// cleaing DESCRIPTION
114 $this->result["description"] = html_entity_decode($this->result["description"]);
115 $this->result["description"] = preg_replace('/\t*/', '', $this->result["description"]);
116 $this->result["description"] = strip_tags($this->result["description"]);
117
118 $this->result['description'] = $this->validateDescription($this->result['description'], $logger);
119 $this->result["description"] = self::convertToUtf8($this->result["description"]);
120
121 $this->result['NAME'] = $this->validateName($this->result['NAME'], $logger);
122 $this->result['NAME'] = self::convertToUtf8($this->result['NAME']);
123
124 return array($data["ID"] => $this->result);
125 }
126
127
135 private function validateName($name, Vk\Logger $logger = NULL)
136 {
137 $newName = $name;
138
139 if (($length = self::matchLength($name)) < self::NAME_LENGHT_MIN)
140 {
141 $newName = self::extendString($name, $length, self::NAME_LENGHT_MIN);
142 if ($logger)
143 {
144 $logger->addError('PRODUCT_SHORT_NAME', $this->result["BX_ID"]);
145 }
146 }
147
148 if (($length = self::matchLength($name)) > self::NAME_LENGHT_MAX)
149 {
150 $newName = self::reduceString($name, $length, self::NAME_LENGHT_MAX);
151 if ($logger)
152 {
153 $logger->addError('PRODUCT_LONG_NAME', $this->result["BX_ID"]);
154 }
155 }
156
157 return $newName;
158 }
159
160
168 private function validateDescription($desc, Vk\Logger $logger = NULL)
169 {
170 $newDesc = $desc;
171
172 if (mb_strlen($desc) < self::DESCRIPTION_LENGHT_MIN)
173 {
174 $newDesc = $this->result['NAME'] . ': ' . $desc;
175 if (mb_strlen($newDesc) < self::DESCRIPTION_LENGHT_MIN)
176 {
177 $newDesc = self::mb_str_pad($newDesc, self::DESCRIPTION_LENGHT_MIN, self::PAD_STRING);
178// ending space trim fix
179 if ($newDesc[mb_strlen($newDesc) - 1] == ' ')
180 {
181 $newDesc .= self::PAD_STRING;
182 }
183 if ($logger)
184 {
185 $logger->addError('PRODUCT_SHORT_DESCRIPTION', $this->result["BX_ID"]);
186 }
187 }
188 }
189
190 if (mb_strlen($newDesc) > self::DESCRIPTION_LENGHT_MAX)
191 {
192 $newDesc = mb_substr($newDesc, 0, self::DESCRIPTION_LENGHT_MAX).'...';
193 }
194
195 return $newDesc;
196 }
197
198
207 private function createOffersDescriptionByPrices($offers)
208 {
209 $mainPrice = isset($this->result['PRICE']) && $this->result['PRICE'] ? $this->result['PRICE'] : 0;
210 $needSkuPriceDescription = false;
211
212// compare main price and SKU prices. Find minimum, check difference
213 foreach ($offers as $offer)
214 {
215 if ($offer['PRICE'])
216 {
217// if not set main price - get them from SKU prices
218 if ($mainPrice == 0)
219 $mainPrice = $offer['PRICE'];
220
221// add price to SKU descriptions only of prices is different
222 if ($offer['PRICE'] != $mainPrice)
223 $needSkuPriceDescription = true;
224
225 $mainPrice = ($mainPrice != 0) ? min($offer['PRICE'], $mainPrice) : $offer['PRICE'];
226 }
227 }
228
229// update SKU DESRIPTIONS if needed
230 $offersDescription = '';
231 if ($needSkuPriceDescription)
232 {
233 foreach ($offers as $offer)
234 {
235 $offersDescription .= $offer["DESCRIPTION_PROPERTIES"] . " - " . Loc::getMessage("SALE_VK_PRODUCT_PRICE") . " " . $offer['PRICE'] . " " . Loc::getMessage("SALE_VK_PRODUCT_CURRENCY") . "\n";
236 }
237 }
238
239 else
240 {
241 foreach ($offers as $offer)
242 {
243 $offersDescription .= $offer["DESCRIPTION_PROPERTIES"] . "\n";
244 }
245 }
246
247 $this->result['PRICE'] = $mainPrice;
248
249 return $offersDescription;
250 }
251
252
258 private function sortPhotosArray()
259 {
260 $newPhotos = array();
261 if (isset($this->result['PHOTOS_FOR_VK']) && !empty($this->result['PHOTOS_FOR_VK']))
262 $newPhotos += $this->result['PHOTOS_FOR_VK'];
263
264 if (isset($this->result['PHOTOS_OFFERS_FOR_VK']) && !empty($this->result['PHOTOS_OFFERS_FOR_VK']))
265 $newPhotos += $this->result['PHOTOS_OFFERS_FOR_VK'];
266
267 if (isset($this->result['PHOTO_MAIN']) && !empty($this->result['PHOTO_MAIN']))
268 $newPhotos += $this->result['PHOTO_MAIN'];
269
270 if (isset($this->result['PHOTOS']) && !empty($this->result['PHOTOS']))
271 $newPhotos += $this->result['PHOTOS'];
272
273 if (isset($this->result['PHOTOS_OFFERS']) && !empty($this->result['PHOTOS_OFFERS']))
274 $newPhotos += $this->result['PHOTOS_OFFERS'];
275
276// delete wasted photos
277 unset(
278 $this->result['PHOTOS_FOR_VK'],
279 $this->result['PHOTOS_OFFERS_FOR_VK'],
280 $this->result['PHOTOS'],
281 $this->result['PHOTO_MAIN'],
282 $this->result['PHOTOS_OFFERS']
283 );
284
285 return $newPhotos;
286 }
287
288
297 private function getItemDataOffersOffer($data)
298 {
299 $result = array("DESCRIPTION" => "");
300
301// create description for SKU PROPERTIES
302 $propertyDescriptions = array();
303 foreach ($this->selectOfferProps as $prop)
304 {
305 if ($propValue = $data["PROPERTIES"][$prop]["VALUE"])
306 {
307// check if HIGLOADBLOCKS
308 if ($data["PROPERTIES"][$prop]["USER_TYPE"] == 'directory')
309 {
310 if (\CModule::IncludeModule('highloadblock'))
311 {
312// get ID for hl-block
313 $resHlBlocks = HighloadBlockTable::getList(array(
314 'filter' => array('=TABLE_NAME' => $data["PROPERTIES"][$prop]["USER_TYPE_SETTINGS"]["TABLE_NAME"]),
315 ));
316 $hlBlockItemId = $resHlBlocks->fetch();
317 $hlBlockItemId = $hlBlockItemId['ID'];
318// HL directory may not exist in some strange situations
319 if(!$hlBlockItemId)
320 continue;
321
322// get entity class for current hl
323 $hlBlock = HighloadBlockTable::getById($hlBlockItemId)->fetch();
324 $hlEntity = HighloadBlockTable::compileEntity($hlBlock);
325 $strEntityDataClass = $hlEntity->getDataClass();
326
327// get value for current hl
328 $resData = $strEntityDataClass::getList(array(
329 'select' => array('ID', 'UF_NAME'),
330 'filter' => array('=UF_XML_ID' => $propValue),
331 ));
332 $propValue = $resData->fetch();
333 $propValue = $propValue['UF_NAME'];
334 }
335 }
336
337 if(is_array($propValue))
338 $propValue = implode(', ', $propValue);
339
340 $propertyDescriptions[] = $data["PROPERTIES"][$prop]["NAME"] . ": " . $propValue;
341 }
342 }
343 if (!empty($propertyDescriptions))
344 $result["DESCRIPTION_PROPERTIES"] = implode("; ", $propertyDescriptions);
345
346// adding MAIN DESCRIPTION
347 $description = strip_tags($data["~DETAIL_TEXT"] <> '' ? $data["~DETAIL_TEXT"] : $data["~PREVIEW_TEXT"]);
348 if ($description)
349 $result["DESCRIPTION"] .= $description;
350
351// adding PRICE. Ib desc we adding prices later
352 $result['PRICE'] = $data["PRICES"]["MIN_RUB"];
353
354// adding PHOTOS
355 $photoId = ($data["DETAIL_PICTURE"] <> '') ? $data["DETAIL_PICTURE"] : $data["PREVIEW_PICTURE"];
356 if ($photoId)
357 $result["PHOTOS"] = array($photoId => array("PHOTO_BX_ID" => $photoId));
358
359// adding special VK photos
360 $vkPhotosKey = 'PHOTOS_FOR_VK_' . $data["IBLOCK_ID"];
361 $resOfferProps = new \_CIBElement();
362 $resOfferProps->fields = array("IBLOCK_ID" => $data["IBLOCK_ID"], "ID" => $data["ID"]);
363 $resOfferProps = $resOfferProps->GetProperties(array(), array("CODE" => $vkPhotosKey));
364 if (!empty($resOfferProps[$vkPhotosKey]["VALUE"]))
365 {
366 foreach ($resOfferProps[$vkPhotosKey]["VALUE"] as $ph)
367 {
368 $result["PHOTOS_FOR_VK"][$ph] = array(
369 "PHOTO_BX_ID" => $ph,
370 );
371 }
372 }
373
374 return $result;
375 }
376
377
384 private function getNotSkuItemData($data)
385 {
386 $result = array();
387 $result["BX_ID"] = $data["ID"];
388 $result["IBLOCK_ID"] = $data["IBLOCK_ID"];
389 $result["NAME"] = $data["~NAME"];
390 $result["SECTION_ID"] = $data["IBLOCK_SECTION_ID"];
391 $result["CATEGORY_VK"] = $this->sectionsList->getVkCategory($data["IBLOCK_SECTION_ID"]);
392
393// todo: DELETED should depended by AVAILABLE
394 $result["deleted"] = 0;
395 $result["PRICE"] = $data["PRICES"]["MIN_RUB"]; // price converted in roubles
396 $result["description"] = $data["~DETAIL_TEXT"] <> '' ? $data["~DETAIL_TEXT"] : $data["~PREVIEW_TEXT"];
397 $result["description"] = trim(preg_replace('/\s{2,}/', "\n", $result["description"]));
398// get main photo from preview or detail
399 $photoMainBxId = $data["DETAIL_PICTURE"] <> '' ? $data["DETAIL_PICTURE"] : $data["PREVIEW_PICTURE"];
400 $photoMainUrl = $data["DETAIL_PICTURE_URL"] <> '' ? $data["DETAIL_PICTURE_URL"] : $data["PREVIEW_PICTURE_URL"];
401 if ($photoMainBxId && $photoMainUrl)
402 $result["PHOTO_MAIN"] = array(
403 $photoMainBxId => array(
404 "PHOTO_BX_ID" => $photoMainBxId,
405 "PHOTO_URL" => $photoMainUrl,
406 ),
407 );
408
409// adding MORE PHOTOS to the all_photos array/ Later we will checked sizes
410 if (isset($data["PROPERTIES"]["MORE_PHOTO"]["VALUE"]) &&
411 is_array($data["PROPERTIES"]["MORE_PHOTO"]["VALUE"]) &&
412 !empty($data["PROPERTIES"]["MORE_PHOTO"]["VALUE"])
413 )
414 {
415 foreach ($data["PROPERTIES"]["MORE_PHOTO"]["VALUE"] as $ph)
416 {
417 $result["PHOTOS"][$ph] = array("PHOTO_BX_ID" => $ph);
418 }
419 }
420
421// take special VK photos
422 $vkPhotosKey = 'PHOTOS_FOR_VK_' . $data["IBLOCK_ID"];
423 if (isset($data["PROPERTIES"][$vkPhotosKey]["VALUE"]) &&
424 is_array($data["PROPERTIES"][$vkPhotosKey]["VALUE"]) &&
425 !empty($data["PROPERTIES"][$vkPhotosKey]["VALUE"])
426 )
427 {
428 foreach ($data["PROPERTIES"][$vkPhotosKey]["VALUE"] as $ph)
429 {
430 $result["PHOTOS_FOR_VK"][$ph] = array(
431 "PHOTO_BX_ID" => $ph,
432 );
433 }
434 }
435
436 return $result;
437 }
438}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29