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