Skip to content

Commit

Permalink
REPOSITORY/MDL-13766
Browse files Browse the repository at this point in the history
pass mimetype and return value type to file picker to filter repository instances
  • Loading branch information
dongsheng committed Nov 26, 2008
1 parent c977816 commit b318bb6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lib/editor/tinymce/tinymce.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,14 @@ function mce_saveOnSubmit(id) {
};
}
function moodlefilemanager(field_name, url, type, win) {
//alert(id2suffix[tinyMCE.selectedInstance.editorId]);
var suffix = id2suffix[tinyMCE.selectedInstance.editorId];
document.body.className += ' yui-skin-sam';
var picker = document.createElement('DIV');
picker.className = "file-picker";
picker.id = 'file-picker-'+suffix;
document.body.appendChild(picker);
var el = win.document.getElementById(field_name);
eval('openpicker_'+suffix+'({"env":"editor","target":el})');
eval('openpicker_'+suffix+'({"env":"editor","target":el, "mimetype":type})');
}
EOF;

Expand Down
6 changes: 4 additions & 2 deletions lib/form/filepicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
var $_helpbutton='';

function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null) {
function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $filetypes = '*', $returnvalue = '*') {
$this->filetypes = $filetypes;
$this->returnvalue = $returnvalue;
parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
}

Expand Down Expand Up @@ -70,7 +72,7 @@ function toHtml() {
} else {
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
}
$repository_info = repository_get_client($context);
$repository_info = repository_get_client($context, $this->filetypes, $this->returnvalue);
$suffix = $repository_info['suffix'];

$id = $this->_attributes['id'];
Expand Down
2 changes: 1 addition & 1 deletion lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5119,7 +5119,7 @@ function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $v
} else {
$ctx = $COURSE->context;
}
$ret = repository_get_client($ctx);
$ret = repository_get_client($ctx, null, true, null, array('image', 'media'), '*');
$str .= $ret['css'].$ret['js'];
$suffix = $ret['suffix'];
$str .= '<div class="textareaicons">';
Expand Down
11 changes: 6 additions & 5 deletions repository/javascript.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @param object $context the context
* @return array
*/
function repository_get_client($context) {
function repository_get_client($context, $filetypes = '*', $returnvalue = '*') {
global $CFG, $USER;
$suffix = uniqid();
$sesskey = sesskey();
Expand Down Expand Up @@ -966,7 +966,7 @@ function _client() {
EOD;

$user_context = get_context_instance(CONTEXT_USER, $USER->id);
$repos = repository_get_instances(array($user_context, $context, get_system_context()));
$repos = repository_get_instances(array($user_context, $context, get_system_context()), null, true, null, $filetypes, $returnvalue);
foreach ($repos as $repo) {
$info = $repo->ajax_info();
$js .= "\r\n";
Expand All @@ -980,11 +980,12 @@ function openpicker_$suffix(params) {
if(!repository_client_$suffix.instance) {
repository_client_$suffix.env = params.env;
repository_client_$suffix.target = params.target;
if(params.type) {
repository_client_$suffix.filetype = params.filetype;
if(params.mimetype) {
repository_client_$suffix.mimetype = params.mimetype;
} else {
repository_client_$suffix.filetype = 'all';
repository_client_$suffix.mimetype = '*';
}
alert(repository_client_$suffix.mimetype);
repository_client_$suffix.instance = new repository_client_$suffix();
repository_client_$suffix.instance.create_picker();
if(params.callback) {
Expand Down
42 changes: 41 additions & 1 deletion repository/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,26 @@ public function get_name() {
}
}

/**
* what kind of files will be in this repository?
* @return array return '*' means this repository support any files, otherwise
* return mimetypes of files, it can be an array
*/
public function supported_mimetype() {
// return array('text/plain', 'image/gif');
return '*';
}

/**
* does it return a file url or a item_id
* @return string
*/
public function supported_return_value() {
// return 'link';
// return 'ref_id';
return '*';
}

/**
* Provide repository instance information for Ajax
* @global object $CFG
Expand Down Expand Up @@ -1088,9 +1108,11 @@ function repository_get_editable_types($context = null) {
* @param boolean $onlyvisible if visible == true, return visible instances only,
* otherwise, return all instances
* @param string $type a type name to retrieve
* @param string $filetypes supported file types
* @param string $returnvalue supportted returned value
* @return array repository instances
*/
function repository_get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null) {
function repository_get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $filetypes = '*', $returnvalue = '*') {
global $DB, $CFG, $USER;

$params = array();
Expand Down Expand Up @@ -1140,6 +1162,24 @@ function repository_get_instances($contexts=array(), $userid = null, $onlyvisibl
$classname = 'repository_' . $repo->repositorytype;//

$repository = new $classname($repo->id, $repo->contextid, $options, $repo->readonly);
if ($filetypes !== '*' and $repository->supported_mimetype() !== '*') {
$mimetypes = $repository->supported_mimetype();
$is_supported = false;
foreach ($mimetypes as $type) {
if (in_array($type, $filetypes)) {
$is_supported = true;
}
}
if (!$is_supported) {
continue;
}
}
if ($returnvalue !== '*' and $repository->supported_return_value() !== '*') {
$tmp = $repository->supported_return_value();
if ($tmp == $returnvalue) {
continue;
}
}
if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) {
$ret[] = $repository;
}
Expand Down

0 comments on commit b318bb6

Please sign in to comment.