|
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
|
|
|
* Corresponding IBLOCK_ID |
|
12
|
|
|
* |
|
13
|
|
|
* @var int |
|
14
|
|
|
*/ |
|
15
|
|
|
const IBLOCK_ID = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Corresponding object class name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $objectClass = 'CIBlockSection'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Getter for corresponding iblock id. |
|
26
|
|
|
* |
|
27
|
|
|
* @throws Exception |
|
28
|
|
|
* |
|
29
|
|
|
* @return int |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function iblockId() |
|
32
|
|
|
{ |
|
33
|
|
|
$id = static::IBLOCK_ID; |
|
34
|
|
|
if (!$id) { |
|
35
|
|
|
throw new Exception('You must set $iblockId property or override iblockId() method'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $id; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Instantiate a query object for the model. |
|
43
|
|
|
* |
|
44
|
|
|
* @return SectionQuery |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function query() |
|
47
|
|
|
{ |
|
48
|
|
|
return new SectionQuery(static::instantiateObject(), get_called_class()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get IDs of direct children of the section. |
|
53
|
|
|
* Additional filter can be specified. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $filter |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDirectChildren(array $filter = []) |
|
60
|
|
|
{ |
|
61
|
|
|
return static::query() |
|
62
|
|
|
->filter($filter) |
|
63
|
|
|
->filter(['SECTION_ID' => $this->id]) |
|
64
|
|
|
->select('ID') |
|
65
|
|
|
->getList() |
|
66
|
|
|
->transform(function ($section) { |
|
67
|
|
|
return (int) $section['ID']; |
|
68
|
|
|
}) |
|
69
|
|
|
->all(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get IDs of all children of the section (direct or not). |
|
74
|
|
|
* Additional filter can be specified. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $filter |
|
77
|
|
|
* @param array|string $sort |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC']) |
|
82
|
|
|
{ |
|
83
|
|
|
if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) { |
|
84
|
|
|
$this->refresh(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return static::query() |
|
88
|
|
|
->sort($sort) |
|
89
|
|
|
->filter($filter) |
|
90
|
|
|
->filter([ |
|
91
|
|
|
'!ID' => $this->id, |
|
92
|
|
|
'>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'], |
|
93
|
|
|
'<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'], |
|
94
|
|
|
]) |
|
95
|
|
|
->select('ID') |
|
96
|
|
|
->getList() |
|
97
|
|
|
->transform(function ($section) { |
|
98
|
|
|
return (int) $section['ID']; |
|
99
|
|
|
}) |
|
100
|
|
|
->all(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|