Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
IblockPropertyElementProvider.php
1<?php
2
4
11
13{
14 protected const ENTITY_ID = 'iblock-property-element';
15 protected const ELEMENTS_LIMIT = 100;
16
17 public function __construct(array $options = [])
18 {
19 parent::__construct();
20
21 $this->options = $options;
22 }
23
24 public function isAvailable(): bool
25 {
26 return $GLOBALS['USER']->isAuthorized();
27 }
28
29 public function getItems(array $ids): array
30 {
31 $items = [];
32
33 $filter = !empty($ids) ? ['ID' => $ids] : [];
34
35 foreach ($this->getElements($filter) as $element)
36 {
37 $items[] = $this->makeItem($element);
38 }
39
40 return $items;
41 }
42
43 public function fillDialog(Dialog $dialog): void
44 {
45 $dialog->loadPreselectedItems();
46
47 if ($dialog->getItemCollection()->count() > 0)
48 {
49 foreach ($dialog->getItemCollection() as $item)
50 {
51 $dialog->addRecentItem($item);
52 }
53 }
54
55 $recentItems = $dialog->getRecentItems()->getEntityItems(self::ENTITY_ID);
56 $recentItemsCount = count($recentItems);
57
58 if ($recentItemsCount < self::ELEMENTS_LIMIT)
59 {
60 $elements = $this->getElements([], self::ELEMENTS_LIMIT);
61 foreach ($elements as $element)
62 {
63 $dialog->addRecentItem($this->makeItem($element));
64 }
65 }
66 }
67
68 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
69 {
70 $filter = [];
71
72 $query = $searchQuery->getQuery();
73 if ($query !== '')
74 {
75 $filter = $this->getQueryFilter($query);
76 }
77
78 $elements = $this->getElements($filter, self::ELEMENTS_LIMIT);
79 if (count($elements) === self::ELEMENTS_LIMIT)
80 {
81 $searchQuery->setCacheable(false);
82 }
83 foreach ($elements as $element)
84 {
85 $dialog->addItem(
86 $this->makeItem($element)
87 );
88 }
89 }
90
91 public function getPreselectedItems(array $ids): array
92 {
93 return $this->getItems($ids);
94 }
95
96 private function getQueryFilter(string $query): array
97 {
98 return [
99 '*SEARCHABLE_CONTENT' => $query,
100 ];
101 }
102
103 protected function getElements(array $additionalFilter = [], ?int $limit = null): array
104 {
105 $elements = [];
106
107 $filter = $this->getDefaultFilter();
108 if (!empty($additionalFilter))
109 {
110 $filter = array_merge($filter, $additionalFilter);
111 }
112
113 $navParams = false;
114 if ($limit)
115 {
116 $navParams = ['nTopCount' => $limit];
117 }
118
119 $selectFields = [
120 'ID',
121 'NAME',
122 'DETAIL_TEXT',
123 'PREVIEW_PICTURE',
124 'IBLOCK_ID',
125 'XML_ID',
126 ];
127
128 if (!empty($filter))
129 {
130 $elementData = \CIBlockElement::GetList(
131 [],
132 $filter,
133 false,
134 $navParams,
135 $selectFields
136 );
137 while ($element = $elementData->fetch())
138 {
139 if (empty($element['PREVIEW_PICTURE']))
140 {
141 $element['PREVIEW_PICTURE'] = $this->getElementImage($element);
142 }
143 $elements[] = $element;
144 }
145 }
146
147 return $elements;
148 }
149
150 protected function makeItem(array $element): Item
151 {
152 $itemParams = [
153 'id' => $element['ID'] ?? null,
154 'entityId' => self::ENTITY_ID,
155 'title' => $element['NAME'] ?? null,
156 'subtitle' => $element['ID'] ?? null,
157 'description' => $element['DETAIL_TEXT'] ?? null,
158 'avatar' => $element['PREVIEW_PICTURE'] ?? null,
159 'customData' => [
160 'xmlId' => $element['XML_ID'] ?? null,
161 ],
162 ];
163
164 return new Item($itemParams);
165 }
166
167 private function getElementImage(array $element): ?string
168 {
169 $iblockId = $element['IBLOCK_ID'] ?? null;
170 if (!$iblockId)
171 {
172 return '';
173 }
174
175 $photoPropertyId = $this->getMorePhotoPropertyId($iblockId);
176 if (!$photoPropertyId)
177 {
178 return '';
179 }
180
181 $propertyFilter = [
182 'ID' => $photoPropertyId,
183 ];
184 $result = \CIBlockElement::GetProperty($iblockId, $element['ID'], 'sort', 'asc', $propertyFilter)->Fetch();
185
186 if (empty($result['VALUE']))
187 {
188 return '';
189 }
190
191 if (is_array($result['VALUE']))
192 {
193 $imageId = (int)$result['VALUE'][0];
194 }
195 else
196 {
197 $imageId = (int)$result['VALUE'];
198 }
199
200 return $this->getImageSource($imageId);
201 }
202
203 private function getDefaultFilter()
204 {
205 $filter = [
206 'CHECK_PERMISSIONS' => 'Y',
207 'MIN_PERMISSION' => 'R',
208 ];
209
210 $iblockId = (int)($this->getOption('iblockId', 0));
211 if (!empty($iblockId))
212 {
213 $filter['IBLOCK_ID'] = $iblockId;
214 }
215
216 return $filter;
217 }
218
219 private function getMorePhotoPropertyId(int $iblockId): ?int
220 {
221 $iterator = PropertyTable::getList([
222 'select' => ['ID'],
223 'filter' => [
224 '=IBLOCK_ID' => $iblockId,
225 '=CODE' => \CIBlockPropertyTools::CODE_MORE_PHOTO,
226 '=ACTIVE' => 'Y',
227 ],
228 ]);
229 if ($row = $iterator->fetch())
230 {
231 return (int)$row['ID'];
232 }
233
234 return null;
235 }
236
237 private function getImageSource(int $id): ?string
238 {
239 if ($id <= 0)
240 {
241 return null;
242 }
243
244 $file = \CFile::GetFileArray($id);
245 if (!$file)
246 {
247 return null;
248 }
249
250 return Tools::getImageSrc($file, false) ?: null;
251 }
252}
static getList(array $parameters=array())
getOption(string $option, $defaultValue=null)
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
$GLOBALS['____1444769544']
Definition license.php:1