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; } }
protected function migrateWebdavElementHistory() { $this->abortIfNeeded(); if ($this->isStepFinished(__METHOD__)) { return; } $userFieldManager = Driver::getInstance()->getUserFieldManager(); $connection = $this->connection; $rsData = CUserTypeEntity::GetList(array(), array("ENTITY_ID" => "BLOG_COMMENT", "XML_ID" => "UF_BLOG_COMMENT_FH")); if (!$rsData->fetch()) { $this->setStepFinished(__METHOD__); $this->log(array('Could not find UF_BLOG_COMMENT_FH in entity BLOG_COMMENT')); return; } unset($rsData); if (!$connection->isTableExists('b_uts_blog_comment') || !$connection->getTableField('b_uts_blog_comment', 'UF_BLOG_COMMENT_FH')) { $this->setStepFinished(__METHOD__); $this->log(array('b_uts_blog_comment does not exist or column UF_BLOG_COMMENT_FH does not exist.')); return; } unset($rsData); list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType('BLOG_COMMENT'); $lastId = $this->getStorageId(); $result = $connection->query("SELECT * from b_uts_blog_comment WHERE VALUE_ID > {$lastId} AND UF_BLOG_COMMENT_FH <> '' ORDER BY VALUE_ID ASC"); while ($userFieldRow = $result->fetch()) { $this->abortIfNeeded(); $dataFromValue = CUserTypeWebdavElementHistory::getDataFromValue($userFieldRow['UF_BLOG_COMMENT_FH']); if (!$dataFromValue) { $this->storeStorageId($userFieldRow['VALUE_ID']); continue; } $dataFromValue = $dataFromValue[0]; //this is head version if (empty($dataFromValue['v'])) { list($newVersionId, $objectId) = $this->getLastVersionByElementId($dataFromValue['id']); if (!$newVersionId || !$objectId) { $this->storeStorageId($userFieldRow['VALUE_ID']); $this->log(array('Could not find migrate Head Version from disk. Skip.', $userFieldRow)); continue; } $errorCollection = new ErrorCollection(); $attachedData = array('MODULE_ID' => $moduleId, 'OBJECT_ID' => $objectId, 'VERSION_ID' => $newVersionId, 'ENTITY_ID' => $userFieldRow['VALUE_ID'], 'ENTITY_TYPE' => $connectorClass, 'IS_EDITABLE' => 2); $attachedModel = AttachedObject::add($attachedData, $errorCollection); if (!$attachedModel || $errorCollection->hasErrors()) { $this->storeStorageId($userFieldRow['VALUE_ID']); $this->log(array('Could not created attached object')); continue; } $connection->queryExecute("UPDATE b_uts_blog_comment SET UF_BLOG_COMMENT_FH = '{$attachedModel->getId()}' WHERE VALUE_ID = {$userFieldRow['VALUE_ID']}"); $this->storeStorageId($userFieldRow['VALUE_ID']); continue; } else { list($newVersionId, $objectId) = $this->getLastVersionByBpId($dataFromValue['v']); if (!$newVersionId || !$objectId) { $this->storeStorageId($userFieldRow['VALUE_ID']); $this->log(array('Could not find migrate Version from disk. Skip.', $userFieldRow)); continue; } $errorCollection = new ErrorCollection(); $attachedData = array('MODULE_ID' => $moduleId, 'OBJECT_ID' => $objectId, 'VERSION_ID' => $newVersionId, 'ENTITY_ID' => $userFieldRow['VALUE_ID'], 'ENTITY_TYPE' => $connectorClass, 'IS_EDITABLE' => 2); $attachedModel = AttachedObject::add($attachedData, $errorCollection); if (!$attachedModel || $errorCollection->hasErrors()) { $this->storeStorageId($userFieldRow['VALUE_ID']); $this->log(array('Could not created attached object', $userFieldRow)); continue; } $connection->queryExecute("UPDATE b_uts_blog_comment SET UF_BLOG_COMMENT_FH = '{$attachedModel->getId()}' WHERE VALUE_ID = {$userFieldRow['VALUE_ID']}"); $this->storeStorageId($userFieldRow['VALUE_ID']); continue; } } $connection->queryExecute("UPDATE b_user_field SET USER_TYPE_ID = 'disk_version' WHERE ENTITY_ID = 'BLOG_COMMENT' AND XML_ID = 'UF_BLOG_COMMENT_FH'"); $this->storeStorageId(0); $this->setStepFinished(__METHOD__); return; }
/** * @param $iblockId * @param string $fileId disk file with the prefix 'n' * @return int|null */ public static function attachFilesWorkflow($iblockId, $fileId) { if (!(int) $iblockId) { return null; } $userFieldManager = Driver::getInstance()->getUserFieldManager(); list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType("iblock_workflow"); list($type, $realId) = FileUserType::detectType($fileId); if ($type != FileUserType::TYPE_NEW_OBJECT) { return null; } $errorCollection = new ErrorCollection(); $fileModel = File::loadById($realId, array('STORAGE')); if (!$fileModel) { return null; } $attachedModel = AttachedObject::load(array('OBJECT_ID' => $fileModel->getId(), '=ENTITY_TYPE' => $connectorClass, '=ENTITY_ID' => $iblockId, '=MODULE_ID' => $moduleId)); if ($attachedModel) { return $fileModel->getId(); } global $USER; if ($USER instanceof \CUser && $USER->getId()) { $userId = $USER->getId(); } else { $userId = SystemUser::SYSTEM_USER_ID; } $securityContext = $fileModel->getStorage()->getSecurityContext($userId); if (!$fileModel->canRead($securityContext)) { return null; } $canUpdate = $fileModel->canUpdate($securityContext); $attachedModel = AttachedObject::add(array('MODULE_ID' => $moduleId, 'OBJECT_ID' => $fileModel->getId(), 'ENTITY_ID' => $iblockId, 'ENTITY_TYPE' => $connectorClass, 'IS_EDITABLE' => (int) $canUpdate, 'ALLOW_EDIT' => (int) ($canUpdate && (int) Application::getInstance()->getContext()->getRequest()->getPost('DISK_FILE_' . $iblockId . '_DISK_ATTACHED_OBJECT_ALLOW_EDIT')), 'CREATED_BY' => $userId), $errorCollection); if (!$attachedModel || $errorCollection->hasErrors()) { return null; } return $fileModel->getId(); }
public static function extractValue(FieldType $fieldType, array $field, array $request) { if (!Loader::includeModule('disk')) { return null; } $value = parent::extractValue($fieldType, $field, $request); if (is_array($value) && isset($value['VALUE'])) { $value = $value['VALUE']; } if (!$value) { return null; } // Attach file disk $userFieldManager = \Bitrix\Disk\Driver::getInstance()->getUserFieldManager(); list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType('lists_workflow'); list($type, $realId) = \Bitrix\Disk\Uf\FileUserType::detectType($value); if ($type != \Bitrix\Disk\Uf\FileUserType::TYPE_NEW_OBJECT) { return null; } $errorCollection = new \Bitrix\Disk\Internals\Error\ErrorCollection(); $fileModel = \Bitrix\Disk\File::loadById($realId, array('STORAGE')); if (!$fileModel) { return null; } $documentType = $fieldType->getDocumentType(); $iblockId = intval(substr($documentType[2], strlen("iblock_"))); $attachedModel = \Bitrix\Disk\AttachedObject::load(array('OBJECT_ID' => $fileModel->getId(), '=ENTITY_TYPE' => $connectorClass, '=ENTITY_ID' => $iblockId, '=MODULE_ID' => $moduleId)); if ($attachedModel) { return $fileModel->getId(); } $securityContext = $fileModel->getStorage()->getCurrentUserSecurityContext(); if (!$fileModel->canRead($securityContext)) { return null; } $canUpdate = $fileModel->canUpdate($securityContext); global $USER; $attachedModel = \Bitrix\Disk\AttachedObject::add(array('MODULE_ID' => $moduleId, 'OBJECT_ID' => $fileModel->getId(), 'ENTITY_ID' => $iblockId, 'ENTITY_TYPE' => $connectorClass, 'IS_EDITABLE' => (int) $canUpdate, 'ALLOW_EDIT' => (int) ($canUpdate && (int) \Bitrix\Main\Application::getInstance()->getContext()->getRequest()->getPost('DISK_FILE_' . $iblockId . '_DISK_ATTACHED_OBJECT_ALLOW_EDIT')), 'CREATED_BY' => $USER->getId()), $errorCollection); if (!$attachedModel || $errorCollection->hasErrors()) { return null; } return $fileModel->getId(); }