Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userfieldhelper.php
1<?php
2
4
13
14final class UserFieldHelper
15{
16 public const ERROR_CODE_USER_FIELD_CREATION = 'ERROR_CODE_USER_FIELD_CREATION';
17
19 private static $instance;
20
21 private function __construct()
22 {
23 }
24
25 private function __clone()
26 {
27 }
28
33 public static function getInstance(): UserFieldHelper
34 {
35 if (!isset(self::$instance))
36 {
37 self::$instance = new UserFieldHelper;
38 }
39
40 return self::$instance;
41 }
42
46 public function getManager(): ?\CUserTypeManager
47 {
48 global $USER_FIELD_MANAGER;
49
50 return $USER_FIELD_MANAGER;
51 }
52
56 public function getApplication(): ?\CMain
57 {
58 global $APPLICATION;
59
60 return $APPLICATION;
61 }
62
67 public function parseUserFieldEntityId(string $entityId): ?array
68 {
69 if(preg_match('/^([A-Z]+)_([0-9A-Z_]+)$/', $entityId, $matches))
70 {
71 $typeCode = TypeFactory::getCodeByPrefix($matches[1]);
72 $factory = Registry::getInstance()->getFactoryByCode($typeCode);
73 if($factory)
74 {
75 $typeId = $factory->prepareIdentifier($matches[2]);
76 if ($typeId > 0)
77 {
78 return [$factory, $typeId];
79 }
80 }
81 }
82
83 return null;
84 }
85
90 public static function OnBeforeUserTypeAdd($field)
91 {
92 if (static::getInstance()->parseUserFieldEntityId($field['ENTITY_ID']))
93 {
94 if (mb_substr($field['FIELD_NAME'], -4) === '_REF')
95 {
100 global $APPLICATION;
101
102 Loc::loadLanguageFile(__DIR__.'/highloadblock.php');
103 $APPLICATION->ThrowException(
104 Loc::getMessage('HIGHLOADBLOCK_HIGHLOAD_BLOCK_ENTITY_FIELD_NAME_REF_RESERVED')
105 );
106
107 return false;
108 }
109
110 return [
111 'PROVIDE_STORAGE' => false
112 ];
113 }
114
115 return true;
116 }
117
122 public static function onAfterUserTypeAdd($field)
123 {
124 $userFieldHelper = static::getInstance();
125 $parseResult = $userFieldHelper->parseUserFieldEntityId($field['ENTITY_ID']);
126 if($parseResult)
127 {
128 [$factory, $typeId] = $parseResult;
129 $userFieldManager = $userFieldHelper->getManager();
130 $application = $userFieldHelper->getApplication();
133 $dataClass = $factory->getTypeDataClass();
134
135 $field['USER_TYPE'] = $userFieldManager->getUserType($field['USER_TYPE_ID']);
136
137 $typeData = $dataClass::getById($typeId)->fetch();
138
139 if (empty($typeData))
140 {
141 $application->throwException(sprintf(
142 'Entity "%s" wasn\'t found.', $factory->getUserFieldEntityId($typeId)
143 ));
144
145 return false;
146 }
147
148 // get usertype info
149 $sql_column_type = $userFieldManager->getUtsDBColumnType($field);
150
151 // create field in db
152 $connection = Application::getConnection();
153 $sqlHelper = $connection->getSqlHelper();
154
155 try
156 {
157 $connection->query(sprintf(
158 'ALTER TABLE %s ADD %s %s',
159 $sqlHelper->quote($typeData['TABLE_NAME']), $sqlHelper->quote($field['FIELD_NAME']), $sql_column_type
160 ));
161
162 if ($field['MULTIPLE'] == 'Y')
163 {
164 // create table for this relation
165 $typeEntity = $dataClass::compileEntity($typeData);
166 $utmEntity = Entity::getInstance($dataClass::getUtmEntityClassName($typeEntity, $field));
167
168 $utmEntity->createDbTable();
169
170 // add indexes
171 $connection->query(sprintf(
172 'CREATE INDEX %s ON %s (%s)',
173 $sqlHelper->quote('IX_UTM_HL'.$typeId.'_'.$field['ID'].'_ID'),
174 $sqlHelper->quote($utmEntity->getDBTableName()),
175 $sqlHelper->quote('ID')
176 ));
177
178 $connection->query(sprintf(
179 'CREATE INDEX %s ON %s (%s)',
180 $sqlHelper->quote('IX_UTM_HL'.$typeId.'_'.$field['ID'].'_VALUE'),
181 $sqlHelper->quote($utmEntity->getDBTableName()),
182 $sqlHelper->quote('VALUE')
183 ));
184 }
185 }
186 catch (SqlQueryException $sqlQueryException)
187 {
188 $userTypeEntity = new \CUserTypeEntity;
189 $userTypeEntity->Delete($field['ID']);
190
191 throw new InvalidOperationException(
192 'Could not create new user field ' . $field['FIELD_NAME'],
193 $sqlQueryException
194 );
195 }
196
197 return [
198 'PROVIDE_STORAGE' => false
199 ];
200 }
201
202 return true;
203 }
204
209 public static function OnBeforeUserTypeDelete($field)
210 {
211 $userFieldHelper = static::getInstance();
212 $parseResult = $userFieldHelper->parseUserFieldEntityId($field['ENTITY_ID']);
213 if($parseResult)
214 {
216 [$factory, $typeId] = $parseResult;
218 $dataClass = $factory->getTypeDataClass();
219 // get entity info
220 $typeData = $dataClass::getById($typeId)->fetch();
221
222 if (empty($typeData))
223 {
224 // non-existent or zombie. let it go
225 return [
226 'PROVIDE_STORAGE' => false,
227 ];
228 }
229
230 $userFieldManager = $userFieldHelper->getManager();
232 $fieldType = $userFieldManager->getUserType($field["USER_TYPE_ID"]);
233
234 if ($fieldType['BASE_TYPE'] === 'file')
235 {
236 // if it was file field, then delete all files
237 $itemEntity = $dataClass::compileEntity($typeData);
238 $query = new Query($itemEntity);
239 $rows = $query->addSelect($field['FIELD_NAME'])->exec();
240
241 while ($oldData = $rows->fetch())
242 {
243 if (empty($oldData[$field['FIELD_NAME']]))
244 {
245 continue;
246 }
247
248 if(is_array($oldData[$field['FIELD_NAME']]))
249 {
250 foreach($oldData[$field['FIELD_NAME']] as $value)
251 {
252 \CFile::delete($value);
253 }
254 }
255 else
256 {
257 \CFile::delete($oldData[$field['FIELD_NAME']]);
258 }
259 }
260 }
261
262 // drop db column
263 $connection = Application::getConnection();
264 try
265 {
266 $connection->dropColumn($typeData['TABLE_NAME'], $field['FIELD_NAME']);
267 }
268 catch(SqlQueryException $e)
269 {
270 // no column is ok
271 }
272
273 // if multiple - drop utm table
274 if ($field['MULTIPLE'] === 'Y')
275 {
276 $utmTableName = $dataClass::getMultipleValueTableName($typeData, $field);
277 if ($connection->isTableExists($utmTableName))
278 {
279 $connection->dropTable($utmTableName);
280 }
281 }
282
283 return [
284 'PROVIDE_STORAGE' => false,
285 ];
286 }
287
288 return true;
289 }
290
291 public static function onGetUserFieldValues(Event $event): EventResult
292 {
293 $result = new EventResult(EventResult::SUCCESS);
294
295 $entityId = $event->getParameter('entityId');
296 $userFieldHelper = static::getInstance();
297 $parseResult = $userFieldHelper->parseUserFieldEntityId($entityId);
298 if($parseResult)
299 {
300 $userFields = $event->getParameter('userFields');
301 $value = $event->getParameter('value');
302
304 [$factory, $typeId] = $parseResult;
305 $dataClass = $factory->getTypeDataClass();
306 $typeData = $dataClass::getById($typeId)->fetch();
307 if(!$typeData)
308 {
309 return $result;
310 }
311 $itemDataClass = $factory->getItemDataClass($typeData);
312 $values = $itemDataClass::getUserFieldValues($value, $userFields);
313
314 if(!$values)
315 {
316 $values = [];
317 }
318
319 $result = new EventResult(EventResult::SUCCESS, [
320 'values' => $values,
321 ]);
322 }
323
324 return $result;
325 }
326
327 public static function onUpdateUserFieldValues(Event $event): EventResult
328 {
329 $result = new EventResult(EventResult::UNDEFINED);
330
331 $entityId = $event->getParameter('entityId');
332 $userFieldHelper = static::getInstance();
333 $parseResult = $userFieldHelper->parseUserFieldEntityId($entityId);
334 if($parseResult)
335 {
336 $fields = $event->getParameter('fields');
337 $id = $event->getParameter('id');
338
340 [$factory, $typeId] = $parseResult;
341 $dataClass = $factory->getTypeDataClass();
342 $typeData = $dataClass::getById($typeId)->fetch();
343 if(!$typeData)
344 {
345 return $result;
346 }
347 $itemDataClass = $factory->getItemDataClass($typeData);
348 $updateResult = $itemDataClass::updateUserFieldValues($id, $fields);
349 if($updateResult->isSuccess())
350 {
351 $result = new EventResult(EventResult::SUCCESS);
352 }
353 else
354 {
355 $result = new EventResult(EventResult::ERROR);
356 }
357 }
358
359 return $result;
360 }
361
362 public static function onDeleteUserFieldValues(Event $event): EventResult
363 {
364 $result = new EventResult(EventResult::UNDEFINED);
365
366 $entityId = $event->getParameter('entityId');
367 $userFieldHelper = static::getInstance();
368 $parseResult = $userFieldHelper->parseUserFieldEntityId($entityId);
369 if($parseResult)
370 {
371 $id = $event->getParameter('id');
372
374 [$factory, $typeId] = $parseResult;
375 $dataClass = $factory->getTypeDataClass();
376 $typeData = $dataClass::getById($typeId)->fetch();
377 if(!$typeData)
378 {
379 return $result;
380 }
381 $itemDataClass = $factory->getItemDataClass($typeData);
382 $updateResult = $itemDataClass::deleteUserFieldValues($id);
383 if($updateResult->isSuccess())
384 {
385 $result = new EventResult(EventResult::SUCCESS);
386 }
387 else
388 {
389 $result = new EventResult(EventResult::ERROR);
390 }
391 }
392
393 return $result;
394 }
395}
static getConnection($name="")
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29