/** * Convert a FolderInterface object into a MongoFolder object. * @param FolderInterface $folder * @return \MongoFolder */ public static function loadFromFolderInterface(FolderInterface $folder) { $conditions = array(); $conditions[] = array("path" => $folder->getPath()); $conditions[] = array("name" => $folder->getName()); $query = array('$and' => $conditions); /* @var $gridFsDoc MongoGridFsFile */ $gridFs = ConnectionHandler::getInstance()->getConnection(); $gridFsDoc = $gridFs->findOne($query); if ($gridFsDoc === null) { print "Failed to find mongo folder for provided folder interface." . PHP_EOL; print "path: " . $folder->getPath() . PHP_EOL; print "name: " . $folder->getName() . PHP_EOL; die; } /* @var $mongoId MongoId */ $folderMongoId = $gridFsDoc->file["_id"]; $name = $gridFsDoc->file['name']; $creationTime = $gridFsDoc->path['creation_time']; $mongoFolder = new MongoFolder($name, $creationTime); if (isset($gridFsDoc->file["parent"])) { $parent = MongoFolder::loadFromMongoId($gridFsDoc->file["parent"]); $mongoFolder->setParentFolder($parent); } $mongoFolder->mMongoId = $folderMongoId; $mongoFolder->mGridFsFile = $gridFsDoc; return $mongoFolder; }
/** * @param FolderInterface $folder * @param FolderInterface $parent * @return array */ public function getValidateFolderResult(FolderInterface $folder, FolderInterface $parent) { $check = array('Child folder name not specified' => !$folder->getName(), 'Child folder path not specified' => !$folder->getPath(), 'Child folder created time not specified' => !$folder->getCreatedTime(), 'Parent folder name not specified' => !$parent->getName(), 'Parent folder path not specified' => !$parent->getPath(), 'Parent folder created time not specified' => !$parent->getCreatedTime()); if ($folder->getPath()) { $key = 'Folder with a path ' . $folder->getPath() . ' has been already created'; $check[$key] = $this->folderExists($folder->getPath()); } return $check; }
/** * Create the specified folder. * I dont understand why this exists with the provided $folder. Surely if we want to * create a folder we should be calling the $folders constructor or one of its static * creation methods? * @param FolderInterface $folder - the folder to be created? * @param FolderInterface $parent - the folder the new folder will be within. * * @return FolderInterface - the newly created folder. */ public function createFolder(FolderInterface $folder, FolderInterface $parent) { $gridFs = ConnectionHandler::getInstance()->getConnection(); $parentMongoFolder = MongoFolder::loadFromFolderInterface($parent); if ($folder->getCreatedTime() === null) { $folder->setCreatedTime(time()); } $metadata = array('name' => $folder->getName(), 'path' => $parent->getPath() . '/' . $parent->getName(), 'creation_time' => $folder->getCreatedTime(), 'parent' => $parentMongoFolder->getMongoId(), 'type' => 'folder'); print "creating folder with details: " . print_r($metadata, true); $gridFs->storeBytes("", $metadata); # folder is an empty file with metadata $mongoFolder = MongoFolder::loadFromFolderInterface($folder); return $mongoFolder; }
/** * @param FolderInterface $folder * @param $newName * * @return FolderInterface */ public function renameFolder(FolderInterface $folder, $newName) { if (@rename($folder->getPath() . "/" . $folder->getName(), $folder->getPath() . "/" . $newName)) { return true; } return false; }