MDL-31901 Styled File Manager Icon view, Table view and Tree view
[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',
e709ddd2
MG
325 * 'fp-size', 'fp-dimensions'. They will receive additional class 'fp-unknown' if information
326 * is unavailable. If there is information available, the content of embedded element
327 * with class 'fp-value' will be substituted with the value;
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 *
e709ddd2
MG
332 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
333 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
334 * events (there may be several elements with class 'fp-file-cancel');
335 *
336 * When confirm button is pressed and file is being selected, the top element receives
337 * additional class 'loading'. It is removed when response from server is received.
338 *
339 * When any of the input fields is changed, the top element receives class 'fp-changed';
340 * When current file can be set as main - top element receives class 'fp-cansetmain';
341 * When current file is folder/zip/file - top element receives respectfully class
342 * 'fp-folder'/'fp-zip'/'fp-file';
343 *
344 * @return string
345 */
346 private function fm_js_template_fileselectlayout() {
9a62f779
MG
347 $strloading = get_string('loading', 'repository');
348 $icon_progress = $this->pix_icon('i/loading_small', $strloading).'';
420e7bb2
BR
349 $rv = '
350<div class="filemanager fp-select">
351 <div class="fp-select-loading">
352 <img src="'.$this->pix_url('i/loading').'" />
353 <p>'.get_string('loading', 'repository').'</p>
354 </div>
355 <form>
356 <div><a class="{!}fp-file-download fp-panel-button" href="#">'.get_string('download').'</a>
357 <a class="{!}fp-file-delete fp-panel-button" href="#">'.get_string('delete').'</a>
358 <a class="{!}fp-file-setmain fp-panel-button" href="#">'.get_string('setmainfile', 'repository').'</a>
359 <a class="{!}fp-file-zip fp-panel-button" href="#">'.get_string('zip', 'editor').'</a>
360 <a class="{!}fp-file-unzip fp-panel-button" href="#">'.get_string('unzip').'</a>
361 </div>
362 <div class="fp-hr"></div>
363 <table>
364 <tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
365 <td class="mdl-left"><input type="text"/></td></tr>
366 <tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
367 <td class="mdl-left"><input type="text" /></td></tr>
368 <tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
369 <td class="mdl-left"><select></select></td></tr>
370 <tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
371 <td class="mdl-left"><select></select></td></tr>
372 <tr class="{!}fp-original"><td class="mdl-right"><label>'.get_string('original', 'repository').'</label>:</td>
373 <td class="mdl-left"><span class="fp-originloading">'.$icon_progress.' '.$strloading.'</span><span class="fp-value"/></td></tr>
374 </table>
375 </form>
376 <p class="{!}fp-thumbnail"></p>
377 <form>
378 <p class="fp-select-update">
379 <button class="{!}fp-file-update" >'.get_string('update', 'moodle').'</button>
380 <button class="{!}fp-file-cancel" >'.get_string('cancel').'</button>
381 </p>
382 </form>
383 <div class="fp-fileinfo">
384 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"/></div>
385 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"/></div>
386 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"/></div>
387 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"/></div>
388 </div>
e709ddd2
MG
389</div>';
390 return preg_replace('/\{\!\}/', '', $rv);
391 }
392
9a62f779
MG
393 /**
394 * FileManager JS template for popup confirm dialogue window.
395 *
396 * Must have one top element, CSS for this element must define width and height of the window;
397 *
398 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
399 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
400 * hold onclick events;
401 *
402 * @return string
403 */
404 private function fm_js_template_confirmdialog() {
420e7bb2
BR
405 $rv = '
406<div class="filemanager fp-dlg">
407 <div class="{!}fp-dlg-text"></div>
408 <!--<div class="fp-dlg-but"><button class="{!}fp-dlg-butconfirm" >'.get_string('ok').'</button></div>
409 <div class="fp-dlg-but"><button class="{!}fp-dlg-butcancel" >'.get_string('cancel').'</button></div>-->
410 <a class="{!}fp-dlg-butconfirm fp-panel-button" >'.get_string('ok').'</a>
411 <a class="{!}fp-dlg-butcancel fp-panel-button">'.get_string('cancel').'</a>
9a62f779
MG
412</div>';
413 return preg_replace('/\{\!\}/', '', $rv);
414 }
415
e709ddd2
MG
416 /**
417 * Returns all FileManager JavaScript templates as an array.
418 *
419 * @return array
420 */
421 public function filemanager_js_templates() {
422 $class_methods = get_class_methods($this);
423 $templates = array();
424 foreach ($class_methods as $method_name) {
425 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
426 $templates[$matches[1]] = $this->$method_name();
427 }
428 return $templates;
906e7d89
MG
429 }
430
431 /**
432 * Displays restrictions for the file manager
433 *
434 * @param form_filemanager $fm
435 * @return string
436 */
d1d18691 437 private function fm_print_restrictions($fm) {
906e7d89
MG
438 $maxbytes = display_size($fm->options->maxbytes);
439 if (empty($options->maxfiles) || $options->maxfiles == -1) {
440 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
441 //$string['maxfilesize'] = 'Maximum size for new files: {$a}';
442 } else {
443 $strparam = (object)array('size' => $maxbytes, 'attachments' => $options->maxfiles);
444 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
445 //$string['maxsizeandattachments'] = 'Maximum size for new files: {$a->size}, maximum attachments: {$a->attachments}';
446 }
447 // TODO MDL-32020 also should say about 'File types accepted'
448 return '<span>'. $maxsize. '</span>';
449 }
64f93798 450
d1d18691
MG
451 /**
452 * Template for FilePicker with general layout (not QuickUpload).
453 *
454 * Must have one top element containing everything else (recommended <div class="file-picker">),
455 * CSS for this element must define width and height of the filepicker window. Or CSS must
456 * define min-width, max-width, min-height and max-height and in this case the filepicker
457 * window will be resizeable;
458 *
459 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
460 * can be changed or not;
461 * Inside element with class 'fp-viewbar' there are expected elements with classes
462 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
463 * between the view modes, the last clicked element will have the class 'checked';
464 *
465 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
466 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
467 * The currently selected repostory will have class 'active'. Contents of element with class
468 * 'fp-repo-name' will be replaced with repository name, source of image with class
469 * 'fp-repo-icon' will be replaced with repository icon;
470 *
471 * Element with class 'fp-content' is obligatory and will hold the current contents;
472 *
473 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
474 *
e709ddd2 475 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
d1d18691 476 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
d1d18691 477 * Parent element will receive class 'empty' when there are no folders to be displayed;
e709ddd2 478 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
d1d18691
MG
479 *
480 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
481 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
482 *
483 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
484 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
485 * class 'enabled' or 'disabled' if particular repository has this functionality.
486 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
487 * be substituted with the search form returned by repository (in the most cases it
488 * is generated with template core_repository_renderer::repository_default_searchform);
489 * Other elements must have either <a> or <button> element inside, it will hold onclick
490 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
491 * replaced with those specified by repository;
492 *
493 * @return string
494 */
495 private function fp_js_template_generallayout() {
496 $rv = '
497<div class="file-picker fp-generallayout">
e88d5641
BR
498 <div class="fp-repo-area">
499 <ul class="fp-list">
500 <li class="{!}fp-repo"><img class="{!}fp-repo-icon" width="16" height="16" />&nbsp;<a class="{!}fp-repo-name" href="#"></a></li>
501 </ul>
d1d18691 502 </div>
e88d5641 503 <div class="fp-repo-items">
420e7bb2
BR
504 <div class="fp-navbar">
505 <div>
506 <div class="{!}fp-viewbar">
507 <a class="{!}fp-vb-icons" href="#"></a>
508 <a class="{!}fp-vb-details" href="#"></a>
509 <a class="{!}fp-vb-tree" href="#"></a>
510 </div>
511 <div class="{!}fp-toolbar">
512 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
513 <div class="{!}fp-tb-search fp-search"><form/></div>
514 <div class="{!}fp-tb-refresh"><a href="#"><img src="'.$this->pix_url('a/refresh').'" /></a></div>
515 <div class="{!}fp-tb-logout"><img src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
516 <div class="{!}fp-tb-manage"><a href="#"><img src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
517 <div class="{!}fp-tb-help"><a href="#"><img src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
518 </div>
e88d5641
BR
519 </div>
520 <div class="fp-pathbar">
521 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
522 </div>
d1d18691 523 </div>
e88d5641 524 <div class="{!}fp-content"></div>
d1d18691 525 </div>
d1d18691
MG
526</div>';
527 return preg_replace('/\{\!\}/', '', $rv);
528 }
529
530 /**
e709ddd2 531 * FilePicker JS template for displaying one file in 'icon view' mode.
d1d18691
MG
532 *
533 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
534 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
535 * an element will be replaced with an appropriate img;
536 *
537 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
538 * (unless min-width is set in css) and the content of an element will be replaced with filename
539 * supplied by repository;
540 *
e709ddd2
MG
541 * top element(s) will have class fp-folder if the element is a folder;
542 *
543 * List of files will have parent <div> element with class 'fp-iconview'
544 *
d1d18691
MG
545 * @return string
546 */
e709ddd2 547 private function fp_js_template_iconfilename() {
420e7bb2
BR
548 $rv = '
549<a class="fp-file" href="#" >
d1d18691 550 <div class="{!}fp-thumbnail"></div>
420e7bb2 551 <p class="{!}fp-filename"></p>
e88d5641 552</a>';
d1d18691
MG
553 return preg_replace('/\{\!\}/', '', $rv);
554 }
555
556 /**
557 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
558 *
559 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
560 *
561 * content of element with class 'fp-filename' will be replaced with filename supplied by
562 * repository;
563 *
e709ddd2
MG
564 * top element(s) will have class fp-folder if the element is a folder;
565 *
d1d18691
MG
566 * Note that tree view and table view are the YUI widgets and therefore there are no
567 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
568 * fp-tableview (respectfully).
569 *
570 * @return string
571 */
572 private function fp_js_template_listfilename() {
e709ddd2 573 $rv = '<span><span class="{!}fp-icon"></span> <span class="{!}fp-filename"></span></span>';
d1d18691
MG
574 return preg_replace('/\{\!\}/', '', $rv);
575 }
576
577 /**
578 * FilePicker JS template for displaying link/loading progress for fetching of the next page
579 *
580 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
581 *
582 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
583 * class 'loading' during loading of the next page (it is recommended that in this case the link
584 * becomes unavailable). Also must contain one element <a> or <button> that will hold
585 * onclick event for displaying of the next page. The event will be triggered automatically
586 * when user scrolls to this link.
587 *
588 * @return string
589 */
590 private function fp_js_template_nextpage() {
420e7bb2
BR
591 $rv = '
592<div class="{!}fp-nextpage">
593 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
594 <div class="fp-nextpage-loading">
595 <img src="'.$this->pix_url('i/loading').'" />
596 <p>'.get_string('loading', 'repository').'</p>
597 </div>
d1d18691
MG
598</div>';
599 return preg_replace('/\{\!\}/', '', $rv);
600 }
601
602 /**
e709ddd2 603 * FilePicker JS template for window appearing to select a file.
d1d18691 604 *
55089a9d
MG
605 * All content must be enclosed in one element, CSS for this class must define width and
606 * height of the window;
d1d18691
MG
607 *
608 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
609 *
610 * Inside the window the elements with the following classnames must be present:
55089a9d
MG
611 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
612 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
613 * fp-setlicense). They may also have labels.
d1d18691
MG
614 * The elements will be assign with class 'uneditable' and input/select element will become
615 * disabled if they are not applicable for the particular file;
616 *
617 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
e709ddd2
MG
618 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
619 * if information is unavailable. If there is information available, the content of embedded
620 * element with class 'fp-value' will be substituted with the value;
d1d18691
MG
621 *
622 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
623 * onclick events;
624 *
625 * When confirm button is pressed and file is being selected, the top element receives
626 * additional class 'loading'. It is removed when response from server is received.
627 *
628 * @return string
629 */
630 private function fp_js_template_selectlayout() {
420e7bb2
BR
631 $rv = '
632<div class="file-picker fp-select">
633 <div class="fp-select-loading">
634 <img src="'.$this->pix_url('i/loading').'" />
635 <p>'.get_string('loading', 'repository').'</p>
636 </div>
637 <form>
638 <div>
639 <a class="{!}fp-select-confirm fp-panel-button" href="#">'.get_string('getfile', 'repository').'</a>
640 <a class="{!}fp-select-cancel fp-panel-button" href="#">'.get_string('cancel').'</a>
641 </div>
642 <div class="fp-hr"></div>
643 <table>
644
645 <tr class="{!}fp-linktype-2">
646 <td></td>
647 <td class="mdl-left"><input type="radio"/><label>'.get_string('makefileinternal', 'repository').'</label></td></tr>
648 <tr class="{!}fp-linktype-1">
649 <td></td>
650 <td class="mdl-left"><input type="radio"/><label>'.get_string('makefilelink', 'repository').'</label></td></tr>
651 <tr class="{!}fp-linktype-4">
652 <td></td>
653 <td class="mdl-left"><input type="radio"/><label>'.get_string('makefilereference', 'repository').'</label></td></tr>
654 <tr class="{!}fp-saveas">
655 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
656 <td class="mdl-left"><input type="text"/></td></tr>
657 <tr class="{!}fp-setauthor">
658 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
659 <td class="mdl-left"><input type="text" /></td></tr>
660 <tr class="{!}fp-setlicense">
661 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
662 <td class="mdl-left"><select></select></td></tr>
663 </table>
664 </form>
665 <p class="{!}fp-thumbnail"></p>
666 <div class="fp-fileinfo">
667 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"/></div>
668 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"/></div>
669 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"/></div>
670 <div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"/></div>
671 <div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"/></div>
672 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"/></div>
673 </div>
d1d18691
MG
674</div>';
675 return preg_replace('/\{\!\}/', '', $rv);
676 }
677
678 /**
679 * FilePicker JS template for 'Upload file' repository
680 *
681 * Content to display when user chooses 'Upload file' repository (will be nested inside
682 * element with class 'fp-content').
683 *
684 * Must contain form (enctype="multipart/form-data" method="POST")
685 *
686 * The elements with the following classnames must be present:
687 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
688 * one input element (or select in case of fp-setlicense). They may also have labels.
689 *
690 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
691 *
692 * Please note that some fields may be hidden using CSS if this is part of quickupload form
693 *
694 * @return string
695 */
696 private function fp_js_template_uploadform() {
420e7bb2
BR
697 $rv = '
698<div class="fp-upload-form mdl-align">
699 <div class="fp-content-center">
700 <form enctype="multipart/form-data" method="POST">
701 <table >
702 <tr class="{!}fp-file">
703 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
704 <td class="mdl-left"><input type="file"/></td></tr>
705 <tr class="{!}fp-saveas">
706 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
707 <td class="mdl-left"><input type="text"/></td></tr>
708 <tr class="{!}fp-setauthor">
709 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
710 <td class="mdl-left"><input type="text"/></td></tr>
711 <tr class="{!}fp-setlicense">
712 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
713 <td class="mdl-left"><select/></td></tr>
714 </table>
715 </form>
716 <div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
717 </div>
d1d18691
MG
718</div> ';
719 return preg_replace('/\{\!\}/', '', $rv);
720 }
721
722 /**
723 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
724 *
725 * @return string
726 */
727 private function fp_js_template_loading() {
420e7bb2
BR
728 return '
729<div style="text-align:center">
730 <img src="'.$this->pix_url('i/loading').'" />
731 <p>'.get_string('loading', 'repository').'</p>
d1d18691
MG
732</div>';
733 }
734
735 /**
736 * FilePicker JS template for error (inside element with class 'fp-content').
737 *
738 * must have element with class 'fp-error', its content will be replaced with error text
739 * and the error code will be assigned as additional class to this element
740 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
741 *
742 * @return string
743 */
744 private function fp_js_template_error() {
420e7bb2
BR
745 $rv = '
746<div class="fp-content-error" ><div class="{!}fp-error" /></div>';
d1d18691
MG
747 return preg_replace('/\{\!\}/', '', $rv);
748 }
749
750 /**
751 * FilePicker JS template for error/info message displayed as a separate popup window.
752 *
9a62f779 753 * Must be wrapped in one element, CSS for this element must define
d1d18691
MG
754 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
755 * or 'fp-msg-info' depending on message type;
756 *
757 * content of element with class 'fp-msg-text' will be replaced with error/info text;
758 *
759 * element with class 'fp-msg-butok' will hold onclick event
760 *
761 * @return string
762 */
763 private function fp_js_template_message() {
420e7bb2
BR
764 $rv = '
765<div class="file-picker fp-msg">
766 <p class="{!}fp-msg-text"></p>
767 <a class="{!}fp-msg-butok fp-panel-button">'.get_string('ok').'</a>
768</div>';
d1d18691
MG
769 return preg_replace('/\{\!\}/', '', $rv);
770 }
771
772 /**
773 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
774 *
9a62f779 775 * Must have one top element, CSS for this element must define width and height of the window;
d1d18691
MG
776 *
777 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
778 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
779 * hold onclick events;
780 *
781 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
782 * (Note that it may have long text)
783 *
784 * @return string
785 */
786 private function fp_js_template_processexistingfile() {
420e7bb2
BR
787 $rv = '
788<div class="file-picker fp-dlg">
789 <p class="{!}fp-dlg-text"></p>
790 <a class="{!}fp-dlg-butoverwrite fp-panel-button" >'.get_string('overwrite', 'repository').'</a>
791 <a class="{!}fp-dlg-butcancel fp-panel-button" >'.get_string('cancel').'</a>
792 <a class="{!}fp-dlg-butrename fp-panel-button" />
d1d18691
MG
793</div>';
794 return preg_replace('/\{\!\}/', '', $rv);
795 }
420e7bb2 796
d1d18691
MG
797 /**
798 * FilePicker JS template for repository login form including templates for each element type
799 *
800 * Must contain one <form> element with templates for different input types inside:
801 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
802 * 'fp-login-input' are templates for displaying respective login form elements. Inside
803 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
804 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
805 * They may also contain the <label> element and it's content will be substituted with
806 * label;
807 *
808 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
809 * but if they are not found, 'fp-login-input' will be used;
810 *
811 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
812 * it should hava a template for one radio input (with class 'fp-login-radio');
813 *
814 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
815 * will be removed if at least one popup element is present;
816 *
817 * @return string
818 */
819 private function fp_js_template_loginform() {
820 $rv = '
821<div class="fp-login-form">
420e7bb2
BR
822 <div class="fp-content-center">
823 <form>
824 <table >
825 <tr class="{!}fp-login-popup">
826 <td colspan="2">
827 <label>'.get_string('popup', 'repository').'</label>
828 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p></td></tr>
829 <tr class="{!}fp-login-textarea">
830 <td colspan="2"><p><textarea></textarea></p></td></tr>
831 <tr class="{!}fp-login-select">
832 <td align="right"><label></label></td>
833 <td align="left"><select></select></td></tr>
834 <tr class="{!}fp-login-input">
835 <td class="label"><label /></td>
836 <td class="input"><input/></td></tr>
837 <tr class="{!}fp-login-radiogroup">
838 <td align="right" width="30%" valign="top"><label /></td>
839 <td align="left" valign="top"><p class="{!}fp-login-radio"><input /> <label /></p></td></tr>
840 </table>
841 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
842 </form>
843 </div>
d1d18691
MG
844</div>';
845 return preg_replace('/\{\!\}/', '', $rv);
846 }
847
848 /**
849 * Returns all FilePicker JavaScript templates as an array.
850 *
851 * @return array
852 */
853 public function filepicker_js_templates() {
854 $class_methods = get_class_methods($this);
855 $templates = array();
856 foreach ($class_methods as $method_name) {
857 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
858 $templates[$matches[1]] = $this->$method_name();
859 }
860 return $templates;
861 }
862
863 /**
864 * Returns HTML for default repository searchform to be passed to Filepicker
865 *
866 * This will be used as contents for search form defined in generallayout template
867 * (form with id {TOOLSEARCHID}).
868 * Default contents is one text input field with name="s"
869 */
870 public function repository_default_searchform() {
e88d5641 871 $str = '<input class="search-entry" name="s" value="Search" />';
d1d18691
MG
872 return $str;
873 }
874}
64f93798
PS
875
876/**
877 * Data structure representing a general moodle file tree viewer
878 *
879 * @copyright 2010 Dongsheng Cai
880 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
881 * @since Moodle 2.0
882 */
883class files_tree_viewer implements renderable {
884 public $tree;
885 public $path;
e921afa8 886 public $context;
64f93798
PS
887
888 /**
889 * Constructor of moodle_file_tree_viewer class
890 * @param file_info $file_info
891 * @param array $options
892 */
893 public function __construct(file_info $file_info, array $options = null) {
894 global $CFG;
895
896 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 897 $this->options = (array)$options;
e921afa8
DC
898 $this->context = $options['context'];
899
64f93798
PS
900 $this->tree = array();
901 $children = $file_info->get_children();
0b259d91 902 $current_file_params = $file_info->get_params();
64f93798 903 $parent_info = $file_info->get_parent();
64f93798
PS
904 $level = $parent_info;
905 $this->path = array();
906 while ($level) {
907 $params = $level->get_params();
908 $context = get_context_instance_by_id($params['contextid']);
0b259d91
DC
909 // $this->context is current context
910 if ($context->id != $this->context->id or empty($params['filearea'])) {
64f93798
PS
911 break;
912 }
e921afa8
DC
913 // unset unused parameters
914 unset($params['component']);
915 unset($params['filearea']);
0b259d91 916 unset($params['filename']);
e921afa8 917 unset($params['itemid']);
64f93798 918 $url = new moodle_url('/files/index.php', $params);
e921afa8 919 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
920 $level = $level->get_parent();
921 }
922 $this->path = array_reverse($this->path);
0b259d91
DC
923 if ($current_file_params['filepath'] != '/') {
924 $this->path[] = $file_info->get_visible_name();
925 }
64f93798
PS
926
927 foreach ($children as $child) {
928 $filedate = $child->get_timemodified();
929 $filesize = $child->get_filesize();
930 $mimetype = $child->get_mimetype();
931 $params = $child->get_params();
0b259d91
DC
932 unset($params['component']);
933 unset($params['filearea']);
934 unset($params['filename']);
935 unset($params['itemid']);
64f93798
PS
936 $fileitem = array(
937 'params' => $params,
938 'filename' => $child->get_visible_name(),
939 'filedate' => $filedate ? userdate($filedate) : '',
940 'filesize' => $filesize ? display_size($filesize) : ''
941 );
e921afa8 942 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
943 if ($child->is_directory()) {
944 $fileitem['isdir'] = true;
945 $fileitem['url'] = $url->out(false);
64f93798
PS
946 } else {
947 $fileitem['url'] = $child->get_url();
948 }
949 $this->tree[] = $fileitem;
950 }
951 }
952}