MDL-31901 fixed forgotten overwrite dialogue in filepicker
[moodle.git] / files / renderer.php
CommitLineData
64f93798
PS
1<?php
2///////////////////////////////////////////////////////////////////////////
3// //
4// This file is part of Moodle - http://moodle.org/ //
5// Moodle - Modular Object-Oriented Dynamic Learning Environment //
6// //
7// Moodle is free software: you can redistribute it and/or modify //
8// it under the terms of the GNU General Public License as published by //
9// the Free Software Foundation, either version 3 of the License, or //
10// (at your option) any later version. //
11// //
12// Moodle is distributed in the hope that it will be useful, //
13// but WITHOUT ANY WARRANTY; without even the implied warranty of //
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15// GNU General Public License for more details. //
16// //
17// You should have received a copy of the GNU General Public License //
18// along with Moodle. If not, see <http://www.gnu.org/licenses/>. //
19// //
20///////////////////////////////////////////////////////////////////////////
21
22defined('MOODLE_INTERNAL') || die();
23
24/**
25 * Rendering of files viewer related widgets.
26 * @package core
27 * @subpackage file
28 * @copyright 2010 Dongsheng Cai
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * @since Moodle 2.0
31 */
32
33/**
e921afa8 34 * File browser render
64f93798
PS
35 *
36 * @copyright 2010 Dongsheng Cai
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.0
39 */
40class core_files_renderer extends plugin_renderer_base {
41
42 public function files_tree_viewer(file_info $file_info, array $options = null) {
43 $tree = new files_tree_viewer($file_info, $options);
44 return $this->render($tree);
45 }
46
47 public function render_files_tree_viewer(files_tree_viewer $tree) {
e0873f13 48 $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
0b259d91
DC
49
50 $html .= $this->output->container_start('coursefilesbreadcrumb');
64f93798
PS
51 foreach($tree->path as $path) {
52 $html .= $path;
53 $html .= ' / ';
54 }
0b259d91 55 $html .= $this->output->container_end();
64f93798 56
0b259d91
DC
57 $html .= $this->output->box_start();
58 $table = new html_table();
276943d4
DC
59 $table->head = array(get_string('filename', 'backup'), get_string('size'), get_string('modified'));
60 $table->align = array('left', 'right', 'right');
0b259d91
DC
61 $table->width = '100%';
62 $table->data = array();
63
64 foreach ($tree->tree as $file) {
65 if (!empty($file['isdir'])) {
66 $table->data[] = array(
67 html_writer::link($file['url'], $this->output->pix_icon('f/folder', 'icon') . ' ' . $file['filename']),
0b259d91 68 '',
276943d4 69 $file['filedate'],
0b259d91
DC
70 );
71 } else {
72 $table->data[] = array(
73 html_writer::link($file['url'], $this->output->pix_icon('f/'.mimeinfo('icon', $file['filename']), get_string('icon')) . ' ' . $file['filename']),
276943d4 74 $file['filesize'],
0b259d91 75 $file['filedate'],
0b259d91 76 );
64f93798 77 }
64f93798 78 }
0b259d91
DC
79
80 $html .= html_writer::table($table);
e921afa8 81 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
0b259d91 82 $html .= $this->output->box_end();
64f93798
PS
83 return $html;
84 }
906e7d89
MG
85
86 /**
87 * Prints the file manager and initializes all necessary libraries
88 *
89 * <pre>
90 * $fm = new form_filemanager($options);
91 * $output = get_renderer('core', 'files');
92 * echo $output->render($fm);
93 * </pre>
94 *
95 * @param form_filemanager $fm File manager to render
96 * @return string HTML fragment
97 */
d1d18691 98 public function render_form_filemanager($fm) {
906e7d89 99 static $filemanagertemplateloaded;
d1d18691 100 $html = $this->fm_print_generallayout($fm);
906e7d89
MG
101 $module = array(
102 'name'=>'form_filemanager',
103 'fullpath'=>'/lib/form/filemanager.js',
4325db53
MG
104 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
105 'strings' => array(
106 array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
107 array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
9a62f779
MG
108 array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
109 array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
110 array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
111 array('confirmrenamefile', 'repository')
906e7d89
MG
112 )
113 );
114 if (empty($filemanagertemplateloaded)) {
115 $filemanagertemplateloaded = true;
116 $this->page->requires->js_init_call('M.form_filemanager.set_templates',
e709ddd2 117 array($this->filemanager_js_templates()), true, $module);
906e7d89
MG
118 }
119 $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
120
121 // non javascript file manager
122 $html .= '<noscript>';
123 $html .= "<div><object type='text/html' data='".$fm->get_nonjsurl()."' height='160' width='600' style='border:1px solid #000'></object></div>";
124 $html .= '</noscript>';
125
126
127 return $html;
128 }
129
130 /**
131 * Returns html for displaying one file manager
132 *
133 * The main element in HTML must have id="filemanager-{$client_id}" and
134 * class="filemanager fm-loading";
135 * After all necessary code on the page (both html and javascript) is loaded,
136 * the class fm-loading will be removed and added class fm-loaded;
137 * The main element (class=filemanager) will be assigned the following classes:
138 * 'fm-maxfiles' - when filemanager has maximum allowed number of files;
139 * 'fm-nofiles' - when filemanager has no files at all (although there might be folders);
140 * 'fm-noitems' - when current view (folder) has no items - neither files nor folders;
141 * 'fm-updating' - when current view is being updated (usually means that loading icon is to be displayed);
142 * 'fm-nomkdir' - when 'Make folder' action is unavailable (empty($fm->options->subdirs) == true)
143 *
144 * Element with class 'filemanager-container' will be holding evens for dnd upload (dragover, etc.).
145 * It will have class:
146 * 'dndupload-ready' - when a file is being dragged over the browser
147 * 'dndupload-over' - when file is being dragged over this filepicker (additional to 'dndupload-ready')
148 * 'dndupload-uploading' - during the upload process (note that after dnd upload process is
149 * over, the file manager will refresh the files list and therefore will have for a while class
150 * fm-updating. Both waiting processes should look similar so the images don't jump for user)
151 *
152 * If browser supports Drag-and-drop, the body element will have class 'dndsupported',
153 * otherwise - 'dndnotsupported';
154 *
e709ddd2
MG
155 * Element with class 'fp-content' will be populated with files list;
156 * Element with class 'fp-btn-add' will hold onclick event for adding a file (opening filepicker);
157 * Element with class 'fp-btn-mkdir' will hold onclick event for adding new folder;
158 * Element with class 'fp-btn-download' will hold onclick event for download action;
159 *
160 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
161 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
162 * Parent element will receive class 'empty' when there are no folders to be displayed;
163 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
164 *
165 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
166 * can be changed or not;
167 * Inside element with class 'fp-viewbar' there are expected elements with classes
168 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
169 * between the view modes, the last clicked element will have the class 'checked';
906e7d89
MG
170 *
171 * @param form_filemanager $fm
172 * @return string
173 */
d1d18691 174 private function fm_print_generallayout($fm) {
906e7d89
MG
175 global $OUTPUT;
176 $options = $fm->options;
177 $client_id = $options->client_id;
178 $straddfile = get_string('addfile', 'repository');
179 $strmakedir = get_string('makeafolder', 'moodle');
180 $strdownload = get_string('downloadfolder', 'repository');
181 $strloading = get_string('loading', 'repository');
182 $strnofilesattached = get_string('nofilesattached', 'repository');
183 $strdroptoupload = get_string('droptoupload', 'moodle');
184 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
d1d18691 185 $restrictions = $this->fm_print_restrictions($fm);
906e7d89
MG
186 $strdndenabled = get_string('dndenabled_insentence', 'moodle').$OUTPUT->help_icon('dndenabled');
187 $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
188 $loading = get_string('loading', 'repository');
189
e709ddd2
MG
190 $html = '
191<div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
420e7bb2 192 <div class="fp-restrictions">
e709ddd2
MG
193 '.$restrictions.'
194 <span class="dndupload-message"> - '.$strdndenabled.' </span>
420e7bb2
BR
195 </div>
196 <div class="fp-navbar">
197 <div class="filemanager-toolbar">
198 <div class="{!}fp-viewbar">
199 <a class="{!}fp-vb-icons" href="#"></a>
200 <a class="{!}fp-vb-details" href="#"></a>
201 <a class="{!}fp-vb-tree" href="#"></a>
202 </div>
203 <div class="fp-toolbar">
204 <div class="{!}fp-btn-add"><a href="#"><img src="'.$this->pix_url('a/add_file').'" /> '.$straddfile.'</a></div>
205 <div class="{!}fp-btn-mkdir"><a href="#"><img src="'.$this->pix_url('a/create_folder').'" /> '.$strmakedir.'</a></div>
206 <div class="{!}fp-btn-download"><a href"#"><img src="'.$this->pix_url('a/download_all').'" /> '.$strdownload.'</a></div>
207 </div>
208 </div>
209 <div class="fp-pathbar">
210 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name"></a></span>
e709ddd2 211 </div>
906e7d89 212 </div>
420e7bb2 213 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
906e7d89 214 <div class="filemanager-container" >
e709ddd2 215 <div class="fm-content-wrapper">
420e7bb2 216 <div class="fp-content"></div>
e709ddd2 217 <div class="fm-empty-container mdl-align">'.$strnofilesattached.'
420e7bb2 218 <span class="dndupload-message">'.$strdndenabledinbox.'</span><div class="dndupload-arrow"></div>
e709ddd2 219 </div>
420e7bb2 220 <div class="dndupload-target">'.$strdroptoupload.'<div class="dndupload-arrow"></div>
e709ddd2 221 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
906e7d89 222 </div>
e709ddd2 223 <div class="filemanager-updating">'.$icon_progress.'</div>
906e7d89 224 </div>
420e7bb2 225</div>';
e709ddd2
MG
226 return preg_replace('/\{\!\}/', '', $html);
227 }
228
229 /**
230 * FileManager JS template for displaying one file in 'icon view' mode.
231 *
23b83009
MG
232 * Except for elements described in fp_js_template_iconfilename, this template may also
233 * contain element with class 'fp-contextmenu'. If context menu is available for this
234 * file, the top element will receive the additional class 'fp-hascontextmenu' and
235 * the element with class 'fp-contextmenu' will hold onclick event for displaying
236 * the context menu.
237 *
e709ddd2
MG
238 * @see fp_js_template_iconfilename()
239 * @return string
240 */
241 private function fm_js_template_iconfilename() {
420e7bb2
BR
242 $rv = '
243<div class="fp-file">
244 <a href="#">
9a62f779 245 <div style="position:relative;">
23b83009 246 <div class="{!}fp-thumbnail"></div>
9a62f779
MG
247 <div class="fp-reficons"></div>
248 </div>
23b83009
MG
249 <div class="{!}fp-filename"></div></a>
250 <a class="{!}fp-contextmenu" href="#">'.$this->pix_icon('i/menu', '▶').'</a>
251</div>';
252 return preg_replace('/\{\!\}/', '', $rv);
e709ddd2
MG
253 }
254
255 /**
256 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
257 *
23b83009
MG
258 * Except for elements described in fp_js_template_listfilename, this template may also
259 * contain element with class 'fp-contextmenu'. If context menu is available for this
260 * file, the top element will receive the additional class 'fp-hascontextmenu' and
261 * the element with class 'fp-contextmenu' will hold onclick event for displaying
262 * the context menu.
263 *
264 * @todo MDL-32736 remove onclick="return false;"
e709ddd2
MG
265 * @see fp_js_template_listfilename()
266 * @return string
267 */
268 private function fm_js_template_listfilename() {
420e7bb2
BR
269 $rv = '
270<span>
271 <span class="{!}fp-icon"></span>
272 <span class="{!}fp-filename"></span>
273 <a class="{!}fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', '▶').'</a>
274</span>';
23b83009 275 return preg_replace('/\{\!\}/', '', $rv);
e709ddd2
MG
276 }
277
694beb54
MG
278 /**
279 * FileManager JS template for displaying 'Make new folder' dialog.
280 *
281 * Must be wrapped in an element, CSS for this element must define width and height of the window;
282 *
283 * Must have one input element with type="text" (for users to enter the new folder name);
284 *
285 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
286 * new folder is about to be created;
287 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
288 *
289 * @return string
290 */
291 private function fm_js_template_mkdir() {
420e7bb2
BR
292 $rv = '
293<div class="fp-mkdir-dlg">
294 <input type="text">
295 <a class="{!}fp-dlg-butcreate fp-panel-button">'.get_string('create').'</a>
694beb54
MG
296</div>';
297 return preg_replace('/\{\!\}/', '', $rv);
298 }
299
300 /**
301 * FileManager JS template for error/info message displayed as a separate popup window.
302 *
303 * @see fp_js_template_message()
304 * @return string
305 */
306 private function fm_js_template_message() {
307 return $this->fp_js_template_message();
308 }
309
e709ddd2
MG
310 /**
311 * FileManager JS template for window with file information/actions.
312 *
55089a9d
MG
313 * All content must be enclosed in one element, CSS for this class must define width and
314 * height of the window;
e709ddd2
MG
315 *
316 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
317 *
318 * Inside the window the elements with the following classnames must be present:
319 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
320 * one input element (or select in case of fp-license and fp-path). They may also have labels.
321 * The elements will be assign with class 'uneditable' and input/select element will become
322 * disabled if they are not applicable for the particular file;
323 *
e0ff0867 324 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
9dbdf31f
MG
325 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
326 * information is unavailable. If there is information available, the content of embedded
327 * element with class 'fp-value' will be substituted with the value;
e709ddd2 328 *
9a62f779
MG
329 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
330 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
331 *
9dbdf31f
MG
332 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
333 * is applicable but not yet loaded the 'fp-original' element receives additional class
334 * 'fp-loading'. The string explaining that XX references exist will replace content of element
335 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
336 *
e709ddd2
MG
337 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
338 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
339 * events (there may be several elements with class 'fp-file-cancel');
340 *
341 * When confirm button is pressed and file is being selected, the top element receives
342 * additional class 'loading'. It is removed when response from server is received.
343 *
344 * When any of the input fields is changed, the top element receives class 'fp-changed';
345 * When current file can be set as main - top element receives class 'fp-cansetmain';
346 * When current file is folder/zip/file - top element receives respectfully class
347 * 'fp-folder'/'fp-zip'/'fp-file';
348 *
349 * @return string
350 */
351 private function fm_js_template_fileselectlayout() {
9a62f779
MG
352 $strloading = get_string('loading', 'repository');
353 $icon_progress = $this->pix_icon('i/loading_small', $strloading).'';
420e7bb2
BR
354 $rv = '
355<div class="filemanager fp-select">
356 <div class="fp-select-loading">
357 <img src="'.$this->pix_url('i/loading').'" />
358 <p>'.get_string('loading', 'repository').'</p>
359 </div>
360 <form>
361 <div><a class="{!}fp-file-download fp-panel-button" href="#">'.get_string('download').'</a>
362 <a class="{!}fp-file-delete fp-panel-button" href="#">'.get_string('delete').'</a>
363 <a class="{!}fp-file-setmain fp-panel-button" href="#">'.get_string('setmainfile', 'repository').'</a>
364 <a class="{!}fp-file-zip fp-panel-button" href="#">'.get_string('zip', 'editor').'</a>
365 <a class="{!}fp-file-unzip fp-panel-button" href="#">'.get_string('unzip').'</a>
366 </div>
367 <div class="fp-hr"></div>
368 <table>
369 <tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
370 <td class="mdl-left"><input type="text"/></td></tr>
371 <tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
372 <td class="mdl-left"><input type="text" /></td></tr>
373 <tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
374 <td class="mdl-left"><select></select></td></tr>
375 <tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
376 <td class="mdl-left"><select></select></td></tr>
377 <tr class="{!}fp-original"><td class="mdl-right"><label>'.get_string('original', 'repository').'</label>:</td>
378 <td class="mdl-left"><span class="fp-originloading">'.$icon_progress.' '.$strloading.'</span><span class="fp-value"/></td></tr>
9dbdf31f
MG
379 <tr class="{!}fp-reflist"><td class="mdl-right"><label>'.get_string('referenceslist', 'repository').'</label>:</td>
380 <td class="mdl-left"><p class="{!}fp-refcount"/><span class="fp-reflistloading">'.$icon_progress.' '.$strloading.'</span><ul class="fp-value"/></td></tr>
420e7bb2
BR
381 </table>
382 </form>
383 <p class="{!}fp-thumbnail"></p>
384 <form>
385 <p class="fp-select-update">
386 <button class="{!}fp-file-update" >'.get_string('update', 'moodle').'</button>
387 <button class="{!}fp-file-cancel" >'.get_string('cancel').'</button>
388 </p>
389 </form>
390 <div class="fp-fileinfo">
391 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"/></div>
392 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"/></div>
393 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"/></div>
394 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"/></div>
395 </div>
e709ddd2
MG
396</div>';
397 return preg_replace('/\{\!\}/', '', $rv);
398 }
399
9a62f779
MG
400 /**
401 * FileManager JS template for popup confirm dialogue window.
402 *
403 * Must have one top element, CSS for this element must define width and height of the window;
404 *
405 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
406 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
407 * hold onclick events;
408 *
409 * @return string
410 */
411 private function fm_js_template_confirmdialog() {
420e7bb2
BR
412 $rv = '
413<div class="filemanager fp-dlg">
414 <div class="{!}fp-dlg-text"></div>
415 <!--<div class="fp-dlg-but"><button class="{!}fp-dlg-butconfirm" >'.get_string('ok').'</button></div>
416 <div class="fp-dlg-but"><button class="{!}fp-dlg-butcancel" >'.get_string('cancel').'</button></div>-->
417 <a class="{!}fp-dlg-butconfirm fp-panel-button" >'.get_string('ok').'</a>
418 <a class="{!}fp-dlg-butcancel fp-panel-button">'.get_string('cancel').'</a>
9a62f779
MG
419</div>';
420 return preg_replace('/\{\!\}/', '', $rv);
421 }
422
e709ddd2
MG
423 /**
424 * Returns all FileManager JavaScript templates as an array.
425 *
426 * @return array
427 */
428 public function filemanager_js_templates() {
429 $class_methods = get_class_methods($this);
430 $templates = array();
431 foreach ($class_methods as $method_name) {
432 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
433 $templates[$matches[1]] = $this->$method_name();
434 }
435 return $templates;
906e7d89
MG
436 }
437
438 /**
439 * Displays restrictions for the file manager
440 *
441 * @param form_filemanager $fm
442 * @return string
443 */
d1d18691 444 private function fm_print_restrictions($fm) {
906e7d89
MG
445 $maxbytes = display_size($fm->options->maxbytes);
446 if (empty($options->maxfiles) || $options->maxfiles == -1) {
447 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
448 //$string['maxfilesize'] = 'Maximum size for new files: {$a}';
449 } else {
450 $strparam = (object)array('size' => $maxbytes, 'attachments' => $options->maxfiles);
451 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
452 //$string['maxsizeandattachments'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}';
453 }
454 // TODO MDL-32020 also should say about 'File types accepted'
455 return '<span>'. $maxsize. '</span>';
456 }
64f93798 457
d1d18691
MG
458 /**
459 * Template for FilePicker with general layout (not QuickUpload).
460 *
461 * Must have one top element containing everything else (recommended <div class="file-picker">),
462 * CSS for this element must define width and height of the filepicker window. Or CSS must
463 * define min-width, max-width, min-height and max-height and in this case the filepicker
464 * window will be resizeable;
465 *
466 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
467 * can be changed or not;
468 * Inside element with class 'fp-viewbar' there are expected elements with classes
469 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
470 * between the view modes, the last clicked element will have the class 'checked';
471 *
472 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
473 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
474 * The currently selected repostory will have class 'active'. Contents of element with class
475 * 'fp-repo-name' will be replaced with repository name, source of image with class
476 * 'fp-repo-icon' will be replaced with repository icon;
477 *
478 * Element with class 'fp-content' is obligatory and will hold the current contents;
479 *
480 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
481 *
e709ddd2 482 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
d1d18691 483 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
d1d18691 484 * Parent element will receive class 'empty' when there are no folders to be displayed;
e709ddd2 485 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
d1d18691
MG
486 *
487 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
488 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
489 *
490 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
491 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
492 * class 'enabled' or 'disabled' if particular repository has this functionality.
493 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
494 * be substituted with the search form returned by repository (in the most cases it
495 * is generated with template core_repository_renderer::repository_default_searchform);
496 * Other elements must have either <a> or <button> element inside, it will hold onclick
497 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
498 * replaced with those specified by repository;
499 *
500 * @return string
501 */
502 private function fp_js_template_generallayout() {
503 $rv = '
504<div class="file-picker fp-generallayout">
e88d5641
BR
505 <div class="fp-repo-area">
506 <ul class="fp-list">
507 <li class="{!}fp-repo"><img class="{!}fp-repo-icon" width="16" height="16" />&nbsp;<a class="{!}fp-repo-name" href="#"></a></li>
508 </ul>
d1d18691 509 </div>
e88d5641 510 <div class="fp-repo-items">
420e7bb2
BR
511 <div class="fp-navbar">
512 <div>
513 <div class="{!}fp-viewbar">
514 <a class="{!}fp-vb-icons" href="#"></a>
515 <a class="{!}fp-vb-details" href="#"></a>
516 <a class="{!}fp-vb-tree" href="#"></a>
517 </div>
518 <div class="{!}fp-toolbar">
519 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
520 <div class="{!}fp-tb-search fp-search"><form/></div>
521 <div class="{!}fp-tb-refresh"><a href="#"><img src="'.$this->pix_url('a/refresh').'" /></a></div>
522 <div class="{!}fp-tb-logout"><img src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
523 <div class="{!}fp-tb-manage"><a href="#"><img src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
524 <div class="{!}fp-tb-help"><a href="#"><img src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
525 </div>
e88d5641
BR
526 </div>
527 <div class="fp-pathbar">
528 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
529 </div>
d1d18691 530 </div>
e88d5641 531 <div class="{!}fp-content"></div>
d1d18691 532 </div>
d1d18691
MG
533</div>';
534 return preg_replace('/\{\!\}/', '', $rv);
535 }
536
537 /**
e709ddd2 538 * FilePicker JS template for displaying one file in 'icon view' mode.
d1d18691
MG
539 *
540 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
541 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
542 * an element will be replaced with an appropriate img;
543 *
544 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
545 * (unless min-width is set in css) and the content of an element will be replaced with filename
546 * supplied by repository;
547 *
e709ddd2
MG
548 * top element(s) will have class fp-folder if the element is a folder;
549 *
550 * List of files will have parent <div> element with class 'fp-iconview'
551 *
d1d18691
MG
552 * @return string
553 */
e709ddd2 554 private function fp_js_template_iconfilename() {
420e7bb2
BR
555 $rv = '
556<a class="fp-file" href="#" >
d1d18691 557 <div class="{!}fp-thumbnail"></div>
420e7bb2 558 <p class="{!}fp-filename"></p>
e88d5641 559</a>';
d1d18691
MG
560 return preg_replace('/\{\!\}/', '', $rv);
561 }
562
563 /**
564 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
565 *
566 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
567 *
568 * content of element with class 'fp-filename' will be replaced with filename supplied by
569 * repository;
570 *
e709ddd2
MG
571 * top element(s) will have class fp-folder if the element is a folder;
572 *
d1d18691
MG
573 * Note that tree view and table view are the YUI widgets and therefore there are no
574 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
575 * fp-tableview (respectfully).
576 *
577 * @return string
578 */
579 private function fp_js_template_listfilename() {
e709ddd2 580 $rv = '<span><span class="{!}fp-icon"></span> <span class="{!}fp-filename"></span></span>';
d1d18691
MG
581 return preg_replace('/\{\!\}/', '', $rv);
582 }
583
584 /**
585 * FilePicker JS template for displaying link/loading progress for fetching of the next page
586 *
587 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
588 *
589 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
590 * class 'loading' during loading of the next page (it is recommended that in this case the link
591 * becomes unavailable). Also must contain one element <a> or <button> that will hold
592 * onclick event for displaying of the next page. The event will be triggered automatically
593 * when user scrolls to this link.
594 *
595 * @return string
596 */
597 private function fp_js_template_nextpage() {
420e7bb2
BR
598 $rv = '
599<div class="{!}fp-nextpage">
600 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
601 <div class="fp-nextpage-loading">
602 <img src="'.$this->pix_url('i/loading').'" />
603 <p>'.get_string('loading', 'repository').'</p>
604 </div>
d1d18691
MG
605</div>';
606 return preg_replace('/\{\!\}/', '', $rv);
607 }
608
609 /**
e709ddd2 610 * FilePicker JS template for window appearing to select a file.
d1d18691 611 *
55089a9d
MG
612 * All content must be enclosed in one element, CSS for this class must define width and
613 * height of the window;
d1d18691
MG
614 *
615 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
616 *
617 * Inside the window the elements with the following classnames must be present:
55089a9d
MG
618 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
619 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
620 * fp-setlicense). They may also have labels.
d1d18691
MG
621 * The elements will be assign with class 'uneditable' and input/select element will become
622 * disabled if they are not applicable for the particular file;
623 *
624 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
e709ddd2
MG
625 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
626 * if information is unavailable. If there is information available, the content of embedded
627 * element with class 'fp-value' will be substituted with the value;
d1d18691
MG
628 *
629 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
630 * onclick events;
631 *
632 * When confirm button is pressed and file is being selected, the top element receives
633 * additional class 'loading'. It is removed when response from server is received.
634 *
635 * @return string
636 */
637 private function fp_js_template_selectlayout() {
420e7bb2
BR
638 $rv = '
639<div class="file-picker fp-select">
640 <div class="fp-select-loading">
641 <img src="'.$this->pix_url('i/loading').'" />
642 <p>'.get_string('loading', 'repository').'</p>
643 </div>
644 <form>
645 <div>
646 <a class="{!}fp-select-confirm fp-panel-button" href="#">'.get_string('getfile', 'repository').'</a>
647 <a class="{!}fp-select-cancel fp-panel-button" href="#">'.get_string('cancel').'</a>
648 </div>
649 <div class="fp-hr"></div>
650 <table>
651
652 <tr class="{!}fp-linktype-2">
653 <td></td>
654 <td class="mdl-left"><input type="radio"/><label>'.get_string('makefileinternal', 'repository').'</label></td></tr>
655 <tr class="{!}fp-linktype-1">
656 <td></td>
657 <td class="mdl-left"><input type="radio"/><label>'.get_string('makefilelink', 'repository').'</label></td></tr>
658 <tr class="{!}fp-linktype-4">
659 <td></td>
660 <td class="mdl-left"><input type="radio"/><label>'.get_string('makefilereference', 'repository').'</label></td></tr>
661 <tr class="{!}fp-saveas">
662 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
663 <td class="mdl-left"><input type="text"/></td></tr>
664 <tr class="{!}fp-setauthor">
665 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
666 <td class="mdl-left"><input type="text" /></td></tr>
667 <tr class="{!}fp-setlicense">
668 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
669 <td class="mdl-left"><select></select></td></tr>
670 </table>
671 </form>
672 <p class="{!}fp-thumbnail"></p>
673 <div class="fp-fileinfo">
674 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"/></div>
675 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"/></div>
676 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"/></div>
677 <div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"/></div>
678 <div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"/></div>
679 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"/></div>
680 </div>
d1d18691
MG
681</div>';
682 return preg_replace('/\{\!\}/', '', $rv);
683 }
684
685 /**
686 * FilePicker JS template for 'Upload file' repository
687 *
688 * Content to display when user chooses 'Upload file' repository (will be nested inside
689 * element with class 'fp-content').
690 *
691 * Must contain form (enctype="multipart/form-data" method="POST")
692 *
693 * The elements with the following classnames must be present:
694 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
695 * one input element (or select in case of fp-setlicense). They may also have labels.
696 *
697 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
698 *
699 * Please note that some fields may be hidden using CSS if this is part of quickupload form
700 *
701 * @return string
702 */
703 private function fp_js_template_uploadform() {
420e7bb2
BR
704 $rv = '
705<div class="fp-upload-form mdl-align">
706 <div class="fp-content-center">
707 <form enctype="multipart/form-data" method="POST">
708 <table >
709 <tr class="{!}fp-file">
710 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
711 <td class="mdl-left"><input type="file"/></td></tr>
712 <tr class="{!}fp-saveas">
713 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
714 <td class="mdl-left"><input type="text"/></td></tr>
715 <tr class="{!}fp-setauthor">
716 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
717 <td class="mdl-left"><input type="text"/></td></tr>
718 <tr class="{!}fp-setlicense">
719 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
720 <td class="mdl-left"><select/></td></tr>
721 </table>
722 </form>
723 <div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
724 </div>
d1d18691
MG
725</div> ';
726 return preg_replace('/\{\!\}/', '', $rv);
727 }
728
729 /**
730 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
731 *
732 * @return string
733 */
734 private function fp_js_template_loading() {
420e7bb2
BR
735 return '
736<div style="text-align:center">
737 <img src="'.$this->pix_url('i/loading').'" />
738 <p>'.get_string('loading', 'repository').'</p>
d1d18691
MG
739</div>';
740 }
741
742 /**
743 * FilePicker JS template for error (inside element with class 'fp-content').
744 *
745 * must have element with class 'fp-error', its content will be replaced with error text
746 * and the error code will be assigned as additional class to this element
747 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
748 *
749 * @return string
750 */
751 private function fp_js_template_error() {
420e7bb2
BR
752 $rv = '
753<div class="fp-content-error" ><div class="{!}fp-error" /></div>';
d1d18691
MG
754 return preg_replace('/\{\!\}/', '', $rv);
755 }
756
757 /**
758 * FilePicker JS template for error/info message displayed as a separate popup window.
759 *
9a62f779 760 * Must be wrapped in one element, CSS for this element must define
d1d18691
MG
761 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
762 * or 'fp-msg-info' depending on message type;
763 *
764 * content of element with class 'fp-msg-text' will be replaced with error/info text;
765 *
766 * element with class 'fp-msg-butok' will hold onclick event
767 *
768 * @return string
769 */
770 private function fp_js_template_message() {
420e7bb2
BR
771 $rv = '
772<div class="file-picker fp-msg">
773 <p class="{!}fp-msg-text"></p>
774 <a class="{!}fp-msg-butok fp-panel-button">'.get_string('ok').'</a>
775</div>';
d1d18691
MG
776 return preg_replace('/\{\!\}/', '', $rv);
777 }
778
779 /**
780 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
781 *
9a62f779 782 * Must have one top element, CSS for this element must define width and height of the window;
d1d18691
MG
783 *
784 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
785 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
786 * hold onclick events;
787 *
788 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
789 * (Note that it may have long text)
790 *
791 * @return string
792 */
793 private function fp_js_template_processexistingfile() {
420e7bb2
BR
794 $rv = '
795<div class="file-picker fp-dlg">
796 <p class="{!}fp-dlg-text"></p>
797 <a class="{!}fp-dlg-butoverwrite fp-panel-button" >'.get_string('overwrite', 'repository').'</a>
798 <a class="{!}fp-dlg-butcancel fp-panel-button" >'.get_string('cancel').'</a>
799 <a class="{!}fp-dlg-butrename fp-panel-button" />
d1d18691
MG
800</div>';
801 return preg_replace('/\{\!\}/', '', $rv);
802 }
420e7bb2 803
d1d18691
MG
804 /**
805 * FilePicker JS template for repository login form including templates for each element type
806 *
807 * Must contain one <form> element with templates for different input types inside:
808 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
809 * 'fp-login-input' are templates for displaying respective login form elements. Inside
810 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
811 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
812 * They may also contain the <label> element and it's content will be substituted with
813 * label;
814 *
815 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
816 * but if they are not found, 'fp-login-input' will be used;
817 *
818 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
819 * it should hava a template for one radio input (with class 'fp-login-radio');
820 *
821 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
822 * will be removed if at least one popup element is present;
823 *
824 * @return string
825 */
826 private function fp_js_template_loginform() {
827 $rv = '
828<div class="fp-login-form">
420e7bb2
BR
829 <div class="fp-content-center">
830 <form>
831 <table >
832 <tr class="{!}fp-login-popup">
833 <td colspan="2">
834 <label>'.get_string('popup', 'repository').'</label>
835 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p></td></tr>
836 <tr class="{!}fp-login-textarea">
837 <td colspan="2"><p><textarea></textarea></p></td></tr>
838 <tr class="{!}fp-login-select">
839 <td align="right"><label></label></td>
840 <td align="left"><select></select></td></tr>
841 <tr class="{!}fp-login-input">
842 <td class="label"><label /></td>
843 <td class="input"><input/></td></tr>
844 <tr class="{!}fp-login-radiogroup">
845 <td align="right" width="30%" valign="top"><label /></td>
846 <td align="left" valign="top"><p class="{!}fp-login-radio"><input /> <label /></p></td></tr>
847 </table>
848 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
849 </form>
850 </div>
d1d18691
MG
851</div>';
852 return preg_replace('/\{\!\}/', '', $rv);
853 }
854
855 /**
856 * Returns all FilePicker JavaScript templates as an array.
857 *
858 * @return array
859 */
860 public function filepicker_js_templates() {
861 $class_methods = get_class_methods($this);
862 $templates = array();
863 foreach ($class_methods as $method_name) {
864 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
865 $templates[$matches[1]] = $this->$method_name();
866 }
867 return $templates;
868 }
869
870 /**
871 * Returns HTML for default repository searchform to be passed to Filepicker
872 *
873 * This will be used as contents for search form defined in generallayout template
874 * (form with id {TOOLSEARCHID}).
875 * Default contents is one text input field with name="s"
876 */
877 public function repository_default_searchform() {
e88d5641 878 $str = '<input class="search-entry" name="s" value="Search" />';
d1d18691
MG
879 return $str;
880 }
881}
64f93798
PS
882
883/**
884 * Data structure representing a general moodle file tree viewer
885 *
886 * @copyright 2010 Dongsheng Cai
887 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
888 * @since Moodle 2.0
889 */
890class files_tree_viewer implements renderable {
891 public $tree;
892 public $path;
e921afa8 893 public $context;
64f93798
PS
894
895 /**
896 * Constructor of moodle_file_tree_viewer class
897 * @param file_info $file_info
898 * @param array $options
899 */
900 public function __construct(file_info $file_info, array $options = null) {
901 global $CFG;
902
903 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 904 $this->options = (array)$options;
e921afa8
DC
905 $this->context = $options['context'];
906
64f93798
PS
907 $this->tree = array();
908 $children = $file_info->get_children();
0b259d91 909 $current_file_params = $file_info->get_params();
64f93798 910 $parent_info = $file_info->get_parent();
64f93798
PS
911 $level = $parent_info;
912 $this->path = array();
913 while ($level) {
914 $params = $level->get_params();
915 $context = get_context_instance_by_id($params['contextid']);
0b259d91
DC
916 // $this->context is current context
917 if ($context->id != $this->context->id or empty($params['filearea'])) {
64f93798
PS
918 break;
919 }
e921afa8
DC
920 // unset unused parameters
921 unset($params['component']);
922 unset($params['filearea']);
0b259d91 923 unset($params['filename']);
e921afa8 924 unset($params['itemid']);
64f93798 925 $url = new moodle_url('/files/index.php', $params);
e921afa8 926 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
927 $level = $level->get_parent();
928 }
929 $this->path = array_reverse($this->path);
0b259d91
DC
930 if ($current_file_params['filepath'] != '/') {
931 $this->path[] = $file_info->get_visible_name();
932 }
64f93798
PS
933
934 foreach ($children as $child) {
935 $filedate = $child->get_timemodified();
936 $filesize = $child->get_filesize();
937 $mimetype = $child->get_mimetype();
938 $params = $child->get_params();
0b259d91
DC
939 unset($params['component']);
940 unset($params['filearea']);
941 unset($params['filename']);
942 unset($params['itemid']);
64f93798
PS
943 $fileitem = array(
944 'params' => $params,
945 'filename' => $child->get_visible_name(),
946 'filedate' => $filedate ? userdate($filedate) : '',
947 'filesize' => $filesize ? display_size($filesize) : ''
948 );
e921afa8 949 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
950 if ($child->is_directory()) {
951 $fileitem['isdir'] = true;
952 $fileitem['url'] = $url->out(false);
64f93798
PS
953 } else {
954 $fileitem['url'] = $child->get_url();
955 }
956 $this->tree[] = $fileitem;
957 }
958 }
959}