2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * url type form element
21 * Contains HTML class for a url type element
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once("HTML/QuickForm/text.php");
29 require_once('templatable_form_element.php');
32 * url type form element
34 * HTML class for a url type element
37 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class MoodleQuickForm_url
extends HTML_QuickForm_text
implements templatable
{
41 use templatable_form_element
{
42 export_for_template
as export_for_template_base
;
45 /** @var string html for help button, if empty then no help */
48 /** @var bool if true label will be hidden */
49 var $_hiddenLabel=false;
51 /** @var string the unique id of the filepicker, if enabled.*/
52 protected $filepickeruniqueid;
57 * @param string $elementName Element name
58 * @param mixed $elementLabel Label(s) for an element
59 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
60 * @param array $options data which need to be posted.
62 public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
64 require_once("$CFG->dirroot/repository/lib.php");
65 $options = (array)$options;
66 foreach ($options as $name=>$value) {
67 $this->_options
[$name] = $value;
69 if (!isset($this->_options
['usefilepicker'])) {
70 $this->_options
['usefilepicker'] = true;
73 parent
::__construct($elementName, $elementLabel, $attributes);
78 * Old syntax of class constructor. Deprecated in PHP7.
80 * @deprecated since Moodle 3.1
82 public function MoodleQuickForm_url($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
83 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
84 self
::__construct($elementName, $elementLabel, $attributes, $options);
88 * Sets label to be hidden
90 * @param bool $hiddenLabel sets if label should be hidden
92 function setHiddenLabel($hiddenLabel){
93 $this->_hiddenLabel
= $hiddenLabel;
97 * Returns HTML for this form element.
103 $id = $this->_attributes
['id'];
104 $elname = $this->_attributes
['name'];
106 // Add the class at the last minute.
107 if ($this->get_force_ltr()) {
108 if (!isset($this->_attributes
['class'])) {
109 $this->_attributes
['class'] = 'text-ltr';
111 $this->_attributes
['class'] .= ' text-ltr';
115 if ($this->_hiddenLabel
) {
116 $this->_generateId();
117 $str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
118 $this->getLabel().'</label>'.parent
::toHtml();
120 $str = parent
::toHtml();
122 if (empty($this->_options
['usefilepicker'])) {
126 // Print out file picker.
127 $str .= $this->getFilePickerHTML();
128 $str = '<div id="url-wrapper-' . $this->get_filepicker_unique_id() . '">' . $str . '</div>';
133 public function getFilePickerHTML() {
134 global $PAGE, $OUTPUT;
137 $clientid = $this->get_filepicker_unique_id();
139 $args = new stdClass();
140 $args->accepted_types
= '*';
141 $args->return_types
= FILE_EXTERNAL
;
142 $args->context
= $PAGE->context
;
143 $args->client_id
= $clientid;
145 $fp = new file_picker($args);
146 $options = $fp->options
;
148 if (count($options->repositories
) > 0) {
149 $straddlink = get_string('choosealink', 'repository');
151 <button type="button" id="filepicker-button-js-{$clientid}" class="visibleifjs btn btn-secondary">
157 // print out file picker
158 $str .= $OUTPUT->render($fp);
160 $module = array('name'=>'form_url', 'fullpath'=>'/lib/form/url.js', 'requires'=>array('core_filepicker'));
161 $PAGE->requires
->js_init_call('M.form_url.init', array($options), true, $module);
167 * get html for help button
169 * @return string html for help button
171 function getHelpButton(){
172 return $this->_helpbutton
;
176 * Slightly different container template when frozen. Don't want to use a label tag
177 * with a for attribute in that case for the element label but instead use a div.
178 * Templates are defined in renderer constructor.
182 function getElementTemplateType(){
183 if ($this->_flagFrozen
){
190 public function export_for_template(renderer_base
$output) {
191 $context = $this->export_for_template_base($output);
192 $context['filepickerhtml'] = !empty($this->_options
['usefilepicker']) ?
$this->getFilePickerHTML() : '';
194 // This will conditionally wrap the element in a div which can be accessed in the DOM using the unique id,
195 // and allows the filepicker callback to find its respective url field, if multiple URLs are used.
196 if ($this->_options
['usefilepicker']) {
197 $context['filepickerclientid'] = $this->get_filepicker_unique_id();
204 * Get force LTR option.
208 public function get_force_ltr() {
213 * Returns the unique id of the file picker associated with this url element, setting it in the process if not set.
215 * @return string the unique id of the file picker.
217 protected function get_filepicker_unique_id() : string {
218 if (empty($this->filepickeruniqueid
)) {
219 $this->filepickeruniqueid
= uniqid();
221 return $this->filepickeruniqueid
;