MDL-31901: user renderer for filemanager, decrease html generation in filemanager...
[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 */
98 function render_form_filemanager($fm) {
99 static $filemanagertemplateloaded;
100 $html = $this->file_manager_html($fm);
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 */
168 private function file_manager_html($fm) {
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).'';
179 $restrictions = $this->file_manager_restrictions($fm);
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 */
216 private function file_manager_restrictions($fm) {
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
PS
229}
230
231
232/**
233 * Data structure representing a general moodle file tree viewer
234 *
235 * @copyright 2010 Dongsheng Cai
236 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
237 * @since Moodle 2.0
238 */
239class files_tree_viewer implements renderable {
240 public $tree;
241 public $path;
e921afa8 242 public $context;
64f93798
PS
243
244 /**
245 * Constructor of moodle_file_tree_viewer class
246 * @param file_info $file_info
247 * @param array $options
248 */
249 public function __construct(file_info $file_info, array $options = null) {
250 global $CFG;
251
252 //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
64f93798 253 $this->options = (array)$options;
e921afa8
DC
254 $this->context = $options['context'];
255
64f93798
PS
256 $this->tree = array();
257 $children = $file_info->get_children();
0b259d91 258 $current_file_params = $file_info->get_params();
64f93798 259 $parent_info = $file_info->get_parent();
64f93798
PS
260 $level = $parent_info;
261 $this->path = array();
262 while ($level) {
263 $params = $level->get_params();
264 $context = get_context_instance_by_id($params['contextid']);
0b259d91
DC
265 // $this->context is current context
266 if ($context->id != $this->context->id or empty($params['filearea'])) {
64f93798
PS
267 break;
268 }
e921afa8
DC
269 // unset unused parameters
270 unset($params['component']);
271 unset($params['filearea']);
0b259d91 272 unset($params['filename']);
e921afa8 273 unset($params['itemid']);
64f93798 274 $url = new moodle_url('/files/index.php', $params);
e921afa8 275 $this->path[] = html_writer::link($url, $level->get_visible_name());
64f93798
PS
276 $level = $level->get_parent();
277 }
278 $this->path = array_reverse($this->path);
0b259d91
DC
279 if ($current_file_params['filepath'] != '/') {
280 $this->path[] = $file_info->get_visible_name();
281 }
64f93798
PS
282
283 foreach ($children as $child) {
284 $filedate = $child->get_timemodified();
285 $filesize = $child->get_filesize();
286 $mimetype = $child->get_mimetype();
287 $params = $child->get_params();
0b259d91
DC
288 unset($params['component']);
289 unset($params['filearea']);
290 unset($params['filename']);
291 unset($params['itemid']);
64f93798
PS
292 $fileitem = array(
293 'params' => $params,
294 'filename' => $child->get_visible_name(),
295 'filedate' => $filedate ? userdate($filedate) : '',
296 'filesize' => $filesize ? display_size($filesize) : ''
297 );
e921afa8 298 $url = new moodle_url('/files/index.php', $params);
64f93798
PS
299 if ($child->is_directory()) {
300 $fileitem['isdir'] = true;
301 $fileitem['url'] = $url->out(false);
64f93798
PS
302 } else {
303 $fileitem['url'] = $child->get_url();
304 }
305 $this->tree[] = $fileitem;
306 }
307 }
308}