MDL-19002 adding noclean option to editor utility functions
[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 {
c5704ec6 16 var $_helpbutton='';
4287fc0d 17
b318bb6d 18 function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $filetypes = '*', $returnvalue = '*') {
19 $this->filetypes = $filetypes;
20 $this->returnvalue = $returnvalue;
b8d9c719 21 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
43023002 22 }
4287fc0d 23
b8d9c719 24 function setHelpButton($helpbuttonargs, $function='helpbutton') {
25 if (!is_array($helpbuttonargs)) {
26 $helpbuttonargs = array($helpbuttonargs);
27 } else {
28 $helpbuttonargs = $helpbuttonargs;
c5704ec6 29 }
30 //we do this to to return html instead of printing it
31 //without having to specify it in every call to make a button.
32 if ('helpbutton' == $function){
b8d9c719 33 $defaultargs = array('', '', 'moodle', true, false, '', true);
34 $helpbuttonargs = $helpbuttonargs + $defaultargs ;
c5704ec6 35 }
36 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
37 }
4287fc0d 38
b8d9c719 39 function getHelpButton() {
c5704ec6 40 return $this->_helpbutton;
41 }
4287fc0d 42
b8d9c719 43 function getElementTemplateType() {
c5704ec6 44 if ($this->_flagFrozen){
45 return 'nodisplay';
46 } else {
47 return 'default';
48 }
49 }
4287fc0d 50
c5704ec6 51 function toHtml() {
b8d9c719 52 global $CFG, $COURSE, $USER;
53
c5704ec6 54 if ($this->_flagFrozen) {
55 return $this->getFrozenHtml();
b8d9c719 56 }
4287fc0d 57
b8d9c719 58 $currentfile = '';
59 $draftvalue = '';
60 if ($draftid = (int)$this->getValue()) {
61 $fs = get_file_storage();
62 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
63 if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftid, '', false)) {
64 $file = reset($files);
65 $currentfile = $file->get_filename();
66 $draftvalue = 'value="'.$draftid.'"';
b6558c3b 67 }
b8d9c719 68 }
69 $strsaved = get_string('filesaved', 'repository');
70 if ($COURSE->id == SITEID) {
71 $context = get_context_instance(CONTEXT_SYSTEM);
72 } else {
73 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
74 }
e189ec00 75 $client_id = uniqid();
76 $repository_info = repository_get_client($context, $client_id, $this->filetypes, $this->returnvalue);
4287fc0d 77
b8d9c719 78 $id = $this->_attributes['id'];
79 $elname = $this->_attributes['name'];
4287fc0d 80
b8d9c719 81 $str = $this->_getTabs();
82 $str .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" '.$draftvalue.' />';
757f30a2 83
b8d9c719 84 $str .= <<<EOD
d8eb6e18 85<script type="text/javascript">
e189ec00 86function updatefile(client_id, obj) {
87 document.getElementById('repo_info_'+client_id).innerHTML = obj['file'];
c2762f06 88}
e189ec00 89function callpicker(client_id, id) {
d373f63f 90 document.body.className += ' yui-skin-sam';
91 var picker = document.createElement('DIV');
e189ec00 92 picker.id = 'file-picker-'+client_id;
b8d9c719 93 picker.className = 'file-picker';
d373f63f 94 document.body.appendChild(picker);
e189ec00 95 var el=document.getElementById(id);
96 open_filepicker({'env':'filepicker', 'target':el, 'callback':updatefile})
d8eb6e18 97}
98</script>
99EOD;
e189ec00 100 $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 101 return $str;
c5704ec6 102 }
4287fc0d 103
21599d8c 104 function exportValue(&$submitValues, $assoc = false) {
105 return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
106 }
c5704ec6 107}