Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[moodle.git] / repository / draftfiles_manager.php
CommitLineData
563d0417
DC
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17//
18
19
20//
21// NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!!
22//
23
24
25/**
26 * This file is used to manage draft files in non-javascript browsers
27 *
28 * @since 2.0
5d354ded 29 * @package core
563d0417 30 * @subpackage repository
5d354ded
PS
31 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
563d0417
DC
33 */
34
35require_once('../config.php');
36require_once($CFG->libdir.'/filelib.php');
37require_once('lib.php');
38
71267723 39require_sesskey();
563d0417
DC
40require_login();
41
42// disable blocks in this page
43$PAGE->set_pagelayout('embedded');
44
45// general parameters
46$action = optional_param('action', '', PARAM_ALPHA);
47$itemid = optional_param('itemid', '', PARAM_INT);
48
49// parameters for repository
50$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
51$courseid = optional_param('course', SITEID, PARAM_INT); // course ID
52$env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
53$filename = optional_param('filename', '', PARAM_FILE);
54$targetpath = optional_param('targetpath', '', PARAM_PATH);
55$maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles
56$maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes
57$subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes
58
59// draft area
60$newdirname = optional_param('newdirname', '', PARAM_FILE);
61$newfilename = optional_param('newfilename', '', PARAM_FILE);
62// path in draft area
63$draftpath = optional_param('draftpath', '/', PARAM_PATH);
64
65// user context
66$user_context = get_context_instance(CONTEXT_USER, $USER->id);
67
68
69$PAGE->set_context($user_context);
70
71$fs = get_file_storage();
72
71267723 73$params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
563d0417
DC
74$PAGE->set_url('/repository/draftfiles_manager.php', $params);
75$filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
76
77$params['action'] = 'browse';
78$home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
79
80switch ($action) {
81
82 // delete draft files
83case 'deletedraft':
84 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
85 if ($file->is_directory()) {
86 $pathname = $draftpath;
87 if ($file->get_parent_directory()) {
88 $draftpath = $file->get_parent_directory()->get_filepath();
89 } else {
90 $draftpath = '/';
91 }
92
93 // delete files in folder
94 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
95 foreach ($files as $storedfile) {
96 $storedfile->delete();
97 }
98 $file->delete();
99 } else {
100 $file->delete();
101 }
102 $home_url->param('draftpath', $draftpath);
103 $home_url->param('action', 'browse');
104 redirect($home_url);
105 }
106 break;
107
108case 'renameform':
109 echo $OUTPUT->header();
110 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
111 $home_url->param('draftpath', $draftpath);
112 $home_url->param('action', 'rename');
113 echo ' <form method="post" action="'.$home_url->out().'">';
71267723
DC
114 echo ' <input name="newfilename" type="text" value="'.s($filename).'" />';
115 echo ' <input name="filename" type="hidden" value="'.s($filename).'" />';
116 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
117 echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />';
563d0417
DC
118 echo ' </form>';
119 echo $OUTPUT->footer();
120 break;
121
122case 'rename':
123
124 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) {
125 print_error('fileexists');
126 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
127 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
128 $file->delete();
129 }
130
131 $home_url->param('action', 'browse');
132 $home_url->param('draftpath', $draftpath);
133 redirect($home_url);
134 break;
135
136case 'downloaddir':
137 $zipper = new zip_packer();
138
139 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
140 if ($file->get_parent_directory()) {
141 $parent_path = $file->get_parent_directory()->get_filepath();
142 $filename = trim($draftpath, '/').'.zip';
143 } else {
144 $parent_path = '/';
145 $filename = get_string('files').'.zip';
146 }
147
148 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) {
149 $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out();
150 header('Location: ' . $fileurl );
151 } else {
152 print_error('cannotdownloaddir', 'repository');
153 }
154 break;
155
156case 'zip':
157 $zipper = new zip_packer();
158
159 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
160 if (!$file->get_parent_directory()) {
161 $parent_path = '/';
162 $filename = get_string('files').'.zip';
163 } else {
164 $parent_path = $file->get_parent_directory()->get_filepath();
165 $filepath = explode('/', trim($file->get_filepath(), '/'));
166 $filename = array_pop($filepath).'.zip';
167 }
168
169 $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
170
171 $home_url->param('action', 'browse');
172 $home_url->param('draftpath', $parent_path);
173 redirect($home_url, get_string('ziped', 'repository'));
174 break;
175
176case 'unzip':
177 $zipper = new zip_packer();
178 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
179
180 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
181 $str = get_string('unzipped', 'repository');
182 } else {
183 $str = get_string('cannotunzip', 'error');
184 }
185 $home_url->param('action', 'browse');
186 $home_url->param('draftpath', $draftpath);
187 redirect($home_url, $str);
188 break;
189
190case 'movefile':
191 if (!empty($targetpath)) {
192 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) {
193 print_error('cannotmovefile');
194 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
195 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file);
196 $file->delete();
197 } else {
198 var_dump('cannot find file');
199 die;
200 }
201 $home_url->param('action', 'browse');
202 $home_url->param('draftpath', $targetpath);
203 redirect($home_url);
204 }
205 echo $OUTPUT->header();
71267723
DC
206
207 echo $OUTPUT->container_start();
208 echo html_writer::link($home_url, get_string('back', 'repository'));
209 echo $OUTPUT->container_end();
210
6bdfef5d 211 $data = new stdClass();
563d0417
DC
212 $home_url->param('action', 'movefile');
213 $home_url->param('draftpath', $draftpath);
214 $home_url->param('filename', $filename);
215 file_get_drafarea_folders($itemid, '/', $data);
216 print_draft_area_tree($data, true, $home_url);
217 echo $OUTPUT->footer();
218 break;
219
220case 'mkdirform':
221 echo $OUTPUT->header();
71267723
DC
222
223 echo $OUTPUT->container_start();
224 echo html_writer::link($home_url, get_string('back', 'repository'));
225 echo $OUTPUT->container_end();
226
563d0417
DC
227 $home_url->param('draftpath', $draftpath);
228 $home_url->param('action', 'mkdir');
229 echo ' <form method="post" action="'.$home_url->out().'">';
230 echo ' <input name="newdirname" type="text" />';
71267723
DC
231 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
232 echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
563d0417
DC
233 echo ' </form>';
234 echo $OUTPUT->footer();
235 break;
236
237case 'mkdir':
238
239 $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
240 $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
241 $home_url->param('action', 'browse');
242 if (!empty($newdirname)) {
243 $home_url->param('draftpath', $newfolderpath);
244 $str = get_string('createfoldersuccess', 'repository');
245 } else {
246 $home_url->param('draftpath', $draftpath);
247 $str = get_string('createfolderfail', 'repository');
248 }
249 redirect($home_url, $str);
250 break;
251
252case 'browse':
253default:
254 $files = file_get_drafarea_files($itemid, $draftpath);
255 $info = file_get_draft_area_info($itemid);
256 $filecount = $info['filecount'];
257
258 echo $OUTPUT->header();
259 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
260 echo '<div class="fm-breadcrumb">';
261 $home_url->param('action', 'browse');
262 $home_url->param('draftpath', '/');
263 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
264 $trail = '';
265 if ($draftpath !== '/') {
266 $path = '/' . trim($draftpath, '/') . '/';
267 $parts = explode('/', $path);
268 foreach ($parts as $part) {
269 if (!empty($part)) {
270 $trail .= ('/'.$part.'/');
271 $data->path[] = array('name'=>$part, 'path'=>$trail);
272 $home_url->param('draftpath', $trail);
273 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
274 }
275 }
276 }
277 echo '</div>';
278 }
279
280 $filepicker_url->param('draftpath', $draftpath);
281 $filepicker_url->param('savepath', $draftpath);
282 $filepicker_url->param('action', 'plugins');
283 echo '<div class="filemanager-toolbar">';
284 if ($env == 'filepicker') {
285 $maxfiles = 1;
286 }
bf1873c6 287 if ($filecount < $maxfiles || $maxfiles == -1) {
563d0417
DC
288 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
289 }
290 if ($env == 'filemanager') {
291 if (!empty($subdirs)) {
292 $home_url->param('action', 'mkdirform');
293 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
294 }
295 $home_url->param('action', 'downloaddir');
71267723 296 echo html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
563d0417
DC
297 }
298 echo '</div>';
299
300 if (!empty($files->list)) {
301 echo '<ul>';
302 foreach ($files->list as $file) {
303 if ($file->type != 'folder') {
304 $drafturl = $file->url;
305 // a file
ede72522
PS
306 $fileicon = $OUTPUT->pix_url(file_extension_icon($file->filename))->out(false);
307 $type = mimeinfo('icon', $file->filename);
563d0417
DC
308 echo '<li>';
309 echo '<img src="'.$fileicon. '" class="iconsmall" />';
71267723 310 echo html_writer::link($drafturl, $file->filename);
563d0417
DC
311
312 $home_url->param('filename', $file->filename);
313 $home_url->param('draftpath', $file->filepath);
314
315 $home_url->param('action', 'deletedraft');
316 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
317
318 $home_url->param('action', 'movefile');
319 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
320
321 $home_url->param('action', 'renameform');
322 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
323
324 if ($type == 'zip') {
325 $home_url->param('action', 'unzip');
326 $home_url->param('draftpath', $file->filepath);
327 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
328 }
329
330 echo '</li>';
331 } else {
332 // a folder
333 echo '<li>';
334 echo '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="iconsmall" />';
335
336 $home_url->param('action', 'browse');
337 $home_url->param('draftpath', $file->filepath);
338 $foldername = trim(array_pop(explode('/', trim($file->filepath, '/'))), '/');
71267723 339 echo html_writer::link($home_url, $foldername);
563d0417
DC
340
341 $home_url->param('draftpath', $file->filepath);
342 $home_url->param('filename', $file->filename);
343 $home_url->param('action', 'deletedraft');
344 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
345
346 $home_url->param('action', 'zip');
347 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
348 echo '</li>';
349 }
350 }
351 echo '</ul>';
352 } else {
353 echo get_string('nofilesavailable', 'repository');
354 }
355 echo $OUTPUT->footer();
356 break;
357}
358
359function print_draft_area_tree($tree, $root, $url) {
360 echo '<ul>';
361 if ($root) {
362 $url->param('targetpath', '/');
363 if ($url->param('draftpath') == '/') {
364 echo '<li>'.get_string('files').'</li>';
365 } else {
366 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
367 }
368 echo '<ul>';
369 if (isset($tree->children)) {
370 $tree = $tree->children;
371 }
372 }
373
374 if (!empty($tree)) {
375 foreach ($tree as $node) {
376 echo '<li>';
377 $url->param('targetpath', $node->filepath);
378 if ($url->param('draftpath') != $node->filepath) {
379 echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
380 } else {
381 echo $node->fullname;
382 }
383 echo '</li>';
384 if (!empty($node->children)) {
385 print_draft_area_tree($node->children, false, $url);
386 }
387 }
388 }
389 if ($root) {
390 echo '</ul>';
391 }
392 echo '</ul>';
393}