Completed
Push — master ( 92ad00...b7fed4 )
by Nekrasov
02:04
created

BitrixMigration::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixMigrations\BaseMigrations;
4
5
use Arrilot\BitrixMigrations\Exceptions\MigrationException;
6
use Arrilot\BitrixMigrations\Interfaces\MigrationInterface;
7
use Bitrix\Main\Application;
8
use Bitrix\Main\DB\Connection;
9
use CIBlock;
10
use CIBlockProperty;
11
use CUserTypeEntity;
12
13
class BitrixMigration implements MigrationInterface
14
{
15
    /**
16
     * DB connection.
17
     *
18
     * @var Connection
19
     */
20
    protected $db;
21
22
    /**
23
     * Constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->db = Application::getConnection();
28
    }
29
30
    /**
31
     * Run the migration.
32
     *
33
     * @return mixed
34
     */
35
    public function up()
36
    {
37
        //
38
    }
39
40
    /**
41
     * Reverse the migration.
42
     *
43
     * @return mixed
44
     */
45
    public function down()
46
    {
47
        //
48
    }
49
50
    /**
51
     * Find iblock id by its code.
52
     *
53
     * @param string $code
54
     * @param null|string $iBlockType
55
     *
56
     * @throws MigrationException
57
     *
58
     * @return int
59
     */
60
    protected function getIblockIdByCode($code, $iBlockType = null)
61
    {
62
        if (!$code) {
63
            throw new MigrationException('Не задан код инфоблока');
64
        }
65
66
        $filter = [
67
            'CODE'              => $code,
68
            'CHECK_PERMISSIONS' => 'N',
69
        ];
70
71
        if ($iBlockType !== null) {
72
            $filter['TYPE'] = $iBlockType;
73
        }
74
75
        $iblock = (new CIBlock())->GetList([], $filter)->fetch();
76
77
        if (!$iblock['ID']) {
78
            throw new MigrationException("Не удалось найти инфоблок с кодом '{$code}'");
79
        }
80
81
        return $iblock['ID'];
82
    }
83
84
    /**
85
     * Delete iblock by its code.
86
     *
87
     * @param string $code
88
     *
89
     * @throws MigrationException
90
     *
91
     * @return void
92
     */
93
    protected function deleteIblockByCode($code)
94
    {
95
        $id = $this->getIblockIdByCode($code);
96
97
        $this->db->startTransaction();
98
        if (!CIBlock::Delete($id)) {
99
            $this->db->rollbackTransaction();
100
            throw new MigrationException('Ошибка при удалении инфоблока');
101
        }
102
103
        $this->db->commitTransaction();
104
    }
105
106
    /**
107
     * Add iblock element property.
108
     *
109
     * @param array $fields
110
     *
111
     * @throws MigrationException
112
     *
113
     * @return int
114
     */
115
    public function addIblockElementProperty($fields)
116
    {
117
        $ibp = new CIBlockProperty();
118
        $propId = $ibp->add($fields);
119
120
        if (!$propId) {
121
            throw new MigrationException('Ошибка при добавлении свойства инфоблока '.$ibp->LAST_ERROR);
122
        }
123
124
        return $propId;
125
    }
126
127
    /**
128
     * Delete iblock element property.
129
     *
130
     * @param string     $code
131
     * @param string|int $iblockId
132
     *
133
     * @throws MigrationException
134
     */
135
    public function deleteIblockElementPropertyByCode($iblockId, $code)
136
    {
137
        if (!$iblockId) {
138
            throw new MigrationException('Не задан ID инфоблока');
139
        }
140
141
        if (!$code) {
142
            throw new MigrationException('Не задан код свойства');
143
        }
144
145
        $id = $this->getIblockPropIdByCode($code, $iblockId);
146
147
        CIBlockProperty::Delete($id);
148
    }
149
150
    /**
151
     * Add User Field.
152
     *
153
     * @param $fields
154
     *
155
     * @throws MigrationException
156
     *
157
     * @return int
158
     */
159
    public function addUF($fields)
160
    {
161
        if (!$fields['FIELD_NAME']) {
162
            throw new MigrationException('Не заполнен FIELD_NAME');
163
        }
164
165
        if (!$fields['ENTITY_ID']) {
166
            throw new MigrationException('Не заполнен код ENTITY_ID');
167
        }
168
169
        $oUserTypeEntity = new CUserTypeEntity();
170
171
        $fieldId = $oUserTypeEntity->Add($fields);
172
173
        if (!$fieldId) {
174
            throw new MigrationException("Не удалось создать пользовательское свойство с FIELD_NAME = {$fields['FIELD_NAME']} и ENTITY_ID = {$fields['ENTITY_ID']}");
175
        }
176
177
        return $fieldId;
178
    }
179
180
    /**
181
     * Get UF by its code.
182
     *
183
     * @param string $entity
184
     * @param string $code
185
     *
186
     * @throws MigrationException
187
     */
188
    public function getUFIdByCode($entity, $code)
189
    {
190
        if (!$entity) {
191
            throw new MigrationException('Не задана сущность свойства');
192
        }
193
194
        if (!$code) {
195
            throw new MigrationException('Не задан код свойства');
196
        }
197
198
        $filter = [
199
            'ENTITY_ID'  => $entity,
200
            'FIELD_NAME' => $code,
201
        ];
202
203
        $arField = CUserTypeEntity::GetList(['ID' => 'ASC'], $filter)->fetch();
204
        if (!$arField || !$arField['ID']) {
205
            throw new MigrationException("Не найдено свойство с FIELD_NAME = {$filter['FIELD_NAME']} и ENTITY_ID = {$filter['ENTITY_ID']}");
206
        }
207
208
        return $arField['ID'];
209
    }
210
211
    /**
212
     * @param $code
213
     * @param $iblockId
214
     *
215
     * @throws MigrationException
216
     *
217
     * @return array
218
     */
219
    protected function getIblockPropIdByCode($code, $iblockId)
220
    {
221
        $filter = [
222
            'CODE'      => $code,
223
            'IBLOCK_ID' => $iblockId,
224
        ];
225
226
        $prop = CIBlockProperty::getList(['sort' => 'asc', 'name' => 'asc'], $filter)->getNext();
227
        if (!$prop || !$prop['ID']) {
228
            throw new MigrationException("Не удалось найти свойство с кодом '{$code}'");
229
        }
230
231
        return $prop['ID'];
232
    }
233
}
234