/** * Moves a folder. * * @param AssetFolderModel $folder The assetFolderModel representing the existing folder. * @param AssetFolderModel $newParentFolder The assetFolderModel representing the new parent folder. * @param bool $overwriteTarget If true, will overwrite folder, if needed. * * @return AssetOperationResponseModel */ public function moveFolder(AssetFolderModel $folder, AssetFolderModel $newParentFolder, $overwriteTarget = false) { $response = new AssetOperationResponseModel(); if ($folder->id == $newParentFolder->id) { return $response->setSuccess(); } $removeFromTree = ''; if ($this->folderExists($newParentFolder, $folder->name)) { if ($overwriteTarget) { $existingFolder = craft()->assets->findFolder(array('parentId' => $newParentFolder->id, 'name' => $folder->name)); if ($existingFolder) { $removeFromTree = $existingFolder->id; $this->deleteFolder($existingFolder); } else { $this->deleteSourceFolder($newParentFolder, $folder->name); } } else { return $response->setPrompt($this->getUserFolderPromptOptions($folder->name, $folder->id))->setDataItem('folderId', $folder->id); } } $response->setSuccess()->setDataItem('deleteList', array($folder->id))->setDataItem('removeFromTree', $removeFromTree); $mirroringData = array('changedFolderIds' => array()); $this->_mirrorStructure($newParentFolder, $folder, $mirroringData); $response->setDataItem('changedFolderIds', $mirroringData['changedFolderIds']); $criteria = craft()->elements->getCriteria(ElementType::Asset); $criteria->folderId = array_keys(craft()->assets->getAllDescendantFolders($folder)); $files = $criteria->find(); $transferList = array(); foreach ($files as $file) { $transferList[] = array('fileId' => $file->id, 'folderId' => $mirroringData['changedFolderIds'][$file->folderId]['newId'], 'fileName' => $file->filename); } return $response->setDataItem('transferList', $transferList); }
/** * Merge a conflicting uploaded file. * * @param string $conflictResolution User response to conflict. * @param int $theNewFileId The id of the new file that is conflicting. * @param string $fileName The filename that is in the conflict. * * @return AssetOperationResponseModel */ private function _mergeUploadedFiles($conflictResolution, $theNewFileId, $fileName) { $theNewFile = $this->getFileById($theNewFileId); $folder = $theNewFile->getFolder(); $source = craft()->assetSources->getSourceTypeById($folder->sourceId); $fileId = null; switch ($conflictResolution) { case AssetConflictResolution::Replace: // Replace the actual file $targetFile = $this->findFile(array('folderId' => $folder->id, 'filename' => $fileName)); $source->replaceFile($targetFile, $theNewFile); $fileId = $targetFile->id; // Falling through to delete the file // Falling through to delete the file case AssetConflictResolution::Cancel: $this->deleteFiles($theNewFileId); break; default: $fileId = $theNewFileId; break; } $response = new AssetOperationResponseModel(); $response->setSuccess(); if ($fileId) { $response->setDataItem('fileId', $fileId); } return $response; }
/** * Merge a conflicting uploaded file. * * @param string $conflictResolution User response to conflict. * @param int $theNewFileId The id of the new file that is conflicting. * @param string $fileName The filename that is in the conflict. * * @return AssetOperationResponseModel */ private function _mergeUploadedFiles($conflictResolution, $theNewFileId, $fileName) { $theNewFile = $this->getFileById($theNewFileId); $folder = $theNewFile->getFolder(); $source = craft()->assetSources->getSourceTypeById($folder->sourceId); $fileId = null; switch ($conflictResolution) { case AssetConflictResolution::Replace: // Replace the actual file $targetFile = $this->findFile(array('folderId' => $folder->id, 'filename' => $fileName)); // If the file doesn't exist in the index, but just in the source, // quick-index it, so we have a File Model to work with. if (!$targetFile) { $targetFile = new AssetFileModel(); $targetFile->sourceId = $folder->sourceId; $targetFile->folderId = $folder->id; $targetFile->filename = $fileName; $targetFile->kind = IOHelper::getFileKind(IOHelper::getExtension($fileName)); $this->storeFile($targetFile); } $source->replaceFile($targetFile, $theNewFile); $fileId = $targetFile->id; // Falling through to delete the file // Falling through to delete the file case AssetConflictResolution::Cancel: $this->deleteFiles($theNewFileId); break; default: $fileId = $theNewFileId; break; } $response = new AssetOperationResponseModel(); $response->setSuccess(); if ($fileId) { $response->setDataItem('fileId', $fileId); } return $response; }
/** * Merge a conflicting uploaded file. * * @param string $userResponse User response to conflict * @param string $responseInfo Additional information about the chosen action * @param string $fileName The filename that is in the conflict * @return array|string */ private function _mergeUploadedFiles($userResponse, $responseInfo, $fileName) { list($folderId, $createdFileId) = explode(":", $responseInfo); $folder = $this->getFolderById($folderId); $source = craft()->assetSources->getSourceTypeById($folder->sourceId); $fileId = null; switch ($userResponse) { case AssetsHelper::ActionReplace: // Replace the actual file $targetFile = $this->findFile(array('folderId' => $folderId, 'filename' => $fileName)); $replaceWith = $this->getFileById($createdFileId); $source->replaceFile($targetFile, $replaceWith); $fileId = $targetFile->id; // Falling through to delete the file // Falling through to delete the file case AssetsHelper::ActionCancel: $this->deleteFiles($createdFileId); break; default: $fileId = $createdFileId; break; } $response = new AssetOperationResponseModel(); $response->setSuccess(); if ($fileId) { $response->setDataItem('fileId', $fileId); } return $response; }