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