/** * @param int $targetDirId - destination folder id * @param array $filesIds - list of files ids to move * @param array $foldersIds - list of folders ids to move * * @return bool * @throws CopyMoveSelectionException */ public function move($targetDirId, array $filesIds, array $foldersIds = array()) { if (empty($filesIds) && empty($foldersIds)) { return false; } $targetDir = $this->getDirectoryById($targetDirId); foreach ($filesIds as $fileId) { $file = $this->getFileById($fileId); $file->setDirectory($targetDir); } if (!empty($foldersIds)) { $targetDirParents = $this->directoryProvider->getDirectoryParentsList($targetDir, false); foreach ($foldersIds as $dirId) { $dir = $this->getDirectoryById($dirId); if ($dir === $targetDir) { throw new CopyMoveSelectionException('Try to move dir to it self: ' . $dir->getName()); } else { if (in_array($dir, $targetDirParents)) { throw new CopyMoveSelectionException('Try to move parent dir to child dir: ' . $dir->getName()); } } $dir->setParent($targetDir); } } return true; }
/** * @covers RI\FileManagerBundle\DataProvider\DirectoryDataProvider::getDirectoryParentsList */ public function testGetDirectoryParentList_ShouldReturnNotConvertedArray() { $rootDirMock = $this->createMock('RI\\FileManagerBundle\\Entity\\Directory'); $rootDirMock->expects($this->once())->method('getParent')->will($this->returnValue(null)); $levelOneDirMock = $this->createMock('RI\\FileManagerBundle\\Entity\\Directory'); $levelOneDirMock->expects($this->once())->method('getParent')->will($this->returnValue($rootDirMock)); $directoryMock = $this->createMock('RI\\FileManagerBundle\\Entity\\Directory'); $directoryMock->expects($this->once())->method('getParent')->will($this->returnValue($levelOneDirMock)); $expectedData = array($rootDirMock, $levelOneDirMock); $this->assertEquals($expectedData, $this->directoryDataProvider->getDirectoryParentsList($directoryMock, false)); }
/** * @param Directory $targetDir * @param array $foldersIds * * @throws CopyMoveSelectionException */ private function copyFolders(Directory $targetDir = null, array $foldersIds = array()) { if (!empty($foldersIds)) { $targetDirParents = $this->directoryProvider->getDirectoryParentsList($targetDir, false); foreach ($foldersIds as $dirId) { $dir = $this->getDirectoryById($dirId); if ($dir === $targetDir) { throw new CopyMoveSelectionException('Try to move dir to it self: ' . $dir->getName()); } else { if (in_array($dir, $targetDirParents)) { throw new CopyMoveSelectionException('Try to move parent dir to child dir: ' . $dir->getName()); } } $this->copyFolderRecursive($targetDir, $dir); } } }