Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/lib/form/ -> url.php (source)

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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.
  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  
  28  require_once("HTML/QuickForm/text.php");
  29  require_once ('templatable_form_element.php');
  30  
  31  /**
  32   * url type form element
  33   *
  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
  39   */
  40  class MoodleQuickForm_url extends HTML_QuickForm_text implements templatable {
  41      use templatable_form_element {
  42          export_for_template as export_for_template_base;
  43      }
  44  
  45      /** @var string html for help button, if empty then no help */
  46      var $_helpbutton='';
  47  
  48      /** @var bool if true label will be hidden */
  49      var $_hiddenLabel=false;
  50  
  51      /** @var string the unique id of the filepicker, if enabled.*/
  52      protected $filepickeruniqueid;
  53  
  54      /**
  55       * Constructor
  56       *
  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.
  61       */
  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;
  68          }
  69          if (!isset($this->_options['usefilepicker'])) {
  70              $this->_options['usefilepicker'] = true;
  71          }
  72  
  73          parent::__construct($elementName, $elementLabel, $attributes);
  74          $this->_type = 'url';
  75      }
  76  
  77      /**
  78       * Old syntax of class constructor. Deprecated in PHP7.
  79       *
  80       * @deprecated since Moodle 3.1
  81       */
  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);
  85      }
  86  
  87      /**
  88       * Sets label to be hidden
  89       *
  90       * @param bool $hiddenLabel sets if label should be hidden
  91       */
  92      function setHiddenLabel($hiddenLabel){
  93          $this->_hiddenLabel = $hiddenLabel;
  94      }
  95  
  96      /**
  97       * Returns HTML for this form element.
  98       *
  99       * @return string
 100       */
 101      function toHtml(){
 102  
 103          $id     = $this->_attributes['id'];
 104          $elname = $this->_attributes['name'];
 105  
 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';
 112              }
 113          }
 114  
 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();
 121          }
 122          if (empty($this->_options['usefilepicker'])) {
 123              return $str;
 124          }
 125  
 126          // Print out file picker.
 127          $str .= $this->getFilePickerHTML();
 128          $str = '<div id="url-wrapper-' . $this->get_filepicker_unique_id() . '">' . $str . '</div>';
 129  
 130          return $str;
 131      }
 132  
 133      public function getFilePickerHTML() {
 134          global $PAGE, $OUTPUT;
 135  
 136          $str = '';
 137          $clientid = $this->get_filepicker_unique_id();
 138  
 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;
 147  
 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;
 155          }
 156  
 157          // print out file picker
 158          $str .= $OUTPUT->render($fp);
 159  
 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);
 162  
 163          return $str;
 164      }
 165  
 166      /**
 167       * get html for help button
 168       *
 169       * @return string html for help button
 170       */
 171      function getHelpButton(){
 172          return $this->_helpbutton;
 173      }
 174  
 175      /**
 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.
 179       *
 180       * @return string
 181       */
 182      function getElementTemplateType(){
 183          if ($this->_flagFrozen){
 184              return 'static';
 185          } else {
 186              return 'default';
 187          }
 188      }
 189  
 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() : '';
 193  
 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();
 198          }
 199  
 200          return $context;
 201      }
 202  
 203      /**
 204       * Get force LTR option.
 205       *
 206       * @return bool
 207       */
 208      public function get_force_ltr() {
 209          return true;
 210      }
 211  
 212      /**
 213       * Returns the unique id of the file picker associated with this url element, setting it in the process if not set.
 214       *
 215       * @return string the unique id of the file picker.
 216       */
 217      protected function get_filepicker_unique_id() : string {
 218          if (empty($this->filepickeruniqueid)) {
 219              $this->filepickeruniqueid = uniqid();
 220          }
 221          return $this->filepickeruniqueid;
 222      }
 223  }