[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/editor/tinymce/ -> lib.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   * TinyMCE text editor integration.
  19   *
  20   * @package    editor
  21   * @subpackage tinymce
  22   * @copyright  2009 Petr Skoda (http://skodak.org)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  class tinymce_texteditor extends texteditor {
  29      /** @var string active version - this is the directory name where to find tinymce code */
  30      public $version = '3.5.10';
  31  
  32      /**
  33       * Is the current browser supported by this editor?
  34       * @return bool
  35       */
  36      public function supported_by_browser() {
  37          // We don't support any browsers which it doesn't support.
  38          return true;
  39      }
  40  
  41      /**
  42       * Returns array of supported text formats.
  43       * @return array
  44       */
  45      public function get_supported_formats() {
  46          // FORMAT_MOODLE is not supported here, sorry.
  47          return array(FORMAT_HTML => FORMAT_HTML);
  48      }
  49  
  50      /**
  51       * Returns text format preferred by this editor.
  52       * @return int
  53       */
  54      public function get_preferred_format() {
  55          return FORMAT_HTML;
  56      }
  57  
  58      /**
  59       * Does this editor support picking from repositories?
  60       * @return bool
  61       */
  62      public function supports_repositories() {
  63          return true;
  64      }
  65  
  66      /**
  67       * Sets up head code if necessary.
  68       */
  69      public function head_setup() {
  70      }
  71  
  72      /**
  73       * Use this editor for give element.
  74       *
  75       * @param string $elementid
  76       * @param array $options
  77       * @param null $fpoptions
  78       */
  79      public function use_editor($elementid, array $options=null, $fpoptions=null) {
  80          global $PAGE, $CFG;
  81          // Note: use full moodle_url instance to prevent standard JS loader, make sure we are using https on profile page if required.
  82          if ($CFG->debugdeveloper) {
  83              $PAGE->requires->js(new moodle_url($CFG->httpswwwroot.'/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce_src.js'));
  84          } else {
  85              $PAGE->requires->js(new moodle_url($CFG->httpswwwroot.'/lib/editor/tinymce/tiny_mce/'.$this->version.'/tiny_mce.js'));
  86          }
  87          $PAGE->requires->js_init_call('M.editor_tinymce.init_editor', array($elementid, $this->get_init_params($elementid, $options)), true);
  88          if ($fpoptions) {
  89              $PAGE->requires->js_init_call('M.editor_tinymce.init_filepicker', array($elementid, $fpoptions), true);
  90          }
  91      }
  92  
  93      protected function get_init_params($elementid, array $options=null) {
  94          global $CFG, $PAGE, $OUTPUT;
  95  
  96          //TODO: we need to implement user preferences that affect the editor setup too
  97  
  98          $directionality = get_string('thisdirection', 'langconfig');
  99          $strtime        = get_string('strftimetime');
 100          $strdate        = get_string('strftimedaydate');
 101          $lang           = current_language();
 102          $contentcss     = $PAGE->theme->editor_css_url()->out(false);
 103  
 104          $context = empty($options['context']) ? context_system::instance() : $options['context'];
 105  
 106          $config = get_config('editor_tinymce');
 107          if (!isset($config->disabledsubplugins)) {
 108              $config->disabledsubplugins = '';
 109          }
 110  
 111          $fontselectlist = empty($config->fontselectlist) ? '' : $config->fontselectlist;
 112  
 113          $langrev = -1;
 114          if (!empty($CFG->cachejs)) {
 115              $langrev = get_string_manager()->get_revision();
 116          }
 117  
 118          $params = array(
 119              'moodle_config' => $config,
 120              'mode' => "exact",
 121              'elements' => $elementid,
 122              'relative_urls' => false,
 123              'document_base_url' => $CFG->httpswwwroot,
 124              'moodle_plugin_base' => "$CFG->httpswwwroot/lib/editor/tinymce/plugins/",
 125              'content_css' => $contentcss,
 126              'language' => $lang,
 127              'directionality' => $directionality,
 128              'plugin_insertdate_dateFormat ' => $strdate,
 129              'plugin_insertdate_timeFormat ' => $strtime,
 130              'theme' => "advanced",
 131              'skin' => "moodle",
 132              'apply_source_formatting' => true,
 133              'remove_script_host' => false,
 134              'entity_encoding' => "raw",
 135              'plugins' => 'lists,table,style,layer,advhr,advlink,emotions,inlinepopups,' .
 136                  'searchreplace,paste,directionality,fullscreen,nonbreaking,contextmenu,' .
 137                  'insertdatetime,save,iespell,preview,print,noneditable,visualchars,' .
 138                  'xhtmlxtras,template,pagebreak',
 139              'gecko_spellcheck' => true,
 140              'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
 141              'theme_advanced_layout_manager' => "SimpleLayout",
 142              'theme_advanced_toolbar_align' => "left",
 143              'theme_advanced_fonts' => $fontselectlist,
 144              'theme_advanced_resize_horizontal' => true,
 145              'theme_advanced_resizing' => true,
 146              'theme_advanced_resizing_min_height' => 30,
 147              'min_height' => 30,
 148              'theme_advanced_toolbar_location' => "top",
 149              'theme_advanced_statusbar_location' => "bottom",
 150              'language_load' => false, // We load all lang strings directly from Moodle.
 151              'langrev' => $langrev,
 152          );
 153  
 154          // Should we override the default toolbar layout unconditionally?
 155          if (!empty($config->customtoolbar) and $customtoolbar = self::parse_toolbar_setting($config->customtoolbar)) {
 156              $i = 1;
 157              foreach ($customtoolbar as $line) {
 158                  $params['theme_advanced_buttons'.$i] = $line;
 159                  $i++;
 160              }
 161          } else {
 162              // At least one line is required.
 163              $params['theme_advanced_buttons1'] = '';
 164          }
 165  
 166          if (!empty($config->customconfig)) {
 167              $config->customconfig = trim($config->customconfig);
 168              $decoded = json_decode($config->customconfig, true);
 169              if (is_array($decoded)) {
 170                  foreach ($decoded as $k=>$v) {
 171                      $params[$k] = $v;
 172                  }
 173              }
 174          }
 175  
 176          if (!empty($options['legacy']) or !empty($options['noclean']) or !empty($options['trusted'])) {
 177              // now deal somehow with non-standard tags, people scream when we do not make moodle code xtml strict,
 178              // but they scream even more when we strip all tags that are not strict :-(
 179              $params['valid_elements'] = 'script[src|type],*[*]'; // for some reason the *[*] does not inlcude javascript src attribute MDL-25836
 180              $params['invalid_elements'] = '';
 181          }
 182          // Add unique moodle elements - unfortunately we have to decide if these are SPANs or DIVs.
 183          $params['extended_valid_elements'] = 'nolink,tex,algebra,lang[lang]';
 184          $params['custom_elements'] = 'nolink,~tex,~algebra,lang';
 185  
 186          //Add onblur event for client side text validation
 187          if (!empty($options['required'])) {
 188              $params['init_instance_callback'] = 'M.editor_tinymce.onblur_event';
 189          }
 190  
 191          // Allow plugins to adjust parameters.
 192          editor_tinymce_plugin::all_update_init_params($params, $context, $options);
 193  
 194          // Remove temporary parameters.
 195          unset($params['moodle_config']);
 196  
 197          return $params;
 198      }
 199  
 200      /**
 201       * Parse the custom toolbar setting.
 202       * @param string $customtoolbar
 203       * @return array csv toolbar lines
 204       */
 205      public static function parse_toolbar_setting($customtoolbar) {
 206          $result = array();
 207          $customtoolbar = trim($customtoolbar);
 208          if ($customtoolbar === '') {
 209              return $result;
 210          }
 211          $customtoolbar = str_replace("\r", "\n", $customtoolbar);
 212          $customtoolbar = strtolower($customtoolbar);
 213          $i = 0;
 214          foreach (explode("\n", $customtoolbar) as $line) {
 215              $line = preg_replace('/[^a-z0-9_,\|\-]/', ',', $line);
 216              $line = str_replace('|', ',|,', $line);
 217              $line = preg_replace('/,,+/', ',', $line);
 218              $line = trim($line, ',|');
 219              if ($line === '') {
 220                  continue;
 221              }
 222              if ($i == 10) {
 223                  // Maximum is ten lines, merge the rest to the last line.
 224                  $result[9] = $result[9].','.$line;
 225              } else {
 226                  $result[] = $line;
 227                  $i++;
 228              }
 229          }
 230          return $result;
 231      }
 232  
 233      /**
 234       * Gets a named plugin object. Will cause fatal error if plugin doesn't
 235       * exist. This is intended for use by plugin files themselves.
 236       *
 237       * @param string $plugin Name of plugin e.g. 'moodleemoticon'
 238       * @return editor_tinymce_plugin Plugin object
 239       */
 240      public function get_plugin($plugin) {
 241          global $CFG;
 242          return editor_tinymce_plugin::get($plugin);
 243      }
 244  
 245      /**
 246       * Equivalent to tinyMCE.baseURL value available from JavaScript,
 247       * always use instead of /../ when referencing tinymce core code from moodle plugins!
 248       *
 249       * @return moodle_url url pointing to the root of TinyMCE javascript code.
 250       */
 251      public function get_tinymce_base_url() {
 252          global $CFG;
 253          return new moodle_url("$CFG->httpswwwroot/lib/editor/tinymce/tiny_mce/$this->version/");
 254      }
 255  
 256  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1