26 static::$isCheckUserFields =
false;
34 public static function getMap(): array
39 ->configureAutocomplete(),
45 if(!static::$temporaryStorage)
50 return static::$temporaryStorage;
56 foreach ($data as $k => $v)
58 if (!(static::getEntity()->hasField($k) && static::getEntity()->getField($k) instanceof
ScalarField))
61 'Field `%s` not found in entity when trying to query %s row.',
62 $k, static::getEntity()->getName()
67 parent::checkFields($result, $primary, $data);
79 if(is_object($application) && $application->getException())
81 $e = $application->getException();
83 $application->resetException();
102 $isUpdate = (isset($options[
'isUpdate']) && $options[
'isUpdate'] ===
true);
105 if (!$userFieldManager)
107 static::$isCheckUserFields =
true;
113 $oldData = static::getByPrimary($id)->fetch();
114 static::getTemporaryStorage()->saveData($id, $oldData);
116 static::$isCheckUserFields
117 && !$userFieldManager->checkFieldsWithOldData(
118 static::getItemUserFieldEntityId(),
124 $result->addError(static::getErrorFromException());
127 $fields = $userFieldManager->getUserFieldsWithReadyData(
128 static::getItemUserFieldEntityId(),
137 $fields = $userFieldManager->getUserFields(static::getItemUserFieldEntityId());
140 static::$isCheckUserFields
141 && !$userFieldManager->checkFields(
142 static::getItemUserFieldEntityId(),
150 $result->addError(static::getErrorFromException());
154 if(!$result->getErrors())
156 $data = static::convertValuesBeforeSave($data, $fields);
157 $result->modifyFields($data);
160 static::$isCheckUserFields =
true;
173 protected static function saveMultipleValues($id, array $data, array $options = []): ORM\
EventResult
175 $id = static::getTemporaryStorage()->getIdByPrimary($id);
178 $isUpdate = (isset($options[
'isUpdate']) && $options[
'isUpdate']);
179 $type = static::getType();
181 $connection = Main\Application::getConnection();
184 $typeDataClass = $factory->getTypeDataClass();
186 $fields = $userFieldManager->getUserFields(static::getItemUserFieldEntityId());
187 $oldData = static::getTemporaryStorage()->getData($id);
190 foreach($fields as $fieldName => $field)
192 if(is_array($data[$fieldName]) && $field[
'MULTIPLE'] ===
'Y')
194 $multiValues[$fieldName] = array_filter($data[$fieldName], array(
'static',
'isNotNull'));
196 elseif($field[
'USER_TYPE'][
'BASE_TYPE'] ===
'file')
198 if(is_numeric($oldData[$fieldName]) && array_key_exists($fieldName, $data) && (
int) $oldData[$fieldName] !== (
int) $data[$fieldName])
200 \CFile::Delete($oldData[$fieldName]);
206 foreach ($multiValues as $fieldName => $values)
208 $utmTableName = $typeDataClass::getMultipleValueTableName($type, $fields[$fieldName]);
213 if($fields[$fieldName][
'USER_TYPE'][
'BASE_TYPE'] ===
'file')
215 foreach($oldData[$fieldName] as $fileId)
217 if(is_numeric($fileId) && !in_array($fileId, $values))
219 \CFile::Delete($fileId);
224 $helper = $connection->getSqlHelper();
226 $connection->query(sprintf(
227 'DELETE FROM %s WHERE %s = %d',
228 $helper->quote($utmTableName), $helper->quote(
'ID'), $id
232 foreach ($values as $value)
234 $connection->add($utmTableName, array(
'ID' => $id,
'VALUE' => $value));
267 if ($oldData = static::getByPrimary($event->
getParameter(
'id'))->fetch())
269 static::getTemporaryStorage()->saveData($event->
getParameter(
'id'), $oldData);
278 $oldData = static::getTemporaryStorage()->getData($event->
getParameter(
'id'));
279 $id = static::getTemporaryStorage()->getIdByPrimary($event->
getParameter(
'id'));
281 $type = static::getType();
282 $connection = Main\Application::getConnection();
283 $helper = $connection->getSqlHelper();
286 $typeDataClass = $factory->getTypeDataClass();
288 $fields = $userFieldManager->getUserFields(static::getItemUserFieldEntityId());
289 foreach ($oldData as $k => $v)
291 $userfield = $fields[$k];
294 if ($userfield[
'MULTIPLE'] ==
'Y')
296 $utmTableName = $typeDataClass::getMultipleValueTableName($type, $userfield);
300 $connection->query(sprintf(
301 'DELETE FROM %s WHERE %s = %d',
302 $helper->quote($utmTableName), $helper->quote(
'ID'), $id
305 catch(Main\DB\SqlQueryException $e)
307 $result->addError(
new ORM\
EntityError($e->getMessage()));
312 if ($userfield[
"USER_TYPE"][
"BASE_TYPE"]==
"file")
314 if(is_array($oldData[$k]))
316 foreach($oldData[$k] as $value)
318 \CFile::delete($value);
323 \CFile::delete($oldData[$k]);
340 foreach ($data as $k => $v)
342 if (static::isOwnField($k))
347 $userField = $userFields[$k];
349 if ($userField[
'MULTIPLE'] ==
'N')
360 foreach ($inputValue as $singleValue)
362 $tmpValue[] = static::convertSingleValueBeforeSave($singleValue, $userField);
366 if ($userField[
'MULTIPLE'] ==
'N')
368 $data[$k] = $tmpValue[0];
373 $tmpValue = array_filter($tmpValue, [
'static',
'isNotNull']);
375 $data[$k] = $tmpValue;
376 $multiValues[$k] = $tmpValue;
391 if (!isset($userField[
'USER_TYPE']) || !is_array($userField[
'USER_TYPE']))
393 $userField[
'USER_TYPE'] = array();
396 isset($userField[
'USER_TYPE'][
'BASE_TYPE'])
397 && $userField[
'USER_TYPE'][
'BASE_TYPE'] ===
'datetime'
400 $useTimezone = isset($userField[
'SETTINGS'][
'USE_TIMEZONE']) && $userField[
'SETTINGS'][
'USE_TIMEZONE'] ===
'Y';
410 $value = Main\Type\DateTime::createFromUserTime($value);
421 $value->disableUserTime();
427 isset($userField[
'USER_TYPE'][
'CLASS_NAME']) &&
428 is_callable(array($userField[
'USER_TYPE'][
'CLASS_NAME'],
'onbeforesave'))
431 $value = call_user_func_array(
432 array($userField[
'USER_TYPE'][
'CLASS_NAME'],
'onbeforesave'), array($userField, $value)
436 if (static::isNotNull($value))
441 isset($userField[
'USER_TYPE'][
'BASE_TYPE']) &&
443 $userField[
'USER_TYPE'][
'BASE_TYPE'] ==
'int' ||
444 $userField[
'USER_TYPE'][
'BASE_TYPE'] ==
'double'
458 return !($value ===
null || $value ===
false || $value ===
'');
463 return array_key_exists($fieldName, static::getOwnFieldNames());
478 foreach(static::getMap() as $field)
480 $fields[$field->getName()] = $field->getName();
489 $data = static::getList([
490 'select' => array_keys($userFields),
506 return static::update($id, $fields);
513 foreach($userFields as $userField)
515 $fields[$userField[
'FIELD_NAME']] =
null;
518 return static::update($id, $fields);
static onAfterDelete(Event $event)
static getFormat(Context\Culture $culture=null)
static isCorrect($time, $format=null)
static modifyValuesBeforeSave($id, array $data, array $options=[])
static isOwnField(string $fieldName)
static checkFields(ORM\Data\Result $result, $primary, array $data)
static getUserFieldValues(int $id, array $userFields)
static updateUserFieldValues(int $id, array $fields)
static convertValuesBeforeSave(array $data, array $userFields)
static convertSingleValueBeforeSave($value, array $userField)
static onBeforeDelete(Event $event)
static onBeforeUpdate(Event $event)