Automatic installer lang files (20110214)
[moodle.git] / lib / form / filemanager.php
blobe7c37de363101a160d378c764d2e8f1b9ffe6436
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * File manager
20 * @package moodlecore
21 * @subpackage file
22 * @copyright 1999 onwards Dongsheng Cai <dongsheng@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 global $CFG;
28 require_once('HTML/QuickForm/element.php');
29 require_once($CFG->dirroot.'/lib/filelib.php');
30 require_once($CFG->dirroot.'/repository/lib.php');
32 class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
33 public $_helpbutton = '';
34 protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
36 function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
37 global $CFG, $PAGE;
39 $options = (array)$options;
40 foreach ($options as $name=>$value) {
41 if (array_key_exists($name, $this->_options)) {
42 $this->_options[$name] = $value;
45 if (!empty($options['maxbytes'])) {
46 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
48 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
51 function setName($name) {
52 $this->updateAttributes(array('name'=>$name));
55 function getName() {
56 return $this->getAttribute('name');
59 function setValue($value) {
60 $this->updateAttributes(array('value'=>$value));
63 function getValue() {
64 return $this->getAttribute('value');
67 function getMaxbytes() {
68 return $this->_options['maxbytes'];
71 function setMaxbytes($maxbytes) {
72 global $CFG;
73 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
76 function getSubdirs() {
77 return $this->_options['subdirs'];
80 function setSubdirs($allow) {
81 $this->_options['subdirs'] = $allow;
84 function getMaxfiles() {
85 return $this->_options['maxfiles'];
88 function setMaxfiles($num) {
89 $this->_options['maxfiles'] = $num;
92 function setHelpButton($helpbuttonargs, $function='helpbutton'){
93 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
96 function getHelpButton() {
97 return $this->_helpbutton;
100 function getElementTemplateType() {
101 if ($this->_flagFrozen){
102 return 'nodisplay';
103 } else {
104 return 'default';
108 function toHtml() {
109 global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
110 require_once("$CFG->dirroot/repository/lib.php");
112 // security - never ever allow guest/not logged in user to upload anything or use this element!
113 if (isguestuser() or !isloggedin()) {
114 print_error('noguest');
117 if ($this->_flagFrozen) {
118 return $this->getFrozenHtml();
121 $id = $this->_attributes['id'];
122 $elname = $this->_attributes['name'];
123 $subdirs = $this->_options['subdirs'];
124 $maxbytes = $this->_options['maxbytes'];
125 $draftitemid = $this->getValue();
126 $accepted_types = $this->_options['accepted_types'];
128 if (empty($draftitemid)) {
129 // no existing area info provided - let's use fresh new draft area
130 require_once("$CFG->libdir/filelib.php");
131 $this->setValue(file_get_unused_draft_itemid());
132 $draftitemid = $this->getValue();
135 $client_id = uniqid();
137 // filemanager options
138 $options = new stdClass();
139 $options->mainfile = $this->_options['mainfile'];
140 $options->maxbytes = $this->_options['maxbytes'];
141 $options->maxfiles = $this->getMaxfiles();
142 $options->client_id = $client_id;
143 $options->itemid = $draftitemid;
144 $options->subdirs = $this->_options['subdirs'];
145 $options->target = $id;
146 $options->accepted_types = $accepted_types;
147 $options->return_types = FILE_INTERNAL;
148 $options->context = $PAGE->context;
150 $html = $this->_getTabs();
151 $html .= form_filemanager_render($options);
153 $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />';
154 // label element needs 'for' attribute work
155 $html .= '<input value="" id="id_'.$elname.'" type="hidden" />';
157 return $html;
164 * Data structure representing a file manager.
166 * @copyright 2010 Dongsheng Cai
167 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
168 * @since Moodle 2.0
170 class form_filemanaer_x {
171 //TODO: do not use this abstraction (skodak)
173 public $options;
174 public function __construct(stdClass $options) {
175 global $CFG, $USER, $PAGE;
176 require_once($CFG->dirroot. '/repository/lib.php');
177 $defaults = array(
178 'maxbytes'=>-1,
179 'maxfiles'=>-1,
180 'itemid'=>0,
181 'subdirs'=>0,
182 'client_id'=>uniqid(),
183 'accepted_types'=>'*',
184 'return_types'=>FILE_INTERNAL,
185 'context'=>$PAGE->context
187 foreach ($defaults as $key=>$value) {
188 if (empty($options->$key)) {
189 $options->$key = $value;
193 $fs = get_file_storage();
195 // initilise options, getting files in root path
196 $this->options = file_get_drafarea_files($options->itemid, '/');
198 // calculate file count
199 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
200 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
201 $filecount = count($files);
202 $this->options->filecount = $filecount;
204 // copying other options
205 foreach ($options as $name=>$value) {
206 $this->options->$name = $value;
209 // building file picker options
210 $params = new stdClass();
211 $params->accepted_types = $options->accepted_types;
212 $params->return_types = $options->return_types;
213 $params->context = $options->context;
214 $params->env = 'filemanager';
215 $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
216 $filepicker_options = initialise_filepicker($params);
217 $this->options->filepicker = $filepicker_options;
222 * Print the file manager
224 * <pre>
225 * $OUTPUT->file_manager($options);
226 * </pre>
228 * @param array $options associative array with file manager options
229 * options are:
230 * maxbytes=>-1,
231 * maxfiles=>-1,
232 * itemid=>0,
233 * subdirs=>false,
234 * client_id=>uniqid(),
235 * acepted_types=>'*',
236 * return_types=>FILE_INTERNAL,
237 * context=>$PAGE->context
238 * @return string HTML fragment
240 function form_filemanager_render($options) {
241 global $CFG, $OUTPUT, $PAGE;
243 $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex
245 static $filemanagertemplateloaded;
247 $html = '';
248 $options = $fm->options;
249 $straddfile = get_string('add', 'repository') . '...';
250 $strmakedir = get_string('makeafolder', 'moodle');
251 $strdownload = get_string('downloadfolder', 'repository');
252 $strloading = get_string('loading', 'repository');
254 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
256 $client_id = $options->client_id;
257 $itemid = $options->itemid;
258 list($context, $course, $cm) = get_context_info_array($options->context->id);
259 if (is_object($course)) {
260 $course_maxbytes = $course->maxbytes;
261 } else {
262 $course_maxbytes = $CFG->maxbytes;
265 if ($options->maxbytes == -1 || empty($options->maxbytes)) {
266 $options->maxbytes = $CFG->maxbytes;
269 if (empty($options->filecount)) {
270 $extra = ' style="display:none"';
271 } else {
272 $extra = '';
275 $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes)));
276 $html .= <<<FMHTML
277 <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'>
278 $icon_progress
279 </div>
280 <div id="filemanager-wrapper-{$client_id}" style="display:none">
281 <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
282 <div class="filemanager-toolbar">
283 <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value=" {$straddfile}" />
284 <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value=" $strmakedir" />
285 <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value=" $strdownload" />
286 <span> $maxsize </span>
287 </div>
288 <div class="filemanager-container" id="filemanager-{$client_id}">
289 <ul id="draftfiles-{$client_id}" class="fm-filelist">
290 <li>Loading...</li>
291 </ul>
292 </div>
293 </div>
294 <div class='clearer'></div>
295 FMHTML;
296 if (empty($filemanagertemplateloaded)) {
297 $filemanagertemplateloaded = true;
298 $html .= <<<FMHTML
299 <div id="fm-template" style="display:none">___fullname___ ___action___</div>
300 FMHTML;
303 $module = array(
304 'name'=>'form_filemanager',
305 'fullpath'=>'/lib/form/filemanager.js',
306 'requires' => array('core_filepicker', 'base', 'io', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'),
307 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
308 array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
309 array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
310 array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
311 array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
312 array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
313 array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
314 array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository')
317 $PAGE->requires->js_module($module);
318 $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
320 // non javascript file manager
321 $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array(
322 'env'=>'filemanager',
323 'action'=>'browse',
324 'itemid'=>$itemid,
325 'subdirs'=>$options->subdirs,
326 'maxbytes'=>$options->maxbytes,
327 'maxfiles'=>$options->maxfiles,
328 'ctx_id'=>$PAGE->context->id,
329 'course'=>$PAGE->course->id,
330 'sesskey'=>sesskey(),
333 $html .= '<noscript>';
334 $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>";
335 $html .= '</noscript>';
338 return $html;