1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ElementProvider.php
См. документацию.
1<?php
2
4
11
13{
14 public const ENTITY_ID = 'highloadblock-element';
15
16 public const QUERY_SEARCH = 'S';
17 public const QUERY_SUBSTRING = 'L';
18 public const QUERY_BEGIN = 'B';
19
20 protected const ELEMENTS_LIMIT = 100;
21
22 private int $highloadblockId;
23
24 private ORM\Entity $highloadblock;
25
26 private array $fields;
27
39 public function __construct(array $options = [])
40 {
41 parent::__construct();
42
43 $this->options = array_filter(
45 function ($row)
46 {
47 return $row !== null;
48 }
49 );
50 }
51
52 public function isAvailable(): bool
53 {
54 global $USER;
55
56 if (!(isset($USER) && $USER instanceof \CUser))
57 {
58 return false;
59 }
60 if (!$USER->isAuthorized())
61 {
62 return false;
63 }
64
65 if (!$this->isHighloadblockExists())
66 {
67 return false;
68 }
69
70 if ($USER->IsAdmin())
71 {
72 return true;
73 }
74
75 if (!$this->canReadHighloadblock())
76 {
77 return false;
78 }
79
80 return true;
81 }
82
83 public function getItems(array $ids): array
84 {
85 if (!$this->isHighloadblockExists())
86 {
87 return [];
88 }
89
90 $items = [];
91
92 $elementList = $this->getElements([
93 'filter' => $this->getFilterByIds($ids),
94 ]);
95 foreach ($elementList as $element)
96 {
97 $items[] = $this->makeItem($element);
98 }
99
100 return $items;
101 }
102
103 public function getPreselectedItems(array $ids): array
104 {
105 return $this->getItems($ids);
106 }
107
108 public function fillDialog(Dialog $dialog): void
109 {
110 $dialog->loadPreselectedItems();
111
112 if ($dialog->getItemCollection()->count() > 0)
113 {
114 foreach ($dialog->getItemCollection() as $item)
115 {
116 $dialog->addRecentItem($item);
117 }
118 }
119
120 $recentItems = $dialog->getRecentItems()->getEntityItems(self::ENTITY_ID);
121 $recentItemsCount = count($recentItems);
122
123 if ($recentItemsCount < self::ELEMENTS_LIMIT)
124 {
125 $elementList = $this->getElements([
126 'filter' => [],
127 'limit' => self::ELEMENTS_LIMIT,
128 ]);
129 foreach ($elementList as $element)
130 {
131 $dialog->addRecentItem($this->makeItem($element));
132 }
133 }
134 }
135
136 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
137 {
138 $filter = $this->getQueryFilter($searchQuery);
139 if ($filter === null)
140 {
141 return;
142 }
143
144 $elementList = $this->getElements([
145 'filter' => $filter,
146 'limit' => self::ELEMENTS_LIMIT,
147 ]);
148 if (count($elementList) === self::ELEMENTS_LIMIT)
149 {
150 $searchQuery->setCacheable(false);
151 }
152 foreach ($elementList as $element)
153 {
154 $dialog->addItem(
155 $this->makeItem($element)
156 );
157 }
158 }
159
160 protected function getHighloadblockId(): ?int
161 {
162 if (!isset($this->highloadblockId))
163 {
164 $this->initHighloadblock();
165 }
166
167 return $this->highloadblockId > 0 ? $this->highloadblockId : null;
168 }
169
170 protected function initHighloadblock(): void
171 {
172 if (isset($this->highloadblockId))
173 {
174 return;
175 }
176
177 $this->highloadblockId = (int)$this->getOption('highloadblockId');
178 if ($this->highloadblockId > 0)
179 {
180 $hlblock = HighloadBlockTable::getRow([
181 'select' => [
182 'ID',
183 ],
184 'filter' => [
185 '=ID' => $this->highloadblockId,
186 ],
187 ]);
188 if ($hlblock === null)
189 {
190 $this->highloadblockId = 0;
191 }
192 }
193 if ($this->highloadblockId > 0)
194 {
195 $this->highloadblock = HighloadBlockTable::compileEntity($this->highloadblockId);
196 $this->fields = $this->highloadblock->getScalarFields();
197 }
198 }
199
200 protected function isHighloadblockExists(): bool
201 {
202 return $this->getHighloadblockId() !== null;
203 }
204
205 protected function canReadHighloadblock(): bool
206 {
207 $hlblockId = $this->getHighloadblockId();
208 if ($hlblockId === null)
209 {
210 return false;
211 }
212
213 return true;
214 }
215
216 protected function getDefaultValueField(): string
217 {
218 return 'UF_XML_ID';
219 }
220
221 protected function getDefaultTitleField(): string
222 {
223 return 'UF_NAME';
224 }
225
226 protected function getDefaultOrderField(): string
227 {
228 return 'UF_SORT';
229 }
230
231 protected function getDefaultQueryMethod(): string
232 {
233 return self::QUERY_BEGIN;
234 }
235
236 protected function getValueField(): ?string
237 {
238 $field = trim((string)$this->getOption('valueField', $this->getDefaultValueField()));
239
240 return $this->checkFieldName($field) ? $field: null;
241 }
242
243 protected function getTitleField(): ?string
244 {
245 $field = trim((string)$this->getOption('titleField', $this->getDefaultTitleField()));
246
247 return $this->checkFieldName($field) ? $field: null;
248 }
249
250 protected function getOrderField(): ?string
251 {
252 $field = trim((string)$this->getOption('orderField', $this->getDefaultOrderField()));
253
254 return $this->checkFieldName($field) ? $field: null;
255 }
256
257 protected function checkFieldName(string $fieldName): bool
258 {
259 if ($fieldName === '')
260 {
261 return false;
262 }
263
264 return isset($this->fields[$fieldName]);
265 }
266
267 protected function getDirection(): string
268 {
269 $result = mb_strtoupper(trim((string)$this->getOption('direction')));
270
271 return $result === 'DESC' ? 'DESC' : 'ASC';
272 }
273
274 protected function getQueryMethod(): string
275 {
276 return trim((string)$this->getOption('queryMethod', $this->getDefaultQueryMethod()));
277 }
278
280 {
281 $fields = [
282 'VALUE' => $this->getValueField(),
283 'TITLE' => $this->getTitleField(),
284 'ORDER' => $this->getOrderField(),
285 ];
286 if (in_array(null, $fields, true))
287 {
288 return null;
289 }
290
291 $select = [
292 'ID',
293 'VALUE' => $fields['VALUE'],
294 'TITLE' => $fields['TITLE'],
295 ];
296 $order = [
297 $fields['ORDER'] => $this->getDirection(),
298 ];
299 if ($fields['ORDER'] !== 'ID')
300 {
301 $select[] = $fields['ORDER'];
302 $order['ID'] = 'ASC';
303 }
304
305 $result = [
306 'select' => $select,
307 'filter' => $settings['filter'] ?? [],
308 'order' => $order,
309 ];
310
311 if (isset($settings['limit']))
312 {
313 $result['limit'] = $settings['limit'];
314 }
315
316 return $result;
317 }
318
319 protected function getQueryFilter(SearchQuery $searchQuery): ?array
320 {
321 $query = $searchQuery->getQuery();
322 if (mb_strlen($query) < 2)
323 {
324 return [];
325 }
326 $titleField = $this->getTitleField();
327 if ($titleField === null)
328 {
329 return null;
330 }
331
332 return match ($this->getQueryMethod())
333 {
334 self::QUERY_SEARCH => [
335 '*'.$titleField => $query,
336 ],
337 self::QUERY_SUBSTRING => [
338 '%'.$titleField => $query,
339 ],
340 default => [
341 $titleField => $query.'%',
342 ],
343 };
344 }
345
346 protected function getFilterByIds(array $ids): array
347 {
348 if (empty($ids))
349 {
350 return [];
351 }
352 $valueField = $this->getValueField();
353 if ($valueField === null)
354 {
355 return [];
356 }
357
358 return [
359 '@' . $valueField => $ids,
360 ];
361 }
362
363 protected function getElements(array $settings = []): array
364 {
366 if ($params === null)
367 {
368 return [];
369 }
370
371 $result = [];
372 $ormClass = $this->highloadblock->getDataClass();
373 $iterator = $ormClass::getList($params);
374 while ($row = $iterator->fetch())
375 {
376 $result[] = $row;
377 }
378 unset ($row, $iterator);
379
380 return $result;
381 }
382
383 protected function makeItem(array $row): Item
384 {
385 $item = [
386 'id' => $row['VALUE'],
387 'entityId' => self::ENTITY_ID,
388 'title' => $row['TITLE'],
389 ];
390
391 return new Item($item);
392 }
393}
doSearch(SearchQuery $searchQuery, Dialog $dialog)
Определения ElementProvider.php:136
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
</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
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
global $USER
Определения csv_new_run.php:40
Определения ufield.php:9
$order
Определения payment.php:8
$settings
Определения product_settings.php:43
</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
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$items
Определения template.php:224
$iterator
Определения yandex_run.php:610