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