MDL-13766
[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 */
4287fc0d 15class MoodleQuickForm_filepicker extends HTML_QuickForm_button {
c5704ec6 16 var $_helpbutton='';
4287fc0d 17
18 function MoodleQuickForm_filepicker($elementName=null, $value=null, $attributes=null) {
41ebc6d3 19 HTML_QuickForm_input::HTML_QuickForm_input($elementName, $value, $attributes); // Set label cause button doesn't
43023002 20 parent::HTML_QuickForm_button($elementName, $value, $attributes);
43023002 21 }
4287fc0d 22
c5704ec6 23 function setHelpButton($helpbuttonargs, $function='helpbutton'){
24 if (!is_array($helpbuttonargs)){
25 $helpbuttonargs=array($helpbuttonargs);
26 }else{
27 $helpbuttonargs=$helpbuttonargs;
28 }
29 //we do this to to return html instead of printing it
30 //without having to specify it in every call to make a button.
31 if ('helpbutton' == $function){
32 $defaultargs=array('', '', 'moodle', true, false, '', true);
33 $helpbuttonargs=$helpbuttonargs + $defaultargs ;
34 }
35 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
36 }
4287fc0d 37
c5704ec6 38 function getHelpButton(){
39 return $this->_helpbutton;
40 }
4287fc0d 41
c5704ec6 42 function getElementTemplateType(){
43 if ($this->_flagFrozen){
44 return 'nodisplay';
45 } else {
46 return 'default';
47 }
48 }
4287fc0d 49
c5704ec6 50 function toHtml() {
b6558c3b 51 global $CFG, $COURSE;
c5704ec6 52 if ($this->_flagFrozen) {
53 return $this->getFrozenHtml();
54 } else {
757f30a2 55 $currentvalue = $this->getValue();
4287fc0d 56
757f30a2 57 $strsaved = get_string('filesaved', 'repository');
4287fc0d 58 if (empty($COURSE->context)) {
757f30a2 59 $context = get_context_instance(CONTEXT_SYSTEM);
b6558c3b 60 } else {
757f30a2 61 $context = $COURSE->context;
b6558c3b 62 }
757f30a2 63 $repository_info = repository_get_client($context);
64 $suffix = $repository_info['suffix'];
4287fc0d 65
4287fc0d 66 $id = $this->_attributes['id'];
67 $elname = $this->_attributes['name'];
68
77a2e146 69 $str = $this->_getTabs();
757f30a2 70 $str .= '<input type="hidden" value="'.$currentvalue.'" name="'.$this->_attributes['name'].'" id="'.$this->_attributes['id'].'_'.$suffix.'" />';
71 $id = $this->_attributes['id'];
72
d8eb6e18 73 $str .= <<<EOD
74<script type="text/javascript">
a846af6f 75function updatefile_$suffix(str){
76 document.getElementById('repo_info_$suffix').innerHTML = str;
c2762f06 77}
78function callpicker_$suffix(){
d373f63f 79 document.body.className += ' yui-skin-sam';
80 var picker = document.createElement('DIV');
81 picker.id = 'file-picker-$suffix';
911e0eb0 82 picker.className = "file-picker";
d373f63f 83 document.body.appendChild(picker);
e1f29368 84 var el=document.getElementById('${id}_${suffix}');
4287fc0d 85 openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix})
d8eb6e18 86}
87</script>
88EOD;
757f30a2 89 $str .= '<input value ="'.get_string('openpicker', 'repository').'" type="button" onclick=\'callpicker_'.$suffix.'()\' />'.'<span id="repo_info_'.$suffix.'" class="notifysuccess">'.$currentvalue.'</span>'.$repository_info['css'].$repository_info['js'];
d8eb6e18 90 return $str;
c5704ec6 91 }
92 }
4287fc0d 93
21599d8c 94 function exportValue(&$submitValues, $assoc = false) {
95 return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
96 }
c5704ec6 97}