19 private static $instance;
21 private function __construct()
25 private function __clone()
35 if (!isset(self::$instance))
40 return self::$instance;
48 global $USER_FIELD_MANAGER;
50 return $USER_FIELD_MANAGER;
69 if(preg_match(
'/^([A-Z]+)_([0-9A-Z_]+)$/', $entityId, $matches))
75 $typeId = $factory->prepareIdentifier($matches[2]);
78 return [$factory, $typeId];
94 if (mb_substr($field[
'FIELD_NAME'], -4) ===
'_REF')
103 $APPLICATION->ThrowException(
104 Loc::getMessage(
'HIGHLOADBLOCK_HIGHLOAD_BLOCK_ENTITY_FIELD_NAME_REF_RESERVED')
111 'PROVIDE_STORAGE' => false
122 public static function onAfterUserTypeAdd($field)
124 $userFieldHelper = static::getInstance();
125 $parseResult = $userFieldHelper->parseUserFieldEntityId($field[
'ENTITY_ID']);
128 [$factory, $typeId] = $parseResult;
129 $userFieldManager = $userFieldHelper->getManager();
130 $application = $userFieldHelper->getApplication();
133 $dataClass = $factory->getTypeDataClass();
135 $field[
'USER_TYPE'] = $userFieldManager->getUserType($field[
'USER_TYPE_ID']);
137 $typeData = $dataClass::getById($typeId)->fetch();
139 if (empty($typeData))
141 $application->throwException(sprintf(
142 'Entity "%s" wasn\'t found.', $factory->getUserFieldEntityId($typeId)
149 $sql_column_type = $userFieldManager->getUtsDBColumnType($field);
153 $sqlHelper = $connection->getSqlHelper();
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
162 if ($field[
'MULTIPLE'] ==
'Y')
165 $typeEntity = $dataClass::compileEntity($typeData);
166 $utmEntity = Entity::getInstance($dataClass::getUtmEntityClassName($typeEntity, $field));
168 $utmEntity->createDbTable();
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')
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')
186 catch (SqlQueryException $sqlQueryException)
188 $userTypeEntity = new \CUserTypeEntity;
189 $userTypeEntity->Delete($field[
'ID']);
191 throw new InvalidOperationException(
192 'Could not create new user field ' . $field[
'FIELD_NAME'],
198 'PROVIDE_STORAGE' => false
209 public static function OnBeforeUserTypeDelete($field)
211 $userFieldHelper = static::getInstance();
212 $parseResult = $userFieldHelper->parseUserFieldEntityId($field[
'ENTITY_ID']);
216 [$factory, $typeId] = $parseResult;
218 $dataClass = $factory->getTypeDataClass();
220 $typeData = $dataClass::getById($typeId)->fetch();
222 if (empty($typeData))
226 'PROVIDE_STORAGE' =>
false,
230 $userFieldManager = $userFieldHelper->getManager();
232 $fieldType = $userFieldManager->getUserType($field[
"USER_TYPE_ID"]);
234 if ($fieldType[
'BASE_TYPE'] ===
'file')
237 $itemEntity = $dataClass::compileEntity($typeData);
238 $query =
new Query($itemEntity);
239 $rows = $query->addSelect($field[
'FIELD_NAME'])->exec();
241 while ($oldData = $rows->fetch())
243 if (empty($oldData[$field[
'FIELD_NAME']]))
248 if(is_array($oldData[$field[
'FIELD_NAME']]))
250 foreach($oldData[$field[
'FIELD_NAME']] as $value)
252 \CFile::delete($value);
257 \CFile::delete($oldData[$field[
'FIELD_NAME']]);
266 $connection->dropColumn($typeData[
'TABLE_NAME'], $field[
'FIELD_NAME']);
268 catch(SqlQueryException $e)
274 if ($field[
'MULTIPLE'] ===
'Y')
276 $utmTableName = $dataClass::getMultipleValueTableName($typeData, $field);
277 if ($connection->isTableExists($utmTableName))
279 $connection->dropTable($utmTableName);
284 'PROVIDE_STORAGE' =>
false,
291 public static function onGetUserFieldValues(Event $event): EventResult
293 $result =
new EventResult(EventResult::SUCCESS);
295 $entityId = $event->getParameter(
'entityId');
296 $userFieldHelper = static::getInstance();
297 $parseResult = $userFieldHelper->parseUserFieldEntityId($entityId);
300 $userFields = $event->getParameter(
'userFields');
301 $value = $event->getParameter(
'value');
304 [$factory, $typeId] = $parseResult;
305 $dataClass = $factory->getTypeDataClass();
306 $typeData = $dataClass::getById($typeId)->fetch();
311 $itemDataClass = $factory->getItemDataClass($typeData);
312 $values = $itemDataClass::getUserFieldValues($value, $userFields);
319 $result =
new EventResult(EventResult::SUCCESS, [
327 public static function onUpdateUserFieldValues(Event $event): EventResult
329 $result =
new EventResult(EventResult::UNDEFINED);
331 $entityId = $event->getParameter(
'entityId');
332 $userFieldHelper = static::getInstance();
333 $parseResult = $userFieldHelper->parseUserFieldEntityId($entityId);
336 $fields = $event->getParameter(
'fields');
337 $id = $event->getParameter(
'id');
340 [$factory, $typeId] = $parseResult;
341 $dataClass = $factory->getTypeDataClass();
342 $typeData = $dataClass::getById($typeId)->fetch();
347 $itemDataClass = $factory->getItemDataClass($typeData);
348 $updateResult = $itemDataClass::updateUserFieldValues($id, $fields);
349 if($updateResult->isSuccess())
351 $result =
new EventResult(EventResult::SUCCESS);
355 $result =
new EventResult(EventResult::ERROR);
362 public static function onDeleteUserFieldValues(Event $event): EventResult
364 $result =
new EventResult(EventResult::UNDEFINED);
366 $entityId = $event->getParameter(
'entityId');
367 $userFieldHelper = static::getInstance();
368 $parseResult = $userFieldHelper->parseUserFieldEntityId($entityId);
371 $id = $event->getParameter(
'id');
374 [$factory, $typeId] = $parseResult;
375 $dataClass = $factory->getTypeDataClass();
376 $typeData = $dataClass::getById($typeId)->fetch();
381 $itemDataClass = $factory->getItemDataClass($typeData);
382 $updateResult = $itemDataClass::deleteUserFieldValues($id);
383 if($updateResult->isSuccess())
385 $result =
new EventResult(EventResult::SUCCESS);
389 $result =
new EventResult(EventResult::ERROR);