UserField::setUserType()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 1
1
<?php
2
3
4
namespace Arrilot\BitrixMigrations\Constructors;
5
6
7
use Arrilot\BitrixMigrations\Helpers;
8
use Arrilot\BitrixMigrations\Logger;
9
use Bitrix\Highloadblock\HighloadBlockTable;
10
use Bitrix\Main\Application;
11
12
class UserField
13
{
14
    use FieldConstructor;
15
16
    /**
17
     * Добавить UF
18
     * @throws \Exception
19
     */
20
    public function add()
21
    {
22
        $uf = new \CUserTypeEntity();
23
        $result = $uf->Add($this->getFieldsWithDefault());
24
25
        if (!$result) {
26
            global $APPLICATION;
27
            throw new \Exception($APPLICATION->GetException());
28
        }
29
30
        Logger::log("Добавлен UF {$this->fields['FIELD_NAME']} для {$this->fields['ENTITY_ID']}", Logger::COLOR_GREEN);
31
32
        return $result;
33
    }
34
35
    /**
36
     * Обновить UF
37
     * @param $id
38
     * @throws \Exception
39
     */
40 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...
41
    {
42
        $uf = new \CUserTypeEntity();
43
        $result = $uf->Update($id, $this->fields);
44
45
        if (!$result) {
46
            global $APPLICATION;
47
            throw new \Exception($APPLICATION->GetException());
48
        }
49
50
        Logger::log("Обновлен UF {$id}", Logger::COLOR_GREEN);
51
    }
52
53
    /**
54
     * Удалить UF
55
     * @param $id
56
     * @throws \Exception
57
     */
58 View Code Duplication
    public static function delete($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...
59
    {
60
        $result = (new \CUserTypeEntity())->Delete($id);
61
62
        if (!$result) {
63
            global $APPLICATION;
64
            throw new \Exception($APPLICATION->GetException());
65
        }
66
67
        Logger::log("Удален UF {$id}", Logger::COLOR_GREEN);
68
    }
69
70
    /**
71
     * Установить настройки для добавления UF по умолчанию
72
     * @param string $entityId Идентификатор сущности
73
     * @param string $fieldName Код поля.
74
     * @return $this
75
     */
76
    public function constructDefault($entityId, $fieldName)
77
    {
78
        return $this->setEntityId($entityId)->setFieldName($fieldName)->setUserType('string');
79
    }
80
81
    /**
82
     * Идентификатор сущности, к которой будет привязано свойство.
83
     * @param string $entityId
84
     * @return $this
85
     */
86
    public function setEntityId($entityId)
87
    {
88
        $this->fields['ENTITY_ID'] = $entityId;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Код поля. Всегда должно начинаться с UF_
95
     * @param string $fieldName
96
     * @return $this
97
     */
98
    public function setFieldName($fieldName)
99
    {
100
        $this->fields['FIELD_NAME'] = static::prepareUf($fieldName);
101
102
        return $this;
103
    }
104
105
    /**
106
     * тип пользовательского свойства
107
     * @param string $userType
108
     * @return $this
109
     */
110
    public function setUserType($userType)
111
    {
112
        $this->fields['USER_TYPE_ID'] = $userType;
113
114
        return $this;
115
    }
116
117
    /**
118
     * тип нового пользовательского свойства HL
119
     * @param string $table_name
120
     * @param string $showField
121
     * @return $this
122
     */
123
    public function setUserTypeHL($table_name, $showField)
124
    {
125
        $linkId = Helpers::getHlId($table_name);
126
        $this->setUserType('hlblock')->setSettings([
127
            'HLBLOCK_ID' => Helpers::getHlId($table_name),
128
            'HLFIELD_ID' => Helpers::getFieldId(Constructor::objHLBlock($linkId), static::prepareUf($showField)),
129
        ]);
130
131
        return $this;
132
    }
133
134
    /**
135
     * тип нового пользовательского свойства "связь с разелом ИБ"
136
     * @param string $iblockId
137
     * @return $this
138
     */
139
    public function setUserTypeIblockSection($iblockId)
140
    {
141
        $this->setUserType('iblock_section')->setSettings([
142
            'IBLOCK_ID' => $iblockId,
143
        ]);
144
145
        return $this;
146
    }
147
148
    /**
149
     * тип нового пользовательского свойства "связь с элементом ИБ"
150
     * @param string $iblockId
151
     * @return $this
152
     */
153
    public function setUserTypeIblockElement($iblockId)
154
    {
155
        $this->setUserType('iblock_element')->setSettings([
156
            'IBLOCK_ID' => $iblockId,
157
        ]);
158
159
        return $this;
160
    }
161
162
    /**
163
     * XML_ID пользовательского свойства. Используется при выгрузке в качестве названия поля
164
     * @param string $xmlId
165
     * @return $this
166
     */
167
    public function setXmlId($xmlId)
168
    {
169
        $this->fields['XML_ID'] = $xmlId;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Сортировка
176
     * @param int $sort
177
     * @return $this
178
     */
179
    public function setSort($sort)
180
    {
181
        $this->fields['SORT'] = $sort;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Является поле множественным или нет
188
     * @param bool $multiple
189
     * @return $this
190
     */
191
    public function setMultiple($multiple)
192
    {
193
        $this->fields['MULTIPLE'] = $multiple ? 'Y' : 'N';
194
195
        return $this;
196
    }
197
198
    /**
199
     * Обязательное или нет свойство
200
     * @param bool $mandatory
201
     * @return $this
202
     */
203
    public function setMandatory($mandatory)
204
    {
205
        $this->fields['MANDATORY'] = $mandatory ? 'Y' : 'N';
206
207
        return $this;
208
    }
209
210
    /**
211
     * Показывать в фильтре списка. Возможные значения: не показывать = N, точное совпадение = I, поиск по маске = E, поиск по подстроке = S
212
     * @param string $showInFilter
213
     * @return $this
214
     */
215
    public function setShowFilter($showInFilter)
216
    {
217
        $this->fields['SHOW_FILTER'] = $showInFilter;
218
219
        return $this;
220
    }
221
222
    /**
223
     * Не показывать в списке. Если передать какое-либо значение, то будет считаться, что флаг выставлен.
224
     * @param bool $showInList
225
     * @return $this
226
     */
227
    public function setShowInList($showInList)
228
    {
229
        $this->fields['SHOW_IN_LIST'] = $showInList ? 'Y' : '';
230
231
        return $this;
232
    }
233
234
    /**
235
     * Пустая строка разрешает редактирование. Если передать какое-либо значение, то будет считаться, что флаг выставлен.
236
     * @param bool $editInList
237
     * @return $this
238
     */
239
    public function setEditInList($editInList)
240
    {
241
        $this->fields['EDIT_IN_LIST'] = $editInList ? 'Y' : '';
242
243
        return $this;
244
    }
245
246
    /**
247
     * Значения поля участвуют в поиске
248
     * @param bool $isSearchable
249
     * @return $this
250
     */
251
    public function setIsSearchable($isSearchable = false)
252
    {
253
        $this->fields['IS_SEARCHABLE'] = $isSearchable ? 'Y' : 'N';
254
255
        return $this;
256
    }
257
258
    /**
259
     * Дополнительные настройки поля (зависят от типа). В нашем случае для типа string
260
     * @param array $settings
261
     * @return $this
262
     */
263
    public function setSettings($settings)
264
    {
265
        $this->fields['SETTINGS'] = array_merge((array)$this->fields['SETTINGS'], $settings);
266
267
        return $this;
268
    }
269
270
    /**
271
     * Языковые фразы
272
     * @param string $lang
273
     * @param string $text
274
     * @return $this
275
     */
276
    public function setLangDefault($lang, $text)
277
    {
278
        $this->setLangForm($lang, $text);
279
        $this->setLangColumn($lang, $text);
280
        $this->setLangFilter($lang, $text);
281
282
        return $this;
283
    }
284
285
    /**
286
     * Текст "Заголовок в списке"
287
     * @param string $lang
288
     * @param string $text
289
     * @return $this
290
     */
291
    public function setLangForm($lang, $text)
292
    {
293
        $this->fields['EDIT_FORM_LABEL'][$lang] = $text;
294
295
        return $this;
296
    }
297
298
    /**
299
     * Текст "Заголовок в списке"
300
     * @param string $lang
301
     * @param string $text
302
     * @return $this
303
     */
304
    public function setLangColumn($lang, $text)
305
    {
306
        $this->fields['LIST_COLUMN_LABEL'][$lang] = $text;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Текст "Подпись фильтра в списке"
313
     * @param string $lang
314
     * @param string $text
315
     * @return $this
316
     */
317
    public function setLangFilter($lang, $text)
318
    {
319
        $this->fields['LIST_FILTER_LABEL'][$lang] = $text;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Текст "Помощь"
326
     * @param string $lang
327
     * @param string $text
328
     * @return $this
329
     */
330
    public function setLangHelp($lang, $text)
331
    {
332
        $this->fields['HELP_MESSAGE'][$lang] = $text;
333
334
        return $this;
335
    }
336
337
    /**
338
     * Текст "Сообщение об ошибке (не обязательное)"
339
     * @param string $lang
340
     * @param string $text
341
     * @return $this
342
     */
343
    public function setLangError($lang, $text)
344
    {
345
        $this->fields['ERROR_MESSAGE'][$lang] = $text;
346
347
        return $this;
348
    }
349
350
    protected static function prepareUf($name)
351
    {
352
        if (substr($name, 0, 3) != 'UF_') {
353
            $name = "UF_{$name}";
354
        }
355
356
        return $name;
357
    }
358
}
359