Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
propertymanager.php
1<?php
3
7
8Loc::loadMessages(__FILE__);
9
11{
12 private $iblockId;
13 private $listProperty;
14 private $filterFields;
15 private $catalogIncluded;
16 private $catalog = null;
17
18 public function __construct($iblockId)
19 {
20 $this->iblockId = (int)$iblockId;
21 $this->listProperty = null;
22 $this->filterFields = null;
23 $this->catalogIncluded = Loader::includeModule('catalog');
24 if ($this->catalogIncluded)
25 {
26 $catalog = \CCatalogSku::GetInfoByIBlock($this->iblockId);
27 if (!empty($catalog))
28 {
29 $this->catalog = $catalog;
30 }
31 unset($catalog);
32 }
33 }
34
38 public function getFilterFields(): array
39 {
40 if ($this->filterFields === null)
41 {
42 $offers = (!empty($this->catalog['CATALOG_TYPE']) && $this->catalog['CATALOG_TYPE'] == \CCatalogSku::TYPE_OFFERS);
43
44 $this->filterFields = [];
45 $listProperty = $this->getListProperty();
46 foreach ($listProperty as $property)
47 {
48 $fieldId = $property['FIELD_ID'];
49 $fieldName = ($offers
50 ? Loc::getMessage('IBLOCK_PROPERTY_FILTER_MANAGER_MESS_OFFER_TITLE', ['#NAME#' => $property['NAME']])
51 : $property['NAME']
52 );
53
54 if (!empty($property['USER_TYPE']))
55 {
56 $field = array();
57 if (isset($property["PROPERTY_USER_TYPE"]["GetUIFilterProperty"]))
58 {
59 $field = [
60 'id' => $fieldId,
61 'name' => $fieldName,
62 'type' => 'custom',
63 'value' => '',
64 'filterable' => '',
65 ];
66 call_user_func_array(
67 $property["PROPERTY_USER_TYPE"]["GetUIFilterProperty"],
68 [
69 $property,
70 ["VALUE" => $fieldId, "FORM_NAME" => "main-ui-filter"],
71 &$field,
72 ]
73 );
74 }
75 elseif (isset($property["PROPERTY_USER_TYPE"]["GetPublicFilterHTML"]))
76 {
77 $field = [
78 'id' => $fieldId,
79 'name' => $fieldName,
80 'type' => 'custom',
81 'value' => call_user_func_array(
82 $property['PROPERTY_USER_TYPE']['GetPublicFilterHTML'],
83 [
84 $property,
85 [
86 'VALUE' => $fieldId,
87 'FORM_NAME' => 'main-ui-filter',
88 ],
89 ]
90 ),
91 'filterable' => '',
92 ];
93 }
94 elseif (isset($property["PROPERTY_USER_TYPE"]["GetAdminFilterHTML"]))
95 {
96 $field = [
97 'id' => $fieldId,
98 'name' => $fieldName,
99 'type' => 'custom',
100 'value' => call_user_func_array(
101 $property['PROPERTY_USER_TYPE']['GetAdminFilterHTML'],
102 [
103 $property,
104 [
105 'VALUE' => $fieldId,
106 'FORM_NAME' => 'main-ui-filter',
107 ],
108 ]
109 ),
110 'filterable' => '',
111 ];
112 }
113 if ($field === null)
114 {
115 continue;
116 }
117 elseif (empty($field))
118 {
119 $field = [
120 'id' => $fieldId,
121 'name' => $fieldName,
122 'filterable' => '',
123 ];
124 }
125 $this->filterFields[] = $field;
126 }
127 else
128 {
129 switch ($property['PROPERTY_TYPE'])
130 {
131 case Iblock\PropertyTable::TYPE_STRING:
132 $this->filterFields[] = [
133 'id' => $fieldId,
134 'name' => $fieldName,
135 'filterable' => '?',
136 ];
137 break;
138 case Iblock\PropertyTable::TYPE_NUMBER:
139 $this->filterFields[] = [
140 'id' => $fieldId,
141 'name' => $fieldName,
142 'type' => 'number',
143 'filterable' => '',
144 ];
145 break;
146 case Iblock\PropertyTable::TYPE_LIST:
147 $items = [
148 'NOT_REF' => Loc::getMessage('IBLOCK_PM_LIST_DEFAULT_OPTION'),
149 ];
150 $enumIterator = Iblock\PropertyEnumerationTable::getList([
151 'select' => [
152 'ID',
153 'VALUE',
154 'SORT',
155 ],
156 'filter' => [
157 '=PROPERTY_ID' => $property['ID'],
158 ],
159 'order' => [
160 'SORT' => 'ASC',
161 'VALUE' => 'ASC',
162 'ID' => 'ASC',
163 ],
164 ]);
165 while ($enumRow = $enumIterator->fetch())
166 {
167 $items[$enumRow['ID']] = $enumRow['VALUE'];
168 }
169 unset($enumRow, $enumIterator);
170 $this->filterFields[] = [
171 'id' => $fieldId,
172 'name' => $fieldName,
173 'type' => 'list',
174 'items' => $items,
175 'params' => [
176 'multiple' => 'Y',
177 ],
178 'filterable' => '',
179 ];
180 break;
181 case Iblock\PropertyTable::TYPE_ELEMENT:
182 $this->filterFields[] = [
183 'id' => $fieldId,
184 'name' => $fieldName,
185 'type' => 'custom_entity',
186 'filterable' => '',
187 'property' => $property,
188 'customRender' => [
189 'Bitrix\Iblock\Helpers\Filter\Property',
190 'render',
191 ],
192 'customFilter' => [
193 'Bitrix\Iblock\Helpers\Filter\Property',
194 'addFilter',
195 ],
196 ];
197 break;
198 case Iblock\PropertyTable::TYPE_SECTION:
199 $items = [];
200 $sectionQueryObject = \CIBlockSection::getList(
201 [
202 'LEFT_MARGIN' => 'ASC',
203 ],
204 [
205 'IBLOCK_ID' => $property['LINK_IBLOCK_ID'],
206 ],
207 false,
208 [
209 'ID',
210 'IBLOCK_ID',
211 'DEPTH_LEVEL',
212 'NAME',
213 'LEFT_MARGIN',
214 ]
215 );
216 while ($section = $sectionQueryObject->fetch())
217 {
218 $margin = max((int)$section['DEPTH_LEVEL'], 1) - 1;
219 $items[$section['ID']] = str_repeat('. ', $margin) . $section['NAME'];
220 }
221 unset($section, $sectionQueryObject);
222 $this->filterFields[] = [
223 'id' => $fieldId,
224 'name' => $fieldName,
225 'type' => 'list',
226 'items' => $items,
227 'params' => [
228 'multiple' => 'Y',
229 ],
230 'filterable' => '',
231 ];
232 unset($items);
233 break;
234 }
235 }
236 }
237 unset($property, $listProperty);
238 unset($offers);
239 }
240
241 return $this->filterFields;
242 }
243
248 public function renderCustomFields($filterId)
249 {
250 foreach ($this->getFilterFields() as $filterField)
251 {
252 if (isset($filterField['customRender']))
253 {
254 echo call_user_func_array(
255 $filterField['customRender'],
256 [
257 $filterId,
258 $filterField['property']['PROPERTY_TYPE'],
259 [
260 $filterField['property'],
261 ],
262 ]
263 );
264 }
265 }
266 }
267
273 public function AddFilter($filterId, array &$filter)
274 {
275 if (empty($filter))
276 {
277 return;
278 }
279
280 $listProperty = $this->getListProperty();
281 if (empty($listProperty))
282 {
283 return;
284 }
285
286 $filterFields = [];
287 foreach($this->getFilterFields() as $row)
288 {
289 $filterFields[$row['id']] = $row;
290 }
291
292 foreach (array_keys($filter) as $index)
293 {
294 $indexData = \CIBlock::MkOperationFilter($index);
295 $propertyId = $indexData['FIELD'];
296
297 if (!isset($listProperty[$propertyId]))
298 {
299 continue;
300 }
301
302 if (isset($filterFields[$propertyId]['customFilter']))
303 {
304 $row = $filterFields[$propertyId];
305 $filtered = false;
306 call_user_func_array(
307 $row['customFilter'],
308 [
309 $row['property'],
310 [
311 "VALUE" => $row["id"],
312 "FILTER_ID" => $filterId,
313 ],
314 &$filter,
315 &$filtered,
316 ]
317 );
318 unset($filtered);
319 unset($row);
320 }
321 elseif (isset($listProperty[$propertyId]['PROPERTY_USER_TYPE']['AddFilterFields']))
322 {
323 $filtered = false;
324 $row = $listProperty[$propertyId];
325 call_user_func_array(
326 $row['PROPERTY_USER_TYPE']['AddFilterFields'],
327 [
328 $row,
329 [
330 'VALUE' => $row['FIELD_ID'],
331 'FILTER_ID' => $filterId,
332 ],
333 &$filter,
334 &$filtered,
335 ]
336 );
337 unset($filtered);
338 unset($row);
339 }
340 else
341 {
342 switch ($listProperty[$propertyId]['PROPERTY_TYPE'])
343 {
344 case Iblock\PropertyTable::TYPE_STRING:
345 case Iblock\PropertyTable::TYPE_NUMBER:
346 $value = (string)$filter[$index];
347 if ($value === '')
348 {
349 unset($filter[$index]);
350 }
351 unset($value);
352 break;
353 case Iblock\PropertyTable::TYPE_LIST:
354 case Iblock\PropertyTable::TYPE_ELEMENT:
355 case IBlock\PropertyTable::TYPE_SECTION:
356 if (is_array($filter[$index]))
357 {
358 $newValues = [];
359 foreach ($filter[$index] as $value)
360 {
361 $newValues[] = ($value === 'NOT_REF' ? false : $value);
362 }
363 unset($filter[$index]);
364 if (!empty($newValues))
365 {
366 $filter['=' . $propertyId] = $newValues;
367 }
368 unset($newValues);
369 }
370 else
371 {
372 $value = $filter[$index];
373 unset($filter[$index]);
374 if ($value === 'NOT_REF')
375 {
376 $value = false;
377 }
378 $filter['='.$propertyId] = $value;
379 unset($value);
380 }
381 break;
382 }
383 }
384 }
385 unset($filterFields);
386 unset($listProperty);
387 }
388
392 private function getListProperty(): array
393 {
394 if ($this->listProperty === null)
395 {
396 $this->listProperty = [];
397 $iterator = Iblock\PropertyTable::getList([
398 'select' => ['*'],
399 'filter' => [
400 '=IBLOCK_ID' => $this->iblockId,
401 '=ACTIVE' => 'Y',
402 '=FILTRABLE' => 'Y',
403 ],
404 'order' => [
405 'SORT' => 'ASC',
406 'NAME' => 'ASC',
407 ],
408 ]);
409 while ($property = $iterator->fetch())
410 {
411 $property['FIELD_ID'] = 'PROPERTY_'.$property['ID'];
412 $property['USER_TYPE_SETTINGS'] = $property['USER_TYPE_SETTINGS_LIST'];
413 unset($property['USER_TYPE_SETTINGS_LIST']);
414 $property['PROPERTY_USER_TYPE'] = (!empty($property['USER_TYPE']) ?
415 \CIBlockProperty::GetUserType($property['USER_TYPE']) : []);
416 $this->listProperty[$property['FIELD_ID']] = $property;
417 }
418 unset($property, $iterator);
419 }
420
421 return $this->listProperty;
422 }
423}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29