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