[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> editor.php (source)

   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   * Editor input element
  20   *
  21   * Contains class to create preffered editor form element
  22   *
  23   * @package   core_form
  24   * @copyright 2009 Petr Skoda {@link http://skodak.org}
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  
  30  require_once('HTML/QuickForm/element.php');
  31  require_once($CFG->dirroot.'/lib/filelib.php');
  32  require_once($CFG->dirroot.'/repository/lib.php');
  33  
  34  /**
  35   * Editor element
  36   *
  37   * It creates preffered editor (textbox/TinyMce) form element for the format (Text/HTML) selected.
  38   *
  39   * @package   core_form
  40   * @category  form
  41   * @copyright 2009 Petr Skoda {@link http://skodak.org}
  42   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   * @todo      MDL-29421 element Freezing
  44   * @todo      MDL-29426 ajax format conversion
  45   */
  46  class MoodleQuickForm_editor extends HTML_QuickForm_element {
  47      /** @var string html for help button, if empty then no help will icon will be dispalyed. */
  48      public $_helpbutton = '';
  49  
  50      /** @var string defines the type of editor */
  51      public $_type       = 'editor';
  52  
  53      /** @var array options provided to initalize filepicker */
  54      protected $_options = array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 0, 'changeformat' => 0,
  55              'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED, 'context' => null, 'noclean' => 0, 'trusttext' => 0,
  56              'return_types' => 7, 'enable_filemanagement' => true);
  57      // $_options['return_types'] = FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE
  58  
  59      /** @var array values for editor */
  60      protected $_values     = array('text'=>null, 'format'=>null, 'itemid'=>null);
  61  
  62      /**
  63       * Constructor
  64       *
  65       * @param string $elementName (optional) name of the editor
  66       * @param string $elementLabel (optional) editor label
  67       * @param array $attributes (optional) Either a typical HTML attribute string
  68       *              or an associative array
  69       * @param array $options set of options to initalize filepicker
  70       */
  71      public function __construct($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
  72          global $CFG, $PAGE;
  73  
  74          $options = (array)$options;
  75          foreach ($options as $name=>$value) {
  76              if (array_key_exists($name, $this->_options)) {
  77                  $this->_options[$name] = $value;
  78              }
  79          }
  80          if (!empty($options['maxbytes'])) {
  81              $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
  82          }
  83          if (!$this->_options['context']) {
  84              // trying to set context to the current page context to make legacy files show in filepicker (e.g. forum post)
  85              if (!empty($PAGE->context->id)) {
  86                  $this->_options['context'] = $PAGE->context;
  87              } else {
  88                  $this->_options['context'] = context_system::instance();
  89              }
  90          }
  91          $this->_options['trusted'] = trusttext_trusted($this->_options['context']);
  92          parent::__construct($elementName, $elementLabel, $attributes);
  93  
  94          // Note: for some reason the code using this setting does not like bools.
  95          $this->_options['subdirs'] = (int)($this->_options['subdirs'] == 1);
  96  
  97          editors_head_setup();
  98      }
  99  
 100      /**
 101       * Old syntax of class constructor. Deprecated in PHP7.
 102       *
 103       * @deprecated since Moodle 3.1
 104       */
 105      public function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
 106          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 107          self::__construct($elementName, $elementLabel, $attributes, $options);
 108      }
 109  
 110      /**
 111       * Called by HTML_QuickForm whenever form event is made on this element
 112       *
 113       * @param string $event Name of event
 114       * @param mixed $arg event arguments
 115       * @param object $caller calling object
 116       * @return bool
 117       */
 118      function onQuickFormEvent($event, $arg, &$caller)
 119      {
 120          switch ($event) {
 121              case 'createElement':
 122                  $caller->setType($arg[0] . '[format]', PARAM_ALPHANUM);
 123                  $caller->setType($arg[0] . '[itemid]', PARAM_INT);
 124                  break;
 125          }
 126          return parent::onQuickFormEvent($event, $arg, $caller);
 127      }
 128  
 129      /**
 130       * Sets name of editor
 131       *
 132       * @param string $name name of the editor
 133       */
 134      function setName($name) {
 135          $this->updateAttributes(array('name'=>$name));
 136      }
 137  
 138      /**
 139       * Returns name of element
 140       *
 141       * @return string
 142       */
 143      function getName() {
 144          return $this->getAttribute('name');
 145      }
 146  
 147      /**
 148       * Updates editor values, if part of $_values
 149       *
 150       * @param array $values associative array of values to set
 151       */
 152      function setValue($values) {
 153          $values = (array)$values;
 154          foreach ($values as $name=>$value) {
 155              if (array_key_exists($name, $this->_values)) {
 156                  $this->_values[$name] = $value;
 157              }
 158          }
 159      }
 160  
 161      /**
 162       * Returns editor values
 163       *
 164       * @return array
 165       */
 166      function getValue() {
 167          return $this->_values;
 168      }
 169  
 170      /**
 171       * Returns maximum file size which can be uploaded
 172       *
 173       * @return int
 174       */
 175      function getMaxbytes() {
 176          return $this->_options['maxbytes'];
 177      }
 178  
 179      /**
 180       * Sets maximum file size which can be uploaded
 181       *
 182       * @param int $maxbytes file size
 183       */
 184      function setMaxbytes($maxbytes) {
 185          global $CFG;
 186          $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $maxbytes);
 187      }
 188  
 189       /**
 190       * Returns the maximum size of the area.
 191       *
 192       * @return int
 193       */
 194      function getAreamaxbytes() {
 195          return $this->_options['areamaxbytes'];
 196      }
 197  
 198      /**
 199       * Sets the maximum size of the area.
 200       *
 201       * @param int $areamaxbytes size limit
 202       */
 203      function setAreamaxbytes($areamaxbytes) {
 204          $this->_options['areamaxbytes'] = $areamaxbytes;
 205      }
 206  
 207      /**
 208       * Returns maximum number of files which can be uploaded
 209       *
 210       * @return int
 211       */
 212      function getMaxfiles() {
 213          return $this->_options['maxfiles'];
 214      }
 215  
 216      /**
 217       * Sets maximum number of files which can be uploaded.
 218       *
 219       * @param int $num number of files
 220       */
 221      function setMaxfiles($num) {
 222          $this->_options['maxfiles'] = $num;
 223      }
 224  
 225      /**
 226       * Returns true if subdirectoy can be created, else false
 227       *
 228       * @return bool
 229       */
 230      function getSubdirs() {
 231          return $this->_options['subdirs'];
 232      }
 233  
 234      /**
 235       * Set option to create sub directory, while uploading  file
 236       *
 237       * @param bool $allow true if sub directory can be created.
 238       */
 239      function setSubdirs($allow) {
 240          $this->_options['subdirs'] = (int)($allow == 1);
 241      }
 242  
 243      /**
 244       * Returns editor format
 245       *
 246       * @return int.
 247       */
 248      function getFormat() {
 249          return $this->_values['format'];
 250      }
 251  
 252      /**
 253       * Checks if editor used is a required field
 254       *
 255       * @return bool true if required field.
 256       */
 257      function isRequired() {
 258          return (isset($this->_options['required']) && $this->_options['required']);
 259      }
 260  
 261      /**
 262       * @deprecated since Moodle 2.0
 263       */
 264      function setHelpButton($_helpbuttonargs, $function='_helpbutton') {
 265          throw new coding_exception('setHelpButton() can not be used any more, please see MoodleQuickForm::addHelpButton().');
 266      }
 267  
 268      /**
 269       * Returns html for help button.
 270       *
 271       * @return string html for help button
 272       */
 273      function getHelpButton() {
 274          return $this->_helpbutton;
 275      }
 276  
 277      /**
 278       * Returns type of editor element
 279       *
 280       * @return string
 281       */
 282      function getElementTemplateType() {
 283          if ($this->_flagFrozen){
 284              return 'nodisplay';
 285          } else {
 286              return 'default';
 287          }
 288      }
 289  
 290      /**
 291       * Returns HTML for editor form element.
 292       *
 293       * @return string
 294       */
 295      function toHtml() {
 296          global $CFG, $PAGE;
 297          require_once($CFG->dirroot.'/repository/lib.php');
 298  
 299          if ($this->_flagFrozen) {
 300              return $this->getFrozenHtml();
 301          }
 302  
 303          $ctx = $this->_options['context'];
 304  
 305          $id           = $this->_attributes['id'];
 306          $elname       = $this->_attributes['name'];
 307  
 308          $subdirs      = $this->_options['subdirs'];
 309          $maxbytes     = $this->_options['maxbytes'];
 310          $areamaxbytes = $this->_options['areamaxbytes'];
 311          $maxfiles     = $this->_options['maxfiles'];
 312          $changeformat = $this->_options['changeformat']; // TO DO: implement as ajax calls
 313  
 314          $text         = $this->_values['text'];
 315          $format       = $this->_values['format'];
 316          $draftitemid  = $this->_values['itemid'];
 317  
 318          // security - never ever allow guest/not logged in user to upload anything
 319          if (isguestuser() or !isloggedin()) {
 320              $maxfiles = 0;
 321          }
 322  
 323          $str = $this->_getTabs();
 324          $str .= '<div>';
 325  
 326          $editor = editors_get_preferred_editor($format);
 327          $strformats = format_text_menu();
 328          $formats =  $editor->get_supported_formats();
 329          foreach ($formats as $fid) {
 330              $formats[$fid] = $strformats[$fid];
 331          }
 332  
 333          // get filepicker info
 334          //
 335          $fpoptions = array();
 336          if ($maxfiles != 0 ) {
 337              if (empty($draftitemid)) {
 338                  // no existing area info provided - let's use fresh new draft area
 339                  require_once("$CFG->libdir/filelib.php");
 340                  $this->setValue(array('itemid'=>file_get_unused_draft_itemid()));
 341                  $draftitemid = $this->_values['itemid'];
 342              }
 343  
 344              $args = new stdClass();
 345              // need these three to filter repositories list
 346              $args->accepted_types = array('web_image');
 347              $args->return_types = $this->_options['return_types'];
 348              $args->context = $ctx;
 349              $args->env = 'filepicker';
 350              // advimage plugin
 351              $image_options = initialise_filepicker($args);
 352              $image_options->context = $ctx;
 353              $image_options->client_id = uniqid();
 354              $image_options->maxbytes = $this->_options['maxbytes'];
 355              $image_options->areamaxbytes = $this->_options['areamaxbytes'];
 356              $image_options->env = 'editor';
 357              $image_options->itemid = $draftitemid;
 358  
 359              // moodlemedia plugin
 360              $args->accepted_types = array('video', 'audio');
 361              $media_options = initialise_filepicker($args);
 362              $media_options->context = $ctx;
 363              $media_options->client_id = uniqid();
 364              $media_options->maxbytes  = $this->_options['maxbytes'];
 365              $media_options->areamaxbytes  = $this->_options['areamaxbytes'];
 366              $media_options->env = 'editor';
 367              $media_options->itemid = $draftitemid;
 368  
 369              // advlink plugin
 370              $args->accepted_types = '*';
 371              $link_options = initialise_filepicker($args);
 372              $link_options->context = $ctx;
 373              $link_options->client_id = uniqid();
 374              $link_options->maxbytes  = $this->_options['maxbytes'];
 375              $link_options->areamaxbytes  = $this->_options['areamaxbytes'];
 376              $link_options->env = 'editor';
 377              $link_options->itemid = $draftitemid;
 378  
 379              $fpoptions['image'] = $image_options;
 380              $fpoptions['media'] = $media_options;
 381              $fpoptions['link'] = $link_options;
 382          }
 383  
 384          //If editor is required and tinymce, then set required_tinymce option to initalize tinymce validation.
 385          if (($editor instanceof tinymce_texteditor)  && !is_null($this->getAttribute('onchange'))) {
 386              $this->_options['required'] = true;
 387          }
 388  
 389          // print text area - TODO: add on-the-fly switching, size configuration, etc.
 390          $editor->set_text($text);
 391          $editor->use_editor($id, $this->_options, $fpoptions);
 392  
 393          $rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];
 394          $cols = empty($this->_attributes['cols']) ? 80 : $this->_attributes['cols'];
 395  
 396          //Apply editor validation if required field
 397          $editorrules = '';
 398          if (!is_null($this->getAttribute('onblur')) && !is_null($this->getAttribute('onchange'))) {
 399              $editorrules = ' onblur="'.htmlspecialchars($this->getAttribute('onblur')).'" onchange="'.htmlspecialchars($this->getAttribute('onchange')).'"';
 400          }
 401          $str .= '<div><textarea id="'.$id.'" name="'.$elname.'[text]" rows="'.$rows.'" cols="'.$cols.'" spellcheck="true"'.$editorrules.'>';
 402          $str .= s($text);
 403          $str .= '</textarea></div>';
 404  
 405          $str .= '<div>';
 406          if (count($formats)>1) {
 407              $str .= html_writer::label(get_string('format'), 'menu'. $elname. 'format', false, array('class' => 'accesshide'));
 408              $str .= html_writer::select($formats, $elname.'[format]', $format, false, array('id' => 'menu'. $elname. 'format'));
 409          } else {
 410              $keys = array_keys($formats);
 411              $str .= html_writer::empty_tag('input',
 412                      array('name'=>$elname.'[format]', 'type'=> 'hidden', 'value' => array_pop($keys)));
 413          }
 414          $str .= '</div>';
 415  
 416          // during moodle installation, user area doesn't exist
 417          // so we need to disable filepicker here.
 418          if (!during_initial_install() && empty($CFG->adminsetuppending)) {
 419              // 0 means no files, -1 unlimited
 420              if ($maxfiles != 0 ) {
 421                  $str .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $elname.'[itemid]',
 422                          'value' => $draftitemid));
 423  
 424                  // used by non js editor only
 425                  $editorurl = new moodle_url("$CFG->wwwroot/repository/draftfiles_manager.php", array(
 426                      'action'=>'browse',
 427                      'env'=>'editor',
 428                      'itemid'=>$draftitemid,
 429                      'subdirs'=>$subdirs,
 430                      'maxbytes'=>$maxbytes,
 431                      'areamaxbytes' => $areamaxbytes,
 432                      'maxfiles'=>$maxfiles,
 433                      'ctx_id'=>$ctx->id,
 434                      'course'=>$PAGE->course->id,
 435                      'sesskey'=>sesskey(),
 436                      ));
 437                  $str .= '<noscript>';
 438                  $str .= "<div><object type='text/html' data='$editorurl' height='160' width='600' style='border:1px solid #000'></object></div>";
 439                  $str .= '</noscript>';
 440              }
 441          }
 442  
 443  
 444          $str .= '</div>';
 445  
 446          return $str;
 447      }
 448  
 449      /**
 450       * What to display when element is frozen.
 451       *
 452       * @return empty string
 453       */
 454      function getFrozenHtml() {
 455  
 456          return '';
 457      }
 458  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1