private function loadFilesData() { if (empty($this->arParams['PARAMS']['arUserField'])) { return array(); } $userId = $this->getUser()->getId(); $values = $this->arParams['PARAMS']['arUserField']['VALUE']; if (!is_array($this->arParams['PARAMS']['arUserField']['VALUE'])) { $values = array($values); } $files = array(); $driver = \Bitrix\Disk\Driver::getInstance(); $urlManager = $driver->getUrlManager(); $userFieldManager = $driver->getUserFieldManager(); $userFieldManager->loadBatchAttachedObject($values); foreach ($values as $id) { $attachedModel = null; list($type, $realValue) = FileUserType::detectType($id); if ($realValue <= 0) { continue; } elseif ($type == FileUserType::TYPE_NEW_OBJECT) { /** @var File $fileModel */ $fileModel = File::loadById($realValue); if (!$fileModel || !$fileModel->canRead($fileModel->getStorage()->getCurrentUserSecurityContext())) { continue; } } else { /** @var \Bitrix\Disk\AttachedObject $attachedModel */ $attachedModel = $userFieldManager->getAttachedObjectById($realValue); if (!$attachedModel) { continue; } if (!$this->editMode) { $attachedModel->setOperableEntity(array('ENTITY_ID' => $this->arParams['PARAMS']['arUserField']['ENTITY_ID'], 'ENTITY_VALUE_ID' => $this->arParams['PARAMS']['arUserField']['ENTITY_VALUE_ID'])); } /** @var File $fileModel */ $fileModel = $attachedModel->getFile(); } $name = $fileModel->getName(); $data = array('ID' => $id, 'NAME' => $name, 'CONVERT_EXTENSION' => DocumentHandler::isNeedConvertExtension($fileModel->getExtension()), 'EDITABLE' => DocumentHandler::isEditable($fileModel->getExtension()), 'CAN_UPDATE' => $attachedModel ? $attachedModel->canUpdate($userId) : $fileModel->canUpdate($fileModel->getStorage()->getCurrentUserSecurityContext()), 'FROM_EXTERNAL_SYSTEM' => $fileModel->getContentProvider() && $fileModel->getCreatedBy() == $userId, 'EXTENSION' => $fileModel->getExtension(), 'SIZE' => \CFile::formatSize($fileModel->getSize()), 'XML_ID' => $fileModel->getXmlId(), 'FILE_ID' => $fileModel->getId(), 'VIEW_URL' => $urlManager->getUrlToShowAttachedFileByService($id, 'gvdrive'), 'EDIT_URL' => $urlManager->getUrlToStartEditUfFileByService($id, 'gdrive'), 'DOWNLOAD_URL' => $urlManager->getUrlUfController('download', array('attachedId' => $id)), 'COPY_TO_ME_URL' => $urlManager->getUrlUfController('copyToMe', array('attachedId' => $id)), 'DELETE_URL' => ""); if (\Bitrix\Disk\TypeFile::isImage($fileModel)) { $this->arParams['PARAMS']['THUMB_SIZE'] = $this->arParams['PARAMS']['THUMB_SIZE'] > 0 ? $this->arParams['PARAMS']['THUMB_SIZE'] : 100; $data["PREVIEW_URL"] = $attachedModel === null ? $urlManager->getUrlForShowFile($fileModel) : $urlManager->getUrlUfController('show', array('attachedId' => $id)); $data["IMAGE"] = $fileModel->getFile(); } if ($this->editMode) { $data['STORAGE'] = $fileModel->getStorage()->getProxyType()->getTitleForCurrentUser() . ' / ' . $fileModel->getParent()->getName(); } else { $data['ATTRIBUTES_FOR_VIEWER'] = Ui\Viewer::getAttributesByAttachedObject($attachedModel, array('canUpdate' => $data['CAN_UPDATE'], 'canFakeUpdate' => true, 'showStorage' => false, 'externalId' => false, 'relativePath' => false)); } $files[] = $data; } unset($id); return $files; }
protected function processActionRenameFile() { $this->checkRequiredPostParams(array('newName', 'attachedId')); if ($this->errorCollection->hasErrors()) { $this->sendJsonErrorResponse(); } list($type, $realValue) = FileUserType::detectType($this->request->getPost('attachedId')); if ($type == FileUserType::TYPE_NEW_OBJECT) { /** @var File $model */ $model = File::loadById((int) $realValue, array('STORAGE')); if (!$model) { $this->errorCollection->add(array(new Error("Could not find file"))); $this->sendJsonErrorResponse(); } if (!$model->canRename($model->getStorage()->getCurrentUserSecurityContext())) { $this->errorCollection->add(array(new Error("Bad permission. Could not read this file"))); $this->sendJsonErrorResponse(); } $newName = Text::correctFilename($this->request->getPost('newName')); if (!$model->renameInternal($newName, true)) { $this->errorCollection->add($model->getErrors()); $this->sendJsonErrorResponse(); } Driver::getInstance()->getIndexManager()->indexFile($model); $this->sendJsonSuccessResponse(array('id' => $this->request->getPost('attachedId'), 'name' => $model->getName())); } else { $this->sendJsonErrorResponse(); } }
/** * @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(); }
/** * Clones uf values from entity and creates new files (copies from attach) to save in new entity. * @param array $attachedIds List of attached objects id. * @param int $userId Id of user. * @internal * @return array */ public function cloneUfValuesFromAttachedObject(array $attachedIds, $userId) { $this->errorCollection->clear(); $userId = (int) $userId; if ($userId <= 0) { $this->errorCollection->addOne(new Error('Invalid $userId')); return null; } $userStorage = Driver::getInstance()->getStorageByUserId($userId); if (!$userStorage) { $this->errorCollection->addOne(new Error("Could not find storage for user {$userId}")); $this->errorCollection->add(Driver::getInstance()->getErrors()); return null; } $folder = $userStorage->getFolderForUploadedFiles(); if (!$folder) { $this->errorCollection->addOne(new Error("Could not create/find folder for upload")); $this->errorCollection->add($userStorage->getErrors()); return null; } $newValues = array(); foreach ($attachedIds as $id) { list($type, $realValue) = FileUserType::detectType($id); if (FileUserType::TYPE_ALREADY_ATTACHED != $type) { continue; } $attachedObject = AttachedObject::loadById($realValue, array('OBJECT')); if (!$attachedObject) { continue; } if (!$attachedObject->canRead($userId)) { continue; } $file = $attachedObject->getFile(); if (!$file) { continue; } $newFile = $file->copyTo($folder, $userId, true); if (!$newFile) { $this->errorCollection->add($file->getErrors()); continue; } $newValues[] = FileUserType::NEW_FILE_PREFIX . $newFile->getId(); } return $newValues; }
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(); }
public static function clearValueSingle(FieldType $fieldType, $value) { if (!Loader::includeModule('disk')) { return; } $value = (int) $value; if (!$value) { return; } $documentType = $fieldType->getDocumentType(); $iblockId = intval(substr($documentType[2], strlen("iblock_"))); if (!$iblockId) { return; } $userFieldManager = \Bitrix\Disk\Driver::getInstance()->getUserFieldManager(); list($type, $realId) = \Bitrix\Disk\Uf\FileUserType::detectType($value); if ($type == \Bitrix\Disk\Uf\FileUserType::TYPE_ALREADY_ATTACHED) { $attachedModel = \Bitrix\Disk\AttachedObject::loadById($realId); if (!$attachedModel) { return; } if ($userFieldManager->belongsToEntity($attachedModel, "iblock_workflow", $iblockId)) { \Bitrix\Disk\AttachedObject::detachByFilter(array('ID' => $realId)); } } }