1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Queries; |
4
|
|
|
|
5
|
|
|
use CIBlock; |
6
|
|
|
use Bitrix\Main\Data\Cache; |
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 BaseQuery |
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
|
|
|
* List of standard entity fields. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $standardFields = [ |
53
|
|
|
'ID', |
54
|
|
|
'TIMESTAMP_X', |
55
|
|
|
'TIMESTAMP_X_UNIX', |
56
|
|
|
'MODIFIED_BY', |
57
|
|
|
'DATE_CREATE', |
58
|
|
|
'DATE_CREATE_UNIX', |
59
|
|
|
'CREATED_BY', |
60
|
|
|
'IBLOCK_ID', |
61
|
|
|
'IBLOCK_SECTION_ID', |
62
|
|
|
'ACTIVE', |
63
|
|
|
'ACTIVE_FROM', |
64
|
|
|
'ACTIVE_TO', |
65
|
|
|
'SORT', |
66
|
|
|
'NAME', |
67
|
|
|
'PREVIEW_PICTURE', |
68
|
|
|
'PREVIEW_TEXT', |
69
|
|
|
'PREVIEW_TEXT_TYPE', |
70
|
|
|
'DETAIL_PICTURE', |
71
|
|
|
'DETAIL_TEXT', |
72
|
|
|
'DETAIL_TEXT_TYPE', |
73
|
|
|
'SEARCHABLE_CONTENT', |
74
|
|
|
'IN_SECTIONS', |
75
|
|
|
'SHOW_COUNTER', |
76
|
|
|
'SHOW_COUNTER_START', |
77
|
|
|
'CODE', |
78
|
|
|
'TAGS', |
79
|
|
|
'XML_ID', |
80
|
|
|
'EXTERNAL_ID', |
81
|
|
|
'TMP_ID', |
82
|
|
|
'CREATED_USER_NAME', |
83
|
|
|
'DETAIL_PAGE_URL', |
84
|
|
|
'LIST_PAGE_URL', |
85
|
|
|
'CREATED_DATE', |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Constructor. |
90
|
|
|
* |
91
|
|
|
* @param object $bxObject |
92
|
|
|
* @param string $modelName |
93
|
|
|
*/ |
94
|
|
|
public function __construct($bxObject, $modelName) |
95
|
|
|
{ |
96
|
|
|
static::instantiateCIblockObject(); |
97
|
|
|
parent::__construct($bxObject, $modelName); |
98
|
|
|
|
99
|
|
|
$this->iblockId = $modelName::iblockId(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Instantiate bitrix entity object. |
104
|
|
|
* |
105
|
|
|
* @throws Exception |
106
|
|
|
* |
107
|
|
|
* @return object |
108
|
|
|
*/ |
109
|
|
|
public static function instantiateCIblockObject() |
110
|
|
|
{ |
111
|
|
|
if (static::$cIblockObject) { |
112
|
|
|
return static::$cIblockObject; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (class_exists('CIBlock')) { |
116
|
|
|
return static::$cIblockObject = new CIBlock(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
throw new Exception('CIblock object initialization failed'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Setter for groupBy. |
124
|
|
|
* |
125
|
|
|
* @param $value |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function groupBy($value) |
130
|
|
|
{ |
131
|
|
|
$this->groupBy = $value; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function cache($ttl) |
137
|
|
|
{ |
138
|
|
|
$this->ttl = $ttl; |
|
|
|
|
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get list of items. |
145
|
|
|
* |
146
|
|
|
* @return Collection |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
private function getListSimple() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
if ($this->queryShouldBeStopped) { |
151
|
|
|
return new Collection(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$items = []; |
155
|
|
|
|
156
|
|
|
$rsItems = $this->bxObject->GetList( |
157
|
|
|
$this->sort, |
158
|
|
|
$this->normalizeFilter(), |
159
|
|
|
$this->groupBy, |
160
|
|
|
$this->navigation, |
161
|
|
|
$this->normalizeSelect() |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
while ($arItem = $rsItems->Fetch()) { |
165
|
|
|
$this->addItemToResultsUsingKeyBy($items, new $this->modelName($arItem['ID'], $arItem)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return new Collection($items); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getList() |
172
|
|
|
{ |
173
|
|
|
if (!$this->ttl) return $this->getListSimple(); |
174
|
|
|
|
175
|
|
|
$cache = Cache::createInstance(); |
176
|
|
|
if ($cache->initCache($this->ttl, $this->getKey(), '/models/')) { |
177
|
|
|
$result = $cache->getVars(); |
178
|
|
|
} elseif ($cache->startDataCache()) { |
179
|
|
|
$result = $this->getListSimple(); |
180
|
|
|
$cache->endDataCache($result); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $result; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
private function getKey() |
187
|
|
|
{ |
188
|
|
|
$data = [ |
189
|
|
|
'sort' => $this->sort, |
190
|
|
|
'filter' => $this->normalizeFilter(), |
191
|
|
|
'groupBy' => $this->groupBy, |
192
|
|
|
'navigation' => $this->navigation, |
193
|
|
|
'select' => $this->normalizeSelect(), |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
return md5( json_encode($data) ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the first element with a given code. |
201
|
|
|
* |
202
|
|
|
* @param string $code |
203
|
|
|
* |
204
|
|
|
* @return ElementModel |
205
|
|
|
*/ |
206
|
|
|
public function getByCode($code) |
207
|
|
|
{ |
208
|
|
|
$this->filter['CODE'] = $code; |
209
|
|
|
|
210
|
|
|
return $this->first(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the first element with a given external id. |
215
|
|
|
* |
216
|
|
|
* @param string $id |
217
|
|
|
* |
218
|
|
|
* @return ElementModel |
219
|
|
|
*/ |
220
|
|
|
public function getByExternalId($id) |
221
|
|
|
{ |
222
|
|
|
$this->filter['EXTERNAL_ID'] = $id; |
223
|
|
|
|
224
|
|
|
return $this->first(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get count of elements that match $filter. |
229
|
|
|
* |
230
|
|
|
* @return int |
231
|
|
|
*/ |
232
|
|
|
public function count() |
233
|
|
|
{ |
234
|
|
|
if ($this->queryShouldBeStopped) { |
235
|
|
|
return 0; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return (int) $this->bxObject->GetList(false, $this->normalizeFilter(), []); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// /** |
242
|
|
|
// * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'. |
243
|
|
|
// * |
244
|
|
|
// * @param array $fields |
245
|
|
|
// * |
246
|
|
|
// * @return null |
247
|
|
|
// */ |
248
|
|
|
// protected function normalizePropertyResultFormat(&$fields) |
249
|
|
|
// { |
250
|
|
|
// if (empty($fields['PROPERTIES'])) { |
251
|
|
|
// return; |
252
|
|
|
// } |
253
|
|
|
// |
254
|
|
|
// foreach ($fields['PROPERTIES'] as $code => $prop) { |
255
|
|
|
// $fields['PROPERTY_'.$code.'_VALUE'] = $prop['VALUE']; |
256
|
|
|
// $fields['~PROPERTY_'.$code.'_VALUE'] = $prop['~VALUE']; |
257
|
|
|
// $fields['PROPERTY_'.$code.'_DESCRIPTION'] = $prop['DESCRIPTION']; |
258
|
|
|
// $fields['~PROPERTY_'.$code.'_DESCRIPTION'] = $prop['~DESCRIPTION']; |
259
|
|
|
// $fields['PROPERTY_'.$code.'_VALUE_ID'] = $prop['PROPERTY_VALUE_ID']; |
260
|
|
|
// if (isset($prop['VALUE_ENUM_ID'])) { |
261
|
|
|
// $fields['PROPERTY_'.$code.'_ENUM_ID'] = $prop['VALUE_ENUM_ID']; |
262
|
|
|
// } |
263
|
|
|
// } |
264
|
|
|
// } |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Normalize filter before sending it to getList. |
268
|
|
|
* This prevents some inconsistency. |
269
|
|
|
* |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
protected function normalizeFilter() |
273
|
|
|
{ |
274
|
|
|
$this->filter['IBLOCK_ID'] = $this->iblockId; |
275
|
|
|
|
276
|
|
|
return $this->filter; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Normalize select before sending it to getList. |
281
|
|
|
* This prevents some inconsistency. |
282
|
|
|
* |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
View Code Duplication |
protected function normalizeSelect() |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
if ($this->fieldsMustBeSelected()) { |
288
|
|
|
$this->select = array_merge($this->standardFields, $this->select); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if ($this->propsMustBeSelected()) { |
292
|
|
|
$this->addAllPropsToSelect(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$this->select[] = 'ID'; |
296
|
|
|
$this->select[] = 'IBLOCK_ID'; |
297
|
|
|
|
298
|
|
|
return $this->clearSelectArray(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Add all iblock property codes to select. |
303
|
|
|
* |
304
|
|
|
* return null |
305
|
|
|
*/ |
306
|
|
|
protected function addAllPropsToSelect() |
307
|
|
|
{ |
308
|
|
|
$this->select[] = 'ID'; |
309
|
|
|
$this->select[] = 'IBLOCK_ID'; |
310
|
|
|
|
311
|
|
|
//dd (static::$cIblockObject); |
|
|
|
|
312
|
|
|
$rsProps = static::$cIblockObject->GetProperties($this->iblockId); |
313
|
|
|
while ($prop = $rsProps->Fetch()) { |
314
|
|
|
$this->select[] = 'PROPERTY_'.$prop['CODE']; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: