1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Models; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixModels\Queries\SectionQuery; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class SectionModel extends BaseModel |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Bitrix entity object. |
12
|
|
|
* |
13
|
|
|
* @var object |
14
|
|
|
*/ |
15
|
|
|
public static $bxObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Corresponding object class name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected static $objectClass = 'CIBlockSection'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List of params that can modify query. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $additionalQueryModifiers = [ |
30
|
|
|
'countElements', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Corresponding iblock id. |
35
|
|
|
* MUST be overridden. |
36
|
|
|
* |
37
|
|
|
* @throws Exception |
38
|
|
|
* |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
|
|
public static function iblockId() |
42
|
|
|
{ |
43
|
|
|
throw new Exception('public static function iblockId() MUST be overridden'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Instantiate a query object for the model. |
48
|
|
|
* |
49
|
|
|
* @return SectionQuery |
50
|
|
|
*/ |
51
|
|
|
public static function query() |
52
|
|
|
{ |
53
|
|
|
return new SectionQuery(static::instantiateObject(), get_called_class()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get all model attributes from cache or database. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function get() |
62
|
|
|
{ |
63
|
|
|
$this->getFields(); |
64
|
|
|
|
65
|
|
|
return $this->fields; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Refresh model from database and place data to $this->fields. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function refresh() |
74
|
|
|
{ |
75
|
|
|
$this->refreshFields(); |
76
|
|
|
|
77
|
|
|
return $this->fields; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Refresh user fields and save them to a class field. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function refreshFields() |
86
|
|
|
{ |
87
|
|
|
if ($this->id === null) { |
88
|
|
|
return $this->fields = []; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->fields = static::query()->getById($this->id)->fields; |
92
|
|
|
|
93
|
|
|
$this->fieldsAreFetched = true; |
94
|
|
|
|
95
|
|
|
return $this->fields; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get IDs of direct children of the section. |
100
|
|
|
* Additional filter can be specified. |
101
|
|
|
* |
102
|
|
|
* @param array $filter |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getDirectChildren(array $filter = []) |
107
|
|
|
{ |
108
|
|
|
return static::query() |
109
|
|
|
->filter($filter) |
110
|
|
|
->filter(['SECTION_ID' => $this->id]) |
111
|
|
|
->select('ID') |
112
|
|
|
->getList() |
113
|
|
|
->transform(function ($section) { |
114
|
|
|
return (int) $section['ID']; |
115
|
|
|
}) |
116
|
|
|
->all(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get IDs of all children of the section (direct or not). |
121
|
|
|
* Additional filter can be specified. |
122
|
|
|
* |
123
|
|
|
* @param array $filter |
124
|
|
|
* @param array|string $sort |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC']) |
129
|
|
|
{ |
130
|
|
|
if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) { |
131
|
|
|
$this->refresh(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return static::query() |
135
|
|
|
->sort($sort) |
136
|
|
|
->filter($filter) |
137
|
|
|
->filter([ |
138
|
|
|
'!ID' => $this->id, |
139
|
|
|
'>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'], |
140
|
|
|
'<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'], |
141
|
|
|
]) |
142
|
|
|
->select('ID') |
143
|
|
|
->getList() |
144
|
|
|
->transform(function ($section) { |
145
|
|
|
return (int) $section['ID']; |
146
|
|
|
}) |
147
|
|
|
->all(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|