Merge branch 'MDL-33509-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / form / url.php
CommitLineData
1a03384f 1<?php
6c1fd304
RT
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * url type form element
20 *
21 * Contains HTML class for a url type element
22 *
23 * @package core_form
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
1a03384f 28require_once("HTML/QuickForm/text.php");
344321e1 29require_once('templatable_form_element.php');
1a03384f 30
31/**
6c1fd304 32 * url type form element
1a03384f 33 *
6c1fd304
RT
34 * HTML class for a url type element
35 * @package core_form
36 * @category form
37 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1a03384f 39 */
91bda4cd 40class MoodleQuickForm_url extends HTML_QuickForm_text implements templatable {
344321e1
DW
41 use templatable_form_element {
42 export_for_template as export_for_template_base;
43 }
44
6c1fd304 45 /** @var string html for help button, if empty then no help */
1a03384f 46 var $_helpbutton='';
6c1fd304
RT
47
48 /** @var bool if true label will be hidden */
1a03384f 49 var $_hiddenLabel=false;
50
6c1fd304
RT
51 /**
52 * Constructor
53 *
54 * @param string $elementName Element name
55 * @param mixed $elementLabel Label(s) for an element
56 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
57 * @param array $options data which need to be posted.
58 */
1a0df553 59 public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
1a03384f 60 global $CFG;
61 require_once("$CFG->dirroot/repository/lib.php");
62 $options = (array)$options;
63 foreach ($options as $name=>$value) {
64 $this->_options[$name] = $value;
65 }
e98a55c0 66 if (!isset($this->_options['usefilepicker'])) {
67 $this->_options['usefilepicker'] = true;
68 }
91bda4cd 69
1a0df553 70 parent::__construct($elementName, $elementLabel, $attributes);
91bda4cd 71 $this->_type = 'url';
1a0df553
MG
72 }
73
74 /**
75 * Old syntax of class constructor. Deprecated in PHP7.
76 *
77 * @deprecated since Moodle 3.1
78 */
79 public function MoodleQuickForm_url($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
80 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
81 self::__construct($elementName, $elementLabel, $attributes, $options);
1a03384f 82 }
4a0e2e63 83
6c1fd304
RT
84 /**
85 * Sets label to be hidden
86 *
87 * @param bool $hiddenLabel sets if label should be hidden
88 */
1a03384f 89 function setHiddenLabel($hiddenLabel){
90 $this->_hiddenLabel = $hiddenLabel;
91 }
6c1fd304
RT
92
93 /**
94 * Returns HTML for this form element.
95 *
96 * @return string
97 */
1a03384f 98 function toHtml(){
91bda4cd 99
1a03384f 100 $id = $this->_attributes['id'];
101 $elname = $this->_attributes['name'];
102
fc7f69e7
FM
103 // Add the class at the last minute.
104 if ($this->get_force_ltr()) {
105 if (!isset($this->_attributes['class'])) {
106 $this->_attributes['class'] = 'text-ltr';
107 } else {
108 $this->_attributes['class'] .= ' text-ltr';
109 }
110 }
111
1a03384f 112 if ($this->_hiddenLabel) {
113 $this->_generateId();
114 $str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
115 $this->getLabel().'</label>'.parent::toHtml();
116 } else {
117 $str = parent::toHtml();
118 }
e98a55c0 119 if (empty($this->_options['usefilepicker'])) {
1a03384f 120 return $str;
121 }
fa4fb786 122
e584e6ae 123 // Print out file picker.
91bda4cd
DW
124 $str .= $this->getFilePickerHTML();
125
126 return $str;
127 }
128
e584e6ae 129 public function getFilePickerHTML() {
91bda4cd
DW
130 global $PAGE, $OUTPUT;
131
132 $str = '';
e584e6ae 133 $clientid = uniqid();
1a03384f 134
6bdfef5d 135 $args = new stdClass();
cfa8f37d
DC
136 $args->accepted_types = '*';
137 $args->return_types = FILE_EXTERNAL;
138 $args->context = $PAGE->context;
e584e6ae 139 $args->client_id = $clientid;
bb496de7
DC
140 $args->env = 'url';
141 $fp = new file_picker($args);
142 $options = $fp->options;
cfa8f37d 143
fa4fb786
AD
144 if (count($options->repositories) > 0) {
145 $straddlink = get_string('choosealink', 'repository');
146 $str .= <<<EOD
daa31a04 147<button type="button" id="filepicker-button-js-{$clientid}" class="visibleifjs btn btn-secondary">
fa4fb786
AD
148$straddlink
149</button>
150EOD;
151 }
152
bb496de7
DC
153 // print out file picker
154 $str .= $OUTPUT->render($fp);
cfa8f37d
DC
155
156 $module = array('name'=>'form_url', 'fullpath'=>'/lib/form/url.js', 'requires'=>array('core_filepicker'));
157 $PAGE->requires->js_init_call('M.form_url.init', array($options), true, $module);
cfa8f37d 158
1a03384f 159 return $str;
160 }
1a03384f 161
1a03384f 162 /**
163 * get html for help button
164 *
6c1fd304 165 * @return string html for help button
1a03384f 166 */
167 function getHelpButton(){
168 return $this->_helpbutton;
169 }
6c1fd304 170
1a03384f 171 /**
172 * Slightly different container template when frozen. Don't want to use a label tag
173 * with a for attribute in that case for the element label but instead use a div.
174 * Templates are defined in renderer constructor.
175 *
176 * @return string
177 */
178 function getElementTemplateType(){
179 if ($this->_flagFrozen){
180 return 'static';
181 } else {
182 return 'default';
183 }
184 }
91bda4cd
DW
185
186 public function export_for_template(renderer_base $output) {
344321e1 187 $context = $this->export_for_template_base($output);
0f13ffbc 188 $context['filepickerhtml'] = !empty($this->_options['usefilepicker']) ? $this->getFilePickerHTML() : '';
91bda4cd
DW
189 return $context;
190 }
fc7f69e7
FM
191
192 /**
193 * Get force LTR option.
194 *
195 * @return bool
196 */
197 public function get_force_ltr() {
198 return true;
199 }
200
1a03384f 201}