Skip to content

Commit

Permalink
MDL-16596 basic areafiles formslib element
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 20, 2008
1 parent 11550b8 commit 8546def
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 36 deletions.
7 changes: 5 additions & 2 deletions draftfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
}

switch ($filearea) {
case 'user_draft' : $itemid = (int)array_shift($args); break;
default: send_file_not_found();
case 'user_draft':
$itemid = (int)array_shift($args);
break;
default:
send_file_not_found();
}

$relativepath = '/'.implode('/', $args);
Expand Down
209 changes: 209 additions & 0 deletions files/areafiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?php // $Id$

require('../config.php');
require_once($CFG->libdir.'/filelib.php');

$contextid = required_param('contextid', PARAM_INT);
$filearea = required_param('filearea', PARAM_ALPHAEXT);
$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);

if (!$context = get_context_instance_by_id($contextid)) {
print_error('invalidcontext');
}

require_login();
if (isguestuser()) {
print_error('noguest');
}

$browser = get_file_browser();

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
}
}

/// 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('&amp;', $params);
redirect("areafiles.php?$params");
} else {
$error = "Could not create new dir"; // TODO: localise
}
}

if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) {
$file = $_FILES['newfile'];
$newfilename = clean_param($file['name'], PARAM_FILE);
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('&amp;', $params);
redirect("areafiles.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 ($file_info and $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();

notice_yesno (get_string('deletecheckfiles'), 'areafiles.php', 'areafiles.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
}
$params = $parent_info->get_params_rawencoded();
$params = implode('&amp;', $params);
redirect("areafiles.php?$params", $error);
}
}

print_header();

if ($error !== '') {
notify($error);
}

echo '<div class="areafiles">';
displaydir($file_info);
echo '</div>';

if ($file_info and $file_info->is_directory() and $file_info->is_writable()) {

echo '<form enctype="multipart/form-data" method="post" action="areafiles.php"><div>';
echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input name="newfile" type="file" />';
echo '<input type="submit" value="'.get_string('uploadafile').'" />';
echo '</div></form>';

echo '<form action="areafiles.php" method="post"><div>';
echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="text" name="newdirname" value="" />';
echo '<input type="submit" value="'.get_string('makeafolder').'" />';
echo '</div></form>';
}


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('&amp;', $params);

echo '<div class="folder">';
echo '<a href="areafiles.php?'.$params.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>';
echo '</div>';
}

if ($children) {
foreach ($children as $child_info) {
$filename = $child_info->get_visible_name();
$filesize = $child_info->get_filesize();
$filesize = $filesize ? display_size($filesize) : '';

$mimetype = $child_info->get_mimetype();

$params = $child_info->get_params_rawencoded();
$params = implode('&amp;', $params);

if ($child_info->is_directory()) {

echo '<div class="folder">';
echo "<a href=\"areafiles.php?$params\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".s($filename)."</a>";
if ($parentwritable) {
echo "<a href=\"areafiles.php?$params&amp;sesskey=".sesskey()."&amp;delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";
}
echo '</div>';

} else {

$icon = mimeinfo_from_type('icon', $mimetype);
echo '<div class="file">';
echo "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".s($filename)." ($filesize)";
if ($viewurl = $child_info->get_url()) {
echo "&nbsp;".link_to_popup_window ($viewurl, "display",
"<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strfile\" />&nbsp;",
480, 640, get_string('viewfileinpopup'), null, true);
}
if ($parentwritable) {
echo "<a href=\"areafiles.php?$params&amp;sesskey=".sesskey()."&amp;delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>";;
}
echo '</div>';
}
}
}
}

?>
20 changes: 12 additions & 8 deletions lib/file/file_browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
}
}
$urlbase = $CFG->wwwroot.'/userfile.php';
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);

} else if ($filearea == 'user_profile') {
if (is_null($itemid)) {
Expand Down Expand Up @@ -112,7 +112,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
}
}
$urlbase = $CFG->wwwroot.'/userfile.php';
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false);


} else if ($filearea == 'user_draft') {
Expand All @@ -126,6 +126,10 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
return null;
}
$urlbase = $CFG->wwwroot.'/draftfile.php';

$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;

