Completed
Push — master ( c26f40...f7e861 )
by Nekrasov
01:21
created

SectionModel::getAllChildren()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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