/** * Adds a folder to its place in a folder tree. * @internal Its crucial that all parent folders are already in the tree or this will fail. * @param array $tree The array of top level folders to add to. {@internal Passed by reference. } * @param Folder $folder The folder to add. * @param string $shortestTree The shortest tree that in the hierarchy. * @throws FolderException Thrown if the $tree is not prepared to recieve the $folder. * @author Björn Hjortsten * @return void */ protected static function addToTree(array &$tree, Folder $folder, $shortestTree) { $rootLevel = count(array_filter(explode('-', $shortestTree))); $folderTree = explode('-', $folder->getTree()); $folderTree = array_filter($folderTree); $folderTree = Folder::arrangeKeys($folderTree); if (count($folderTree) == $rootLevel) { $tree[$folder->getId()] = $folder; } else { try { $currentFolder = $tree[intval($folderTree[$rootLevel - 1], 16)]; if ($currentFolder == null) { throw new FolderException(sprintf('Error while getting great parent folder. The great parent folder is supposed to be: %d, child folder: %d', intval($folderTree[$rootLevel - 1], 16), $folder->getId())); } for ($i = $rootLevel; $i < count($folderTree) - 1; $i++) { $currentFolder = $currentFolder->getChild(intval($folderTree[$i], 16)); } $currentFolder->addChild($folder); } catch (FolderException $fe) { if ($currentFolder instanceof Folder) { $parentId = $currentFolder->getId(); } else { $parentId = 'null'; } throw new FolderException(sprintf('Error while adding folder to tree. Current folder id: %s, child folder id: %d. Previous exception: %s', $parentId, $folder->getId(), $fe->getMessage())); } } }
/** * Gets a folder from QBank. * @param int $id The folders id. * @param bool $simple If true, gets {@link SimpleFolder}s, otherwise a {@link Folder}. * @param bool $recursive If the folders subfolders also should be returned. * @throws CommunicationException Thrown if something went wrong while getting the folder. * @throws ConnectionException Thrown if something went wrong with the connection. * @author Björn Hjortsten * @return mixed If $simple is TRUE and it may return an array of {@link SimpleFolder}s or a {@link SimpleFolder} depending on $recursive. If $simple is FALSE it will return a {@link Folder}. */ public function getFolder($id, $simple = true, $recursive = false) { if ($recursive === true) { $calls[] = array('name' => 'subfolders', 'function' => 'getfolderstructure', 'arguments' => array('folderId' => $id, 'depth' => 23, 'fetchProperties' => !$simple)); } $calls[] = array('name' => 'folder', 'function' => 'getfolderinformation', 'arguments' => array('folderId' => $id)); $result = $this->call('batch', array('calls' => $calls)); if ($result->results->folder->success !== true) { $this->wrapperLog->error('Error while getting folder: ' . $result->results->folder->error->message, array('code' => $result->results->folder->error->code, 'type' => $result->results->folder->error->type)); throw new CommunicationException($result->results->folder->error->message, $result->results->folder->error->code, $result->results->folder->error->type); } $folder = $result->results->folder->folder; if ($simple === true) { $folder = new SimpleFolder($folder->name, $folder->tree, $folder->owner, strtotime($folder->created), strtotime($folder->updated)); } else { $properties = array(); foreach ($folder->properties as $property) { $properties[] = Property::createFromRawObject($property); } $folder = new Folder($folder->name, $folder->tree, $folder->owner, strtotime($folder->created), strtotime($folder->updated), $properties); } if ($recursive === true) { if ($result->results->subfolders->success !== true) { $this->wrapperLog->error('Error while getting folder: ' . $result->results->subfolders->error->message, array('code' => $result->results->subfolders->error->code, 'type' => $result->results->subfolders->error->type)); throw new CommunicationException($result->results->subfolders->error->message, $result->results->subfolders->error->code, $result->results->subfolders->error->type); } if (is_array($result->results->subfolders->data)) { foreach ($result->results->subfolders->data as $subfolder) { if ($simple === true) { $folders[$subfolder->folderId] = new SimpleFolder($subfolder->name, $subfolder->tree, $subfolder->owner, strtotime($subfolder->created), strtotime($subfolder->updated)); } else { $properties = array(); foreach ($subfolder->properties as $property) { $properties[] = Property::createFromRawObject($property); } $folders[$subfolder->folderId] = new Folder($subfolder->name, $subfolder->tree, $subfolder->owner, strtotime($subfolder->created), strtotime($subfolder->updated), $properties); } } } $folders[$folder->getId()] = $folder; if ($simple === false) { $folders = Folder::createTree($folders); return $folders[$folder->getId()]; } $folder = $folders; } return $folder; }