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