Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / form / url.php
blob93f0cba0441fcfcc4df87d7e3f66299f566fb974
1 <?php
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.
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/>.
18 /**
19 * url type form element
21 * Contains HTML class for a url type element
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
28 require_once("HTML/QuickForm/text.php");
29 require_once('templatable_form_element.php');
31 /**
32 * url type form element
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
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 */
46 var $_helpbutton='';
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;
54 /**
55 * Constructor
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) {
63 global $CFG;
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);
74 $this->_type = 'url';
77 /**
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);
87 /**
88 * Sets label to be hidden
90 * @param bool $hiddenLabel sets if label should be hidden
92 function setHiddenLabel($hiddenLabel){
93 $this->_hiddenLabel = $hiddenLabel;
96 /**
97 * Returns HTML for this form element.
99 * @return string
101 function toHtml(){
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';
110 } else {
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();
119 } else {
120 $str = parent::toHtml();
122 if (empty($this->_options['usefilepicker'])) {
123 return $str;
126 // Print out file picker.
127 $str .= $this->getFilePickerHTML();
128 $str = '<div id="url-wrapper-' . $this->get_filepicker_unique_id() . '">' . $str . '</div>';
130 return $str;
133 public function getFilePickerHTML() {
134 global $PAGE, $OUTPUT;
136 $str = '';
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;
144 $args->env = 'url';
145 $fp = new file_picker($args);
146 $options = $fp->options;
148 if (count($options->repositories) > 0) {
149 $straddlink = get_string('choosealink', 'repository');
150 $str .= <<<EOD
151 <button type="button" id="filepicker-button-js-{$clientid}" class="visibleifjs btn btn-secondary">
152 $straddlink
153 </button>
154 EOD;
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);
163 return $str;
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.
180 * @return string
182 function getElementTemplateType(){
183 if ($this->_flagFrozen){
184 return 'static';
185 } else {
186 return 'default';
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();
200 return $context;
204 * Get force LTR option.
206 * @return bool
208 public function get_force_ltr() {
209 return true;
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;