Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/lib/form/ -> url.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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      /** @var array data which need to be posted. */
  55      protected $_options;
  56  
  57      /**
  58       * Constructor
  59       *
  60       * @param string $elementName Element name
  61       * @param mixed $elementLabel Label(s) for an element
  62       * @param mixed $attributes Either a typical HTML attribute string or an associative array.
  63       * @param array $options data which need to be posted.
  64       */
  65      public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  66          global $CFG;
  67          require_once("$CFG->dirroot/repository/lib.php");
  68          $options = (array)$options;
  69          foreach ($options as $name=>$value) {
  70              $this->_options[$name] = $value;
  71          }
  72          if (!isset($this->_options['usefilepicker'])) {
  73              $this->_options['usefilepicker'] = true;
  74          }
  75  
  76          parent::__construct($elementName, $elementLabel, $attributes);
  77          $this->_type = 'url';
  78      }
  79  
  80      /**
  81       * Old syntax of class constructor. Deprecated in PHP7.
  82       *
  83       * @deprecated since Moodle 3.1
  84       */
  85      public function MoodleQuickForm_url($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  86          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  87          self::__construct($elementName, $elementLabel, $attributes, $options);
  88      }
  89  
  90      /**
  91       * Sets label to be hidden
  92       *
  93       * @param bool $hiddenLabel sets if label should be hidden
  94       */
  95      function setHiddenLabel($hiddenLabel){
  96          $this->_hiddenLabel = $hiddenLabel;
  97      }
  98  
  99      /**
 100       * Returns HTML for this form element.
 101       *
 102       * @return string
 103       */
 104      function toHtml(){
 105  
 106          $id     = $this->_attributes['id'];
 107          $elname = $this->_attributes['name'];
 108  
 109          // Add the class at the last minute.
 110          if ($this->get_force_ltr()) {
 111              if (!isset($this->_attributes['class'])) {
 112                  $this->_attributes['class'] = 'text-ltr';
 113              } else {
 114                  $this->_attributes['class'] .= ' text-ltr';
 115              }
 116          }
 117  
 118          if ($this->_hiddenLabel) {
 119              $this->_generateId();
 120              $str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
 121                          $this->getLabel().'</label>'.parent::toHtml();
 122          } else {
 123              $str = parent::toHtml();
 124          }
 125          if (empty($this->_options['usefilepicker'])) {
 126              return $str;
 127          }
 128  
 129          // Print out file picker.
 130          $str .= $this->getFilePickerHTML();
 131          $str = '<div id="url-wrapper-' . $this->get_filepicker_unique_id() . '">' . $str . '</div>';
 132  
 133          return $str;
 134      }
 135  
 136      public function getFilePickerHTML() {
 137          global $PAGE, $OUTPUT;
 138  
 139          $str = '';
 140          $clientid = $this->get_filepicker_unique_id();
 141  
 142          $args = new stdClass();
 143          $args->accepted_types = '*';
 144          $args->return_types = FILE_EXTERNAL;
 145          $args->context = $PAGE->context;
 146          $args->client_id = $clientid;
 147          $args->env = 'url';
 148          $fp = new file_picker($args);
 149          $options = $fp->options;
 150  
 151          if (count($options->repositories) > 0) {
 152              $straddlink = get_string('choosealink', 'repository');
 153              $str .= <<<EOD
 154  <button type="button" id="filepicker-button-js-{$clientid}" class="visibleifjs btn btn-secondary">
 155  $straddlink
 156  </button>
 157  EOD;
 158          }
 159  
 160          // print out file picker
 161          $str .= $OUTPUT->render($fp);
 162  
 163          $module = array('name'=>'form_url', 'fullpath'=>'/lib/form/url.js', 'requires'=>array('core_filepicker'));
 164          $PAGE->requires->js_init_call('M.form_url.init', array($options), true, $module);
 165  
 166          return $str;
 167      }
 168  
 169      /**
 170       * get html for help button
 171       *
 172       * @return string html for help button
 173       */
 174      function getHelpButton(){
 175          return $this->_helpbutton;
 176      }
 177  
 178      /**
 179       * Slightly different container template when frozen. Don't want to use a label tag
 180       * with a for attribute in that case for the element label but instead use a div.
 181       * Templates are defined in renderer constructor.
 182       *
 183       * @return string
 184       */
 185      function getElementTemplateType(){
 186          if ($this->_flagFrozen){
 187              return 'static';
 188          } else {
 189              return 'default';
 190          }
 191      }
 192  
 193      public function export_for_template(renderer_base $output) {
 194          $context = $this->export_for_template_base($output);
 195          $context['filepickerhtml'] = !empty($this->_options['usefilepicker']) ? $this->getFilePickerHTML() : '';
 196  
 197          // This will conditionally wrap the element in a div which can be accessed in the DOM using the unique id,
 198          // and allows the filepicker callback to find its respective url field, if multiple URLs are used.
 199          if ($this->_options['usefilepicker']) {
 200              $context['filepickerclientid'] = $this->get_filepicker_unique_id();
 201          }
 202  
 203          return $context;
 204      }
 205  
 206      /**
 207       * Get force LTR option.
 208       *
 209       * @return bool
 210       */
 211      public function get_force_ltr() {
 212          return true;
 213      }
 214  
 215      /**
 216       * Returns the unique id of the file picker associated with this url element, setting it in the process if not set.
 217       *
 218       * @return string the unique id of the file picker.
 219       */
 220      protected function get_filepicker_unique_id() : string {
 221          if (empty($this->filepickeruniqueid)) {
 222              $this->filepickeruniqueid = uniqid();
 223          }
 224          return $this->filepickeruniqueid;
 225      }
 226  }