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