Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
SectionProvider.php
1<?php
2
4
9
11{
12 private const SECTION_LIMIT = 20;
13 protected const SECTION_ENTITY_ID = 'section';
14
15 public function __construct(array $options = [])
16 {
17 parent::__construct();
18
19 $this->options = $options;
20 }
21
22 public function isAvailable(): bool
23 {
24 return $GLOBALS['USER']->isAuthorized();
25 }
26
27 public function getItems(array $ids): array
28 {
29 $items = [];
30
31 $filter = !empty($ids) ? ['ID' => $ids] : [];
32
33 foreach ($this->getActiveSections($filter) as $section)
34 {
35 $items[] = $this->makeItem($section);
36 }
37
38 return $items;
39 }
40
41 public function getSelectedItems(array $ids): array
42 {
43 $selectedItems = [];
44
45 $filter = !empty($ids) ? ['ID' => $ids] : [];
46
47 foreach ($this->getSections($filter) as $section)
48 {
49 $selectedItems[] = $this->makeItem($section);
50 }
51
52 return $selectedItems;
53 }
54
55 public function fillDialog(Dialog $dialog): void
56 {
57 $dialog->loadPreselectedItems();
58
59 if ($dialog->getItemCollection()->count() > 0)
60 {
61 foreach ($dialog->getItemCollection() as $item)
62 {
63 $dialog->addRecentItem($item);
64 }
65 }
66
67 $recentItemsCount = count($dialog->getRecentItems()->getEntityItems(static::SECTION_ENTITY_ID));
68
69 if ($recentItemsCount < self::SECTION_LIMIT)
70 {
71 foreach ($this->getActiveSections() as $section)
72 {
73 $dialog->addRecentItem(
74 $this->makeItem($section)
75 );
76 }
77 }
78 }
79
80 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
81 {
82 $filter = [];
83
84 $query = $searchQuery->getQuery();
85 if ($query !== '')
86 {
87 $filter['%NAME'] = $query;
88 }
89
90 foreach ($this->getActiveSections($filter) as $section)
91 {
92 $dialog->addItem(
93 $this->makeItem($section)
94 );
95 }
96
97 if ($dialog->getItemCollection()->count() >= self::SECTION_LIMIT)
98 {
99 $searchQuery->setCacheable(false);
100 }
101 }
102
103 protected function getActiveSections(array $additionalFilter = []): array
104 {
105 return $this->getSections(array_merge(['=ACTIVE' => 'Y'], $additionalFilter));
106 }
107
108 protected function getSections(array $additionalFilter = []): array
109 {
110 $sections = [];
111
112 $filter = $this->getDefaultFilter();
113 if (!empty($additionalFilter))
114 {
115 $filter = array_merge($filter, $additionalFilter);
116 }
117
118 if (!empty($filter))
119 {
120 $sectionData = \CIBlockSection::GetList(
121 [],
122 $filter,
123 false,
124 ['ID', 'NAME', 'PICTURE'],
125 [
126 'nTopCount' => self::SECTION_LIMIT,
127 ]
128 );
129 while ($section = $sectionData->fetch())
130 {
131 if (!empty($section['PICTURE']))
132 {
133 $section['PICTURE'] = \CFile::resizeImageGet(
134 $section['PICTURE'],
135 [
136 'width' => 100,
137 'height' => 100,
138 ],
139 BX_RESIZE_IMAGE_EXACT,
140 false
141 )['src'];
142 }
143
144 $sections[] = $section;
145 }
146 }
147
148 return $sections;
149 }
150
151 protected function makeItem(array $section): Item
152 {
153 return new Item([
154 'id' => $section['ID'],
155 'entityId' => static::SECTION_ENTITY_ID,
156 'title' => $section['NAME'],
157 'avatar' => $section['PICTURE'],
158 ]);
159 }
160
161 private function getDefaultFilter(): array
162 {
163 $filter = [];
164
165 $iblockId = (int)($this->getOptions()['iblockId'] ?? 0);
166 if (!empty($iblockId))
167 {
168 $filter['IBLOCK_ID'] = $iblockId;
169 }
170
171 return $filter;
172 }
173}
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
$GLOBALS['____1444769544']
Definition license.php:1