Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / files / renderer.php
blob5f9eb61d9bcadc1bb2b7a1435672ef161a6e7ff6
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 ///////////////////////////////////////////////////////////////////////////
22 defined('MOODLE_INTERNAL') || die();
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
33 /**
34 * File browser render
36 * @copyright 2010 Dongsheng Cai
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.0
40 class core_files_renderer extends plugin_renderer_base {
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);
47 public function render_files_tree_viewer(files_tree_viewer $tree) {
48 $html = $this->output->heading_with_help(get_string('coursefiles'), 'courselegacyfiles', 'moodle');
50 $html .= $this->output->container_start('coursefilesbreadcrumb');
51 foreach($tree->path as $path) {
52 $html .= $path;
53 $html .= ' / ';
55 $html .= $this->output->container_end();
57 $html .= $this->output->box_start();
58 $table = new html_table();
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');
61 $table->width = '100%';
62 $table->data = array();
64 foreach ($tree->tree as $file) {
65 $filedate = $filesize = $filetype = '';
66 if ($file['filedate']) {
67 $filedate = userdate($file['filedate'], get_string('strftimedatetimeshort', 'langconfig'));
69 if (empty($file['isdir'])) {
70 if ($file['filesize']) {
71 $filesize = display_size($file['filesize']);
73 $fileicon = file_file_icon($file, 24);
74 $filetype = get_mimetype_description($file);
75 } else {
76 $fileicon = file_folder_icon(24);
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
86 $html .= html_writer::table($table);
87 $html .= $this->output->single_button(new moodle_url('/files/coursefilesedit.php', array('contextid'=>$tree->context->id)), get_string('coursefilesedit'), 'get');
88 $html .= $this->output->box_end();
89 return $html;
92 /**
93 * Prints the file manager and initializes all necessary libraries
95 * <pre>
96 * $fm = new form_filemanager($options);
97 * $output = get_renderer('core', 'files');
98 * echo $output->render($fm);
99 * </pre>
101 * @param form_filemanager $fm File manager to render
102 * @return string HTML fragment
104 public function render_form_filemanager($fm) {
105 static $filemanagertemplateloaded;
106 $html = $this->fm_print_generallayout($fm);
107 $module = array(
108 'name'=>'form_filemanager',
109 'fullpath'=>'/lib/form/filemanager.js',
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'),
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'), array('newfolder', 'repository')
120 if (empty($filemanagertemplateloaded)) {
121 $filemanagertemplateloaded = true;
122 $this->page->requires->js_init_call('M.form_filemanager.set_templates',
123 array($this->filemanager_js_templates()), true, $module);
125 $this->page->requires->js_init_call('M.form_filemanager.init', array($fm->options), true, $module);
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>';
133 return $html;
137 * Returns html for displaying one file manager
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)
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)
158 * If browser supports Drag-and-drop, the body element will have class 'dndsupported',
159 * otherwise - 'dndnotsupported';
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;
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;
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';
177 * @param form_filemanager $fm
178 * @return string
180 private function fm_print_generallayout($fm) {
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 $strdroptoupload = get_string('droptoupload', 'moodle');
189 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
190 $restrictions = $this->fm_print_restrictions($fm);
191 $strdndnotsupported = get_string('dndnotsupported_insentence', 'moodle').$OUTPUT->help_icon('dndnotsupported');
192 $strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
193 $loading = get_string('loading', 'repository');
195 $html = '
196 <div id="filemanager-'.$client_id.'" class="filemanager fm-loading">
197 <div class="fp-restrictions">
198 '.$restrictions.'
199 <span class="dnduploadnotsupported-message"> - '.$strdndnotsupported.' </span>
200 </div>
201 <div class="fp-navbar">
202 <div class="filemanager-toolbar">
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 class="{!}fp-viewbar">
209 <a title="'. get_string('displayicons', 'repository') .'" class="{!}fp-vb-icons" href="#"></a>
210 <a title="'. get_string('displaydetails', 'repository') .'" class="{!}fp-vb-details" href="#"></a>
211 <a title="'. get_string('displaytree', 'repository') .'" class="{!}fp-vb-tree" href="#"></a>
212 </div>
213 </div>
214 <div class="fp-pathbar">
215 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
216 </div>
217 </div>
218 <div class="filemanager-loading mdl-align">'.$icon_progress.'</div>
219 <div class="filemanager-container" >
220 <div class="fm-content-wrapper">
221 <div class="fp-content"></div>
222 <div class="fm-empty-container">
223 <div class="dndupload-message">'.$strdndenabledinbox.'<br/><div class="dndupload-arrow"></div></div>
224 </div>
225 <div class="dndupload-target">'.$strdroptoupload.'<br/><div class="dndupload-arrow"></div></div>
226 <div class="dndupload-progressbars"></div>
227 <div class="dndupload-uploadinprogress">'.$icon_progress.'</div>
228 </div>
229 <div class="filemanager-updating">'.$icon_progress.'</div>
230 </div>
231 </div>';
232 return preg_replace('/\{\!\}/', '', $html);
236 * FileManager JS template for displaying one file in 'icon view' mode.
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.
244 * @see fp_js_template_iconfilename()
245 * @return string
247 private function fm_js_template_iconfilename() {
248 $rv = '
249 <div class="fp-file">
250 <a href="#">
251 <div style="position:relative;">
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>
258 </div>
259 </a>
260 <a class="{!}fp-contextmenu" href="#">'.$this->pix_icon('i/menu', 'â–¶').'</a>
261 </div>';
262 return preg_replace('/\{\!\}/', '', $rv);
266 * FileManager JS template for displaying file name in 'table view' and 'tree view' modes.
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.
274 * @todo MDL-32736 remove onclick="return false;"
275 * @see fp_js_template_listfilename()
276 * @return string
278 private function fm_js_template_listfilename() {
279 $rv = '
280 <span class="fp-filename-icon">
281 <a href="#">
282 <span class="{!}fp-icon"></span>
283 <span class="fp-reficons1"></span>
284 <span class="fp-reficons2"></span>
285 <span class="{!}fp-filename"></span>
286 </a>
287 <a class="{!}fp-contextmenu" href="#" onclick="return false;">'.$this->pix_icon('i/menu', 'â–¶').'</a>
288 </span>';
289 return preg_replace('/\{\!\}/', '', $rv);
293 * FileManager JS template for displaying 'Make new folder' dialog.
295 * Must be wrapped in an element, CSS for this element must define width and height of the window;
297 * Must have one input element with type="text" (for users to enter the new folder name);
299 * content of element with class 'fp-dlg-curpath' will be replaced with current path where
300 * new folder is about to be created;
301 * elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
303 * @return string
305 private function fm_js_template_mkdir() {
306 $rv = '
307 <div class="filemanager fp-mkdir-dlg">
308 <div class="fp-mkdir-dlg-text">
309 <label>' . get_string('newfoldername', 'repository') . '</label><br/>
310 <input type="text" />
311 </div>
312 <button class="{!}fp-dlg-butcreate">'.get_string('makeafolder').'</button>
313 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
314 </div>';
315 return preg_replace('/\{\!\}/', '', $rv);
319 * FileManager JS template for error/info message displayed as a separate popup window.
321 * @see fp_js_template_message()
322 * @return string
324 private function fm_js_template_message() {
325 return $this->fp_js_template_message();
329 * FileManager JS template for window with file information/actions.
331 * All content must be enclosed in one element, CSS for this class must define width and
332 * height of the window;
334 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
336 * Inside the window the elements with the following classnames must be present:
337 * 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
338 * one input element (or select in case of fp-license and fp-path). They may also have labels.
339 * The elements will be assign with class 'uneditable' and input/select element will become
340 * disabled if they are not applicable for the particular file;
342 * There may be present elements with classes 'fp-original', 'fp-datemodified', 'fp-datecreated',
343 * 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
344 * information is unavailable. If there is information available, the content of embedded
345 * element with class 'fp-value' will be substituted with the value;
347 * The value of Original ('fp-original') is loaded in separate request. When it is applicable
348 * but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
350 * The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
351 * is applicable but not yet loaded the 'fp-original' element receives additional class
352 * 'fp-loading'. The string explaining that XX references exist will replace content of element
353 * 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
355 * Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
356 * 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
357 * events (there may be several elements with class 'fp-file-cancel');
359 * When confirm button is pressed and file is being selected, the top element receives
360 * additional class 'loading'. It is removed when response from server is received.
362 * When any of the input fields is changed, the top element receives class 'fp-changed';
363 * When current file can be set as main - top element receives class 'fp-cansetmain';
364 * When current file is folder/zip/file - top element receives respectfully class
365 * 'fp-folder'/'fp-zip'/'fp-file';
367 * @return string
369 private function fm_js_template_fileselectlayout() {
370 $strloading = get_string('loading', 'repository');
371 $icon_progress = $this->pix_icon('i/loading_small', $strloading).'';
372 $rv = '
373 <div class="filemanager fp-select">
374 <div class="fp-select-loading">
375 <img src="'.$this->pix_url('i/loading_small').'" />
376 </div>
377 <form>
378 <button class="{!}fp-file-download">'.get_string('download').'</button>
379 <button class="{!}fp-file-delete">'.get_string('delete').'</button>
380 <button class="{!}fp-file-setmain">'.get_string('setmainfile', 'repository').'</button>
381 <button class="{!}fp-file-zip">'.get_string('zip', 'editor').'</button>
382 <button class="{!}fp-file-unzip">'.get_string('unzip').'</button>
383 <div class="fp-hr"></div>
384 <table>
385 <tr class="{!}fp-saveas"><td class="mdl-right"><label>'.get_string('name', 'moodle').'</label>:</td>
386 <td class="mdl-left"><input type="text"/></td></tr>
387 <tr class="{!}fp-author"><td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
388 <td class="mdl-left"><input type="text"/></td></tr>
389 <tr class="{!}fp-license"><td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
390 <td class="mdl-left"><select></select></td></tr>
391 <tr class="{!}fp-path"><td class="mdl-right"><label>'.get_string('path', 'moodle').'</label>:</td>
392 <td class="mdl-left"><select></select></td></tr>
393 <tr class="{!}fp-original"><td class="mdl-right"><label>'.get_string('original', 'repository').'</label>:</td>
394 <td class="mdl-left"><span class="fp-originloading">'.$icon_progress.' '.$strloading.'</span><span class="fp-value"></span></td></tr>
395 <tr class="{!}fp-reflist"><td class="mdl-right"><label>'.get_string('referenceslist', 'repository').'</label>:</td>
396 <td class="mdl-left"><p class="{!}fp-refcount"></p><span class="fp-reflistloading">'.$icon_progress.' '.$strloading.'</span><ul class="fp-value"></ul></td></tr>
397 </table>
398 <div class="fp-select-buttons">
399 <button class="{!}fp-file-update">'.get_string('update', 'moodle').'</button>
400 <button class="{!}fp-file-cancel">'.get_string('cancel').'</button>
401 </div>
402 </form>
403 <div class="fp-info">
404 <div class="fp-hr"></div>
405 <p class="{!}fp-thumbnail"></p>
406 <div class="fp-fileinfo">
407 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
408 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
409 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
410 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
411 </div>
412 </div>
413 </div>';
414 return preg_replace('/\{\!\}/', '', $rv);
418 * FileManager JS template for popup confirm dialogue window.
420 * Must have one top element, CSS for this element must define width and height of the window;
422 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
423 * elements with classes 'fp-dlg-butconfirm' and 'fp-dlg-butcancel' will
424 * hold onclick events;
426 * @return string
428 private function fm_js_template_confirmdialog() {
429 $rv = '
430 <div class="filemanager fp-dlg">
431 <div class="{!}fp-dlg-text"></div>
432 <button class="{!}fp-dlg-butconfirm">'.get_string('ok').'</button>
433 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
434 </div>';
435 return preg_replace('/\{\!\}/', '', $rv);
439 * Returns all FileManager JavaScript templates as an array.
441 * @return array
443 public function filemanager_js_templates() {
444 $class_methods = get_class_methods($this);
445 $templates = array();
446 foreach ($class_methods as $method_name) {
447 if (preg_match('/^fm_js_template_(.*)$/', $method_name, $matches))
448 $templates[$matches[1]] = $this->$method_name();
450 return $templates;
454 * Displays restrictions for the file manager
456 * @param form_filemanager $fm
457 * @return string
459 private function fm_print_restrictions($fm) {
460 $maxbytes = display_size($fm->options->maxbytes);
461 $strparam = (object) array('size' => $maxbytes, 'attachments' => $fm->options->maxfiles,
462 'areasize' => display_size($fm->options->areamaxbytes));
463 $hasmaxfiles = !empty($fm->options->maxfiles) && $fm->options->maxfiles > 0;
464 $hasarealimit = !empty($fm->options->areamaxbytes) && $fm->options->areamaxbytes != -1;
465 if ($hasmaxfiles && $hasarealimit) {
466 $maxsize = get_string('maxsizeandattachmentsandareasize', 'moodle', $strparam);
467 } else if ($hasmaxfiles) {
468 $maxsize = get_string('maxsizeandattachments', 'moodle', $strparam);
469 } else if ($hasarealimit) {
470 $maxsize = get_string('maxsizeandareasize', 'moodle', $strparam);
471 } else {
472 $maxsize = get_string('maxfilesize', 'moodle', $maxbytes);
474 // TODO MDL-32020 also should say about 'File types accepted'
475 return '<span>'. $maxsize . '</span>';
479 * Template for FilePicker with general layout (not QuickUpload).
481 * Must have one top element containing everything else (recommended <div class="file-picker">),
482 * CSS for this element must define width and height of the filepicker window. Or CSS must
483 * define min-width, max-width, min-height and max-height and in this case the filepicker
484 * window will be resizeable;
486 * Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
487 * can be changed or not;
488 * Inside element with class 'fp-viewbar' there are expected elements with classes
489 * 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
490 * between the view modes, the last clicked element will have the class 'checked';
492 * Element with class 'fp-repo' is a template for displaying one repository. Other repositories
493 * will be attached as siblings (classes first/last/even/odd will be added respectfully).
494 * The currently selected repostory will have class 'active'. Contents of element with class
495 * 'fp-repo-name' will be replaced with repository name, source of image with class
496 * 'fp-repo-icon' will be replaced with repository icon;
498 * Element with class 'fp-content' is obligatory and will hold the current contents;
500 * Element with class 'fp-paging' will contain page navigation (will be deprecated soon);
502 * Element with class 'fp-path-folder' is a template for one folder in path toolbar.
503 * It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
504 * Parent element will receive class 'empty' when there are no folders to be displayed;
505 * The content of subelement with class 'fp-path-folder-name' will be substituted with folder name;
507 * Element with class 'fp-toolbar' will have class 'empty' if all 'Back', 'Search', 'Refresh',
508 * 'Logout', 'Manage' and 'Help' are unavailable for this repo;
510 * Inside fp-toolbar there are expected elements with classes fp-tb-back, fp-tb-search,
511 * fp-tb-refresh, fp-tb-logout, fp-tb-manage and fp-tb-help. Each of them will have
512 * class 'enabled' or 'disabled' if particular repository has this functionality.
513 * Element with class 'fp-tb-search' must contain empty form inside, it's contents will
514 * be substituted with the search form returned by repository (in the most cases it
515 * is generated with template core_repository_renderer::repository_default_searchform);
516 * Other elements must have either <a> or <button> element inside, it will hold onclick
517 * event for corresponding action; labels for fp-tb-back and fp-tb-logout may be
518 * replaced with those specified by repository;
520 * @return string
522 private function fp_js_template_generallayout() {
523 $rv = '
524 <div class="file-picker fp-generallayout">
525 <div class="fp-repo-area">
526 <ul class="fp-list">
527 <li class="{!}fp-repo"><a href="#"><img class="{!}fp-repo-icon" alt="'. get_string('repositoryicon', 'repository') .'" width="16" height="16" />&nbsp;<span class="{!}fp-repo-name"></span></a></li>
528 </ul>
529 </div>
530 <div class="fp-repo-items">
531 <div class="fp-navbar">
532 <div>
533 <div class="{!}fp-toolbar">
534 <div class="{!}fp-tb-back"><a href="#">'.get_string('back', 'repository').'</a></div>
535 <div class="{!}fp-tb-search"><form></form></div>
536 <div class="{!}fp-tb-refresh"><a href="#"><img alt="'. get_string('refresh', 'repository') .'" src="'.$this->pix_url('a/refresh').'" /></a></div>
537 <div class="{!}fp-tb-logout"><img alt="'. get_string('logout', 'repository') .'" src="'.$this->pix_url('a/logout').'" /><a href="#"></a></div>
538 <div class="{!}fp-tb-manage"><a href="#"><img alt="'. get_string('settings', 'repository') .'" src="'.$this->pix_url('a/setting').'" /> '.get_string('manageurl', 'repository').'</a></div>
539 <div class="{!}fp-tb-help"><a href="#"><img alt="'. get_string('help', 'repository') .'" src="'.$this->pix_url('a/help').'" /> '.get_string('help').'</a></div>
540 <div class="{!}fp-tb-message"></div>
541 </div>
542 <div class="{!}fp-viewbar">
543 <a title="'. get_string('displayicons', 'repository') .'" class="{!}fp-vb-icons" href="#"></a>
544 <a title="'. get_string('displaydetails', 'repository') .'" class="{!}fp-vb-details" href="#"></a>
545 <a title="'. get_string('displaytree', 'repository') .'" class="{!}fp-vb-tree" href="#"></a>
546 </div>
547 <div class="fp-clear-left"></div>
548 </div>
549 <div class="fp-pathbar">
550 <span class="{!}fp-path-folder"><a class="{!}fp-path-folder-name" href="#"></a></span>
551 </div>
552 </div>
553 <div class="{!}fp-content"></div>
554 </div>
555 </div>';
556 return preg_replace('/\{\!\}/', '', $rv);
560 * FilePicker JS template for displaying one file in 'icon view' mode.
562 * the element with class 'fp-thumbnail' will be resized to the repository thumbnail size
563 * (both width and height, unless min-width and/or min-height is set in CSS) and the content of
564 * an element will be replaced with an appropriate img;
566 * the width of element with class 'fp-filename' will be set to the repository thumbnail width
567 * (unless min-width is set in css) and the content of an element will be replaced with filename
568 * supplied by repository;
570 * top element(s) will have class fp-folder if the element is a folder;
572 * List of files will have parent <div> element with class 'fp-iconview'
574 * @return string
576 private function fp_js_template_iconfilename() {
577 $rv = '
578 <a class="fp-file" href="#" >
579 <div style="position:relative;">
580 <div class="{!}fp-thumbnail"></div>
581 <div class="fp-reficons1"></div>
582 <div class="fp-reficons2"></div>
583 </div>
584 <div class="fp-filename-field">
585 <p class="{!}fp-filename"></p>
586 </div>
587 </a>';
588 return preg_replace('/\{\!\}/', '', $rv);
592 * FilePicker JS template for displaying file name in 'table view' and 'tree view' modes.
594 * content of the element with class 'fp-icon' will be replaced with an appropriate img;
596 * content of element with class 'fp-filename' will be replaced with filename supplied by
597 * repository;
599 * top element(s) will have class fp-folder if the element is a folder;
601 * Note that tree view and table view are the YUI widgets and therefore there are no
602 * other templates. The widgets will be wrapped in <div> with class fp-treeview or
603 * fp-tableview (respectfully).
605 * @return string
607 private function fp_js_template_listfilename() {
608 $rv = '
609 <span class="fp-filename-icon">
610 <a href="#">
611 <span class="{!}fp-icon"></span>
612 <span class="{!}fp-filename"></span>
613 </a>
614 </span>';
615 return preg_replace('/\{\!\}/', '', $rv);
619 * FilePicker JS template for displaying link/loading progress for fetching of the next page
621 * This text is added to .fp-content AFTER .fp-iconview/.fp-treeview/.fp-tableview
623 * Must have one parent element with class 'fp-nextpage'. It will be assigned additional
624 * class 'loading' during loading of the next page (it is recommended that in this case the link
625 * becomes unavailable). Also must contain one element <a> or <button> that will hold
626 * onclick event for displaying of the next page. The event will be triggered automatically
627 * when user scrolls to this link.
629 * @return string
631 private function fp_js_template_nextpage() {
632 $rv = '
633 <div class="{!}fp-nextpage">
634 <div class="fp-nextpage-link"><a href="#">'.get_string('more').'</a></div>
635 <div class="fp-nextpage-loading">
636 <img src="'.$this->pix_url('i/loading_small').'" />
637 </div>
638 </div>';
639 return preg_replace('/\{\!\}/', '', $rv);
643 * FilePicker JS template for window appearing to select a file.
645 * All content must be enclosed in one element, CSS for this class must define width and
646 * height of the window;
648 * Thumbnail image will be added as content to the element with class 'fp-thumbnail';
650 * Inside the window the elements with the following classnames must be present:
651 * 'fp-saveas', 'fp-linktype-2', 'fp-linktype-1', 'fp-linktype-4', 'fp-setauthor',
652 * 'fp-setlicense'. Inside each of them must have one input element (or select in case of
653 * fp-setlicense). They may also have labels.
654 * The elements will be assign with class 'uneditable' and input/select element will become
655 * disabled if they are not applicable for the particular file;
657 * There may be present elements with classes 'fp-datemodified', 'fp-datecreated', 'fp-size',
658 * 'fp-license', 'fp-author', 'fp-dimensions'. They will receive additional class 'fp-unknown'
659 * if information is unavailable. If there is information available, the content of embedded
660 * element with class 'fp-value' will be substituted with the value;
662 * Elements with classes 'fp-select-confirm' and 'fp-select-cancel' will hold corresponding
663 * onclick events;
665 * When confirm button is pressed and file is being selected, the top element receives
666 * additional class 'loading'. It is removed when response from server is received.
668 * @return string
670 private function fp_js_template_selectlayout() {
671 $rv = '
672 <div class="file-picker fp-select">
673 <div class="fp-select-loading">
674 <img src="'.$this->pix_url('i/loading_small').'" />
675 </div>
676 <form>
677 <table>
678 <tr class="{!}fp-linktype-2">
679 <td class="mdl-right"></td>
680 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefileinternal', 'repository').'</label></td></tr>
681 <tr class="{!}fp-linktype-1">
682 <td class="mdl-right"></td>
683 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefilelink', 'repository').'</label></td></tr>
684 <tr class="{!}fp-linktype-4">
685 <td class="mdl-right"></td>
686 <td class="mdl-left"><input type="radio"/><label>&nbsp;'.get_string('makefilereference', 'repository').'</label></td></tr>
687 <tr class="{!}fp-saveas">
688 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
689 <td class="mdl-left"><input type="text"/></td></tr>
690 <tr class="{!}fp-setauthor">
691 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
692 <td class="mdl-left"><input type="text" /></td></tr>
693 <tr class="{!}fp-setlicense">
694 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
695 <td class="mdl-left"><select></select></td></tr>
696 </table>
697 <div class="fp-select-buttons">
698 <button class="{!}fp-select-confirm">'.get_string('getfile', 'repository').'</button>
699 <button class="{!}fp-select-cancel">'.get_string('cancel').'</button>
700 </div>
701 </form>
702 <div class="fp-info">
703 <div class="fp-hr"></div>
704 <p class="{!}fp-thumbnail"></p>
705 <div class="fp-fileinfo">
706 <div class="{!}fp-datemodified">'.get_string('lastmodified', 'moodle').': <span class="fp-value"></span></div>
707 <div class="{!}fp-datecreated">'.get_string('datecreated', 'repository').': <span class="fp-value"></span></div>
708 <div class="{!}fp-size">'.get_string('size', 'repository').': <span class="fp-value"></span></div>
709 <div class="{!}fp-license">'.get_string('license', 'moodle').': <span class="fp-value"></span></div>
710 <div class="{!}fp-author">'.get_string('author', 'repository').': <span class="fp-value"></span></div>
711 <div class="{!}fp-dimensions">'.get_string('dimensions', 'repository').': <span class="fp-value"></span></div>
712 </div>
713 <div>
714 </div>';
715 return preg_replace('/\{\!\}/', '', $rv);
719 * FilePicker JS template for 'Upload file' repository
721 * Content to display when user chooses 'Upload file' repository (will be nested inside
722 * element with class 'fp-content').
724 * Must contain form (enctype="multipart/form-data" method="POST")
726 * The elements with the following classnames must be present:
727 * 'fp-file', 'fp-saveas', 'fp-setauthor', 'fp-setlicense'. Inside each of them must have
728 * one input element (or select in case of fp-setlicense). They may also have labels.
730 * Element with class 'fp-upload-btn' will hold onclick event for uploading the file;
732 * Please note that some fields may be hidden using CSS if this is part of quickupload form
734 * @return string
736 private function fp_js_template_uploadform() {
737 $rv = '
738 <div class="fp-upload-form mdl-align">
739 <div class="fp-content-center">
740 <form enctype="multipart/form-data" method="POST">
741 <table >
742 <tr class="{!}fp-file">
743 <td class="mdl-right"><label>'.get_string('attachment', 'repository').'</label>:</td>
744 <td class="mdl-left"><input type="file"/></td></tr>
745 <tr class="{!}fp-saveas">
746 <td class="mdl-right"><label>'.get_string('saveas', 'repository').'</label>:</td>
747 <td class="mdl-left"><input type="text"/></td></tr>
748 <tr class="{!}fp-setauthor">
749 <td class="mdl-right"><label>'.get_string('author', 'repository').'</label>:</td>
750 <td class="mdl-left"><input type="text"/></td></tr>
751 <tr class="{!}fp-setlicense">
752 <td class="mdl-right"><label>'.get_string('chooselicense', 'repository').'</label>:</td>
753 <td class="mdl-left"><select></select></td></tr>
754 </table>
755 </form>
756 <div><button class="{!}fp-upload-btn">'.get_string('upload', 'repository').'</button></div>
757 </div>
758 </div> ';
759 return preg_replace('/\{\!\}/', '', $rv);
763 * FilePicker JS template to display during loading process (inside element with class 'fp-content').
765 * @return string
767 private function fp_js_template_loading() {
768 return '
769 <div class="fp-content-loading">
770 <div class="fp-content-center">
771 <img src="'.$this->pix_url('i/loading_small').'" />
772 </div>
773 </div>';
777 * FilePicker JS template for error (inside element with class 'fp-content').
779 * must have element with class 'fp-error', its content will be replaced with error text
780 * and the error code will be assigned as additional class to this element
781 * used errors: invalidjson, nofilesavailable, norepositoriesavailable
783 * @return string
785 private function fp_js_template_error() {
786 $rv = '
787 <div class="fp-content-error" ><div class="{!}fp-error"></div></div>';
788 return preg_replace('/\{\!\}/', '', $rv);
792 * FilePicker JS template for error/info message displayed as a separate popup window.
794 * Must be wrapped in one element, CSS for this element must define
795 * width and height of the window. It will be assigned with an additional class 'fp-msg-error'
796 * or 'fp-msg-info' depending on message type;
798 * content of element with class 'fp-msg-text' will be replaced with error/info text;
800 * element with class 'fp-msg-butok' will hold onclick event
802 * @return string
804 private function fp_js_template_message() {
805 $rv = '
806 <div class="file-picker fp-msg">
807 <p class="{!}fp-msg-text"></p>
808 <button class="{!}fp-msg-butok">'.get_string('ok').'</button>
809 </div>';
810 return preg_replace('/\{\!\}/', '', $rv);
814 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists.
816 * Must have one top element, CSS for this element must define width and height of the window;
818 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
819 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename',
820 * 'fp-dlg-butoverwriteall', 'fp-dlg-butrenameall' and 'fp-dlg-butcancel' will
821 * hold onclick events;
823 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
824 * (Note that it may have long text)
826 * @return string
828 private function fp_js_template_processexistingfile() {
829 $rv = '
830 <div class="file-picker fp-dlg">
831 <p class="{!}fp-dlg-text"></p>
832 <div class="fp-dlg-buttons">
833 <button class="{!}fp-dlg-butoverwrite">'.get_string('overwrite', 'repository').'</button>
834 <button class="{!}fp-dlg-butrename"></button>
835 <button class="{!}fp-dlg-butcancel">'.get_string('cancel').'</button>
836 </div>
837 </div>';
838 return preg_replace('/\{\!\}/', '', $rv);
842 * FilePicker JS template for popup dialogue window asking for action when file with the same name already exists (multiple-file version).
844 * Must have one top element, CSS for this element must define width and height of the window;
846 * content of element with class 'fp-dlg-text' will be replaced with dialog text;
847 * elements with classes 'fp-dlg-butoverwrite', 'fp-dlg-butrename' and 'fp-dlg-butcancel' will
848 * hold onclick events;
850 * content of element with class 'fp-dlg-butrename' will be substituted with appropriate string
851 * (Note that it may have long text)
853 * @return string
855 private function fp_js_template_processexistingfilemultiple() {
856 $rv = '
857 <div class="file-picker fp-dlg">
858 <p class="{!}fp-dlg-text"></p>
859 <a class="{!}fp-dlg-butoverwrite fp-panel-button" href="#">'.get_string('overwrite', 'repository').'</a>
860 <a class="{!}fp-dlg-butcancel fp-panel-button" href="#">'.get_string('cancel').'</a>
861 <a class="{!}fp-dlg-butrename fp-panel-button" href="#"></a>
862 <br/>
863 <a class="{!}fp-dlg-butoverwriteall fp-panel-button" href="#">'.get_string('overwriteall', 'repository').'</a>
864 <a class="{!}fp-dlg-butrenameall fp-panel-button" href="#">'.get_string('renameall', 'repository').'</a>
865 </div>';
866 return preg_replace('/\{\!\}/', '', $rv);
870 * FilePicker JS template for repository login form including templates for each element type
872 * Must contain one <form> element with templates for different input types inside:
873 * Elements with classes 'fp-login-popup', 'fp-login-textarea', 'fp-login-select' and
874 * 'fp-login-input' are templates for displaying respective login form elements. Inside
875 * there must be exactly one element with type <button>, <textarea>, <select> or <input>
876 * (i.e. fp-login-popup should have <button>, fp-login-textarea should have <textarea>, etc.);
877 * They may also contain the <label> element and it's content will be substituted with
878 * label;
880 * You can also define elements with classes 'fp-login-checkbox', 'fp-login-text'
881 * but if they are not found, 'fp-login-input' will be used;
883 * Element with class 'fp-login-radiogroup' will be used for group of radio inputs. Inside
884 * it should hava a template for one radio input (with class 'fp-login-radio');
886 * Element with class 'fp-login-submit' will hold on click mouse event (form submission). It
887 * will be removed if at least one popup element is present;
889 * @return string
891 private function fp_js_template_loginform() {
892 $rv = '
893 <div class="fp-login-form">
894 <div class="fp-content-center">
895 <form>
896 <table >
897 <tr class="{!}fp-login-popup">
898 <td colspan="2">
899 <label>'.get_string('popup', 'repository').'</label>
900 <p class="fp-popup"><button class="{!}fp-login-popup-but">'.get_string('login', 'repository').'</button></p></td></tr>
901 <tr class="{!}fp-login-textarea">
902 <td colspan="2"><p><textarea></textarea></p></td></tr>
903 <tr class="{!}fp-login-select">
904 <td align="right"><label></label></td>
905 <td align="left"><select></select></td></tr>
906 <tr class="{!}fp-login-input">
907 <td class="label"><label></label></td>
908 <td class="input"><input/></td></tr>
909 <tr class="{!}fp-login-radiogroup">
910 <td align="right" width="30%" valign="top"><label></label></td>
911 <td align="left" valign="top"><p class="{!}fp-login-radio"><input /> <label></label></p></td></tr>
912 </table>
913 <p><button class="{!}fp-login-submit">'.get_string('submit', 'repository').'</button></p>
914 </form>
915 </div>
916 </div>';
917 return preg_replace('/\{\!\}/', '', $rv);
921 * Returns all FilePicker JavaScript templates as an array.
923 * @return array
925 public function filepicker_js_templates() {
926 $class_methods = get_class_methods($this);
927 $templates = array();
928 foreach ($class_methods as $method_name) {
929 if (preg_match('/^fp_js_template_(.*)$/', $method_name, $matches))
930 $templates[$matches[1]] = $this->$method_name();
932 return $templates;
936 * Returns HTML for default repository searchform to be passed to Filepicker
938 * This will be used as contents for search form defined in generallayout template
939 * (form with id {TOOLSEARCHID}).
940 * Default contents is one text input field with name="s"
942 public function repository_default_searchform() {
943 $searchinput = html_writer::label(get_string('searchrepo', 'repository'),
944 'reposearch', false, array('class' => 'accesshide'));
945 $searchinput .= html_writer::empty_tag('input', array('type' => 'text',
946 'id' => 'reposearch', 'name' => 's', 'value' => get_string('search', 'repository')));
947 $str = html_writer::tag('div', $searchinput, array('class' => "fp-def-search"));
949 return $str;
954 * Data structure representing a general moodle file tree viewer
956 * @copyright 2010 Dongsheng Cai
957 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
958 * @since Moodle 2.0
960 class files_tree_viewer implements renderable {
961 public $tree;
962 public $path;
963 public $context;
966 * Constructor of moodle_file_tree_viewer class
967 * @param file_info $file_info
968 * @param array $options
970 public function __construct(file_info $file_info, array $options = null) {
971 global $CFG;
973 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
974 $this->options = (array)$options;
975 $this->context = $options['context'];
977 $this->tree = array();
978 $children = $file_info->get_children();
979 $current_file_params = $file_info->get_params();
980 $parent_info = $file_info->get_parent();
981 $level = $parent_info;
982 $this->path = array();
983 while ($level) {
984 $params = $level->get_params();
985 $context = context::instance_by_id($params['contextid']);
986 // $this->context is current context
987 if ($context->id != $this->context->id or empty($params['filearea'])) {
988 break;
990 // unset unused parameters
991 unset($params['component']);
992 unset($params['filearea']);
993 unset($params['filename']);
994 unset($params['itemid']);
995 $url = new moodle_url('/files/index.php', $params);
996 $this->path[] = html_writer::link($url, $level->get_visible_name());
997 $level = $level->get_parent();
999 $this->path = array_reverse($this->path);
1000 if ($current_file_params['filepath'] != '/') {
1001 $this->path[] = $file_info->get_visible_name();
1004 foreach ($children as $child) {
1005 $filedate = $child->get_timemodified();
1006 $filesize = $child->get_filesize();
1007 $mimetype = $child->get_mimetype();
1008 $params = $child->get_params();
1009 unset($params['component']);
1010 unset($params['filearea']);
1011 unset($params['filename']);
1012 unset($params['itemid']);
1013 $fileitem = array(
1014 'params' => $params,
1015 'filename' => $child->get_visible_name(),
1016 'mimetype' => $child->get_mimetype(),
1017 'filedate' => $filedate ? $filedate : '',
1018 'filesize' => $filesize ? $filesize : ''
1020 $url = new moodle_url('/files/index.php', $params);
1021 if ($child->is_directory()) {
1022 $fileitem['isdir'] = true;
1023 $fileitem['url'] = $url->out(false);
1024 } else {
1025 $fileitem['url'] = $child->get_url();
1027 $this->tree[] = $fileitem;