44 'data_type' =>
'integer',
46 'autocomplete' =>
true,
49 'data_type' =>
'string',
52 'FIELD_NAME' => array(
53 'data_type' =>
'string',
56 'USER_TYPE_ID' => array(
57 'data_type' =>
'string',
61 'data_type' =>
'string',
64 'data_type' =>
'integer',
68 'data_type' =>
'boolean',
69 'values' => array(
'N',
'Y'),
73 'data_type' =>
'boolean',
74 'values' => array(
'N',
'Y'),
77 'SHOW_FILTER' => array(
78 'data_type' =>
'boolean',
79 'values' => array(
'N',
'Y'),
82 'SHOW_IN_LIST' => array(
83 'data_type' =>
'boolean',
84 'values' => array(
'N',
'Y'),
87 'EDIT_IN_LIST' => array(
88 'data_type' =>
'boolean',
89 'values' => array(
'N',
'Y'),
92 'IS_SEARCHABLE' => array(
93 'data_type' =>
'boolean',
94 'values' => array(
'N',
'Y'),
95 'title' =>
Loc::getMessage(
'MAIN_USER_FIELD_TABLE_IS_SEARCHABLE_TITLE'),
98 'data_type' =>
'text',
109 $referenceName =
'LABELS';
113 '=this.ID' =>
'ref.USER_FIELD_ID',
118 $filter[
'=ref.LANGUAGE_ID'] =
new SqlExpression(
'?s', $languageId);
123 UserFieldLangTable::class,
144 $referenceName =
'LABELS';
148 foreach(static::getLabelFields() as $labelField)
150 $result[$labelField] = $referenceName .
'.' . $labelField;
158 $labelFields = static::getLabelFields();
160 $list = static::getList([
166 static::getLabelsReference(),
169 foreach($list as $data)
175 $field[
'LANGUAGE_ID'],
176 $field[
'EDIT_FORM_LABEL'],
177 $field[
'LIST_COLUMN_LABEL'],
178 $field[
'LIST_FILTER_LABEL'],
179 $field[
'ERROR_MESSAGE'],
180 $field[
'HELP_MESSAGE'],
185 foreach($labelFields as $labelField)
187 $field[$labelField][$data[
'LANGUAGE_ID']] = $data[$labelField];
196 if($field[
'USER_TYPE_ID'] ===
'enumeration')
199 $enumEntity = new \CUserFieldEnum();
200 $enumList = $enumEntity->GetList(
204 'USER_FIELD_ID' => $field[
'ID'],
207 while($enum = $enumList->Fetch())
209 $field[
'ENUM'][] = $enum;
222 public static function add(array $data)
234 public static function update($primary, array $data)
245 public static function delete($primary)
257 public static function attachFields(ORM\
Entity $entity, $ufId)
259 global $USER_FIELD_MANAGER;
261 $utsFields = array();
262 $utsFieldNames = array();
264 $utmFields = array();
265 $utmFieldNames = array();
267 $fields = $USER_FIELD_MANAGER->getUserFields($ufId);
269 foreach ($fields as $field)
271 if ($field[
'MULTIPLE'] ===
'Y')
273 $utmFields[] = $field;
274 $utmFieldNames[$field[
'FIELD_NAME']] =
true;
278 $utsFields[] = $field;
279 $utsFieldNames[$field[
'FIELD_NAME']] =
true;
283 if (!empty($utsFields) || !empty($utmFields))
286 $utsEntity = static::createUtsEntity(
$entity, $utsFields, $utmFields, $ufId);
289 $utsReference =
new ORM\Fields\Relations\Reference(
'UTS_OBJECT', $utsEntity->getDataClass(), array(
290 '=this.ID' =>
'ref.VALUE_ID'
293 $entity->addField($utsReference);
296 foreach ($fields as $userfield)
298 $utsFieldName = $userfield[
'FIELD_NAME'];
301 $utsField = $utsEntity->getField($utsFieldName);
303 $aliasField =
new ORM\Fields\UserTypeField(
306 'UTS_OBJECT.'.$utsFieldName,
307 array(
'data_type' => get_class($utsField))
310 $aliasField->configureValueField($utsField);
312 if ($userfield[
'MULTIPLE'] ==
'Y')
314 $aliasField->configureMultiple();
317 $entity->addField($aliasField);
321 if (!empty($utsFields))
323 foreach ($utsFields as $utsField)
326 $utsEntityField = $utsEntity->getField($utsField[
'FIELD_NAME']);
328 foreach ($USER_FIELD_MANAGER->getEntityReferences($utsField, $utsEntityField) as $reference)
331 $referenceDesc = static::rewriteUtsReference($reference->getReference());
333 $aliasReference =
new ORM\Fields\Relations\Reference(
334 $reference->getName(),
335 $reference->getRefEntityName(),
339 $entity->addField($aliasReference);
344 if (!empty($utmFields))
347 $utmEntity = static::createUtmEntity(
$entity, $utmFields, $ufId);
350 foreach ($utmFieldNames as $utmFieldName => $true)
353 $utmField = $utmEntity->getField($utmFieldName);
355 $aliasField =
new ORM\Fields\ExpressionField(
356 $utmFieldName.
'_SINGLE',
358 $utmEntity->getFullName().
':PARENT_'.$utmFieldName.
'.'.$utmField->getColumnName(),
359 array(
'data_type' => get_class($utmField))
362 $entity->addField($aliasField);
378 protected static function createUtsEntity(ORM\Entity $srcEntity, array $utsFields, array $utmFields, $ufId =
null)
380 global $USER_FIELD_MANAGER;
384 $utsClassFull = static::getUtsEntityClassNameBySrcEntity($srcEntity);
385 $utsClassPath = explode(
'\\', ltrim($utsClassFull,
'\\'));
387 $utsNamespace = join(
'\\', array_slice($utsClassPath, 0, -1));
388 $utsClass = end($utsClassPath);
391 $utsTable = static::getUtsEntityTableNameBySrcEntity($srcEntity, $ufId);
396 'data_type' =>
'integer',
400 'data_type' => $srcEntity->getDataClass(),
401 'reference' => array(
402 '=this.VALUE_ID' =>
'ref.ID'
408 if (class_exists($utsNamespace.
"\\".$utsClass))
410 ORM\Entity::destroy($utsNamespace.
"\\".$utsClass);
411 $entity = ORM\Entity::getInstance($utsNamespace.
"\\".$utsClass);
413 foreach ($fieldsMap as $fieldName => $field)
415 $entity->addField($field, $fieldName);
420 $entity = ORM\Entity::compileEntity($utsClass, $fieldsMap, array(
421 'namespace' => $utsNamespace,
'table_name' => $utsTable
425 foreach ($utsFields as $utsField)
427 $field = $USER_FIELD_MANAGER->getEntityField($utsField);
430 foreach ($USER_FIELD_MANAGER->getEntityReferences($utsField, $field) as $reference)
436 foreach ($utmFields as $utmFieldMeta)
439 $utmField = $USER_FIELD_MANAGER->getEntityField($utmFieldMeta);
442 $cacheField = (
new ORM\Fields\UserTypeUtsMultipleField($utmField->getName()))
443 ->configureUtmField($utmField);
445 static::setMultipleFieldSerialization($cacheField, $utmField);
446 $entity->addField($cacheField);
460 global $USER_FIELD_MANAGER;
462 if (!($fieldAsType instanceof ORM\Fields\
Field))
464 $fieldAsType = $USER_FIELD_MANAGER->getEntityField($fieldAsType);
469 if ($entityField instanceof ORM\Fields\
ArrayField)
471 $entityField->configureSerializeCallback([__CLASS__,
'serializeMultipleDatetime']);
472 $entityField->configureUnserializeCallback([__CLASS__,
'unserializeMultipleDatetime']);
476 $entityField->addSaveDataModifier([__CLASS__,
'serializeMultipleDatetime']);
477 $entityField->addFetchDataModifier([__CLASS__,
'unserializeMultipleDatetime']);
480 elseif ($fieldAsType instanceof ORM\Fields\
DateField)
482 if ($entityField instanceof ORM\Fields\
ArrayField)
484 $entityField->configureSerializeCallback([__CLASS__,
'serializeMultipleDate']);
485 $entityField->configureUnserializeCallback([__CLASS__,
'unserializeMultipleDate']);
489 $entityField->addSaveDataModifier([__CLASS__,
'serializeMultipleDate']);
490 $entityField->addFetchDataModifier([__CLASS__,
'unserializeMultipleDate']);
495 if ($entityField instanceof ORM\Fields\
ArrayField)
497 $entityField->configureSerializationPhp();
501 $entityField->setSerialized();
510 foreach ($referenceDesc as $k => $v)
514 $new[$k] = static::rewriteUtsReference($v);
518 $k = str_replace(
'this.',
'this.UTS_OBJECT.', $k);
528 return $srcEntity->getFullName().
'UtsTable';
533 return 'b_uts_'.mb_strtolower($ufId ?: $srcEntity->getUfId());
545 protected static function createUtmEntity(ORM\
Entity $srcEntity, array $utmFields, $ufId =
null)
547 global $USER_FIELD_MANAGER;
550 $utmClassFull = static::getUtmEntityClassNameBySrcEntity($srcEntity);
551 $utmClassPath = explode(
'\\', ltrim($utmClassFull,
'\\'));
553 $utmNamespace = join(
'\\', array_slice($utmClassPath, 0, -1));
554 $utmClass = end($utmClassPath);
557 $utmTable = static::getUtmEntityTableNameBySrcEntity($srcEntity, $ufId);
562 'data_type' =>
'integer',
564 'autocomplete' =>
true
567 'data_type' =>
'integer',
571 'data_type' => $srcEntity->getDataClass(),
572 'reference' => array(
573 '=this.VALUE_ID' =>
'ref.ID'
577 'data_type' =>
'integer'
582 'data_type' =>
'text'
584 'VALUE_INT' => array(
585 'data_type' =>
'integer'
587 'VALUE_DOUBLE' => array(
588 'data_type' =>
'float'
590 'VALUE_DATE' => array(
591 'data_type' =>
'datetime'
596 if (class_exists($utmNamespace.
"\\".$utmClass))
598 ORM\Entity::destroy($utmNamespace.
"\\".$utmClass);
599 $entity = ORM\Entity::getInstance($utmNamespace.
"\\".$utmClass);
601 foreach ($fieldsMap as $fieldName => $field)
603 $entity->addField($field, $fieldName);
608 $entity = ORM\Entity::compileEntity($utmClass, $fieldsMap, array(
609 'namespace' => $utmNamespace,
'table_name' => $utmTable
614 foreach ($utmFields as $utmField)
616 $field = $USER_FIELD_MANAGER->getEntityField($utmField);
618 if ($field instanceof ORM\Fields\IntegerField)
620 $columnName =
'VALUE_INT';
622 elseif ($field instanceof ORM\Fields\FloatField)
624 $columnName =
'VALUE_DOUBLE';
626 elseif ($field instanceof ORM\Fields\DateField)
628 $columnName =
'VALUE_DATE';
632 $columnName =
'VALUE';
635 $field->setColumnName($columnName);
639 foreach ($USER_FIELD_MANAGER->getEntityReferences($utmField, $field) as $reference)
645 $refField =
new ORM\Fields\Relations\Reference(
646 'PARENT_'.$utmField[
'FIELD_NAME'],
647 $srcEntity->getDataClass(),
648 array(
'=this.VALUE_ID' =>
'ref.ID',
'=this.FIELD_ID' => array(
'?i', $utmField[
'ID']))
659 return $srcEntity->getFullName().
'UtmTable';
664 return 'b_utm_'.mb_strtolower($ufId ?: $srcEntity->getUfId());
672 public static function serializeMultipleDatetime($value)
674 if (is_array($value) || $value instanceof \Traversable)
678 foreach ($value as $k => $singleValue)
681 $tmpValue[$k] = $singleValue->format(static::MULTIPLE_DATETIME_FORMAT);
684 return serialize($tmpValue);
700 $value = unserialize($value, [
"allowed_classes" =>
false]);
702 foreach($value as &$singleValue)
707 $singleValue =
new Type\DateTime($singleValue, static::MULTIPLE_DATETIME_FORMAT);
725 public static function serializeMultipleDate($value)
727 if (is_array($value) || $value instanceof \Traversable)
731 foreach ($value as $k => $singleValue)
734 $tmpValue[$k] = $singleValue->format(static::MULTIPLE_DATE_FORMAT);
737 return serialize($tmpValue);
753 $value = unserialize($value, [
"allowed_classes" =>
false]);
755 foreach($value as &$singleValue)
760 $singleValue =
new Type\Date($singleValue, static::MULTIPLE_DATE_FORMAT);
765 $singleValue =
new Type\Date($singleValue);