Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PropertyProduct.php
1<?php
2
3namespace Bitrix\Catalog\UI;
4
9use Bitrix\Crm;
12
14{
15 private const PRICE_PRECISION = 2;
16
26 public static function getSkuProperties(int $iblockId, int $skuId, array $filter = []): array
27 {
28 $properties = self::getIblockProperties($iblockId, $skuId, $filter);
29 $product = Catalog\ProductTable::getRow([
30 'select' => [
31 'SKU_NAME' => 'IBLOCK_ELEMENT.NAME',
32 'SKU_DESCRIPTION' => 'IBLOCK_ELEMENT.DETAIL_TEXT',
33 'PURCHASING_PRICE',
34 'PURCHASING_CURRENCY',
35 'LENGTH',
36 'WIDTH',
37 'HEIGHT',
38 'WEIGHT',
39 ],
40 'filter' => ['=ID' => $skuId],
41 ]);
42
43 $properties['SKU_ID'] = $skuId;
44
45 if ($product !== null)
46 {
47 $properties['PURCHASING_PRICE'] = round((float)$product['PURCHASING_PRICE'], self::PRICE_PRECISION);
48 if (Loader::includeModule('crm'))
49 {
50 $properties['PURCHASING_PRICE_FORMATTED'] = \CCrmCurrency::MoneyToString(
51 $product['PURCHASING_PRICE'],
52 $product['PURCHASING_CURRENCY']
53 );
54 }
55 elseif (Loader::includeModule('currency'))
56 {
57 $properties['PURCHASING_PRICE_FORMATTED'] = \CCurrencyLang::CurrencyFormat(
58 $product['PURCHASING_PRICE'],
59 $product['PURCHASING_CURRENCY']
60 );
61 }
62 else
63 {
64 $properties['PURCHASING_PRICE_FORMATTED'] = htmlspecialcharsbx(
65 $product['PURCHASING_PRICE'] . ' ' . $product['PURCHASING_CURRENCY']
66 );
67 }
68 $properties['LENGTH'] = $product['LENGTH'];
69 $properties['WEIGHT'] = $product['WEIGHT'];
70 $properties['WIDTH'] = $product['WIDTH'];
71 $properties['HEIGHT'] = $product['HEIGHT'];
72 $properties['SKU_NAME'] = htmlspecialcharsbx($product['SKU_NAME']);
73 $properties['SKU_DESCRIPTION'] = (new \CBXSanitizer())->SanitizeHtml($product['SKU_DESCRIPTION']);
74 }
75 else
76 {
77 $properties['PURCHASING_PRICE'] = 0;
78 $properties['PURCHASING_PRICE_FORMATTED'] = '';
79 $properties['LENGTH'] = null;
80 $properties['WEIGHT'] = null;
81 $properties['WIDTH'] = null;
82 $properties['HEIGHT'] = null;
83 $properties['SKU_NAME'] = '';
84 $properties['SKU_DESCRIPTION'] = '';
85 }
86
87 return $properties;
88 }
89
99 public static function getIblockProperties(int $iblockId, int $productId, array $filter = []): array
100 {
101 $result = [];
102
103 $filter['ACTIVE'] = 'Y';
104
105 $props = \CIBlockElement::GetProperty($iblockId, $productId, 'id', 'asc', $filter);
106 while ($prop = $props->GetNext())
107 {
108 if (empty($prop['VALUE'])
109 && !($prop['PROPERTY_TYPE'] === 'L' && $prop['LIST_TYPE'] === 'C')
110 )
111 {
112 continue;
113 }
114
115 $code = 'PROPERTY_' . $prop['ID'];
116
117 switch ($prop['PROPERTY_TYPE'])
118 {
119 case PropertyTable::TYPE_STRING:
120 case PropertyTable::TYPE_NUMBER:
121 if ($prop['USER_TYPE'] === PropertyTable::USER_TYPE_DIRECTORY
122 && isset($prop['USER_TYPE_SETTINGS']['TABLE_NAME'])
123 && Loader::includeModule('highloadblock')
124 )
125 {
126 $value = self::getDirectoryValue($prop);
127 }
128 else if ($prop['USER_TYPE'] === PropertyTable::USER_TYPE_HTML)
129 {
130 $value = (new \CBXSanitizer())->SanitizeHtml($prop['~VALUE']['TEXT']);
131 }
132 else
133 {
134 $value = $prop['VALUE'];
135 }
136
137 if (!isset($result[$code]))
138 {
139 $result[$code] = $value;
140 }
141 else
142 {
143 $result[$code] .= ', ' . $value;
144 }
145
146 break;
147 case PropertyTable::TYPE_LIST:
148 if ($prop['LIST_TYPE'] === PropertyTable::CHECKBOX)
149 {
150 switch ($prop['VALUE_ENUM'])
151 {
152 case 'Y':
153 $value = Loc::getMessage('CRM_ENTITY_PRODUCT_LIST_COLUMN_CHECKBOX_YES');
154 break;
155 case 'N':
156 case '':
157 $value = Loc::getMessage('CRM_ENTITY_PRODUCT_LIST_COLUMN_CHECKBOX_NO');
158 break;
159 default:
160 $value = htmlspecialcharsbx($prop['VALUE_ENUM']);
161 }
162 $result[$code] = $value;
163
164 break;
165 }
166
167 if ($prop['MULTIPLE'] !== 'Y')
168 {
169 $result[$code] = $prop['VALUE_ENUM'];
170
171 break;
172 }
173
174 if (!isset($result[$code]))
175 {
176 $result[$code] = $prop['VALUE_ENUM'];
177 }
178 else
179 {
180 $result[$code] .= ', ' . $prop['VALUE_ENUM'];
181 }
182
183 break;
184 case PropertyTable::TYPE_FILE:
185 Loader::includeModule('fileman'); // always exists
186 $listImageSize = (int)Option::get('iblock', 'list_image_size');
187 $minImageSize = [
188 'W' => 1,
189 'H' => 1,
190 ];
191 $maxImageSize = [
192 'W' => $listImageSize,
193 'H' => $listImageSize,
194 ];
195 $result[$code] ??= '';
196 $result[$code] .= \CFileInput::Show(
197 'NO_FIELDS[' . $productId . ']',
198 $prop['VALUE'],
199 [
200 'IMAGE' => 'Y',
201 'PATH' => 'Y',
202 'FILE_SIZE' => 'Y',
203 'DIMENSIONS' => 'Y',
204 'IMAGE_POPUP' => 'N',
205 'MAX_SIZE' => $maxImageSize,
206 'MIN_SIZE' => $minImageSize,
207 ],
208 [
209 'upload' => false,
210 'medialib' => false,
211 'file_dialog' => false,
212 'cloud' => false,
213 'del' => false,
214 'description' => false,
215 ]
216 );
217
218 break;
219 default:
220 $result[$code] = htmlspecialcharsbx($prop['VALUE']);
221 }
222 }
223
224 return $result;
225 }
226
234 private static function getDirectoryValue(array $prop): ?string
235 {
236 $hlblock = HighloadBlockTable::getRow([
237 'filter' => [
238 '=TABLE_NAME' => $prop['USER_TYPE_SETTINGS']['TABLE_NAME'],
239 ],
240 ]);
241
242 if ($hlblock)
243 {
244 $entity = HighloadBlockTable::compileEntity($hlblock);
245 $entityClass = $entity->getDataClass();
246 $row = $entityClass::getRow([
247 'filter' => [
248 '=UF_XML_ID' => $prop['VALUE'],
249 ],
250 ]);
251
252 if (isset($row['UF_NAME']))
253 {
254 return htmlspecialcharsbx($row['UF_NAME']);
255 }
256
257 return null;
258 }
259
260 return null;
261 }
262
268 public static function getColumnNames(): array
269 {
270 $result = [];
271 $iterator = PropertyTable::getList([
272 'select' => [
273 'ID',
274 'IBLOCK_ID',
275 'SORT',
276 'NAME',
277 ],
278 'filter' => [
279 '=IBLOCK_ID' => self::getIblockIds(),
280 '=ACTIVE' => 'Y',
281 [
282 'LOGIC' => 'OR',
283 '==USER_TYPE' => null,
284 '=USER_TYPE' => '',
285 '@USER_TYPE' => self::getAllowedPropertyUserTypes(),
286 ],
287 '!@PROPERTY_TYPE' => self::getRestrictedPropertyTypes(),
288 '!@CODE' => self::getRestrictedProperties(),
289 ],
290 'order' => [
291 'IBLOCK_ID' => 'ASC',
292 'SORT' => 'ASC',
293 'NAME' => 'ASC',
294 ],
295 ]);
296
297 while ($prop = $iterator->fetch())
298 {
299 $result[] = 'PROPERTY_' . $prop['ID'];
300 }
301 unset($iterator);
302
303 $skuFields = [
304 'SKU_ID',
305 'SKU_NAME',
306 'SKU_DESCRIPTION',
307 'LENGTH',
308 'WIDTH',
309 'HEIGHT',
310 'WEIGHT',
311 ];
312
313 return array_merge($result, $skuFields);
314 }
315
322 public static function getRestrictedPropertyTypes(): array
323 {
324 return [
325 PropertyTable::TYPE_ELEMENT,
326 PropertyTable::TYPE_SECTION,
327 ];
328 }
329
336 public static function getRestrictedProperties(): array
337 {
338 return [
339 'MORE_PHOTO',
340 'BLOG_POST_ID',
341 'BLOG_COMMENTS_CNT',
342 ];
343 }
344
351 public static function getAllowedPropertyUserTypes(): array
352 {
353 return [
354 PropertyTable::USER_TYPE_DATE,
355 PropertyTable::USER_TYPE_DATETIME,
356 PropertyTable::USER_TYPE_DIRECTORY,
357 PropertyTable::USER_TYPE_HTML,
358 ];
359 }
360
364 private static function getIblockIds(): array
365 {
366 if (Loader::includeModule('crm'))
367 {
368 return [
369 Crm\Product\Catalog::getDefaultId(),
370 Crm\Product\Catalog::getDefaultOfferId(),
371 ];
372 }
373
374 return [];
375 }
376}
static getIblockProperties(int $iblockId, int $productId, array $filter=[])
static getSkuProperties(int $iblockId, int $skuId, array $filter=[])
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29