Merge branch 'm20_MDL-26672_aiccfix' of git://github.com/danmarsden/moodle
[moodle.git] / lib / form / url.php
blob8822aa8ec466a0ae391be7bba811861b8b34b9cd
1 <?php
2 require_once("HTML/QuickForm/text.php");
4 /**
5 * HTML class for a url type element
7 * @author Jamie Pratt
8 * @access public
9 */
10 class MoodleQuickForm_url extends HTML_QuickForm_text{
11 /**
12 * html for help button, if empty then no help
14 * @var string
16 var $_helpbutton='';
17 var $_hiddenLabel=false;
19 function MoodleQuickForm_url($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
20 global $CFG;
21 require_once("$CFG->dirroot/repository/lib.php");
22 $options = (array)$options;
23 foreach ($options as $name=>$value) {
24 $this->_options[$name] = $value;
26 if (!isset($this->_options['usefilepicker'])) {
27 $this->_options['usefilepicker'] = true;
29 parent::HTML_QuickForm_text($elementName, $elementLabel, $attributes);
32 function setHiddenLabel($hiddenLabel){
33 $this->_hiddenLabel = $hiddenLabel;
35 function toHtml(){
36 global $CFG, $COURSE, $USER, $PAGE, $OUTPUT;
38 $id = $this->_attributes['id'];
39 $elname = $this->_attributes['name'];
41 if ($this->_hiddenLabel) {
42 $this->_generateId();
43 $str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
44 $this->getLabel().'</label>'.parent::toHtml();
45 } else {
46 $str = parent::toHtml();
48 if (empty($this->_options['usefilepicker'])) {
49 return $str;
51 $strsaved = get_string('filesaved', 'repository');
52 $straddlink = get_string('choosealink', 'repository');
53 if ($COURSE->id == SITEID) {
54 $context = get_context_instance(CONTEXT_SYSTEM);
55 } else {
56 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
58 $client_id = uniqid();
60 $str .= <<<EOD
61 <button id="filepicker-button-{$client_id}" style="display:none">
62 $straddlink
63 </button>
64 EOD;
65 $args = new stdClass();
66 $args->accepted_types = '*';
67 $args->return_types = FILE_EXTERNAL;
68 $args->context = $PAGE->context;
69 $args->client_id = $client_id;
70 $args->env = 'url';
71 $fp = new file_picker($args);
72 $options = $fp->options;
74 // print out file picker
75 $str .= $OUTPUT->render($fp);
77 $module = array('name'=>'form_url', 'fullpath'=>'/lib/form/url.js', 'requires'=>array('core_filepicker'));
78 $PAGE->requires->js_init_call('M.form_url.init', array($options), true, $module);
79 $PAGE->requires->js_function_call('show_item', array('filepicker-button-'.$client_id));
81 return $str;
83 /**
84 * Automatically generates and assigns an 'id' attribute for the element.
86 * Currently used to ensure that labels work on radio buttons and
87 * checkboxes. Per idea of Alexander Radivanovich.
88 * Overriden in moodleforms to remove qf_ prefix.
90 * @access private
91 * @return void
93 function _generateId()
95 static $idx = 1;
97 if (!$this->getAttribute('id')) {
98 $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
100 } // end func _generateId
102 * set html for help button
104 * @access public
105 * @param array $help array of arguments to make a help button
106 * @param string $function function name to call to get html
108 function setHelpButton($helpbuttonargs, $function='helpbutton'){
109 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
112 * get html for help button
114 * @access public
115 * @return string html for help button
117 function getHelpButton(){
118 return $this->_helpbutton;
121 * Slightly different container template when frozen. Don't want to use a label tag
122 * with a for attribute in that case for the element label but instead use a div.
123 * Templates are defined in renderer constructor.
125 * @return string
127 function getElementTemplateType(){
128 if ($this->_flagFrozen){
129 return 'static';
130 } else {
131 return 'default';