1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Models; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixModels\Queries\SectionQuery; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SectionQuery methods |
11
|
|
|
* @method static SectionModel getByCode(string $code) |
12
|
|
|
* @method static SectionModel getByExternalId(string $id) |
13
|
|
|
* @method static SectionQuery countElements($value) |
14
|
|
|
* |
15
|
|
|
* Base Query methods |
16
|
|
|
* @method static Collection getList() |
17
|
|
|
* @method static SectionModel first() |
18
|
|
|
* @method static SectionModel getById(int $id) |
19
|
|
|
* @method static SectionQuery sort(string|array $by, string $order='ASC') |
20
|
|
|
* @method static SectionQuery filter(array $filter) |
21
|
|
|
* @method static SectionQuery addFilter(array $filters) |
22
|
|
|
* @method static SectionQuery resetFilter() |
23
|
|
|
* @method static SectionQuery navigation(array $filter) |
24
|
|
|
* @method static SectionQuery select($value) |
25
|
|
|
* @method static SectionQuery keyBy(string $value) |
26
|
|
|
* @method static SectionQuery limit(int $value) |
27
|
|
|
* @method static SectionQuery page(int $num) |
28
|
|
|
* @method static SectionQuery take(int $value) |
29
|
|
|
* @method static SectionQuery forPage(int $page, int $perPage=15) |
30
|
|
|
* @method static \Illuminate\Pagination\LengthAwarePaginator paginate(int $perPage = 15, string $pageName = 'page') |
31
|
|
|
* @method static \Illuminate\Pagination\Paginator simplePaginate(int $perPage = 15, string $pageName = 'page') |
32
|
|
|
* @method static SectionQuery stopQuery() |
33
|
|
|
* |
34
|
|
|
* Scopes |
35
|
|
|
* @method static SectionQuery active() |
36
|
|
|
*/ |
37
|
|
|
class SectionModel extends BitrixModel |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Corresponding IBLOCK_ID |
41
|
|
|
* |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
const IBLOCK_ID = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Bitrix entity object. |
48
|
|
|
* |
49
|
|
|
* @var object |
50
|
|
|
*/ |
51
|
|
|
public static $bxObject; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Corresponding object class name. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected static $objectClass = 'CIBlockSection'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Getter for corresponding iblock id. |
62
|
|
|
* |
63
|
|
|
* @throws Exception |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
public static function iblockId() |
68
|
|
|
{ |
69
|
|
|
$id = static::IBLOCK_ID; |
70
|
|
|
if (!$id) { |
71
|
|
|
throw new Exception('You must set $iblockId property or override iblockId() method'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $id; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Instantiate a query object for the model. |
79
|
|
|
* |
80
|
|
|
* @return SectionQuery |
81
|
|
|
*/ |
82
|
|
|
public static function query() |
83
|
|
|
{ |
84
|
|
|
return new SectionQuery(static::instantiateObject(), get_called_class()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create new item in database. |
89
|
|
|
* |
90
|
|
|
* @param $fields |
91
|
|
|
* |
92
|
|
|
* @throws Exception |
93
|
|
|
* |
94
|
|
|
* @return static|bool |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public static function create($fields) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (!isset($fields['IBLOCK_ID'])) { |
99
|
|
|
$fields['IBLOCK_ID'] = static::iblockId(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return parent::create($fields); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get IDs of direct children of the section. |
107
|
|
|
* Additional filter can be specified. |
108
|
|
|
* |
109
|
|
|
* @param array $filter |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getDirectChildren(array $filter = []) |
114
|
|
|
{ |
115
|
|
|
return static::query() |
116
|
|
|
->filter($filter) |
117
|
|
|
->filter(['SECTION_ID' => $this->id]) |
118
|
|
|
->select('ID') |
119
|
|
|
->getList() |
120
|
|
|
->transform(function ($section) { |
121
|
|
|
return (int) $section['ID']; |
122
|
|
|
}) |
123
|
|
|
->all(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get IDs of all children of the section (direct or not). |
128
|
|
|
* Additional filter can be specified. |
129
|
|
|
* |
130
|
|
|
* @param array $filter |
131
|
|
|
* @param array|string $sort |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC']) |
136
|
|
|
{ |
137
|
|
|
if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) { |
138
|
|
|
$this->refresh(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return static::query() |
142
|
|
|
->sort($sort) |
143
|
|
|
->filter($filter) |
144
|
|
|
->filter([ |
145
|
|
|
'!ID' => $this->id, |
146
|
|
|
'>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'], |
147
|
|
|
'<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'], |
148
|
|
|
]) |
149
|
|
|
->select('ID') |
150
|
|
|
->getList() |
151
|
|
|
->transform(function ($section) { |
152
|
|
|
return (int) $section['ID']; |
153
|
|
|
}) |
154
|
|
|
->all(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.