1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Models; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixModels\Exceptions\ExceptionFromBitrix; |
6
|
|
|
use Arrilot\BitrixModels\Queries\SectionQuery; |
7
|
|
|
use CIBlock; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SectionQuery methods |
13
|
|
|
* @method static SectionModel getByCode(string $code) |
14
|
|
|
* @method static SectionModel getByExternalId(string $id) |
15
|
|
|
* @method static SectionQuery countElements($value) |
16
|
|
|
* |
17
|
|
|
* Base Query methods |
18
|
|
|
* @method static Collection getList() |
19
|
|
|
* @method static SectionModel first() |
20
|
|
|
* @method static SectionModel getById(int $id) |
21
|
|
|
* @method static SectionQuery sort(string|array $by, string $order='ASC') |
22
|
|
|
* @method static SectionQuery order(string|array $by, string $order='ASC') // same as sort() |
23
|
|
|
* @method static SectionQuery filter(array $filter) |
24
|
|
|
* @method static SectionQuery addFilter(array $filters) |
25
|
|
|
* @method static SectionQuery resetFilter() |
26
|
|
|
* @method static SectionQuery navigation(array $filter) |
27
|
|
|
* @method static SectionQuery select($value) |
28
|
|
|
* @method static SectionQuery keyBy(string $value) |
29
|
|
|
* @method static SectionQuery limit(int $value) |
30
|
|
|
* @method static SectionQuery offset(int $value) |
31
|
|
|
* @method static SectionQuery page(int $num) |
32
|
|
|
* @method static SectionQuery take(int $value) // same as limit() |
33
|
|
|
* @method static SectionQuery forPage(int $page, int $perPage=15) |
34
|
|
|
* @method static \Illuminate\Pagination\LengthAwarePaginator paginate(int $perPage = 15, string $pageName = 'page') |
35
|
|
|
* @method static \Illuminate\Pagination\Paginator simplePaginate(int $perPage = 15, string $pageName = 'page') |
36
|
|
|
* @method static SectionQuery stopQuery() |
37
|
|
|
* @method static SectionQuery cache(float|int $minutes) |
38
|
|
|
* |
39
|
|
|
* Scopes |
40
|
|
|
* @method static SectionQuery active() |
41
|
|
|
* @method static SectionQuery childrenOf(SectionModel|int $section) |
42
|
|
|
* @method static SectionQuery directChildrenOf(SectionModel|int $section) |
43
|
|
|
*/ |
44
|
|
|
class SectionModel extends BitrixModel |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* Corresponding IBLOCK_ID |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
const IBLOCK_ID = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Bitrix entity object. |
55
|
|
|
* |
56
|
|
|
* @var object |
57
|
|
|
*/ |
58
|
|
|
public static $bxObject; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Corresponding object class name. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected static $objectClass = 'CIBlockSection'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Update search after each create or update. |
69
|
|
|
* |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
protected static $updateSearch = true; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Getter for corresponding iblock id. |
76
|
|
|
* |
77
|
|
|
* @throws LogicException |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public static function iblockId() |
82
|
|
|
{ |
83
|
|
|
$id = static::IBLOCK_ID; |
84
|
|
|
if (!$id) { |
85
|
|
|
throw new LogicException('You must set $iblockId property or override iblockId() method'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $id; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Instantiate a query object for the model. |
93
|
|
|
* |
94
|
|
|
* @return SectionQuery |
95
|
|
|
*/ |
96
|
|
|
public static function query() |
97
|
|
|
{ |
98
|
|
|
return new SectionQuery(static::instantiateObject(), get_called_class()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create new item in database. |
103
|
|
|
* |
104
|
|
|
* @param $fields |
105
|
|
|
* |
106
|
|
|
* @throws ExceptionFromBitrix |
107
|
|
|
* |
108
|
|
|
* @return static|bool |
109
|
|
|
*/ |
110
|
|
|
public static function create($fields) |
111
|
|
|
{ |
112
|
|
|
if (!isset($fields['IBLOCK_ID'])) { |
113
|
|
|
$fields['IBLOCK_ID'] = static::iblockId(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return static::internalCreate($fields); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get IDs of direct children of the section. |
121
|
|
|
* Additional filter can be specified. |
122
|
|
|
* |
123
|
|
|
* @param array $filter |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getDirectChildren(array $filter = []) |
128
|
|
|
{ |
129
|
|
|
return static::query() |
130
|
|
|
->filter($filter) |
131
|
|
|
->filter(['SECTION_ID' => $this->id]) |
132
|
|
|
->select('ID') |
133
|
|
|
->getList() |
134
|
|
|
->transform(function ($section) { |
135
|
|
|
return (int) $section['ID']; |
136
|
|
|
}) |
137
|
|
|
->all(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get IDs of all children of the section (direct or not). |
142
|
|
|
* Additional filter can be specified. |
143
|
|
|
* |
144
|
|
|
* @param array $filter |
145
|
|
|
* @param array|string $sort |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC']) |
150
|
|
|
{ |
151
|
|
|
if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) { |
152
|
|
|
$this->refresh(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return static::query() |
156
|
|
|
->sort($sort) |
157
|
|
|
->filter($filter) |
158
|
|
|
->filter([ |
159
|
|
|
'!ID' => $this->id, |
160
|
|
|
'>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'], |
161
|
|
|
'<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'], |
162
|
|
|
]) |
163
|
|
|
->select('ID') |
164
|
|
|
->getList() |
165
|
|
|
->transform(function ($section) { |
166
|
|
|
return (int) $section['ID']; |
167
|
|
|
}) |
168
|
|
|
->all(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Proxy for GetPanelButtons |
173
|
|
|
* |
174
|
|
|
* @param array $options |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
public function getPanelButtons($options = []) |
178
|
|
|
{ |
179
|
|
|
return CIBlock::GetPanelButtons( |
180
|
|
|
static::iblockId(), |
181
|
|
|
0, |
182
|
|
|
$this->id, |
183
|
|
|
$options |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public static function internalDirectCreate($bxObject, $fields) |
188
|
|
|
{ |
189
|
|
|
return $bxObject->add($fields, true, static::$updateSearch); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $fields |
194
|
|
|
* @param $fieldsSelectedForSave |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
protected function internalUpdate($fields, $fieldsSelectedForSave) |
198
|
|
|
{ |
199
|
|
|
return !empty($fields) ? static::$bxObject->update($this->id, $fields, true, static::$updateSearch) : false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param $value |
204
|
|
|
*/ |
205
|
|
|
public static function setUpdateSearch($value) |
206
|
|
|
{ |
207
|
|
|
static::$updateSearch = $value; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param $query |
212
|
|
|
* @param SectionModel $section |
213
|
|
|
* @return SectionQuery |
214
|
|
|
*/ |
215
|
|
|
public function scopeChildrenOf(SectionQuery $query, SectionModel $section) |
216
|
|
|
{ |
217
|
|
|
$query->filter['>LEFT_MARGIN'] = $section->fields['LEFT_MARGIN']; |
218
|
|
|
$query->filter['<RIGHT_MARGIN'] = $section->fields['RIGHT_MARGIN']; |
219
|
|
|
|
220
|
|
|
return $query; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $query |
225
|
|
|
* @param SectionModel|int $section |
226
|
|
|
* @return SectionQuery |
227
|
|
|
*/ |
228
|
|
|
public function scopeDirectChildrenOf(SectionQuery $query, $section) |
229
|
|
|
{ |
230
|
|
|
$query->filter['SECTION_ID'] = is_int($section) ? $section : $section->id; |
231
|
|
|
|
232
|
|
|
return $query; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|