Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
IblockPropertySectionProvider.php
1<?php
2
4
10
12{
13 private const ENTITY_ID = 'iblock-property-section';
14 private const ELEMENTS_LIMIT = 100;
15
16 public function __construct(array $options = [])
17 {
18 parent::__construct();
19
20 $this->options = $options;
21 }
22
23 public function isAvailable(): bool
24 {
25 return $GLOBALS['USER']->isAuthorized();
26 }
27
28 public function getItems(array $ids): array
29 {
30 $items = [];
31
32 $filter = !empty($ids) ? ['ID' => $ids] : [];
33
34 foreach ($this->getElements($filter) as $element)
35 {
36 $items[] = $this->makeItem($element);
37 }
38
39 return $items;
40 }
41
42 public function fillDialog(Dialog $dialog): void
43 {
44 $dialog->loadPreselectedItems();
45
46 if ($dialog->getItemCollection()->count() > 0)
47 {
48 foreach ($dialog->getItemCollection() as $item)
49 {
50 $dialog->addRecentItem($item);
51 }
52 }
53
54 $recentItems = $dialog->getRecentItems()->getEntityItems(self::ENTITY_ID);
55 $recentItemsCount = count($recentItems);
56
57 if ($recentItemsCount < self::ELEMENTS_LIMIT)
58 {
59 $elements = $this->getElements([], self::ELEMENTS_LIMIT);
60 foreach ($elements 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, self::ELEMENTS_LIMIT);
78 if (count($elements) === self::ELEMENTS_LIMIT)
79 {
80 $searchQuery->setCacheable(false);
81 }
82 foreach ($elements as $element)
83 {
84 $dialog->addItem(
85 $this->makeItem($element)
86 );
87 }
88 }
89
90 public function getPreselectedItems(array $ids): array
91 {
92 return $this->getItems($ids);
93 }
94
95 private function getQueryFilter(string $query): array
96 {
97 return [
98 '%NAME' => $query,
99 ];
100 }
101
102 private function getElements(array $additionalFilter = [], ?int $limit = null): array
103 {
104 $elements = [];
105
106 $filter = $this->getDefaultFilter();
107 if (!empty($additionalFilter))
108 {
109 $filter = array_merge($filter, $additionalFilter);
110 }
111
112 $navParams = false;
113 if ($limit)
114 {
115 $navParams = ['nTopCount' => $limit];
116 }
117
118 $selectFields = [
119 'ID',
120 'NAME',
121 'DESCRIPTION',
122 'PICTURE',
123 'IBLOCK_ID',
124 'XML_ID',
125 ];
126
127 if (!empty($filter))
128 {
129 $elementData = \CIBlockSection::GetList(
130 [],
131 $filter,
132 false,
133 $selectFields,
134 $navParams,
135 );
136 while ($element = $elementData->fetch())
137 {
138 $element['PICTURE'] = $this->getImageSource((int)$element['PICTURE']);
139 $elements[] = $element;
140 }
141 }
142
143 return $elements;
144 }
145
146 private function makeItem(array $element): Item
147 {
148 $itemParams = [
149 'id' => $element['ID'] ?? null,
150 'entityId' => self::ENTITY_ID,
151 'title' => $element['NAME'] ?? null,
152 'subtitle' => $element['ID'] ?? null,
153 'description' => $element['DESCRIPTION'] ?? null,
154 'avatar' => $element['PICTURE'] ?? null,
155 'customData' => [
156 'xmlId' => $element['XML_ID'] ?? null,
157 ],
158 ];
159
160 return new Item($itemParams);
161 }
162
163 private function getDefaultFilter(): array
164 {
165 $filter = [
166 'CHECK_PERMISSIONS' => 'Y',
167 'MIN_PERMISSION' => 'R',
168 ];
169
170 $iblockId = (int)($this->getOption('iblockId', 0));
171 if (!empty($iblockId))
172 {
173 $filter['IBLOCK_ID'] = $iblockId;
174 }
175
176 return $filter;
177 }
178
179 private function getImageSource(int $id): ?string
180 {
181 if ($id <= 0)
182 {
183 return null;
184 }
185
186 $file = \CFile::GetFileArray($id);
187 if (!$file)
188 {
189 return null;
190 }
191
192 return Tools::getImageSrc($file, false) ?: null;
193 }
194}
getOption(string $option, $defaultValue=null)
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
$GLOBALS['____1444769544']
Definition license.php:1