IBlockProperty::setIsRequired()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
namespace Arrilot\BitrixMigrations\Constructors;
5
6
7
use Arrilot\BitrixMigrations\Logger;
8
use Bitrix\Main\Application;
9
10
class IBlockProperty
11
{
12
    use FieldConstructor;
13
14
    /**
15
     * Добавить свойство инфоблока
16
     * @throws \Exception
17
     */
18
    public function add()
19
    {
20
        $obj = new \CIBlockProperty();
21
22
        $property_id = $obj->Add($this->getFieldsWithDefault());
23
24
        if (!$property_id) {
25
            throw new \Exception($obj->LAST_ERROR);
26
        }
27
28
        Logger::log("Добавлено свойство инфоблока {$this->fields['CODE']}", Logger::COLOR_GREEN);
29
30
        return $property_id;
31
    }
32
33
    /**
34
     * Обновить свойство инфоблока
35
     * @param $id
36
     * @throws \Exception
37
     */
38 View Code Duplication
    public function update($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
39
    {
40
        $obj = new \CIBlockProperty();
41
        if (!$obj->Update($id, $this->fields)) {
42
            throw new \Exception($obj->LAST_ERROR);
43
        }
44
45
        Logger::log("Обновлено свойство инфоблока {$id}", Logger::COLOR_GREEN);
46
    }
47
48
    /**
49
     * Удалить свойство инфоблока
50
     * @param $id
51
     * @throws \Exception
52
     */
53
    public static function delete($id)
54
    {
55
        if (!\CIBlockProperty::Delete($id)) {
56
            throw new \Exception('Ошибка при удалении свойства инфоблока');
57
        }
58
59
        Logger::log("Удалено свойство инфоблока {$id}", Logger::COLOR_GREEN);
60
    }
61
62
    /**
63
     * Установить настройки для добавления свойства инфоблока по умолчанию
64
     * @param string $code
65
     * @param string $name
66
     * @param int $iblockId
67
     * @return IBlockProperty
68
     */
69
    public function constructDefault($code, $name, $iblockId)
70
    {
71
        return $this->setPropertyType('S')->setCode($code)->setName($name)->setIblockId($iblockId);
72
    }
73
74
    /**
75
     * Символьный идентификатор.
76
     * @param string $code
77
     * @return $this
78
     */
79
    public function setCode($code)
80
    {
81
        $this->fields['CODE'] = $code;
82
83
        return $this;
84
    }
85
86
    /**
87
     * Внешний код.
88
     * @param string $xml_id
89
     * @return $this
90
     */
91
    public function setXmlId($xml_id)
92
    {
93
        $this->fields['XML_ID'] = $xml_id;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Код информационного блока.
100
     * @param string $iblock_id
101
     * @return $this
102
     */
103
    public function setIblockId($iblock_id)
104
    {
105
        $this->fields['IBLOCK_ID'] = $iblock_id;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Название.
112
     * @param string $name
113
     * @return $this
114
     */
115
    public function setName($name)
116
    {
117
        $this->fields['NAME'] = $name;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Флаг активности
124
     * @param bool $active
125
     * @return $this
126
     */
127
    public function setActive($active = true)
128
    {
129
        $this->fields['ACTIVE'] = $active ? 'Y' : 'N';
130
131
        return $this;
132
    }
133
134
    /**
135
     * Обязательное (Y|N).
136
     * @param bool $isRequired
137
     * @return $this
138
     */
139
    public function setIsRequired($isRequired = true)
140
    {
141
        $this->fields['IS_REQUIRED'] = $isRequired ? 'Y' : 'N';
142
143
        return $this;
144
    }
145
146
    /**
147
     * Индекс сортировки.
148
     * @param int $sort
149
     * @return $this
150
     */
151
    public function setSort($sort = 500)
152
    {
153
        $this->fields['SORT'] = $sort;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Тип свойства. Возможные значения: S - строка, N - число, F - файл, L - список, E - привязка к элементам, G - привязка к группам.
160
     * @param string $propertyType
161
     * @return $this
162
     */
163
    public function setPropertyType($propertyType = 'S')
164
    {
165
        $this->fields['PROPERTY_TYPE'] = $propertyType;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Установить тип свойства "Список"
172
     * @param array $values массив доступных значений (можно собрать с помощью класса IBlockPropertyEnum)
173
     * @param string $listType Тип, может быть "L" - выпадающий список или "C" - флажки.
174
     * @param int $multipleCnt Количество строк в выпадающем списке
175
     * @return $this
176
     */
177
    public function setPropertyTypeList($values, $listType = null, $multipleCnt = null)
178
    {
179
        $this->setPropertyType('L');
180
        $this->fields['VALUES'] = $values;
181
182
        if (!is_null($listType)) {
183
            $this->setListType($listType);
184
        }
185
186
        if (!is_null($multipleCnt)) {
187
            $this->setMultipleCnt($multipleCnt);
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Установить тип свойства "Файл"
195
     * @param string $fileType Список допустимых расширений (через запятую).
196
     * @return $this
197
     */
198
    public function setPropertyTypeFile($fileType = null)
199
    {
200
        $this->setPropertyType('F');
201
202
        if (!is_null($fileType)) {
203
            $this->setFileType($fileType);
204
        }
205
206
        return $this;
207
    }
208
209
    /**
210
     * Установить тип свойства "привязка к элементам" или "привязка к группам"
211
     * @param string $property_type Тип свойства. Возможные значения: E - привязка к элементам, G - привязка к группам.
212
     * @param string $linkIblockId код информационного блока с элементами/группами которого и будут связано значение.
213
     * @return $this
214
     */
215
    public function setPropertyTypeIblock($property_type, $linkIblockId)
216
    {
217
        $this->setPropertyType($property_type)->setLinkIblockId($linkIblockId);
218
219
        return $this;
220
    }
221
222
    /**
223
     * Установить тип свойства "справочник"
224
     * @param string $table_name таблица HL для связи
225
     * @return $this
226
     */
227
    public function setPropertyTypeHl($table_name)
228
    {
229
        $this->setPropertyType('S')->setUserType('directory')->setUserTypeSettings([
230
            'TABLE_NAME' => $table_name
231
        ]);
232
233
        return $this;
234
    }
235
236
    /**
237
     * Множественность (Y|N).
238
     * @param bool $multiple
239
     * @return $this
240
     */
241
    public function setMultiple($multiple = false)
242
    {
243
        $this->fields['MULTIPLE'] = $multiple ? 'Y' : 'N';
244
245
        return $this;
246
    }
247
248
    /**
249
     * Количество строк в выпадающем списке для свойств типа "список".
250
     * @param int $multipleCnt
251
     * @return $this
252
     */
253
    public function setMultipleCnt($multipleCnt)
254
    {
255
        $this->fields['MULTIPLE_CNT'] = $multipleCnt;
256
257
        return $this;
258
    }
259
260
    /**
261
     * Значение свойства по умолчанию (кроме свойства типа список L).
262
     * @param string $defaultValue
263
     * @return $this
264
     */
265
    public function setDefaultValue($defaultValue)
266
    {
267
        $this->fields['DEFAULT_VALUE'] = $defaultValue;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Количество строк в ячейке ввода значения свойства.
274
     * @param int $rowCount
275
     * @return $this
276
     */
277
    public function setRowCount($rowCount)
278
    {
279
        $this->fields['ROW_COUNT'] = $rowCount;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Количество столбцов в ячейке ввода значения свойства.
286
     * @param int $colCount
287
     * @return $this
288
     */
289
    public function setColCount($colCount)
290
    {
291
        $this->fields['COL_COUNT'] = $colCount;
292
293
        return $this;
294
    }
295
296
    /**
297
     * Тип для свойства список (L). Может быть "L" - выпадающий список или "C" - флажки.
298
     * @param string $listType
299
     * @return $this
300
     */
301
    public function setListType($listType = 'L')
302
    {
303
        $this->fields['LIST_TYPE'] = $listType;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Список допустимых расширений для свойств файл "F" (через запятую).
310
     * @param string $fileType
311
     * @return $this
312
     */
313
    public function setFileType($fileType)
314
    {
315
        $this->fields['FILE_TYPE'] = $fileType;
316
317
        return $this;
318
    }
319
320
    /**
321
     * Индексировать значения данного свойства.
322
     * @param bool $searchable
323
     * @return $this
324
     */
325
    public function setSearchable($searchable = false)
326
    {
327
        $this->fields['SEARCHABLE'] = $searchable ? 'Y' : 'N';
328
329
        return $this;
330
    }
331
332
    /**
333
     * Выводить поля для фильтрации по данному свойству на странице списка элементов в административном разделе.
334
     * @param bool $filtrable
335
     * @return $this
336
     */
337
    public function setFiltrable($filtrable = false)
338
    {
339
        $this->fields['FILTRABLE'] = $filtrable ? 'Y' : 'N';
340
341
        return $this;
342
    }
343
344
    /**
345
     * Для свойств типа привязки к элементам и группам задает код информационного блока с элементами/группами которого и будут связано значение.
346
     * @param int $linkIblockId
347
     * @return $this
348
     */
349
    public function setLinkIblockId($linkIblockId)
350
    {
351
        $this->fields['LINK_IBLOCK_ID'] = $linkIblockId;
352
353
        return $this;
354
    }
355
356
    /**
357
     * Признак наличия у значения свойства дополнительного поля описания. Только для типов S - строка, N - число и F - файл (Y|N).
358
     * @param bool $withDescription
359
     * @return $this
360
     */
361
    public function setWithDescription($withDescription)
362
    {
363
        $this->fields['WITH_DESCRIPTION'] = $withDescription ? 'Y' : 'N';
364
365
        return $this;
366
    }
367
368
    /**
369
     * Идентификатор пользовательского типа свойства.
370
     * @param string $user_type
371
     * @return $this
372
     */
373
    public function setUserType($user_type)
374
    {
375
        $this->fields['USER_TYPE'] = $user_type;
376
377
        return $this;
378
    }
379
380
    /**
381
     * Идентификатор пользовательского типа свойства.
382
     * @param array $user_type_settings
383
     * @return $this
384
     */
385
    public function setUserTypeSettings($user_type_settings)
386
    {
387
        $this->fields['USER_TYPE_SETTINGS'] = array_merge((array)$this->fields['USER_TYPE_SETTINGS'], $user_type_settings);
388
389
        return $this;
390
    }
391
392
    /**
393
     * Подсказка
394
     * @param string $hint
395
     * @return $this
396
     */
397
    public function setHint($hint)
398
    {
399
        $this->fields['HINT'] = $hint;
400
401
        return $this;
402
    }
403
}