- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/lib/entityrequisite.php
- Класс: Bitrix\Crm\EntityRequisite
- Вызов: EntityRequisite::checkBeforeUpdate
public function checkBeforeUpdate($id, $fields)
{
unset($fields['ID'], $fields['DATE_CREATE'], $fields['CREATED_BY_ID']);
$fields['DATE_MODIFY'] = new \Bitrix\Main\Type\DateTime();
$fields['MODIFY_BY_ID'] = \CCrmSecurityHelper::GetCurrentUserID();
$result = $this->checkRqFieldsBeforeSave($id, $fields);
if (!$result->isSuccess())
{
return $result;
}
$this->separateAddressFields($fields);
// rewrite some fields
$entity = RequisiteTable::getEntity();
foreach ($entity->getFields() as $field)
{
$fieldName = $field->getName();
if ($field instanceof Entity\BooleanField && array_key_exists($fieldName, $fields))
{
if ($fields[$fieldName] !== 'Y' && $fields[$fieldName] !== 'N'
&& $fields[$fieldName] !== true && $fields[$fieldName] !== false)
{
$fields[$fieldName] = $field->getDefaultValue();
}
}
}
global $USER_FIELD_MANAGER, $APPLICATION;
$result = new Entity\UpdateResult();
$entity = RequisiteTable::getEntity();
$entity_primary = $entity->getPrimaryArray();
// normalize primary
if ($id === null)
{
$id = array();
// extract primary from data array
foreach ($entity_primary as $key)
{
/** @var Entity\ScalarField $field */
$field = $entity->getField($key);
if ($field->isAutocomplete())
{
continue;
}
if (!isset($fields[$key]))
{
throw new Main\ArgumentException(sprintf(
'Primary `%s` was not found when trying to query %s row.', $key, $entity->getName()
));
}
$id[$key] = $fields[$key];
}
}
elseif (is_scalar($id))
{
if (count($entity_primary) > 1)
{
throw new Main\ArgumentException(sprintf(
'Require multi primary {`%s`}, but one scalar value "%s" found when trying to query %s row.',
join('`, `', $entity_primary), $id, $entity->getName()
));
}
$id = array($entity_primary[0] => $id);
}
// validate primary
if (is_array($id))
{
if(empty($id))
{
throw new Main\ArgumentException(sprintf(
'Empty primary found when trying to query %s row.', $entity->getName()
));
}
foreach (array_keys($id) as $key)
{
if (!in_array($key, $entity_primary, true))
{
throw new Main\ArgumentException(sprintf(
'Unknown primary `%s` found when trying to query %s row.',
$key, $entity->getName()
));
}
}
}
else
{
throw new Main\ArgumentException(sprintf(
'Unknown type of primary "%s" found when trying to query %s row.', gettype($id), $entity->getName()
));
}
foreach ($id as $key => $value)
{
if (!is_scalar($value) && !($value instanceof Main\Type\Date))
{
throw new Main\ArgumentException(sprintf(
'Unknown value type "%s" for primary "%s" found when trying to query %s row.',
gettype($value), $key, $entity->getName()
));
}
}
try
{
// uf values
$ufdata = array();
// separate userfields
if ($entity->getUfId())
{
// collect uf data
$userfields = $USER_FIELD_MANAGER->GetUserFields($entity->getUfId());
foreach ($userfields as $userfield)
{
if (array_key_exists($userfield['FIELD_NAME'], $fields))
{
// copy value
$ufdata[$userfield['FIELD_NAME']] = $fields[$userfield['FIELD_NAME']];
// remove original
unset($fields[$userfield['FIELD_NAME']]);
}
}
}
// check data
RequisiteTable::checkFields($result, $id, $fields);
// check uf data
if (!empty($ufdata))
{
if (!$USER_FIELD_MANAGER->CheckFields($entity->getUfId(), end($id), $ufdata))
{
if (is_object($APPLICATION) && $APPLICATION->getException())
{
$e = $APPLICATION->getException();
$result->addError(new Entity\EntityError($e->getString()));
$APPLICATION->resetException();
}
else
{
$result->addError(new Entity\EntityError("Unknown error while checking userfields"));
}
}
}
// check if there is still some data
if (!count($fields + $ufdata))
{
$result->addError(new Entity\EntityError("There is no data to update."));
}
// return if any error
if (!$result->isSuccess(true))
{
return $result;
}
}
catch (\Exception $e)
{
// check result to avoid warning
$result->isSuccess();
throw $e;
}
return $result;
}