1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
IblockPropertyElementProvider.php
См. документацию.
1<?php
2
3namespace Bitrix\Iblock\Integration\UI\EntitySelector;
4
5use Bitrix\Iblock\Component\Tools;
6use Bitrix\Iblock\PropertyTable;
7use Bitrix\UI\EntitySelector\BaseProvider;
8use Bitrix\UI\EntitySelector\Dialog;
9use Bitrix\UI\EntitySelector\Item;
10use Bitrix\UI\EntitySelector\SearchQuery;
11
13{
14 public 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,
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(): array
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 $activeFilter = $this->getOption('activeFilter', false);
217 if ($activeFilter)
218 {
219 $filter['ACTIVE'] = 'Y';
220 }
221
222 return $filter;
223 }
224
225 private function getMorePhotoPropertyId(int $iblockId): ?int
226 {
228 'select' => ['ID'],
229 'filter' => [
230 '=IBLOCK_ID' => $iblockId,
232 '=ACTIVE' => 'Y',
233 ],
234 ]);
235 if ($row = $iterator->fetch())
236 {
237 return (int)$row['ID'];
238 }
239
240 return null;
241 }
242
243 private function getImageSource(int $id): ?string
244 {
245 if ($id <= 0)
246 {
247 return null;
248 }
249
250 $file = \CFile::GetFileArray($id);
251 if (!$file)
252 {
253 return null;
254 }
255
256 return Tools::getImageSrc($file, false) ?: null;
257 }
258}
static getList(array $parameters=array())
Определения datamanager.php:431
getOption(string $option, $defaultValue=null)
Определения baseprovider.php:48
loadPreselectedItems($preselectedMode=true)
Определения dialog.php:410
addItem(Item $item)
Определения dialog.php:126
addRecentItem(Item $item)
Определения dialog.php:143
getItemCollection()
Определения dialog.php:116
setCacheable(bool $flag=true)
Определения searchquery.php:72
const CODE_MORE_PHOTO
Определения iblockproptools.php:11
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$iblockId
Определения iblock_catalog_edit.php:30
$filter
Определения iblock_catalog_list.php:54
$selectFields
Определения iblock_catalog_list.php:160
$navParams
Определения csv_new_run.php:35
$GLOBALS['____1690880296']
Определения license.php:1
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$items
Определения template.php:224
$iterator
Определения yandex_run.php:610