MDL-19002 standardized constructor parameter order
[moodle.git] / lib / form / filepicker.php
CommitLineData
c5704ec6 1<?php
2// $Id$
3
4require_once("HTML/QuickForm/button.php");
5require_once(dirname(dirname(dirname(__FILE__))) . '/repository/lib.php');
6
7/**
757f30a2 8 * HTML class for a single filepicker element (based on button)
c5704ec6 9 *
757f30a2 10 * @author Moodle.com
c5704ec6 11 * @version 1.0
12 * @since Moodle 2.0
13 * @access public
14 */
b8d9c719 15class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
9d54b8cd 16 protected $_helpbutton = '';
17 protected $_options = array('maxbytes'=>0, 'filetypes'=>'*', 'returnvalue'=>'*');
18
19 function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
20 global $CFG;
4287fc0d 21
9d54b8cd 22 $options = (array)$options;
23 foreach ($options as $name=>$value) {
24 if (array_key_exists($name, $this->_options)) {
25 $this->_options[$name] = $value;
26 }
27 }
28 if (!empty($options['maxbytes'])) {
29 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
30 }
b8d9c719 31 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
43023002 32 }
4287fc0d 33
b8d9c719 34 function setHelpButton($helpbuttonargs, $function='helpbutton') {
35 if (!is_array($helpbuttonargs)) {
36 $helpbuttonargs = array($helpbuttonargs);
37 } else {
38 $helpbuttonargs = $helpbuttonargs;
c5704ec6 39 }
40 //we do this to to return html instead of printing it
41 //without having to specify it in every call to make a button.
42 if ('helpbutton' == $function){
b8d9c719 43 $defaultargs = array('', '', 'moodle', true, false, '', true);
44 $helpbuttonargs = $helpbuttonargs + $defaultargs ;
c5704ec6 45 }
46 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
47 }
4287fc0d 48
b8d9c719 49 function getHelpButton() {
c5704ec6 50 return $this->_helpbutton;
51 }
4287fc0d 52
b8d9c719 53 function getElementTemplateType() {
c5704ec6 54 if ($this->_flagFrozen){
55 return 'nodisplay';
56 } else {
57 return 'default';
58 }
59 }
4287fc0d 60
c5704ec6 61 function toHtml() {
b8d9c719 62 global $CFG, $COURSE, $USER;
63
c5704ec6 64 if ($this->_flagFrozen) {
65 return $this->getFrozenHtml();
b8d9c719 66 }
4287fc0d 67
b8d9c719 68 $currentfile = '';
69 $draftvalue = '';
70 if ($draftid = (int)$this->getValue()) {
71 $fs = get_file_storage();
72 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
73 if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftid, '', false)) {
74 $file = reset($files);
75 $currentfile = $file->get_filename();
76 $draftvalue = 'value="'.$draftid.'"';
b6558c3b 77 }
b8d9c719 78 }
79 $strsaved = get_string('filesaved', 'repository');
80 if ($COURSE->id == SITEID) {
81 $context = get_context_instance(CONTEXT_SYSTEM);
82 } else {
83 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
84 }
e189ec00 85 $client_id = uniqid();
9d54b8cd 86 $repository_info = repository_get_client($context, $client_id, $this->_options['filetypes'], $this->_options['returnvalue']);
4287fc0d 87
b8d9c719 88 $id = $this->_attributes['id'];
89 $elname = $this->_attributes['name'];
4287fc0d 90
b8d9c719 91 $str = $this->_getTabs();
92 $str .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" '.$draftvalue.' />';
757f30a2 93
b8d9c719 94 $str .= <<<EOD
d8eb6e18 95<script type="text/javascript">
e189ec00 96function updatefile(client_id, obj) {
97 document.getElementById('repo_info_'+client_id).innerHTML = obj['file'];
c2762f06 98}
e189ec00 99function callpicker(client_id, id) {
d373f63f 100 document.body.className += ' yui-skin-sam';
101 var picker = document.createElement('DIV');
e189ec00 102 picker.id = 'file-picker-'+client_id;
b8d9c719 103 picker.className = 'file-picker';
d373f63f 104 document.body.appendChild(picker);
e189ec00 105 var el=document.getElementById(id);
106 open_filepicker({'env':'filepicker', 'target':el, 'callback':updatefile})
d8eb6e18 107}
108</script>
109EOD;
e189ec00 110 $str .= '<input value="'.get_string('openpicker', 'repository').'" type="button" onclick="callpicker(\''+$client_id+'\', \''+$id+'\')" />'.'<span id="repo_info_'.$client_id.'" class="notifysuccess">'.$currentfile.'</span>'.$repository_info['css'].$repository_info['js'];
b8d9c719 111 return $str;
c5704ec6 112 }
4287fc0d 113
21599d8c 114 function exportValue(&$submitValues, $assoc = false) {
115 return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
116 }
c5704ec6 117}