* @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();
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;
}
}
}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');
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 = {
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();
$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 .= <<<EOD
function openpicker_$suffix(params) {
if(params.filetype) {
if(params.filetype == 'image') {
- repository_client_$suffix.filetype = $image_file_ext;
+ repository_client_$suffix.accepted_types = $image_file_ext;
} else if(params.filetype == 'video' || params.filetype== 'media') {
- repository_client_$suffix.filetype = $video_file_ext;
+ repository_client_$suffix.accepted_types = $video_file_ext;
}
} else {
- repository_client_$suffix.filetype = '*';
+ repository_client_$suffix.accepted_types = $accpeted_file_ext;
}
if(!repository_client_$suffix.instance) {
repository_client_$suffix.env = params.env;
* @param string $type a type name to retrieve
* @return array repository instances
*/
- public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $filetypes = '*', $returnvalue = '*') {
+ public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $accepted_types = '*', $returnvalue = '*') {
global $DB, $CFG, $USER;
$params = array();
}
$ret = array();
+ $ft = new file_type_to_ext();
foreach ($repos as $repo) {
require_once($CFG->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;
}
}
$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;
}
}
}
+ 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.
*