This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Arrilot\BitrixModels\Queries; |
||
4 | |||
5 | use Arrilot\BitrixCacher\Cache; |
||
6 | use CIBlock; |
||
7 | use Illuminate\Support\Collection; |
||
8 | use Arrilot\BitrixModels\Models\ElementModel; |
||
9 | use Exception; |
||
10 | |||
11 | /** |
||
12 | * @method ElementQuery active() |
||
13 | * @method ElementQuery sortByDate(string $sort = 'desc') |
||
14 | * @method ElementQuery fromSectionWithId(int $id) |
||
15 | * @method ElementQuery fromSectionWithCode(string $code) |
||
16 | */ |
||
17 | class ElementQuery extends OldCoreQuery |
||
18 | { |
||
19 | /** |
||
20 | * CIblock object or test double. |
||
21 | * |
||
22 | * @var object. |
||
23 | */ |
||
24 | public static $cIblockObject; |
||
25 | |||
26 | /** |
||
27 | * Query sort. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | public $sort = ['SORT' => 'ASC']; |
||
32 | |||
33 | /** |
||
34 | * Query group by. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | public $groupBy = false; |
||
39 | |||
40 | /** |
||
41 | * Iblock id. |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $iblockId; |
||
46 | |||
47 | /** |
||
48 | * Iblock version. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $iblockVersion; |
||
53 | |||
54 | /** |
||
55 | * List of standard entity fields. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $standardFields = [ |
||
60 | 'ID', |
||
61 | 'TIMESTAMP_X', |
||
62 | 'TIMESTAMP_X_UNIX', |
||
63 | 'MODIFIED_BY', |
||
64 | 'DATE_CREATE', |
||
65 | 'DATE_CREATE_UNIX', |
||
66 | 'CREATED_BY', |
||
67 | 'IBLOCK_ID', |
||
68 | 'IBLOCK_SECTION_ID', |
||
69 | 'ACTIVE', |
||
70 | 'ACTIVE_FROM', |
||
71 | 'ACTIVE_TO', |
||
72 | 'SORT', |
||
73 | 'NAME', |
||
74 | 'PREVIEW_PICTURE', |
||
75 | 'PREVIEW_TEXT', |
||
76 | 'PREVIEW_TEXT_TYPE', |
||
77 | 'DETAIL_PICTURE', |
||
78 | 'DETAIL_TEXT', |
||
79 | 'DETAIL_TEXT_TYPE', |
||
80 | 'SEARCHABLE_CONTENT', |
||
81 | 'IN_SECTIONS', |
||
82 | 'SHOW_COUNTER', |
||
83 | 'SHOW_COUNTER_START', |
||
84 | 'CODE', |
||
85 | 'TAGS', |
||
86 | 'XML_ID', |
||
87 | 'EXTERNAL_ID', |
||
88 | 'TMP_ID', |
||
89 | 'CREATED_USER_NAME', |
||
90 | 'DETAIL_PAGE_URL', |
||
91 | 'LIST_PAGE_URL', |
||
92 | 'CREATED_DATE', |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * Constructor. |
||
97 | * |
||
98 | * @param object $bxObject |
||
99 | * @param string $modelName |
||
100 | */ |
||
101 | public function __construct($bxObject, $modelName) |
||
102 | { |
||
103 | static::instantiateCIblockObject(); |
||
104 | parent::__construct($bxObject, $modelName); |
||
105 | |||
106 | $this->iblockId = $modelName::iblockId(); |
||
107 | $this->iblockVersion = $modelName::IBLOCK_VERSION ?: 2; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Instantiate bitrix entity object. |
||
112 | * |
||
113 | * @throws Exception |
||
114 | * |
||
115 | * @return object |
||
116 | */ |
||
117 | public static function instantiateCIblockObject() |
||
118 | { |
||
119 | if (static::$cIblockObject) { |
||
120 | return static::$cIblockObject; |
||
121 | } |
||
122 | |||
123 | if (class_exists('CIBlock')) { |
||
124 | return static::$cIblockObject = new CIBlock(); |
||
125 | } |
||
126 | |||
127 | throw new Exception('CIblock object initialization failed'); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Setter for groupBy. |
||
132 | * |
||
133 | * @param $value |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function groupBy($value) |
||
138 | { |
||
139 | $this->groupBy = $value; |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get list of items. |
||
146 | * |
||
147 | * @return Collection |
||
148 | */ |
||
149 | protected function loadModels() |
||
150 | { |
||
151 | $sort = $this->sort; |
||
152 | $filter = $this->normalizeFilter(); |
||
153 | $groupBy = $this->groupBy; |
||
154 | $navigation = $this->navigation; |
||
155 | $select = $this->normalizeSelect(); |
||
156 | $queryType = 'ElementQuery::getList'; |
||
157 | $fetchUsing = $this->fetchUsing; |
||
158 | $keyBy = $this->keyBy; |
||
159 | list($select, $chunkQuery) = $this->multiplySelectForMaxJoinsRestrictionIfNeeded($select); |
||
160 | |||
161 | $callback = function() use ($sort, $filter, $groupBy, $navigation, $select, $chunkQuery) { |
||
162 | if ($chunkQuery) { |
||
163 | $itemsChunks = []; |
||
164 | foreach ($select as $chunkIndex => $selectForChunk) { |
||
165 | $rsItems = $this->bxObject->GetList($sort, $filter, $groupBy, $navigation, $selectForChunk); |
||
166 | while ($arItem = $this->performFetchUsingSelectedMethod($rsItems)) { |
||
167 | $this->addItemToResultsUsingKeyBy($itemsChunks[$chunkIndex], new $this->modelName($arItem['ID'], $arItem)); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $items = $this->mergeChunks($itemsChunks); |
||
172 | } else { |
||
173 | $items = []; |
||
174 | $rsItems = $this->bxObject->GetList($sort, $filter, $groupBy, $navigation, $select); |
||
175 | while ($arItem = $this->performFetchUsingSelectedMethod($rsItems)) { |
||
176 | $this->addItemToResultsUsingKeyBy($items, new $this->modelName($arItem['ID'], $arItem)); |
||
177 | } |
||
178 | } |
||
179 | return new Collection($items); |
||
180 | }; |
||
181 | |||
182 | $cacheKeyParams = compact('sort', 'filter', 'groupBy', 'navigation', 'select', 'queryType', 'keyBy', 'fetchUsing'); |
||
183 | |||
184 | return $this->handleCacheIfNeeded($cacheKeyParams, $callback); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get the first element with a given code. |
||
189 | * |
||
190 | * @param string $code |
||
191 | * |
||
192 | * @return ElementModel |
||
193 | */ |
||
194 | public function getByCode($code) |
||
195 | { |
||
196 | $this->filter['=CODE'] = $code; |
||
197 | |||
198 | return $this->first(); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Get the first element with a given external id. |
||
203 | * |
||
204 | * @param string $id |
||
205 | * |
||
206 | * @return ElementModel |
||
207 | */ |
||
208 | public function getByExternalId($id) |
||
209 | { |
||
210 | $this->filter['EXTERNAL_ID'] = $id; |
||
211 | |||
212 | return $this->first(); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Get count of elements that match $filter. |
||
217 | * |
||
218 | * @return int |
||
219 | */ |
||
220 | View Code Duplication | public function count() |
|
0 ignored issues
–
show
|
|||
221 | { |
||
222 | if ($this->queryShouldBeStopped) { |
||
223 | return 0; |
||
224 | } |
||
225 | |||
226 | $filter = $this->normalizeFilter(); |
||
227 | $queryType = "ElementQuery::count"; |
||
228 | |||
229 | $callback = function () use ($filter) { |
||
230 | return (int) $this->bxObject->GetList(false, $filter, []); |
||
231 | }; |
||
232 | |||
233 | return $this->handleCacheIfNeeded(compact('filter', 'queryType'), $callback); |
||
234 | } |
||
235 | |||
236 | // /** |
||
237 | // * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'. |
||
238 | // * |
||
239 | // * @param array $fields |
||
240 | // * |
||
241 | // * @return null |
||
242 | // */ |
||
243 | // protected function normalizePropertyResultFormat(&$fields) |
||
244 | // { |
||
245 | // if (empty($fields['PROPERTIES'])) { |
||
246 | // return; |
||
247 | // } |
||
248 | // |
||
249 | // foreach ($fields['PROPERTIES'] as $code => $prop) { |
||
250 | // $fields['PROPERTY_'.$code.'_VALUE'] = $prop['VALUE']; |
||
251 | // $fields['~PROPERTY_'.$code.'_VALUE'] = $prop['~VALUE']; |
||
252 | // $fields['PROPERTY_'.$code.'_DESCRIPTION'] = $prop['DESCRIPTION']; |
||
253 | // $fields['~PROPERTY_'.$code.'_DESCRIPTION'] = $prop['~DESCRIPTION']; |
||
254 | // $fields['PROPERTY_'.$code.'_VALUE_ID'] = $prop['PROPERTY_VALUE_ID']; |
||
255 | // if (isset($prop['VALUE_ENUM_ID'])) { |
||
256 | // $fields['PROPERTY_'.$code.'_ENUM_ID'] = $prop['VALUE_ENUM_ID']; |
||
257 | // } |
||
258 | // } |
||
259 | // } |
||
260 | |||
261 | /** |
||
262 | * Normalize filter before sending it to getList. |
||
263 | * This prevents some inconsistency. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | protected function normalizeFilter() |
||
268 | { |
||
269 | $this->filter['IBLOCK_ID'] = $this->iblockId; |
||
270 | |||
271 | return $this->filter; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Normalize select before sending it to getList. |
||
276 | * This prevents some inconsistency. |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | View Code Duplication | protected function normalizeSelect() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
281 | { |
||
282 | if ($this->fieldsMustBeSelected()) { |
||
283 | $this->select = array_merge($this->standardFields, $this->select); |
||
284 | } |
||
285 | |||
286 | $this->select[] = 'ID'; |
||
287 | $this->select[] = 'IBLOCK_ID'; |
||
288 | |||
289 | return $this->clearSelectArray(); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Fetch all iblock property codes from database |
||
294 | * |
||
295 | * return array |
||
296 | */ |
||
297 | protected function fetchAllPropsForSelect() |
||
298 | { |
||
299 | $props = []; |
||
300 | $rsProps = static::$cIblockObject->GetProperties($this->iblockId); |
||
301 | while ($prop = $rsProps->Fetch()) { |
||
302 | $props[] = 'PROPERTY_'.$prop['CODE']; |
||
303 | } |
||
304 | |||
305 | return $props; |
||
306 | } |
||
307 | |||
308 | protected function multiplySelectForMaxJoinsRestrictionIfNeeded($select) |
||
309 | { |
||
310 | if (!$this->propsMustBeSelected()) { |
||
311 | return [$select, false]; |
||
312 | } |
||
313 | |||
314 | $chunkSize = 20; |
||
315 | $props = $this->fetchAllPropsForSelect(); |
||
316 | if ($this->iblockVersion !== 1 || (count($props) <= $chunkSize)) { |
||
317 | return [array_merge($select, $props), false]; |
||
318 | } |
||
319 | |||
320 | // начинаем формировать селекты из свойств |
||
321 | $multipleSelect = array_chunk($props, $chunkSize); |
||
322 | |||
323 | // добавляем в каждый селект поля "несвойства" |
||
324 | foreach ($multipleSelect as $i => $partOfProps) { |
||
325 | $multipleSelect[$i] = array_merge($select, $partOfProps); |
||
326 | } |
||
327 | |||
328 | return [$multipleSelect, true]; |
||
329 | } |
||
330 | |||
331 | protected function mergeChunks($chunks) |
||
332 | { |
||
333 | $items = []; |
||
334 | foreach ($chunks as $chunk) { |
||
335 | foreach ($chunk as $k => $item) { |
||
336 | if (isset($items[$k])) { |
||
337 | $item->fields['_were_multiplied'] = array_merge((array) $items[$k]->fields['_were_multiplied'], (array) $item->fields['_were_multiplied']); |
||
338 | $items[$k]->fields = (array) $item->fields + (array) $items[$k]->fields; |
||
339 | } else { |
||
340 | $items[$k] = $item; |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | |||
345 | return $items; |
||
346 | } |
||
347 | } |
||
348 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.