Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
itemfinder.php
1<?php
2
4
9
10trait ItemFinder
11{
12 abstract protected function getIblockId(): int;
13
14 abstract protected function getListMode(): string;
15
16 abstract protected function getRequestRows(HttpRequest $request): ?array;
17
18 protected function prepareItemIds(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter = null): array
19 {
20 if ($isSelectedAllRows)
21 {
22 return $this->getItemIdsByFilter($filter);
23 }
24 else
25 {
26 $ids = $this->getRequestRows($request);
27 if (empty($ids))
28 {
29 return [[], []];
30 }
31
32 [$elementIds, $sectionIds] = RowType::parseIndexList($ids);
33
34 return [
35 $this->validateElementIds($elementIds),
36 $this->validateSectionIds($sectionIds)
37 ];
38 }
39 }
40
41 private function getPageSize(): int
42 {
43 return 500;
44 }
45
46 protected function getBaseFilter(): array
47 {
48 return [
49 'IBLOCK_ID' => $this->getIblockId(),
50 'CHECK_PERMISSIONS' => 'Y',
51 'MIN_PERMISSION' => 'R',
52 ];
53 }
54
55 protected function getItemIdsByFilter(?Filter $filter = null): array
56 {
57 $itemFilter = $this->getBaseFilter();
58
59 if ($filter !== null)
60 {
61 $itemFilter += $filter->getValue();
62 }
63
64 $elementIds = [];
65 $sectionIds = [];
66
67 $iterator = $this->getItemsList([
68 'select' => ['ID'],
69 'filter' => $itemFilter,
70 ]);
71 while ($row = $iterator->Fetch())
72 {
73 $id = (int)$row['ID'];
74 if (($row['TYPE'] ?? RowType::ELEMENT) === RowType::SECTION)
75 {
76 $sectionIds[] = $id;
77 }
78 else
79 {
80 $elementIds[] = $id;
81 }
82 }
83 unset($row, $iterator);
84
85 return [$elementIds, $sectionIds];
86 }
87
88 protected function getItemsByFilter(array $select, ?Filter $filter = null): array
89 {
90 $select[] = 'ID';
91 $select = array_values(array_unique($select));
92
93 $itemFilter = $this->getBaseFilter();
94
95 if ($filter !== null)
96 {
97 $itemFilter += $filter->getValue();
98 }
99
100 $elements = [];
101 $sections = [];
102
103 $iterator = $this->getItemsList([
104 'select' => $select,
105 'filter' => $itemFilter,
106 ]);
107 while ($row = $iterator->Fetch())
108 {
109 $row['ID'] = (int)$row['ID'];
110 if (($row['TYPE'] ?? RowType::ELEMENT) === RowType::SECTION)
111 {
112 $sections[] = $row;
113 }
114 else
115 {
116 $elements[] = $row;
117 }
118 }
119 unset($row, $iterator);
120
121 return [$elements, $sections];
122 }
123
124 protected function getElementIdsByFilter(?Filter $filter = null): array
125 {
126 $itemFilter = $this->getBaseFilter();
127
128 if ($filter !== null)
129 {
130 $itemFilter += $filter->getValue();
131 }
132
133 $elementIds = [];
134
135 $iterator = \CIBlockElement::GetList(
136 [],
137 $itemFilter,
138 false,
139 false,
140 ['ID']
141 );
142 while ($row = $iterator->Fetch())
143 {
144 $elementIds[] = (int)$row['ID'];
145 }
146 unset($row, $iterator);
147
148 return $elementIds;
149 }
150
151 protected function validateElementIds(array $elementIds): array
152 {
153 if (empty($elementIds))
154 {
155 return [];
156 }
157
158 $itemFilter = $this->getBaseFilter();
159
160 $result = [];
161
162 foreach (array_chunk($elementIds, $this->getPageSize()) as $pageIds)
163 {
164 $itemFilter['ID'] = $pageIds;
165 $iterator = \CIBlockElement::GetList(
166 [],
167 $itemFilter,
168 false,
169 false,
170 ['ID']
171 );
172 while ($row = $iterator->Fetch())
173 {
174 $result[] = (int)$row['ID'];
175 }
176 unset($row, $iterator);
177 }
178
179 return $result;
180 }
181
182 protected function validateSectionIds(array $sectionIds): array
183 {
184 if (empty($sectionIds))
185 {
186 return [];
187 }
188
189 $itemFilter = $this->getBaseFilter();
190
191 $result = [];
192
193 foreach (array_chunk($sectionIds, $this->getPageSize()) as $pageIds)
194 {
195 $itemFilter['ID'] = $pageIds;
196 $iterator = \CIBlockSection::GetList(
197 [],
198 $itemFilter,
199 false,
200 ['ID']
201 );
202 while ($row = $iterator->Fetch())
203 {
204 $result[] = (int)$row['ID'];
205 }
206 unset($row, $iterator);
207 }
208
209 return $result;
210 }
211
212 protected function getElementsByIdList(array $select, array $elementIds): array
213 {
214 if (empty($elementIds))
215 {
216 return [];
217 }
218 $select[] = 'ID';
219 $select = array_values(array_unique($select));
220
221 $itemFilter = $this->getBaseFilter();
222
223 $result = [];
224
225 foreach (array_chunk($elementIds, $this->getPageSize()) as $pageIds)
226 {
227 $itemFilter['ID'] = $pageIds;
228 $iterator = \CIBlockElement::GetList(
229 [],
230 $itemFilter,
231 false,
232 false,
233 $select
234 );
235 while ($row = $iterator->Fetch())
236 {
237 $row['ID'] = (int)$row['ID'];
238 $result[] = $row;
239 }
240 unset($row, $iterator);
241 }
242
243 return $result;
244 }
245
246 protected function getSectionsByIdList(array $select, array $sectionIds): array
247 {
248 if (empty($sectionIds))
249 {
250 return [];
251 }
252 $select[] = 'ID';
253 $select = array_values(array_unique($select));
254
255 $itemFilter = $this->getBaseFilter();
256
257 $result = [];
258
259 foreach (array_chunk($sectionIds, $this->getPageSize()) as $pageIds)
260 {
261 $itemFilter['ID'] = $pageIds;
262 $iterator = \CIBlockSection::GetList(
263 [],
264 $itemFilter,
265 false,
266 $select
267 );
268 while ($row = $iterator->Fetch())
269 {
270 $row['ID'] = (int)$row['ID'];
271 $result[] = $row;
272 }
273 unset($row, $iterator);
274 }
275
276 return $result;
277 }
278
279 private function getItemsList(array $params): \CDBResult
280 {
282 {
283 $iterator = \CIBlockSection::GetMixedList(
284 [],
285 $params['filter'],
286 false,
287 $params['select']
288 );
289 }
290 else
291 {
292 $iterator = \CIBlockElement::GetList(
293 [],
294 $params['filter'],
295 false,
296 false,
297 $params['select']
298 );
299 }
300
301 return $iterator;
302 }
303}
static parseIndexList(array $ids)
Definition rowtype.php:52
getElementsByIdList(array $select, array $elementIds)
prepareItemIds(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter=null)
getSectionsByIdList(array $select, array $sectionIds)
getItemsByFilter(array $select, ?Filter $filter=null)