From c167aa26c10625140eb5ddf4ee5154a5f3f8bf57 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Tue, 9 Dec 2008 02:11:57 +0000 Subject: [PATCH] "REPOSITORY, BOXNET/MDL-17391, create a helper function to filter files in listing" --- repository/boxnet/repository.class.php | 2 +- repository/javascript.php | 24 +++++++++-------- repository/lib.php | 37 +++++++++++++++++++++----- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/repository/boxnet/repository.class.php b/repository/boxnet/repository.class.php index 1a0b698ab7..e4ff381d0e 100755 --- a/repository/boxnet/repository.class.php +++ b/repository/boxnet/repository.class.php @@ -181,7 +181,7 @@ class repository_boxnet extends repository { $ret['manage'] = 'http://www.box.net/files'; $ret['path'] = array(array('name'=>'Root', 'path'=>0)); if(!empty($tree)) { - $ret['list'] = $tree; + $ret['list'] = array_filter($tree, array($this, 'filter')); } return $ret; } diff --git a/repository/javascript.php b/repository/javascript.php index 99de61161d..b1f9b85d10 100644 --- a/repository/javascript.php +++ b/repository/javascript.php @@ -11,7 +11,7 @@ * @param object $context the context * @return array */ -function repository_get_client($context, $filetypes = '*', $returnvalue = '*') { +function repository_get_client($context, $accepted_filetypes = '*', $returnvalue = '*') { global $CFG, $USER; $suffix = uniqid(); $sesskey = sesskey(); @@ -309,9 +309,9 @@ _client.print_instances = function() { var repo = _client.repos[i]; var support = false; if(repository_client_$suffix.env=='editor'){ - if(repo.filetype!='*'){ - for (var j in repo.filetype){ - if(mdl_in_array(repo.filetype[j], _client.filetype)){ + if(repo.supported_types!='*'){ + for (var j in repo.supported_types){ + if(mdl_in_array(repo.supported_types[j], _client.accepted_types)){ support = true; } } @@ -319,7 +319,7 @@ _client.print_instances = function() { }else{ support = true; } - if(repo.filetype == '*' || support){ + if(repo.supported_types == '*' || support){ var li = document.createElement('li'); li.id = 'repo-$suffix-'+repo.id; var icon = document.createElement('img'); @@ -905,6 +905,7 @@ _client.req = function(id, path, logout) { params['sesskey']='$sesskey'; params['ctx_id']=$context->id; params['repo_id']=id; + params['accepted_types'] = _client.accepted_types; var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action='+action, _client.req_cb, _client.postdata(params)); } _client.search_form_cb = { @@ -1013,10 +1014,10 @@ return _client; EOD; $user_context = get_context_instance(CONTEXT_USER, $USER->id); -if (is_array($filetypes) && in_array('*', $filetypes)) { - $filetypes = '*'; +if (is_array($accepted_filetypes) && in_array('*', $accepted_filetypes)) { + $accepted_filetypes = '*'; } -$repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $filetypes, $returnvalue); +$repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue); $js .= "\r\n".'repository_client_'.$suffix.'.repos=[];'."\r\n"; foreach ($repos as $repo) { $info = $repo->ajax_info(); @@ -1028,16 +1029,17 @@ $js .= "\r\n"; $ft = new file_type_to_ext(); $image_file_ext = json_encode($ft->get_file_ext(array('image'))); $video_file_ext = json_encode($ft->get_file_ext(array('video'))); +$accpeted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes)); $js .= <<dirroot . '/repository/'. $repo->repositorytype.'/repository.class.php'); $options['visible'] = $repo->visible; $options['name'] = $repo->name; $options['type'] = $repo->repositorytype; $options['typeid'] = $repo->typeid; + // tell instance what file types will be accepted by file picker + $options['accepted_types'] = $ft->get_file_ext($accepted_types); $classname = 'repository_' . $repo->repositorytype;// $is_supported = true; $repository = new $classname($repo->id, $repo->contextid, $options, $repo->readonly); - $ft = new file_type_to_ext(); - if ($filetypes !== '*' and $repository->supported_filetypes() !== '*') { - $filetypes = $ft->get_file_ext($filetypes); + if ($accepted_types !== '*' and $repository->supported_filetypes() !== '*') { + $accepted_types = $ft->get_file_ext($accepted_types); $supported_filetypes = $ft->get_file_ext($repository->supported_filetypes()); $is_supported = false; foreach ($supported_filetypes as $type) { - if (in_array($type, $filetypes)) { + if (in_array($type, $accepted_types)) { $is_supported = true; } } @@ -1274,7 +1276,8 @@ abstract class repository { $repo->name = $this->get_name(); $repo->type = $this->options['type']; $repo->icon = $CFG->httpswwwroot.'/repository/'.$repo->type.'/icon.png'; - $repo->filetype = $ft->get_file_ext($this->supported_filetypes()); + $repo->supported_types = $ft->get_file_ext($this->supported_filetypes()); + $repo->accepted_types = $this->accepted_types; return $repo; } @@ -1455,6 +1458,28 @@ abstract class repository { } } + public function filter(&$value) { + $pass = false; + $accepted_types = optional_param('accepted_types', '', PARAM_RAW); + $ft = new file_type_to_ext; + $ext = $ft->get_file_ext($this->supported_filetypes()); + if (isset($value->children)) { + $pass = true; + $value->children = array_filter($value->children, array($this, 'filter')); + } else { + if ($accepted_types == '*') { + $pass = true; + } elseif (is_array($accepted_types)) { + foreach ($accepted_types as $type) { + if (preg_match('#'.$type.'$#', $value['title'])) { + $pass = true; + } + } + } + } + return $pass; + } + /** * Given a path, and perhaps a search, get a list of files. * -- 2.39.5