Merge branch 'wip-mdl-30974' of git://github.com/rajeshtaneja/moodle
[moodle.git] / lib / form / filemanager.php
blobe3403d491efcd7d0796536e48c8e86456333b779
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/>.
18 /**
19 * FileManager form element
21 * Contains HTML class for a filemanager form element
23 * @package core_form
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $CFG;
30 require_once('HTML/QuickForm/element.php');
31 require_once($CFG->dirroot.'/lib/filelib.php');
32 require_once($CFG->dirroot.'/repository/lib.php');
34 /**
35 * Filemanager form element
37 * FilemaneManager lets user to upload/manage multiple files
38 * @package core_form
39 * @category form
40 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class MoodleQuickForm_filemanager extends HTML_QuickForm_element {
44 /** @var string html for help button, if empty then no help will icon will be dispalyed. */
45 public $_helpbutton = '';
47 /** @var array options provided to initalize filemanager */
48 protected $_options = array('mainfile'=>'', 'subdirs'=>1, 'maxbytes'=>-1, 'maxfiles'=>-1, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
50 /**
51 * Constructor
53 * @param string $elementName (optional) name of the filemanager
54 * @param string $elementLabel (optional) filemanager label
55 * @param array $attributes (optional) Either a typical HTML attribute string
56 * or an associative array
57 * @param array $options set of options to initalize filemanager
59 function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
60 global $CFG, $PAGE;
62 $options = (array)$options;
63 foreach ($options as $name=>$value) {
64 if (array_key_exists($name, $this->_options)) {
65 $this->_options[$name] = $value;
68 if (!empty($options['maxbytes'])) {
69 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
71 $this->_type = 'filemanager';
72 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
75 /**
76 * Sets name of filemanager
78 * @param string $name name of the filemanager
80 function setName($name) {
81 $this->updateAttributes(array('name'=>$name));
84 /**
85 * Returns name of filemanager
87 * @return string
89 function getName() {
90 return $this->getAttribute('name');
93 /**
94 * Updates filemanager attribute value
96 * @param string $value value to set
98 function setValue($value) {
99 $this->updateAttributes(array('value'=>$value));
103 * Returns filemanager attribute value
105 * @return string
107 function getValue() {
108 return $this->getAttribute('value');
112 * Returns maximum file size which can be uploaded
114 * @return int
116 function getMaxbytes() {
117 return $this->_options['maxbytes'];
121 * Sets maximum file size which can be uploaded
123 * @param int $maxbytes file size
125 function setMaxbytes($maxbytes) {
126 global $CFG;
127 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
131 * Returns true if subdirectoy can be created, else false
133 * @return bool
135 function getSubdirs() {
136 return $this->_options['subdirs'];
140 * Set option to create sub directory, while uploading file
142 * @param bool $allow true if sub directory can be created.
144 function setSubdirs($allow) {
145 $this->_options['subdirs'] = $allow;
149 * Returns maximum number of files which can be uploaded
151 * @return int
153 function getMaxfiles() {
154 return $this->_options['maxfiles'];
158 * Sets maximum number of files which can be uploaded.
160 * @param int $num number of files
162 function setMaxfiles($num) {
163 $this->_options['maxfiles'] = $num;
167 * Sets help button for filemanager
169 * @param mixed $helpbuttonargs arguments to create help button
170 * @param string $function name of the callback function
171 * @deprecated since Moodle 2.0. Please do not call this function any more.
172 * @todo MDL-31047 this api will be removed.
173 * @see MoodleQuickForm::setHelpButton()
175 function setHelpButton($helpbuttonargs, $function='helpbutton'){
176 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
180 * Returns html for help button.
182 * @return string html for help button
184 function getHelpButton() {
185 return $this->_helpbutton;
189 * Returns type of filemanager element
191 * @return string
193 function getElementTemplateType() {
194 if ($this->_flagFrozen){
195 return 'nodisplay';
196 } else {
197 return 'default';
202 * Returns HTML for filemanager form element.
204 * @return string
206 function toHtml() {
207 global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
208 require_once("$CFG->dirroot/repository/lib.php");
210 // security - never ever allow guest/not logged in user to upload anything or use this element!
211 if (isguestuser() or !isloggedin()) {
212 print_error('noguest');
215 if ($this->_flagFrozen) {
216 return $this->getFrozenHtml();
219 $id = $this->_attributes['id'];
220 $elname = $this->_attributes['name'];
221 $subdirs = $this->_options['subdirs'];
222 $maxbytes = $this->_options['maxbytes'];
223 $draftitemid = $this->getValue();
224 $accepted_types = $this->_options['accepted_types'];
226 if (empty($draftitemid)) {
227 // no existing area info provided - let's use fresh new draft area
228 require_once("$CFG->libdir/filelib.php");
229 $this->setValue(file_get_unused_draft_itemid());
230 $draftitemid = $this->getValue();
233 $client_id = uniqid();
235 // filemanager options
236 $options = new stdClass();
237 $options->mainfile = $this->_options['mainfile'];
238 $options->maxbytes = $this->_options['maxbytes'];
239 $options->maxfiles = $this->getMaxfiles();
240 $options->client_id = $client_id;
241 $options->itemid = $draftitemid;
242 $options->subdirs = $this->_options['subdirs'];
243 $options->target = $id;
244 $options->accepted_types = $accepted_types;
245 $options->return_types = FILE_INTERNAL;
246 $options->context = $PAGE->context;
248 $html = $this->_getTabs();
249 $html .= form_filemanager_render($options);
251 $html .= '<input value="'.$draftitemid.'" name="'.$elname.'" type="hidden" />';
252 // label element needs 'for' attribute work
253 $html .= '<input value="" id="id_'.$elname.'" type="hidden" />';
255 return $html;
260 * Data structure representing a file manager.
262 * This class defines the data structure for file mnager
264 * @package core_form
265 * @copyright 2010 Dongsheng Cai
266 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
267 * @todo do not use this abstraction (skodak)
269 class form_filemanaer_x {
270 /** @var stdClass $options options for filemanager */
271 public $options;
274 * Constructor
276 * @param stdClass $options options for filemanager
278 public function __construct(stdClass $options) {
279 global $CFG, $USER, $PAGE;
280 require_once($CFG->dirroot. '/repository/lib.php');
281 $defaults = array(
282 'maxbytes'=>-1,
283 'maxfiles'=>-1,
284 'itemid'=>0,
285 'subdirs'=>0,
286 'client_id'=>uniqid(),
287 'accepted_types'=>'*',
288 'return_types'=>FILE_INTERNAL,
289 'context'=>$PAGE->context
291 foreach ($defaults as $key=>$value) {
292 if (empty($options->$key)) {
293 $options->$key = $value;
297 $fs = get_file_storage();
299 // initilise options, getting files in root path
300 $this->options = file_get_drafarea_files($options->itemid, '/');
302 // calculate file count
303 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
304 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
305 $filecount = count($files);
306 $this->options->filecount = $filecount;
308 // copying other options
309 foreach ($options as $name=>$value) {
310 $this->options->$name = $value;
313 // building file picker options
314 $params = new stdClass();
315 $params->accepted_types = $options->accepted_types;
316 $params->return_types = $options->return_types;
317 $params->context = $options->context;
318 $params->env = 'filemanager';
319 $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
320 $filepicker_options = initialise_filepicker($params);
321 $this->options->filepicker = $filepicker_options;
326 * Print the file manager
328 * <pre>
329 * $OUTPUT->file_manager($options);
330 * </pre>
332 * @param array $options associative array with file manager options
333 * options are:
334 * maxbytes=>-1,
335 * maxfiles=>-1,
336 * itemid=>0,
337 * subdirs=>false,
338 * client_id=>uniqid(),
339 * acepted_types=>'*',
340 * return_types=>FILE_INTERNAL,
341 * context=>$PAGE->context
342 * @return string HTML fragment
344 function form_filemanager_render($options) {
345 global $CFG, $OUTPUT, $PAGE;
347 $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex
349 static $filemanagertemplateloaded;
351 $html = '';
352 $options = $fm->options;
353 $straddfile = get_string('addfile', 'repository');
354 $strmakedir = get_string('makeafolder', 'moodle');
355 $strdownload = get_string('downloadfolder', 'repository');
356 $strloading = get_string('loading', 'repository');
358 $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
360 $client_id = $options->client_id;
361 $itemid = $options->itemid;
362 list($context, $course, $cm) = get_context_info_array($options->context->id);
363 if (is_object($course)) {
364 $course_maxbytes = $course->maxbytes;
365 } else {
366 $course_maxbytes = $CFG->maxbytes;
369 if ($options->maxbytes == -1 || empty($options->maxbytes)) {
370 $options->maxbytes = $CFG->maxbytes;
373 if (empty($options->filecount)) {
374 $extra = ' style="display:none"';
375 } else {
376 $extra = '';
379 $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes)));
380 $strdndenabled = get_string('dndenabled_insentence', 'moodle').$OUTPUT->help_icon('dndenabled');
381 $html .= <<<FMHTML
382 <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'>
383 $icon_progress
384 </div>
385 <div id="filemanager-wrapper-{$client_id}" style="display:none">
386 <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
387 <div class="filemanager-toolbar">
388 <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" />
389 <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" />
390 <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" />
391 <span> $maxsize </span>
392 <span id="dndenabled-{$client_id}" style="display: none"> - $strdndenabled </span>
393 </div>
394 <div class="filemanager-container" id="filemanager-{$client_id}" style="position: relative" >
395 <ul id="draftfiles-{$client_id}" class="fm-filelist">
396 <li>Loading...</li>
397 </ul>
398 </div>
399 </div>
400 <div class='clearer'></div>
401 FMHTML;
402 if (empty($filemanagertemplateloaded)) {
403 $filemanagertemplateloaded = true;
404 $html .= <<<FMHTML
405 <div id="fm-template" style="display:none">___fullname___ ___action___</div>
406 FMHTML;
409 $module = array(
410 'name'=>'form_filemanager',
411 'fullpath'=>'/lib/form/filemanager.js',
412 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview', 'core_dndupload'),
413 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
414 array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
415 array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
416 array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
417 array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
418 array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
419 array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
420 array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'),
421 array('moving', 'repository'), array('files', 'moodle'), array('serverconnection', 'error')
424 $PAGE->requires->js_module($module);
425 $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
427 // non javascript file manager
428 $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array(
429 'env'=>'filemanager',
430 'action'=>'browse',
431 'itemid'=>$itemid,
432 'subdirs'=>$options->subdirs,
433 'maxbytes'=>$options->maxbytes,
434 'maxfiles'=>$options->maxfiles,
435 'ctx_id'=>$PAGE->context->id,
436 'course'=>$PAGE->course->id,
437 'sesskey'=>sesskey(),
440 $html .= '<noscript>';
441 $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>";
442 $html .= '</noscript>';
445 return $html;