34 public static function getMap(): array
39 ->configureAutocomplete(),
44 ->configureFormat(
'/^[A-Z][A-Za-z0-9]*$/')
45 ->addValidator(
new RegExp(
52 ->configureFormat(
'/^[a-z0-9_]+$/')
53 ->addValidator([get_called_class(),
'validateTableExisting']),
64 if(!static::$temporaryStorage)
69 return static::$temporaryStorage;
81 static::compileEntity($id)->createDbTable();
92 $data = static::getByPrimary($event->
getParameter(
'id'))->fetch();
93 static::getTemporaryStorage()->saveData($event->
getParameter(
'id'), $data);
107 $oldData = static::getTemporaryStorage()->getData($id);
114 if (isset($data[
'TABLE_NAME']) && $data[
'TABLE_NAME'] !== $oldData[
'TABLE_NAME'])
118 $sqlHelper = $connection->getSqlHelper();
119 $connection->renameTable($oldData[
'TABLE_NAME'], $data[
'TABLE_NAME']);
124 $connection->query(sprintf(
125 "EXEC sp_rename %s, %s, 'OBJECT'",
126 $sqlHelper->quote($oldData[
'TABLE_NAME'].
'_ibpk_1'),
127 $sqlHelper->quote($data[
'TABLE_NAME'].
'_ibpk_1')
133 foreach ($userFieldManager->getUserFields(static::getFactory()->getUserFieldEntityId($oldData[
'ID'])) as $field)
135 if ($field[
'MULTIPLE'] ==
'Y')
137 $oldUtmTableName = static::getMultipleValueTableName($oldData, $field);
138 $newUtmTableName = static::getMultipleValueTableName($data, $field);
140 $connection->renameTable($oldUtmTableName, $newUtmTableName);
160 $oldData = static::getByPrimary($id)->fetch();
161 static::getTemporaryStorage()->saveData($id, $oldData);
172 $fields = $userFieldManager->getUserFields(static::getFactory()->getUserFieldEntityId($oldData[
'ID']));
174 foreach ($fields as $name => $field)
176 if ($field[
'USER_TYPE'][
'BASE_TYPE'] ===
'file')
178 $file_fields[] = $name;
183 if (!empty($file_fields))
185 $oldEntity = static::compileEntity($oldData);
187 $query =
new Query($oldEntity);
190 $query->setSelect($file_fields);
193 $filter = array(
'LOGIC' =>
'OR');
195 foreach ($file_fields as $file_field)
197 $filter[
'!'.$file_field] =
false;
200 $query->setFilter($filter);
203 $queryResult = $query->exec();
205 while ($row = $queryResult->fetch())
207 foreach ($file_fields as $file_field)
209 if (!empty($row[$file_field]))
211 if (is_array($row[$file_field]))
213 foreach ($row[$file_field] as $value)
215 \CFile::delete($value);
220 \CFile::delete($row[$file_field]);
229 foreach ($fields as $field)
232 if ($field[
'USER_TYPE'][
'BASE_TYPE'] ===
'enum')
234 $enumField = new \CUserFieldEnum;
235 $enumField->DeleteFieldEnum($field[
'ID']);
238 $connection->query(
"DELETE FROM b_user_field_lang WHERE USER_FIELD_ID = ".$field[
'ID']);
239 $connection->query(
"DELETE FROM b_user_field WHERE ID = ".$field[
'ID']);
242 if ($field[
'MULTIPLE'] ==
'Y')
246 $utmTableName = static::getMultipleValueTableName($oldData, $field);
247 $connection->dropTable($utmTableName);
251 $result->addError(
new EntityError($e->getMessage()));
258 if(CACHED_b_user_field !==
false)
260 $managedCache->cleanDir(
"b_user_field");
275 $oldData = static::getTemporaryStorage()->getData($id);
295 if($type instanceof
Type)
297 $type = $type->collectValues();
299 if (!is_array($type))
301 if (is_int($type) || is_numeric(mb_substr($type, 0, 1)))
304 $type = static::getById($type)->fetch();
306 elseif (is_string($type) && $type !==
'')
309 $type = static::query()->addSelect(
'*')->where(
'NAME', $type)->exec()->fetch();
319 if (!isset($type[
'ID']))
321 if (!isset($type[
'NAME']) || !preg_match(
'/^[a-z0-9_]+$/i', $type[
'NAME']))
323 if (empty($type[
'TABLE_NAME']))
335 public static function compileEntity($type):
Entity
338 $type = static::resolveType($type);
342 'Invalid type description `%s`.', mydump($rawType)
345 $factory = static::getFactory();
346 $type[
'code'] = $factory->getCode();
348 $itemDataClass = $factory->getItemPrototypeDataClass();
351 $userFields = $userFieldManager->getUserFields($factory->getUserFieldEntityId($type[
'ID']));
353 $entityName = $type[
'code'] .
'_items_' . $type[
'ID'];
354 $entityClassName = $entityName.
'Table';
355 $entityTableName = $type[
'TABLE_NAME'];
356 if(class_exists($entityClassName))
359 Entity::destroy($entityClassName);
360 $entity = Entity::getInstance($entityClassName);
364 $entity = Entity::compileEntity($entityName, [], [
365 'table_name' => $entityTableName,
366 'parent' => $itemDataClass,
367 'object_parent' => $factory->getItemParentClass(),
373 foreach ($userFields as $userField)
375 if ($userField[
'MULTIPLE'] ==
'N')
379 'required' => ($userField[
'MANDATORY'] ===
'Y')
381 $field = $userFieldManager->getEntityField($userField, $userField[
'FIELD_NAME'], $params);
383 foreach ($userFieldManager->getEntityReferences($userField, $field) as $reference)
391 static::compileUtmEntity(
$entity, $userField);
403 protected static function compileUtmEntity(Entity $typeEntity, $userField): Entity
409 $itemDataClass = $typeEntity->getDataClass();
410 $typeData = $itemDataClass::getType();
412 $utmClassName = static::getUtmEntityClassName($typeEntity, $userField);
413 $utmTableName = static::getMultipleValueTableName($typeData, $userField);
415 if (class_exists($utmClassName.
'Table'))
418 Entity::destroy($utmClassName.
'Table');
419 $utmEntity = Entity::getInstance($utmClassName);
424 $utmEntity = Entity::compileEntity($utmClassName, [], [
425 'table_name' => $utmTableName,
431 $utmValueField = $userFieldManager->getEntityField($userField,
'VALUE');
433 $utmEntityFields = array(
434 new IntegerField(
'ID'),
439 $references = $userFieldManager->getEntityReferences($userField, $utmValueField);
441 foreach ($references as $reference)
443 $utmEntityFields[] = $reference;
446 foreach ($utmEntityFields as $field)
448 $utmEntity->addField($field);
452 $referenceField =
new Reference(
455 [
'=this.ID' =>
'ref.ID']
458 $utmEntity->addField($referenceField);
461 $aliasField =
new ExpressionField(
462 $userField[
'FIELD_NAME'].
'_SINGLE',
464 $utmEntity->getFullName().
':'.
'OBJECT.VALUE',
466 'data_type' => get_class($utmEntity->getField(
'VALUE')),
467 'required' => $userField[
'MANDATORY'] ==
'Y'
471 $typeEntity->addField($aliasField);
473 $cacheField =
new ArrayField($userField[
'FIELD_NAME']);
474 $cacheField->configureRequired($userField[
'MANDATORY'] ===
'Y');
475 $cacheField->configureSerializationPhp();
477 $typeEntity->addField($cacheField);
489 return $typeEntity->
getName() .
'Utm' . StringHelper::snake2camel($userField[
'FIELD_NAME']);
499 $tableName = $type[
'TABLE_NAME'] .
'_' . mb_strtolower($userField[
'FIELD_NAME']);
501 if (mb_strlen($tableName) > static::MAXIMUM_TABLE_NAME_LENGTH && !empty($userField[
'ID']))
503 $tableName = $type[
'TABLE_NAME'] .
'_' . $userField[
'ID'];
520 $factory = static::getFactory();
521 $typeDataClass = $factory->getTypeDataClass();
523 $oldData = $typeDataClass::getByPrimary($primary)->fetch();
525 if ($value != $oldData[
'TABLE_NAME'])
532 if (!empty($checkName))
537 return Loc::getMessage(
'HIGHLOADBLOCK_HIGHLOAD_BLOCK_ENTITY_TABLE_NAME_ALREADY_EXISTS',
538 [
'#TABLE_NAME#' => $value]
static getConnection($name="")
static loadLanguageFile($file, $language=null, $normalize=true)
static getMessage($code, $replace=null, $language=null)
static onAfterUpdate(Event $event)
static onAfterAdd(Event $event)
static getUtmEntityClassName(Entity $typeEntity, array $userField)
static onAfterDelete(Event $event)
static onBeforeDelete(Event $event)
static onBeforeUpdate(Event $event)
static validateTableExisting($value, $primary, array $row, Field $field)
static getMultipleValueTableName(array $type, array $userField)