MDL-24474, fixed warning message from filepicker element, fixed missing packageurl...
[moodle.git] / lib / form / filepicker.php
CommitLineData
c5704ec6 1<?php
ae4a98a1 2
3global $CFG;
c5704ec6 4
5require_once("HTML/QuickForm/button.php");
ae4a98a1 6require_once($CFG->dirroot.'/repository/lib.php');
c5704ec6 7
8/**
757f30a2 9 * HTML class for a single filepicker element (based on button)
c5704ec6 10 *
757f30a2 11 * @author Moodle.com
c5704ec6 12 * @version 1.0
13 * @since Moodle 2.0
14 * @access public
15 */
b8d9c719 16class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
12f11f23 17 public $_helpbutton = '';
99eaca9d 18 protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
b7335412 19
9d54b8cd 20 function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
21 global $CFG;
4287fc0d 22
9d54b8cd 23 $options = (array)$options;
24 foreach ($options as $name=>$value) {
25 if (array_key_exists($name, $this->_options)) {
26 $this->_options[$name] = $value;
27 }
28 }
29 if (!empty($options['maxbytes'])) {
30 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
31 }
b8d9c719 32 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
43023002 33 }
4287fc0d 34
b8d9c719 35 function setHelpButton($helpbuttonargs, $function='helpbutton') {
4bcc5118 36 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
c5704ec6 37 }
4287fc0d 38
b8d9c719 39 function getHelpButton() {
c5704ec6 40 return $this->_helpbutton;
41 }
4287fc0d 42
b8d9c719 43 function getElementTemplateType() {
c5704ec6 44 if ($this->_flagFrozen){
45 return 'nodisplay';
46 } else {
47 return 'default';
48 }
49 }
4287fc0d 50
c5704ec6 51 function toHtml() {
bb496de7
DC
52 global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
53 $id = $this->_attributes['id'];
54 $elname = $this->_attributes['name'];
dd070162 55
c5704ec6 56 if ($this->_flagFrozen) {
57 return $this->getFrozenHtml();
b8d9c719 58 }
bb496de7 59 if (!$draftitemid = (int)$this->getValue()) {
2289c0e4 60 // no existing area info provided - let's use fresh new draft area
842f2914
PS
61 $draftitemid = file_get_unused_draft_itemid();
62 $this->setValue($draftitemid);
b8d9c719 63 }
bb496de7 64
b8d9c719 65 if ($COURSE->id == SITEID) {
66 $context = get_context_instance(CONTEXT_SYSTEM);
67 } else {
68 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
69 }
99eaca9d 70
e189ec00 71 $client_id = uniqid();
4287fc0d 72
6bdfef5d 73 $args = new stdClass();
99eaca9d
DC
74 // need these three to filter repositories list
75 $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
76 $args->return_types = FILE_INTERNAL;
bb496de7 77 $args->itemid = $draftitemid;
b817205b 78 $args->maxbytes = $this->_options['maxbytes'];
99eaca9d 79 $args->context = $PAGE->context;
99eaca9d 80
7e074670 81 $html = $this->_getTabs();
bb496de7
DC
82 $fp = new file_picker($args);
83 $options = $fp->options;
be85f7ab 84 $options->context = $PAGE->context;
7e074670
DC
85 $html .= $OUTPUT->render($fp);
86 $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" />';
87
88 $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker'));
89 $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
90
563d0417
DC
91 $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
92 'env'=>'filepicker',
93 'action'=>'browse',
94 'itemid'=>$draftitemid,
95 'subdirs'=>0,
96 'maxbytes'=>$options->maxbytes,
97 'maxfiles'=>1,
98 'ctx_id'=>$PAGE->context->id,
99 'course'=>$PAGE->course->id,
71267723 100 'sesskey'=>sesskey(),
563d0417
DC
101 ));
102
103 // non js file picker
104 $html .= '<noscript>';
6ef1402e 105 $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
563d0417
DC
106 $html .= '</noscript>';
107
7e074670 108 return $html;
c5704ec6 109 }
4287fc0d 110
21599d8c 111 function exportValue(&$submitValues, $assoc = false) {
b7335412 112 global $USER;
113
114 // make sure max one file is present and it is not too big
783f1486
DC
115 if (!empty($submitValues[$this->_attributes['name']])) {
116 $draftitemid = $submitValues[$this->_attributes['name']];
b7335412 117 $fs = get_file_storage();
118 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 119 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
b7335412 120 $file = array_shift($files);
121 if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) {
122 // bad luck, somebody tries to sneak in oversized file
123 $file->delete();
124 }
125 foreach ($files as $file) {
126 // only one file expected
127 $file->delete();
128 }
129 }
783f1486
DC
130 return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
131 } else {
132 return null;
b7335412 133 }
134
21599d8c 135 }
c5704ec6 136}