public static function onBeforeSave($userField, $value, $userId = false) { $userFieldManager = Driver::getInstance()->getUserFieldManager(); list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType($userField['ENTITY_ID']); list($type, $realValue) = static::detectType($value); if (empty($value)) { $alreadyExistsValues = $userField['VALUE']; if (!is_array($alreadyExistsValues)) { $alreadyExistsValues = array($userField['VALUE']); } AttachedObject::detachByFilter(array('ID' => $alreadyExistsValues)); return $value; } if ($type == self::TYPE_NEW_OBJECT) { $errorCollection = new ErrorCollection(); $version = static::getVersionById($realValue); if (!$version) { return ''; } $file = $version->getObject(); if ($userId === false) { $securityContext = $file->getStorage()->getCurrentUserSecurityContext(); } else { $securityContext = $file->getStorage()->getSecurityContext($userId); } $canUpdate = $allowEdit = false; //todo this is great hack for disk_version and sync IS_EDITABLE, ALLOW_EDIT by parent AttachedObject $hackData = AttachedObject::getStoredDataByObjectId($file->getId()); if ($hackData !== null) { if (isset($hackData['IS_EDITABLE'])) { $canUpdate = $hackData['IS_EDITABLE']; } if (isset($hackData['ALLOW_EDIT'])) { $allowEdit = $hackData['ALLOW_EDIT']; } } $canUpdate = $canUpdate || $file->canUpdate($securityContext); $allowEdit = $allowEdit || $canUpdate && (int) Application::getInstance()->getContext()->getRequest()->getPost($userFieldManager->getInputNameForAllowEditByEntityType($userField['ENTITY_ID'])); $attachedModel = AttachedObject::add(array('MODULE_ID' => $moduleId, 'OBJECT_ID' => $file->getId(), 'VERSION_ID' => $version->getId(), 'ENTITY_ID' => $userField['VALUE_ID'], 'ENTITY_TYPE' => $connectorClass, 'IS_EDITABLE' => (int) $canUpdate, 'ALLOW_EDIT' => (int) $allowEdit, 'CREATED_BY' => $userId === false ? self::getActivityUserId() : $userId), $errorCollection); if (!$attachedModel || $errorCollection->hasErrors()) { $errorCollection->add(array(new Error(Loc::getMessage('DISK_VERSION_USER_TYPE_ERROR_COULD_NOT_FIND_ATTACHED_OBJECT'), self::ERROR_COULD_NOT_FIND_ATTACHED_OBJECT))); return ''; } return $attachedModel->getId(); } else { return $realValue; } }