Completed
Push — master ( 21d850...86ece3 )
by Nekrasov
01:48
created

SectionModel::setResizePictures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 static getByCode(string $code)
14
 * @method static static getByExternalId(string $id)
15
 * @method static SectionQuery countElements($value)
16
 *
17
 * Base Query methods
18
 * @method static Collection|static[] getList()
19
 * @method static static first()
20
 * @method static static 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 $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
     * Recalculate LEFT_MARGIN and RIGHT_MARGIN during add/update ($bResort for CIBlockSection::Add/Update).
69
     *
70
     * @var bool
71
     */
72
    protected static $resort = true;
73
74
    /**
75
     * Update search after each create or update.
76
     *
77
     * @var bool
78
     */
79
    protected static $updateSearch = true;
80
81
    /**
82
     * Resize pictures during add/update ($bResizePictures for CIBlockSection::Add/Update).
83
     *
84
     * @var bool
85
     */
86
    protected static $resizePictures = false;
87
88
    /**
89
     * Getter for corresponding iblock id.
90
     *
91
     * @throws LogicException
92
     *
93
     * @return int
94
     */
95
    public static function iblockId()
96
    {
97
        $id = static::IBLOCK_ID;
98
        if (!$id) {
99
            throw new LogicException('You must set $iblockId property or override iblockId() method');
100
        }
101
        
102
        return $id;
103
    }
104
105
    /**
106
     * Instantiate a query object for the model.
107
     *
108
     * @return SectionQuery
109
     */
110
    public static function query()
111
    {
112
        return new SectionQuery(static::instantiateObject(), get_called_class());
113
    }
114
115
    /**
116
     * Create new item in database.
117
     *
118
     * @param $fields
119
     *
120
     * @throws ExceptionFromBitrix
121
     *
122
     * @return static|bool
123
     */
124
    public static function create($fields)
125
    {
126
        if (!isset($fields['IBLOCK_ID'])) {
127
            $fields['IBLOCK_ID'] = static::iblockId();
128
        }
129
130
        return static::internalCreate($fields);
131
    }
132
133
    /**
134
     * Get IDs of direct children of the section.
135
     * Additional filter can be specified.
136
     *
137
     * @param array $filter
138
     *
139
     * @return array
140
     */
141
    public function getDirectChildren(array $filter = [])
142
    {
143
        return static::query()
144
            ->filter($filter)
145
            ->filter(['SECTION_ID' => $this->id])
146
            ->select('ID')
147
            ->getList()
148
            ->transform(function ($section) {
149
                return (int) $section['ID'];
150
            })
151
            ->all();
152
    }
153
154
    /**
155
     * Get IDs of all children of the section (direct or not).
156
     * Additional filter can be specified.
157
     *
158
     * @param array $filter
159
     * @param array|string $sort
160
     *
161
     * @return array
162
     */
163
    public function getAllChildren(array $filter = [], $sort = ['LEFT_MARGIN' => 'ASC'])
164
    {
165
        if (!isset($this->fields['LEFT_MARGIN']) || !isset($this->fields['RIGHT_MARGIN'])) {
166
            $this->refresh();
167
        }
168
169
        return static::query()
170
            ->sort($sort)
171
            ->filter($filter)
172
            ->filter([
173
                '!ID' => $this->id,
174
                '>LEFT_MARGIN' => $this->fields['LEFT_MARGIN'],
175
                '<RIGHT_MARGIN' => $this->fields['RIGHT_MARGIN'],
176
            ])
177
            ->select('ID')
178
            ->getList()
179
            ->transform(function ($section) {
180
                return (int) $section['ID'];
181
            })
182
            ->all();
183
    }
184
185
    /**
186
     * Proxy for GetPanelButtons
187
     *
188
     * @param array $options
189
     * @return array
190
     */
191
    public function getPanelButtons($options = [])
192
    {
193
        return CIBlock::GetPanelButtons(
194
            static::iblockId(),
195
            0,
196
            $this->id,
197
            $options
198
        );
199
    }
200
201
    public static function internalDirectCreate($bxObject, $fields)
202
    {
203
        return $bxObject->add($fields, static::$resort, static::$updateSearch, static::$resizePictures);
204
    }
205
206
    /**
207
     * @param $fields
208
     * @param $fieldsSelectedForSave
209
     * @return bool
210
     */
211
    protected function internalUpdate($fields, $fieldsSelectedForSave)
212
    {
213
        return !empty($fields) ? static::$bxObject->update($this->id, $fields, static::$resort, static::$updateSearch, static::$resizePictures) : false;
214
    }
215
216
    /**
217
     * @param $value
218
     */
219
    public static function setResort($value)
220
    {
221
        static::$resort = $value;
222
    }
223
224
    /**
225
     * @param $value
226
     */
227
    public static function setUpdateSearch($value)
228
    {
229
        static::$updateSearch = $value;
230
    }
231
232
    /**
233
     * @param $value
234
     */
235
    public static function setResizePictures($value)
236
    {
237
        static::$resizePictures = $value;
238
    }
239
240
    /**
241
     * @param $query
242
     * @param SectionModel $section
243
     * @return SectionQuery
244
     */
245
    public function scopeChildrenOf(SectionQuery $query, SectionModel $section)
246
    {
247
        $query->filter['>LEFT_MARGIN'] = $section->fields['LEFT_MARGIN'];
248
        $query->filter['<RIGHT_MARGIN'] = $section->fields['RIGHT_MARGIN'];
249
250
        return $query;
251
    }
252
253
    /**
254
     * @param $query
255
     * @param SectionModel|int $section
256
     * @return SectionQuery
257
     */
258
    public function scopeDirectChildrenOf(SectionQuery $query, $section)
259
    {
260
        $query->filter['SECTION_ID'] = is_int($section) ? $section : $section->id;
261
262
        return $query;
263
    }
264
}
265