REPOSITORY, FILEMANGER FORM ELEMENT/MDL-16597
new ajax filemanager.
This commit is contained in:
@@ -22,6 +22,7 @@ $string['copying'] = 'Copying';
|
||||
$string['configcacheexpire'] = 'The amount of time that file listings are cached locally (in seconds) when browsing external repositories.';
|
||||
$string['configsaved'] = 'Configuration saved!';
|
||||
$string['confirmdelete'] = 'Are you sure you want to delete this repository - $a?';
|
||||
$string['confirmdeletefile'] = 'Are you sure to delete this file?';
|
||||
$string['confirmremove'] = 'Are you sure you want to remove this repository plugin, its options and <strong style=color:red>all of its instances</strong> - $a?';
|
||||
$string['create'] = 'Create';
|
||||
$string['createrepository'] = 'Create a repository instance';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php // $Id$
|
||||
|
||||
require_once('HTML/QuickForm/element.php');
|
||||
require_once(dirname(dirname(dirname(__FILE__))) . '/lib/filelib.php');
|
||||
|
||||
class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
|
||||
protected $_helpbutton = '';
|
||||
@@ -89,8 +90,46 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
|
||||
}
|
||||
}
|
||||
|
||||
function _get_draftfiles($draftid, $suffix) {
|
||||
global $USER, $CFG;
|
||||
$html = '';
|
||||
if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
|
||||
}
|
||||
$contextid = $context->id;
|
||||
$filearea = 'user_draft';
|
||||
|
||||
$browser = get_file_browser();
|
||||
$fs = get_file_storage();
|
||||
$filepath = '/';
|
||||
if (!$directory = $fs->get_file($context->id, 'user_draft', $draftid, $filepath, '.')) {
|
||||
$directory = new virtual_root_file($context->id, 'user_draft', $draftid);
|
||||
$filepath = $directory->get_filepath();
|
||||
}
|
||||
$files = $fs->get_directory_files($context->id, 'user_draft', $draftid, $directory->get_filepath());
|
||||
$parent = $directory->get_parent_directory();
|
||||
$html .= '<ul id="draftfiles-'.$suffix.'">';
|
||||
foreach ($files as $file) {
|
||||
$filename = $file->get_filename();
|
||||
$filenameurl = rawurlencode($filename);
|
||||
$filepath = $file->get_filepath();
|
||||
$filesize = $file->get_filesize();
|
||||
$filesize = $filesize ? display_size($filesize) : '';
|
||||
$mimetype = $file->get_mimetype();
|
||||
$icon = mimeinfo_from_type('icon', $mimetype);
|
||||
$viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$draftid".$filepath.$filename, false, false);
|
||||
$html .= '<li>';
|
||||
$html .= "<a href=\"$viewurl\"><img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" /> ".s($filename)." ($filesize)</a> ";
|
||||
$html .= "<a href=\"###\" onclick='rm_$suffix(".$file->get_itemid().", \"".$filename."\", this)'><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" /></a>";;
|
||||
$html .= '</li>';
|
||||
}
|
||||
$html .= '</ul>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
function toHtml() {
|
||||
global $CFG, $USER;
|
||||
global $CFG, $USER, $COURSE;
|
||||
$strdelete = get_string('confirmdeletefile', 'repository');
|
||||
$straddfile = get_string('add', 'repository');
|
||||
|
||||
// security - never ever allow guest/not logged in user to upload anything or use this element!
|
||||
if (isguestuser() or !isloggedin()) {
|
||||
@@ -114,12 +153,63 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
|
||||
$draftitemid = $this->getValue();
|
||||
}
|
||||
|
||||
$editorurl = "$CFG->wwwroot/files/draftfiles.php?itemid=$draftitemid&subdirs=$subdirs&maxbytes=$maxbytes";
|
||||
if ($COURSE->id == SITEID) {
|
||||
$context = get_context_instance(CONTEXT_SYSTEM);
|
||||
} else {
|
||||
$context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
|
||||
}
|
||||
|
||||
$repo_info = repository_get_client($context);
|
||||
$suffix = $repo_info['suffix'];
|
||||
|
||||
$html = $this->_get_draftfiles($draftitemid, $suffix);
|
||||
|
||||
$str = $this->_getTabs();
|
||||
$str .= '<input type="hidden" name="'.$elname.'" value="'.$draftitemid.'" />';
|
||||
$str .= '<object type="text/html" id="'.$id.'" data="'.$editorurl.'" height="160" width="600" style="border:1px solid #000">Error</object>'; // TODO: localise, fix styles, etc.
|
||||
|
||||
$str .= $html;
|
||||
$str .= $repo_info['css'];
|
||||
$str .= $repo_info['js'];
|
||||
$str .= <<<EOD
|
||||
<script type="text/javascript">
|
||||
var elitem = null;
|
||||
var rm_cb_$suffix = {
|
||||
success: function(o) {
|
||||
if(o.responseText && o.responseText == 200){
|
||||
elitem.parentNode.removeChild(elitem);
|
||||
}
|
||||
}
|
||||
}
|
||||
function rm_$suffix(id, name, context) {
|
||||
if (confirm('$strdelete')) {
|
||||
var trans = YAHOO.util.Connect.asyncRequest('POST',
|
||||
'{$CFG->httpswwwroot}/repository/ws.php?action=delete&itemid='+id,
|
||||
rm_cb_$suffix,
|
||||
'title='+name
|
||||
);
|
||||
elitem = context.parentNode;
|
||||
}
|
||||
}
|
||||
function uf_$suffix(obj) {
|
||||
var list = document.getElementById('draftfiles-$suffix');
|
||||
var html = '<li><a href="'+obj['url']+'"><img src="'+obj['icon']+'" class="icon" /> '+obj['file']+'</a> ';
|
||||
html += '<a href="###" onclick=\'rm_$suffix('+obj['id']+', "'+obj['file']+'", this)\'><img src="{$CFG->pixpath}/t/delete.gif" class="iconsmall" /></a>';;
|
||||
html += '</li>';
|
||||
list.innerHTML += html;
|
||||
}
|
||||
function callpicker_$suffix() {
|
||||
document.body.className += ' yui-skin-sam';
|
||||
var picker = document.createElement('DIV');
|
||||
picker.id = 'file-picker-$suffix';
|
||||
picker.className = 'file-picker';
|
||||
document.body.appendChild(picker);
|
||||
var el=document.getElementById('$id');
|
||||
openpicker_$suffix({'env':'filemanager', 'target':el, 'itemid': $draftitemid, 'callback':uf_$suffix})
|
||||
}
|
||||
</script>
|
||||
<input value="$draftitemid" name="{$this->_attributes['name']}" type="hidden" />
|
||||
<div>
|
||||
<input value="$straddfile" onclick="callpicker_$suffix()" type="button" />
|
||||
</div>
|
||||
EOD;
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
@@ -83,8 +83,8 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
|
||||
|
||||
$str .= <<<EOD
|
||||
<script type="text/javascript">
|
||||
function updatefile_$suffix(str) {
|
||||
document.getElementById('repo_info_$suffix').innerHTML = str;
|
||||
function updatefile_$suffix(obj) {
|
||||
document.getElementById('repo_info_$suffix').innerHTML = obj['file'];
|
||||
}
|
||||
function callpicker_$suffix() {
|
||||
document.body.className += ' yui-skin-sam';
|
||||
@@ -93,7 +93,7 @@ function callpicker_$suffix() {
|
||||
picker.className = 'file-picker';
|
||||
document.body.appendChild(picker);
|
||||
var el=document.getElementById('$id');
|
||||
openpicker_$suffix({'env':'form', 'target':el, 'callback':updatefile_$suffix})
|
||||
openpicker_$suffix({'env':'filepicker', 'target':el, 'callback':updatefile_$suffix})
|
||||
}
|
||||
</script>
|
||||
EOD;
|
||||
|
||||
@@ -806,6 +806,9 @@ _client.download = function() {
|
||||
var file = document.getElementById('fileurl-$suffix').value;
|
||||
_client.loading('download', title);
|
||||
var params = [];
|
||||
if(_client.itemid){
|
||||
params['itemid']=_client.itemid;
|
||||
}
|
||||
params['env']=_client.env;
|
||||
params['file']=file;
|
||||
params['title']=title;
|
||||
@@ -838,14 +841,14 @@ _client.login = function() {
|
||||
var trans = YAHOO.util.Connect.asyncRequest('POST',
|
||||
'$CFG->httpswwwroot/repository/ws.php?action=sign', _client.req_cb, _client.postdata(params));
|
||||
}
|
||||
_client.end = function(str) {
|
||||
if(_client.env=='form') {
|
||||
_client.target.value = str['id'];
|
||||
}else{
|
||||
_client.target.value = str['url'];
|
||||
_client.end = function(obj) {
|
||||
if(_client.env=='filepicker') {
|
||||
_client.target.value = obj['id'];
|
||||
}else if(_client.env=='editor'){
|
||||
_client.target.value = obj['url'];
|
||||
_client.target.onchange();
|
||||
}
|
||||
_client.formcallback(str['file']);
|
||||
_client.formcallback(obj);
|
||||
_client.instance.hide();
|
||||
_client.viewfiles();
|
||||
}
|
||||
@@ -992,6 +995,9 @@ function openpicker_$suffix(params) {
|
||||
if(!repository_client_$suffix.instance) {
|
||||
repository_client_$suffix.env = params.env;
|
||||
repository_client_$suffix.target = params.target;
|
||||
if(params.itemid){
|
||||
repository_client_$suffix.itemid = params.itemid;
|
||||
}
|
||||
if(params.mimetype) {
|
||||
repository_client_$suffix.mimetype = params.mimetype;
|
||||
} else {
|
||||
|
||||
+16
-6
@@ -698,23 +698,31 @@ abstract class repository {
|
||||
$entry->filearea = $filearea;
|
||||
$entry->contextid = $context->id;
|
||||
$entry->filename = $name;
|
||||
$entry->filepath = '/'.uniqid().'/';
|
||||
//$entry->filepath = '/'.uniqid().'/';
|
||||
$entry->filepath = '/';
|
||||
$entry->timecreated = $now;
|
||||
$entry->timemodified = $now;
|
||||
$entry->userid = $USER->id;
|
||||
$entry->mimetype = mimeinfo('type', $path);
|
||||
if(is_numeric($itemid)) {
|
||||
$entry->itemid = $itemid;
|
||||
} else {
|
||||
$entry->itemid = 0;
|
||||
}
|
||||
$entry->mimetype = mimeinfo('type', $path);
|
||||
$entry->userid = $USER->id;
|
||||
$fs = get_file_storage();
|
||||
$browser = get_file_browser();
|
||||
if ($existingfile = $fs->get_file($context->id, $filearea, $itemid, $path, $name)) {
|
||||
$existingfile->delete();
|
||||
}
|
||||
if ($file = $fs->create_file_from_pathname($entry, $path)) {
|
||||
$delete = unlink($path);
|
||||
$ret = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
|
||||
if(!empty($ret)) {
|
||||
return array('url'=>$ret->get_url(),'id'=>$file->get_itemid(), 'file'=>$file->get_filename());
|
||||
return array('url'=>$ret->get_url(),
|
||||
'id'=>$file->get_itemid(),
|
||||
'file'=>$file->get_filename(),
|
||||
'icon'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $path)
|
||||
);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -750,7 +758,7 @@ abstract class repository {
|
||||
* @param bool $override override file if exists
|
||||
* @return mixed stored_file object or false if error; may throw exception if duplicate found
|
||||
*/
|
||||
public static function store_to_filepool($elname, $filearea='user_draft', $filepath='/', $filename = '', $override = false) {
|
||||
public static function store_to_filepool($elname, $filearea='user_draft', $filepath='/', $itemid='', $filename = '', $override = false) {
|
||||
global $USER;
|
||||
if (!isset($_FILES[$elname])) {
|
||||
return false;
|
||||
@@ -760,7 +768,9 @@ abstract class repository {
|
||||
$filename = $_FILES[$elname]['name'];
|
||||
}
|
||||
$context = get_context_instance(CONTEXT_USER, $USER->id);
|
||||
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
||||
if (empty($itemid)) {
|
||||
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
||||
}
|
||||
$fs = get_file_storage();
|
||||
$browser = get_file_browser();
|
||||
|
||||
|
||||
@@ -24,9 +24,10 @@ class repository_upload extends repository {
|
||||
public function __construct($repositoryid, $context = SITEID, $options = array()){
|
||||
global $SESSION, $action, $CFG;
|
||||
parent::__construct($repositoryid, $context, $options);
|
||||
$itemid = optional_param('itemid', '', PARAM_INT);
|
||||
if($action=='upload'){
|
||||
$filepath = '/'.uniqid().'/';
|
||||
$this->info = repository::store_to_filepool('repo_upload_file', 'user_draft', $filepath);
|
||||
$this->info = repository::store_to_filepool('repo_upload_file', 'user_draft', $filepath, $itemid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
-5
@@ -2,16 +2,16 @@
|
||||
|
||||
/// The Web service script that is called from the filepicker front end
|
||||
|
||||
|
||||
require_once('../config.php');
|
||||
require_once('../lib/filelib.php');
|
||||
require_once('lib.php');
|
||||
|
||||
/// Parameters
|
||||
$p = optional_param('p', '', PARAM_RAW); // page or path
|
||||
$env = optional_param('env', 'form', PARAM_ALPHA); // opened in editor or moodleform
|
||||
$env = optional_param('env', 'filepicker', PARAM_ALPHA);// opened in editor or moodleform
|
||||
$file = optional_param('file', '', PARAM_RAW); // file to download
|
||||
$title = optional_param('title', '', PARAM_FILE); // new file name
|
||||
$itemid = optional_param('itemid', '', PARAM_INT);
|
||||
$action = optional_param('action', '', PARAM_ALPHA);
|
||||
$ctx_id = optional_param('ctx_id', SITEID, PARAM_INT); // context ID
|
||||
$repo_id = optional_param('repo_id', 1, PARAM_INT); // repository ID
|
||||
@@ -34,7 +34,26 @@
|
||||
|
||||
/// Check for actions that do not need repository ID
|
||||
switch ($action) {
|
||||
|
||||
// delete a file from filemanger
|
||||
case 'delete':
|
||||
try {
|
||||
if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
|
||||
}
|
||||
$contextid = $context->id;
|
||||
$fs = get_file_storage();
|
||||
if ($file = $fs->get_file($contextid, 'user_draft', $itemid, '/', $title)) {
|
||||
$file->delete();
|
||||
echo 200;
|
||||
} else {
|
||||
echo '';
|
||||
}
|
||||
exit;
|
||||
} catch (repository_exception $e) {
|
||||
$err = new stdclass;
|
||||
$err->e = $e->getMessage();
|
||||
die(json_encode($err));
|
||||
}
|
||||
break;
|
||||
case 'gsearch': // Global Search
|
||||
$repos = repository::get_instances(array(get_context_instance_by_id($ctx_id), get_system_context()));
|
||||
$list = array();
|
||||
@@ -153,13 +172,15 @@ EOD;
|
||||
break;
|
||||
case 'download':
|
||||
$path = $repo->get_file($file, $title);
|
||||
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
||||
if (empty($itemid)) {
|
||||
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
||||
}
|
||||
try {
|
||||
if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) {
|
||||
echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path));
|
||||
} else {
|
||||
$info = repository::move_to_filepool($path, $title, $itemid);
|
||||
if ($env == 'form'){
|
||||
if ($env == 'filepicker' || $env == 'filemanager'){
|
||||
echo json_encode($info);
|
||||
} else if ($env == 'editor') {
|
||||
echo json_encode($info);
|
||||
|
||||
Reference in New Issue
Block a user