MDL-59299 filemanager: Display what filetypes are accepted
[moodle.git] / lib / form / filepicker.php
CommitLineData
c5704ec6 1<?php
6c1fd304
RT
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.
13//
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/>.
16
17
18/**
19 * Filepicker form element
20 *
21 * Contains HTML class for a single filepicker form element
22 *
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
26 */
ae4a98a1 27
28global $CFG;
c5704ec6 29
30require_once("HTML/QuickForm/button.php");
ae4a98a1 31require_once($CFG->dirroot.'/repository/lib.php');
344321e1 32require_once('templatable_form_element.php');
c5704ec6 33
34/**
6c1fd304
RT
35 * Filepicker form element
36 *
757f30a2 37 * HTML class for a single filepicker element (based on button)
c5704ec6 38 *
6c1fd304
RT
39 * @package core_form
40 * @category form
41 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
58b7d48f 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c5704ec6 43 */
91bda4cd 44class MoodleQuickForm_filepicker extends HTML_QuickForm_input implements templatable {
344321e1
DW
45 use templatable_form_element {
46 export_for_template as export_for_template_base;
47 }
6c1fd304 48 /** @var string html for help button, if empty then no help will icon will be dispalyed. */
12f11f23 49 public $_helpbutton = '';
6c1fd304
RT
50
51 /** @var array options provided to initalize filemanager */
67233725
DC
52 // PHP doesn't support 'key' => $value1 | $value2 in class definition
53 // We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE);
54 // So I have to set null here, and do it in constructor
55 protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>null);
b7335412 56
6c1fd304
RT
57 /**
58 * Constructor
59 *
60 * @param string $elementName (optional) name of the filepicker
61 * @param string $elementLabel (optional) filepicker label
62 * @param array $attributes (optional) Either a typical HTML attribute string
63 * or an associative array
64 * @param array $options set of options to initalize filepicker
65 */
1a0df553 66 public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
2d7c1117 67 global $CFG, $PAGE;
4287fc0d 68
9d54b8cd 69 $options = (array)$options;
70 foreach ($options as $name=>$value) {
71 if (array_key_exists($name, $this->_options)) {
72 $this->_options[$name] = $value;
73 }
74 }
2d7c1117
MG
75 if (empty($options['return_types'])) {
76 $this->_options['return_types'] = FILE_INTERNAL;
67233725 77 }
2d7c1117 78 $fpmaxbytes = 0;
9d54b8cd 79 if (!empty($options['maxbytes'])) {
2d7c1117 80 $fpmaxbytes = $options['maxbytes'];
9d54b8cd 81 }
2d7c1117 82 $coursemaxbytes = 0;
703ffaea 83 if (!empty($PAGE->course->maxbytes)) {
2d7c1117
MG
84 $coursemaxbytes = $PAGE->course->maxbytes;
85 }
845c2ae1 86 $this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $coursemaxbytes, $fpmaxbytes);
c6aa3670 87 $this->_type = 'filepicker';
1a0df553
MG
88 parent::__construct($elementName, $elementLabel, $attributes);
89 }
90
91 /**
92 * Old syntax of class constructor. Deprecated in PHP7.
93 *
94 * @deprecated since Moodle 3.1
95 */
96 public function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
97 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
98 self::__construct($elementName, $elementLabel, $attributes, $options);
43023002 99 }
4287fc0d 100
6c1fd304
RT
101 /**
102 * Returns html for help button.
103 *
104 * @return string html for help button
105 */
b8d9c719 106 function getHelpButton() {
c5704ec6 107 return $this->_helpbutton;
108 }
4287fc0d 109
6c1fd304
RT
110 /**
111 * Returns type of filepicker element
112 *
113 * @return string
114 */
b8d9c719 115 function getElementTemplateType() {
c5704ec6 116 if ($this->_flagFrozen){
117 return 'nodisplay';
118 } else {
119 return 'default';
120 }
121 }
4287fc0d 122
6c1fd304
RT
123 /**
124 * Returns HTML for filepicker form element.
125 *
126 * @return string
127 */
c5704ec6 128 function toHtml() {
bb496de7
DC
129 global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
130 $id = $this->_attributes['id'];
131 $elname = $this->_attributes['name'];
dd070162 132
c5704ec6 133 if ($this->_flagFrozen) {
134 return $this->getFrozenHtml();
b8d9c719 135 }
bb496de7 136 if (!$draftitemid = (int)$this->getValue()) {
2289c0e4 137 // no existing area info provided - let's use fresh new draft area
842f2914
PS
138 $draftitemid = file_get_unused_draft_itemid();
139 $this->setValue($draftitemid);
b8d9c719 140 }
bb496de7 141
b8d9c719 142 if ($COURSE->id == SITEID) {
b0c6dc1c 143 $context = context_system::instance();
b8d9c719 144 } else {
b0c6dc1c 145 $context = context_course::instance($COURSE->id);
b8d9c719 146 }
99eaca9d 147
e189ec00 148 $client_id = uniqid();
4287fc0d 149
6bdfef5d 150 $args = new stdClass();
99eaca9d
DC
151 // need these three to filter repositories list
152 $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
2d7c1117 153 $args->return_types = $this->_options['return_types'];
bb496de7 154 $args->itemid = $draftitemid;
b817205b 155 $args->maxbytes = $this->_options['maxbytes'];
99eaca9d 156 $args->context = $PAGE->context;
4b72f9eb 157 $args->buttonname = $elname.'choose';
63d5c4ac 158 $args->elementname = $elname;
99eaca9d 159
7e074670 160 $html = $this->_getTabs();
bb496de7
DC
161 $fp = new file_picker($args);
162 $options = $fp->options;
be85f7ab 163 $options->context = $PAGE->context;
7e074670 164 $html .= $OUTPUT->render($fp);
4b72f9eb 165 $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
7e074670 166
f08fac7c 167 $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload'));
7e074670
DC
168 $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
169
563d0417
DC
170 $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
171 'env'=>'filepicker',
172 'action'=>'browse',
173 'itemid'=>$draftitemid,
174 'subdirs'=>0,
175 'maxbytes'=>$options->maxbytes,
176 'maxfiles'=>1,
177 'ctx_id'=>$PAGE->context->id,
178 'course'=>$PAGE->course->id,
71267723 179 'sesskey'=>sesskey(),
563d0417
DC
180 ));
181
182 // non js file picker
183 $html .= '<noscript>';
6ef1402e 184 $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
563d0417
DC
185 $html .= '</noscript>';
186
7e074670 187 return $html;
c5704ec6 188 }
4287fc0d 189
6c1fd304
RT
190 /**
191 * export uploaded file
192 *
193 * @param array $submitValues values submitted.
194 * @param bool $assoc specifies if returned array is associative
195 * @return array
196 */
21599d8c 197 function exportValue(&$submitValues, $assoc = false) {
b7335412 198 global $USER;
199
b1eca344
JP
200 $draftitemid = $this->_findValue($submitValues);
201 if (null === $draftitemid) {
202 $draftitemid = $this->getValue();
203 }
204
b7335412 205 // make sure max one file is present and it is not too big
b1eca344 206 if (!is_null($draftitemid)) {
b7335412 207 $fs = get_file_storage();
b0c6dc1c 208 $usercontext = context_user::instance($USER->id);
64f93798 209 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
b7335412 210 $file = array_shift($files);
bc8da017
AD
211 if ($this->_options['maxbytes']
212 and $this->_options['maxbytes'] !== USER_CAN_IGNORE_FILE_SIZE_LIMITS
213 and $file->get_filesize() > $this->_options['maxbytes']) {
214
b7335412 215 // bad luck, somebody tries to sneak in oversized file
216 $file->delete();
217 }
218 foreach ($files as $file) {
219 // only one file expected
220 $file->delete();
221 }
222 }
223 }
5b5206e1
JP
224
225 return $this->_prepareValue($draftitemid, true);
21599d8c 226 }
91bda4cd
DW
227
228 public function export_for_template(renderer_base $output) {
344321e1 229 $context = $this->export_for_template_base($output);
91bda4cd
DW
230 $context['html'] = $this->toHtml();
231 return $context;
232 }
1661204a
DM
233
234 /**
235 * Check that the file has the allowed type.
236 *
237 * @param array $value Draft item id with the uploaded files.
238 * @return string|null Validation error message or null.
239 */
240 public function validateSubmitValue($value) {
241
242 $filetypesutil = new \core_form\filetypes_util();
243 $whitelist = $filetypesutil->normalize_file_types($this->_options['accepted_types']);
244
245 if (empty($whitelist) || $whitelist === ['*']) {
246 // Any file type is allowed, nothing to check here.
247 return;
248 }
249
250 $draftfiles = file_get_drafarea_files($value);
251 $wrongfiles = array();
252
253 if (empty($draftfiles)) {
254 // No file uploaded, nothing to check here.
255 return;
256 }
257
258 foreach ($draftfiles->list as $file) {
259 if (!$filetypesutil->is_allowed_file_type($file->filename, $whitelist)) {
260 $wrongfiles[] = $file->filename;
261 }
262 }
263
264 if ($wrongfiles) {
265 $a = array(
266 'whitelist' => implode(', ', $whitelist),
267 'wrongfiles' => implode(', ', $wrongfiles),
268 );
269 return get_string('err_wrongfileextension', 'core_form', $a);
270 }
271
272 return;
273 }
c5704ec6 274}