Skip to content

Commit

Permalink
MDL-15351: it's now possible to search the local repository
Browse files Browse the repository at this point in the history
  • Loading branch information
scyrma committed Sep 11, 2008
1 parent 3701fd3 commit 79902e8
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions repository/local/repository.class.php
Expand Up @@ -54,46 +54,42 @@ public function get_listing($encodedpath = '', $search = '') {
$filename = null;
$filearea = null;
$path = '/';
$ret['dynload'] = false;

/// useful only if dynamic mode can be worked out.
//if ($encodedpath != '') {
// list($filearea, $path) = $this->_decode_path($encodedpath);
//}

if ($encodedpath != '') {
list($filearea, $path) = $this->_decode_path($encodedpath);
}

$count = 0;

if ($fileinfo = $browser->get_file_info(get_system_context(), $filearea, $itemid, $path, $filename)) {
$ret['path'] = array();
$params = $fileinfo->get_params();
$filearea = $params['filearea'];
//todo: fix this call, and similar ones here and in build_tree - encoding path works only for real folders
$ret['path'][] = $this->_encode_path($filearea, $path, $fileinfo->get_visible_name());
if ($fileinfo->is_directory()) {
$level = $fileinfo->get_parent();
while ($level) {
$params = $level->get_params();
$ret['path'] = $this->_encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
$ret['path'][] = $this->_encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
$level = $level->get_parent();
}
}
$ret['list'] = $this->build_tree($fileinfo, $search); //, $ret['path']);
$filecount = $this->build_tree($fileinfo, $search, $ret['dynload'], $ret['list']);
$ret['path'] = array_reverse($ret['path']);
} else {
// throw some "context/filearea/item/path/file not found" exception?
}

// this statement tells the file picker to load files dynamically (don't send the content of directories)
// todo: add a thresold, where the picker automatically uses the dynamic mode - if there are too many files in
// sub-directories - this should be calculated with a quick query, for the whole tree. Better optimizations
// (like loading just a part of the sub-tree) can come later.
// if ($count > $config_thresold) {
// $ret['dynload'] = true;
//} else {
$ret['dynload'] = false;
//}

if (empty($ret['list'])) {
throw new repository_exception('emptyfilelist', 'repository_local');
} else {
return $ret;
// if using dynamic mode, only the subfolder needs be returned.
//if ($loadroot) {
return $ret;
//} else {
// return $ret['list'];
//}
}
}

Expand All @@ -103,18 +99,17 @@ public function get_listing($encodedpath = '', $search = '') {
*
* @param $fileinfo an object returned by file_browser::get_file_info()
* @param $search searched string
* @param $path path to prepend to current element
* @param $currentcount (in recursion) current number of elements, triggers dynamic mode if there's more than thresold.
* @returns array describing the files under the passed $fileinfo
* @param $dynamicmode bool no recursive call is done when in dynamic mode
* @param $list - the array containing the files under the passed $fileinfo
* @returns int the number of files found
*
* todo: take $search into account, and respect a threshold for dynamic loading
*/
private function build_tree($fileinfo, $search) { //, $path, &$currentcount) {
private function build_tree($fileinfo, $search, $dynamicmode, &$list) {
global $CFG;

$children = $fileinfo->get_children();

$list = array();
foreach ($children as $child) {
$filename = $child->get_visible_name();
$filesize = $child->get_filesize();
Expand All @@ -128,7 +123,7 @@ private function build_tree($fileinfo, $search) { //, $path, &$currentcount) {
$level = $child->get_parent();
while ($level) {
$params = $level->get_params();
$path = $this->_encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
$path[] = $this->_encode_path($params['filearea'], $params['filepath'], $level->get_visible_name());
$level = $level->get_parent();
}

Expand All @@ -139,10 +134,27 @@ private function build_tree($fileinfo, $search) { //, $path, &$currentcount) {
'path' => array_reverse($path),
'thumbnail' => $CFG->pixpath .'/f/folder.gif'
);
$tmp['children'] = $this->build_tree($child, $search);
$list[] = $tmp;

//if ($dynamicmode && $child->is_writable()) {
// $tmp['children'] = array();
//} else {
// if folder name matches search, we send back all files contained.
$_search = $search;
if ($search && stristr($tmp['title'], $search) !== false) {
$_search = false;
}
$filecount = $this->build_tree($child, $_search, $dynamicmode, $tmp['children']);
//}

if (!$search || $filecount || (stristr($tmp['title'], $search) !== false)) {
$list[] = $tmp;
}

} else { // not a directory
// skip the file, if we're in search mode and it's not a match
if ($search && (stristr($filename, $search) === false)) {
continue;
}
$list[] = array(
'title' => $filename,
'size' => $filesize,
Expand Down

0 comments on commit 79902e8

Please sign in to comment.