From: skodak Date: Sun, 21 Sep 2008 19:54:40 +0000 (+0000) Subject: MDL-16596 improved and simplified draftarea browsing X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=84c18f4d2c92507f9cb677ccfc8564bc755df788;p=moodle.git MDL-16596 improved and simplified draftarea browsing --- diff --git a/files/draftfiles.php b/files/draftfiles.php index b79dcc7fb3..d0c1ab15cb 100644 --- a/files/draftfiles.php +++ b/files/draftfiles.php @@ -4,12 +4,9 @@ require_once($CFG->libdir.'/filelib.php'); $itemid = required_param('itemid', PARAM_INT); - $filepath = optional_param('filepath', '/', PARAM_PATH); - $filename = optional_param('filename', '', PARAM_FILE); - $newdirname = optional_param('newdirname', '', PARAM_FILE); - $delete = optional_param('delete', 0, PARAM_BOOL); + $delete = optional_param('delete', '', PARAM_PATH); require_login(); if (isguestuser()) { @@ -24,187 +21,118 @@ $filearea = 'user_draft'; $browser = get_file_browser(); + $fs = get_file_storage(); - if (!$area_info = $browser->get_file_info($context, $filearea, $itemid, '/', null)) { - error('Can not browse this area!'); // TODO: localise - } - - if ($filename === '') { - $filename = null; - } - - $error = ''; - - if ($filepath === '/' and is_null($filename)) { - $file_info = $area_info; - } else { - if (!$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) { - error('Can not browse this directory!'); // TODO: localise - } + if (!$directory = $fs->get_file($context->id, 'user_draft', $itemid, $filepath, '.')) { + $directory = new virtual_root_file($context->id, 'user_draft', $itemid); + $filepath = $directory->get_filepath(); } + $files = $fs->get_directory_files($context->id, 'user_draft', $itemid, $directory->get_filepath()); + $parent = $directory->get_parent_directory(); /// process actions - if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) { - if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) { - $params = $newdir_info->get_params_rawencoded(); - $params = implode('&', $params); - redirect("draftfiles.php?$params"); - } else { - $error = "Could not create new dir"; // TODO: localise - } + if ($newdirname !== '' and data_submitted() and confirm_sesskey()) { + $newdirname = $directory->get_filepath().$newdirname.'/'; + $fs->create_directory($contextid, $filearea, $itemid, $newdirname, $USER->id); + redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($newdirname)); } - if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { + if (isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) { $file = $_FILES['newfile']; $newfilename = clean_param($file['name'], PARAM_FILE); + // TODO: some better error handling or use some upload manager if (is_uploaded_file($_FILES['newfile']['tmp_name'])) { - try { - if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) { - $params = $file_info->get_params_rawencoded(); - $params = implode('&', $params); - redirect("draftfiles.php?$params"); - - } else { - $error = "Could not create upload file"; // TODO: localise - } - } catch (file_exception $e) { - $error = "Exception: Could not create upload file"; // TODO: localise + if ($existingfile = $fs->get_file($contextid, $filearea, $itemid, $filepath, $newfilename)) { + $existingfile->delete(); } + $filerecord = array('contextid'=>$contextid, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, + 'filename'=>$newfilename, 'userid'=>$USER->id); + $newfile = $fs->create_file_from_pathname($filerecord, $_FILES['newfile']['tmp_name']); + redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath)); } } - if ($file_info and $delete) { + if ($delete !== '' and $file = $fs->get_file($contextid, $filearea, $itemid, $filepath, $delete)) { if (!data_submitted() or !confirm_sesskey()) { print_header(); - notify(get_string('deletecheckwarning').': '.$file_info->get_visible_name()); - $parent_info = $file_info->get_parent(); - - $optionsno = $parent_info->get_params(); - $optionsyes = $file_info->get_params(); - $optionsyes['delete'] = 1; - $optionsyes['sesskey'] = sesskey(); - + notify(get_string('deletecheckwarning').': '.s($file->get_filepath().$file->get_filename())); + $optionsno = array('itemid'=>$itemid, 'filepath'=>$filepath); + $optionsyes = array('itemid'=>$itemid, 'filepath'=>$filepath, 'delete'=>$delete, 'sesskey'=>sesskey()); notice_yesno (get_string('deletecheckfiles'), 'draftfiles.php', 'draftfiles.php', $optionsyes, $optionsno, 'post', 'get'); print_footer('empty'); die; - } - if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) { - if (!$file_info->delete()) { - $error = "Could not delete file!"; // TODO: localise + } else { + $isdir = $file->is_directory(); + $file->delete(); + if ($isdir) { + redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($parent->get_filepath())); + } else { + redirect('draftfiles.php?itemid='.$itemid.'&filepath='.rawurlencode($filepath)); } - $params = $parent_info->get_params_rawencoded(); - $params = implode('&', $params); - redirect("draftfiles.php?$params", $error); } } print_header(); - if ($error !== '') { - notify($error); - } - echo '
'; - displaydir($file_info); - echo '
'; - - if ($file_info and $file_info->is_directory() and $file_info->is_writable()) { - - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; - - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
'; - } - - - print_footer('empty'); - -/// FILE FUNCTIONS /////////////////////////////////////////////////////////// -function displaydir($file_info) { - global $CFG; - - $children = $file_info->get_children(); - $parent_info = $file_info->get_parent(); - - $strname = get_string('name'); - $strsize = get_string('size'); - $strmodified = get_string('modified'); $strfolder = get_string('folder'); $strfile = get_string('file'); $strdownload = get_string('download'); $strdelete = get_string('delete'); - $straction = get_string('action'); - - $parentwritable = $file_info->is_writable(); - - $directory = $file_info->get_params(); - $directory = $directory['filepath']; - - if ($parent_info and $directory !== '/') { - $params = $parent_info->get_params_rawencoded(); - $params = implode('&', $params); + if ($parent) { echo '
'; - echo ' '.get_string('parentfolder').''; + echo ' '.get_string('parentfolder').''; echo '
'; } - if ($children) { - foreach ($children as $child_info) { - $filename = $child_info->get_visible_name(); - $filesize = $child_info->get_filesize(); - $filesize = $filesize ? display_size($filesize) : ''; + 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 = $child_info->get_mimetype(); + $mimetype = $file->get_mimetype(); - $params = $child_info->get_params_rawencoded(); - $params = implode('&', $params); + if ($file->is_directory()) { + $dirname = explode('/', trim($filepath, '/')); + $dirname = array_pop($dirname); + echo '
'; + echo "pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($dirname)." "; + echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />"; + echo '
'; - if ($child_info->is_directory()) { + } else { + $icon = mimeinfo_from_type('icon', $mimetype); + $viewurl = $browser->encodepath("$CFG->wwwroot/draftfile.php", "/$contextid/user_draft/$itemid".$filepath.$filename, false, false); + echo '
'; + echo "pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize) "; + echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />";; + echo '
'; + } + } - echo '
'; - echo "pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" /> ".s($filename).""; - if ($parentwritable) { - echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />"; - } - echo '
'; + echo ''; - } else { + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; + + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo '
'; - $icon = mimeinfo_from_type('icon', $mimetype); - echo '
'; - echo "pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" /> ".s($filename)." ($filesize)"; - if ($viewurl = $child_info->get_url()) { - echo " ".link_to_popup_window ($viewurl, "display", - "pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strfile\" /> ", - 480, 640, get_string('viewfileinpopup'), null, true); - } - if ($parentwritable) { - echo "pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" />";; - } - echo '
'; - } - } - } -} + print_footer('empty'); ?>