From b423b4af24cf2c46cb73e43f4101a6e867f307a6 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Tue, 2 Dec 2008 07:05:15 +0000 Subject: [PATCH] REPOSITORY, FILEMANGER FORM ELEMENT/MDL-16597 new ajax filemanager. --- lang/en_utf8/repository.php | 1 + lib/form/filemanager.php | 100 +++++++++++++++++++++++-- lib/form/filepicker.php | 6 +- repository/javascript.php | 18 +++-- repository/lib.php | 22 ++++-- repository/upload/repository.class.php | 3 +- repository/ws.php | 31 ++++++-- 7 files changed, 155 insertions(+), 26 deletions(-) diff --git a/lang/en_utf8/repository.php b/lang/en_utf8/repository.php index 36ddfda759..7da6a4f163 100644 --- a/lang/en_utf8/repository.php +++ b/lang/en_utf8/repository.php @@ -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 all of its instances - $a?'; $string['create'] = 'Create'; $string['createrepository'] = 'Create a repository instance'; diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index 2b3bfcfe7b..295a317f74 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -1,6 +1,7 @@ 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 .= ''; + 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); + } - $str = $this->_getTabs(); - $str .= ''; - $str .= 'Error'; // TODO: localise, fix styles, etc. + $repo_info = repository_get_client($context); + $suffix = $repo_info['suffix']; + + $html = $this->_get_draftfiles($draftitemid, $suffix); + $str = $this->_getTabs(); + $str .= $html; + $str .= $repo_info['css']; + $str .= $repo_info['js']; + $str .= << +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 = '
  • '+obj['file']+' '; + html += '';; + html += '
  • '; + 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}) +} + + +
    + +
    +EOD; return $str; } diff --git a/lib/form/filepicker.php b/lib/form/filepicker.php index d270c69872..c4878311bd 100644 --- a/lib/form/filepicker.php +++ b/lib/form/filepicker.php @@ -83,8 +83,8 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input { $str .= << -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}) } EOD; diff --git a/repository/javascript.php b/repository/javascript.php index 6ef08e8efb..2e320f1f6a 100644 --- a/repository/javascript.php +++ b/repository/javascript.php @@ -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 { diff --git a/repository/lib.php b/repository/lib.php index fc66a0ec62..832f4a3859 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -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(); diff --git a/repository/upload/repository.class.php b/repository/upload/repository.class.php index 304d47ee55..0161cddcfa 100755 --- a/repository/upload/repository.class.php +++ b/repository/upload/repository.class.php @@ -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); } } diff --git a/repository/ws.php b/repository/ws.php index 9bfa782830..e4c07f37a9 100644 --- a/repository/ws.php +++ b/repository/ws.php @@ -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); -- 2.39.5