on-demand release 2.3dev
[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');
c5704ec6 32
33/**
6c1fd304
RT
34 * Filepicker form element
35 *
757f30a2 36 * HTML class for a single filepicker element (based on button)
c5704ec6 37 *
6c1fd304
RT
38 * @package core_form
39 * @category form
40 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
58b7d48f 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c5704ec6 42 */
b8d9c719 43class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
6c1fd304 44 /** @var string html for help button, if empty then no help will icon will be dispalyed. */
12f11f23 45 public $_helpbutton = '';
6c1fd304
RT
46
47 /** @var array options provided to initalize filemanager */
67233725
DC
48 // PHP doesn't support 'key' => $value1 | $value2 in class definition
49 // We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE);
50 // So I have to set null here, and do it in constructor
51 protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>null);
b7335412 52
6c1fd304
RT
53 /**
54 * Constructor
55 *
56 * @param string $elementName (optional) name of the filepicker
57 * @param string $elementLabel (optional) filepicker label
58 * @param array $attributes (optional) Either a typical HTML attribute string
59 * or an associative array
60 * @param array $options set of options to initalize filepicker
61 */
9d54b8cd 62 function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
63 global $CFG;
4287fc0d 64
9d54b8cd 65 $options = (array)$options;
66 foreach ($options as $name=>$value) {
67 if (array_key_exists($name, $this->_options)) {
68 $this->_options[$name] = $value;
69 }
70 }
67233725
DC
71 if (!empty($options['return_types'])) {
72 $this->_options['return_types'] = FILE_INTERNAL | FILE_REFERENCE;
73 }
9d54b8cd 74 if (!empty($options['maxbytes'])) {
75 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
76 }
c6aa3670 77 $this->_type = 'filepicker';
b8d9c719 78 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
43023002 79 }
4287fc0d 80
6c1fd304
RT
81 /**
82 * Sets help button for filepicker
58b7d48f 83 *
6c1fd304
RT
84 * @param mixed $helpbuttonargs arguments to create help button
85 * @param string $function name of the callback function
86 * @deprecated since Moodle 2.0. Please do not call this function any more.
87 * @todo MDL-31047 this api will be removed.
88 * @see MoodleQuickForm::setHelpButton()
89 */
b8d9c719 90 function setHelpButton($helpbuttonargs, $function='helpbutton') {
4bcc5118 91 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
c5704ec6 92 }
4287fc0d 93
6c1fd304
RT
94 /**
95 * Returns html for help button.
96 *
97 * @return string html for help button
98 */
b8d9c719 99 function getHelpButton() {
c5704ec6 100 return $this->_helpbutton;
101 }
4287fc0d 102
6c1fd304
RT
103 /**
104 * Returns type of filepicker element
105 *
106 * @return string
107 */
b8d9c719 108 function getElementTemplateType() {
c5704ec6 109 if ($this->_flagFrozen){
110 return 'nodisplay';
111 } else {
112 return 'default';
113 }
114 }
4287fc0d 115
6c1fd304
RT
116 /**
117 * Returns HTML for filepicker form element.
118 *
119 * @return string
120 */
c5704ec6 121 function toHtml() {
bb496de7
DC
122 global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
123 $id = $this->_attributes['id'];
124 $elname = $this->_attributes['name'];
dd070162 125
c5704ec6 126 if ($this->_flagFrozen) {
127 return $this->getFrozenHtml();
b8d9c719 128 }
bb496de7 129 if (!$draftitemid = (int)$this->getValue()) {
2289c0e4 130 // no existing area info provided - let's use fresh new draft area
842f2914
PS
131 $draftitemid = file_get_unused_draft_itemid();
132 $this->setValue($draftitemid);
b8d9c719 133 }
bb496de7 134
b8d9c719 135 if ($COURSE->id == SITEID) {
136 $context = get_context_instance(CONTEXT_SYSTEM);
137 } else {
138 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
139 }
99eaca9d 140
e189ec00 141 $client_id = uniqid();
4287fc0d 142
6bdfef5d 143 $args = new stdClass();
99eaca9d
DC
144 // need these three to filter repositories list
145 $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
67233725 146 $args->return_types = FILE_INTERNAL | FILE_REFERENCE;
bb496de7 147 $args->itemid = $draftitemid;
b817205b 148 $args->maxbytes = $this->_options['maxbytes'];
99eaca9d 149 $args->context = $PAGE->context;
4b72f9eb 150 $args->buttonname = $elname.'choose';
63d5c4ac 151 $args->elementname = $elname;
99eaca9d 152
7e074670 153 $html = $this->_getTabs();
bb496de7
DC
154 $fp = new file_picker($args);
155 $options = $fp->options;
be85f7ab 156 $options->context = $PAGE->context;
7e074670 157 $html .= $OUTPUT->render($fp);
4b72f9eb 158 $html .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" class="filepickerhidden"/>';
7e074670 159
f08fac7c 160 $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker', 'node', 'node-event-simulate', 'core_dndupload'));
7e074670
DC
161 $PAGE->requires->js_init_call('M.form_filepicker.init', array($fp->options), true, $module);
162
563d0417
DC
163 $nonjsfilepicker = new moodle_url('/repository/draftfiles_manager.php', array(
164 'env'=>'filepicker',
165 'action'=>'browse',
166 'itemid'=>$draftitemid,
167 'subdirs'=>0,
168 'maxbytes'=>$options->maxbytes,
169 'maxfiles'=>1,
170 'ctx_id'=>$PAGE->context->id,
171 'course'=>$PAGE->course->id,
71267723 172 'sesskey'=>sesskey(),
563d0417
DC
173 ));
174
175 // non js file picker
176 $html .= '<noscript>';
6ef1402e 177 $html .= "<div><object type='text/html' data='$nonjsfilepicker' height='160' width='600' style='border:1px solid #000'></object></div>";
563d0417
DC
178 $html .= '</noscript>';
179
7e074670 180 return $html;
c5704ec6 181 }
4287fc0d 182
6c1fd304
RT
183 /**
184 * export uploaded file
185 *
186 * @param array $submitValues values submitted.
187 * @param bool $assoc specifies if returned array is associative
188 * @return array
189 */
21599d8c 190 function exportValue(&$submitValues, $assoc = false) {
b7335412 191 global $USER;
192
b1eca344
JP
193 $draftitemid = $this->_findValue($submitValues);
194 if (null === $draftitemid) {
195 $draftitemid = $this->getValue();
196 }
197
b7335412 198 // make sure max one file is present and it is not too big
b1eca344 199 if (!is_null($draftitemid)) {
b7335412 200 $fs = get_file_storage();
201 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 202 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id DESC', false)) {
b7335412 203 $file = array_shift($files);
204 if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) {
205 // bad luck, somebody tries to sneak in oversized file
206 $file->delete();
207 }
208 foreach ($files as $file) {
209 // only one file expected
210 $file->delete();
211 }
212 }
213 }
5b5206e1
JP
214
215 return $this->_prepareValue($draftitemid, true);
21599d8c 216 }
c5704ec6 217}