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