MDL-13766
[moodle.git] / lib / form / filepicker.php
1 <?php
2 // $Id$
4 require_once("HTML/QuickForm/button.php");
5 require_once(dirname(dirname(dirname(__FILE__))) . '/repository/lib.php');
7 /**
8  * HTML class for a single filepicker element (based on button)
9  *
10  * @author       Moodle.com
11  * @version      1.0
12  * @since        Moodle 2.0
13  * @access       public
14  */
15 class MoodleQuickForm_filepicker extends HTML_QuickForm_button {
16     var $_helpbutton='';
18     function MoodleQuickForm_filepicker($elementName=null, $value=null, $attributes=null) {
19         HTML_QuickForm_input::HTML_QuickForm_input($elementName, $value, $attributes); // Set label cause button doesn't
20         parent::HTML_QuickForm_button($elementName, $value, $attributes);
21     }
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     }
38     function getHelpButton(){
39         return $this->_helpbutton;
40     }
42     function getElementTemplateType(){
43         if ($this->_flagFrozen){
44             return 'nodisplay';
45         } else {
46             return 'default';
47         }
48     }
50     function toHtml() {
51         global $CFG, $COURSE;
52         if ($this->_flagFrozen) {
53             return $this->getFrozenHtml();
54         } else {
55             $currentvalue = $this->getValue();
57             $strsaved = get_string('filesaved', 'repository');
58             if (empty($COURSE->context)) {
59                 $context = get_context_instance(CONTEXT_SYSTEM);
60             } else {
61                 $context = $COURSE->context;
62             }
63             $repository_info = repository_get_client($context);
64             $suffix = $repository_info['suffix'];
66             $id     = $this->_attributes['id'];
67             $elname = $this->_attributes['name'];
69             $str = $this->_getTabs();
70             $str .= '<input type="hidden" value="'.$currentvalue.'" name="'.$this->_attributes['name'].'" id="'.$this->_attributes['id'].'_'.$suffix.'" />';
71             $id = $this->_attributes['id'];
73             $str .= <<<EOD
74 <script type="text/javascript">
75 function updatefile_$suffix(str){
76     document.getElementById('repo_info_$suffix').innerHTML = str;
77 }
78 function callpicker_$suffix(){
79     document.body.className += ' yui-skin-sam';
80     var picker = document.createElement('DIV');
81     picker.id = 'file-picker-$suffix';
82     picker.className = "file-picker";
83     document.body.appendChild(picker);
84     var el=document.getElementById('${id}_${suffix}');
85     openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix})
86 }
87 </script>
88 EOD;
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'];
90             return $str;
91         }
92     }
94     function exportValue(&$submitValues, $assoc = false) {
95         return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
96     }
97 }