if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
if ($filepath === '/' and $filename === '.') {
$storedfile = new virtual_root_file($context->id, $filearea, $itemid);
Expand All @@ -134,7 +138,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
return null;
}
}
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
}
}

Expand Down Expand Up @@ -173,7 +177,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
return null;
}
}
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
}
}

Expand Down Expand Up @@ -214,7 +218,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
return null;
}
}
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);

} else if ($filearea == 'course_backup') {
if (!has_capability('moodle/site:backup', $context) and !has_capability('moodle/site:restore', $context)) {
Expand All @@ -233,7 +237,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=

$downloadable = has_capability('moodle/site:backupdownload', $context);
$uploadable = has_capability('moodle/site:backupupload', $context);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable);
return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable, false);

} else if ($filearea == 'course_content') {
if (!has_capability('moodle/course:managefiles', $context)) {
Expand Down Expand Up @@ -307,7 +311,7 @@ public function get_file_info($context, $filearea=null, $itemid=null, $filepath=
return null;
}
}
return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true);
return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false);

} else {
$fileinfofunction = $modname.'_get_file_info';
Expand All @@ -329,7 +333,7 @@ public function build_stored_file_children($context, $filearea, $itemid, $filepa

$storedfiles = $fs->get_directory_files($context->id, $filearea, $itemid, $filepath, false, true, "filepath, filename");
foreach ($storedfiles as $file) {
$result[] = new file_info_stored($this, $context, $file, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess);
$result[] = new file_info_stored($this, $context, $file, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess, false);
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion lib/file/file_info_coursefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class file_info_coursefile extends file_info_stored {
public function __construct($browser, $context, $storedfile) {
global $CFG;
$urlbase = $CFG->wwwroot.'/file.php';
parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true);
parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
}

public function get_url($forcedownload=false, $https=false) {
Expand Down
25 changes: 16 additions & 9 deletions lib/file/file_info_stored.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class file_info_stored extends file_info {
protected $itemidused;
protected $readaccess;
protected $writeaccess;
protected $areaonly;

public function __construct($browser, $context, $storedfile, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess) {
public function __construct($browser, $context, $storedfile, $urlbase, $areavisiblename, $itemidused, $readaccess, $writeaccess, $areaonly) {
parent::__construct($browser, $context);

$this->lf = $storedfile;
Expand All @@ -17,6 +18,7 @@ public function __construct($browser, $context, $storedfile, $urlbase, $areavisi
$this->itemidused = $itemidused;
$this->readaccess = $readaccess;
$this->writeaccess = $writeaccess;
$this->areaonly = $areaonly;
}

public function get_params() {
Expand All @@ -39,7 +41,9 @@ public function get_visible_name() {
$dir = explode('/', $dir);
$dir = array_pop($dir);
if ($dir === '') {
if ($this->itemidused) {
if ($this->areaonly) {
return $this->areavisiblename;
} else if ($this->itemidused) {
return $this->lf->get_itemid();
} else {
return $this->areavisiblename;
Expand Down Expand Up @@ -110,22 +114,25 @@ public function get_children() {
return array();
}
return $this->browser->build_stored_file_children($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(),
$this->urlbase, $this->areavisiblename, $this->itemidused, $this->readaccess, $this->writeaccess);
$this->urlbase, $this->areavisiblename, $this->itemidused, $this->readaccess, $this->writeaccess,
$this->areaonly);
}

public function get_parent() {
if (!$this->lf->is_directory()) {
return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.');
}

if ($this->lf->get_filepath() === '/') {
if ($this->itemidused) {
if ($this->lf->get_filepath() === '/' and $this->lf->is_directory()) {
if ($this->areaonly) {
return null;
} else if ($this->itemidused) {
return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid());
} else {
return $this->browser->get_file_info($this->context, $this->lf->get_filearea());
}
}

if (!$this->lf->is_directory()) {
return $this->browser->get_file_info($this->context, $this->lf->get_filearea(), $this->lf->get_itemid(), $this->lf->get_filepath(), '.');
}

$filepath = $this->lf->get_filepath();
$filepath = trim($filepath, '/');
$dirs = explode('/', $filepath);
Expand Down
Loading

0 comments on commit 8546def

Please sign in to comment.