[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/editor/tinymce/ -> tinymce.class.php (source)

   1  <?php // $Id: tinymce.class.php,v 1.5 2007/01/03 14:44:41 moodler Exp $
   2  
   3  /**
   4   * This file contains the tinymce subclass for moodle editorObject.
   5   *
   6   * @author Janne Mikkonen
   7   * @version  $Id: tinymce.class.php,v 1.5 2007/01/03 14:44:41 moodler Exp $
   8   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   9   * @package editorObject
  10   */
  11  class tinymce extends editorObject {
  12  
  13      /**
  14      * The tinyconf variable holds custom variable keys and values pairs in array.
  15      * @var array $tinyconf
  16      */
  17      var $tinyconf = array();
  18  
  19      /**
  20      * Internal tinyconfkeys is an array of valid configuration keys.
  21      * @var array $tinyconfkeys
  22      */
  23      var $tinyconfkeys = array(
  24          "mode","theme","plugins","language","ask","textarea_trigger",
  25          "editor_selector","editor_deselector","elements","docs_language",
  26          "debug","focus_alert","directionality","auto_reset_designmode",
  27          "auto_focus","nowrap","button_tile_map","auto_resize","browsers",
  28          "dialog_type","accessibility_warnings","accessibility_focus",
  29          "event_elements","table_inline_editing","object_resizing","custom_shortcuts",
  30          "cleanup","valid_elements","extended_valid_elements","invalid_elements",
  31          "verify_css_classes","verify_html","preformatted","encoding","cleanup_on_startup",
  32          "fix_content_duplication","inline_styles","convert_newlines_to_brs","force_br_newlines",
  33          "force_p_newlines","entities","entity_encoding","remove_linebreaks","convert_fonts_to_spans",
  34          "font_size_classes","font_size_style_values","merge_styles_invalid_parents",
  35          "force_hex_style_colors","apply_source_formatting","trim_span_elements","doctype",
  36          "convert_urls","relative_urls","remove_script_host","document_base_url",
  37          "urlconverter_callback","insertlink_callback","insertimage_callback","setupcontent_callback",
  38          "save_callback","onchange_callback","init_instance_callback","file_browser_callback",
  39          "cleanup_callback","handle_event_callback","execcommand_callback","oninit","onpageload",
  40          "content_css","popups_css","editor_css","width","height","visual","visual_table_class",
  41          "custom_undo_redo","custom_undo_redo_levels","custom_undo_redo_keyboard_shortcuts",
  42          "custom_undo_redo_restore_selection","external_link_list_url","external_image_list_url",
  43          "add_form_submit_trigger","add_unload_trigger","submit_patch");
  44  
  45      /**
  46      * Array of valid advanced theme configuration keys.
  47      * @var array $tinythemekeys
  48      */
  49      var $tinythemekeys = array(
  50          "theme_advanced_layout_manager","theme_advanced_blockformats","theme_advanced_styles",
  51          "theme_advanced_source_editor_width","theme_advanced_source_editor_height",
  52          "theme_advanced_toolbar_location","theme_advanced_toolbar_align",
  53          "theme_advanced_statusbar_location","theme_advanced_buttons<1-n>","theme_advanced_buttons<1-n>_add",
  54          "theme_advanced_buttons<1-n>_add_before","theme_advanced_disable","theme_advanced_containers",
  55          "theme_advanced_containers_default_class","theme_advanced_containers_default_align",
  56          "theme_advanced_container_<container>","theme_advanced_container_<container>_class",
  57          "theme_advanced_container_<container>_align","theme_advanced_custom_layout",
  58          "theme_advanced_link_targets","theme_advanced_resizing","theme_advanced_resizing_use_cookie",
  59          "theme_advanced_resize_horizontal","theme_advanced_path","theme_advanced_fonts");
  60  
  61      /**
  62      * The defaults configuration array for internal use.
  63      * @var array $defaults
  64      */
  65      var $defaults = array();
  66  
  67      /**
  68      * For internal usage variable which holds the information
  69      * should dialogs script be printed after configuration.
  70      * @var bool $printdialogs
  71      */
  72      var $printdialogs = false;
  73  
  74      /**
  75      * PHP4 style class constructor.
  76      *
  77      * @param int $courseid
  78      */
  79      function tinymce ($courseid) {
  80          parent::editorObject();
  81          $this->courseid = clean_param($courseid, PARAM_INT);
  82  
  83          $isteacher = isteacher($courseid);
  84  
  85          $this->defaults = array(
  86              "mode"     => "textareas",
  87              "theme"    => $this->cfg->tinymcetheme,
  88              "language" => $this->__get_language(),
  89              "width"    => "100%",
  90              "plugins"      => !empty($this->cfg->tinymceplugins) ?
  91                                $this->cfg->tinymceplugins : '',
  92              "content_css"  => !empty($this->cfg->tinymcecontentcss) ?
  93                                $this->cfg->tinymcecontentcss : '',
  94              "popup_css"    => !empty($this->cfg->tinymcepopupcss) ?
  95                                $this->cfg->tinymcepopupcss : '',
  96              "editor_css"   => !empty($this->cfg->tinymceeditorcss) ?
  97                                $this->cfg->tinymceeditorcss : '',
  98              "file_browser_callback" => has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $courseid)) ? 'moodleFileBrowser' : '',
  99              "convert_urls"  => false,
 100              "relative_urls" => false);
 101  
 102              if ( $this->cfg->tinymcetheme == 'advanced' ) {
 103                  $this->defaults['theme_advanced_buttons1_add'] = "fontselect,fontsizeselect";
 104                  $this->defaults['theme_advanced_buttons2_add'] = "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor,liststyle";
 105                  $this->defaults['theme_advanced_buttons2_add_before'] = "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator";
 106                  $this->defaults['theme_advanced_buttons3_add_before'] = "tablecontrols,separator";
 107                  $this->defaults['theme_advanced_buttons3_add'] = "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen";
 108                  $this->defaults['theme_advanced_toolbar_location'] = "top";
 109                  $this->defaults['theme_advanced_toolbar_align'] = "left";
 110                  $this->defaults['theme_advanced_statusbar_location'] = "bottom";
 111                  $this->defaults['theme_advanced_resizing'] = true;
 112                  $this->defaults['theme_advanced_resize_horizontal'] = true;
 113              }
 114  
 115              $this->printdialogs = has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $courseid)) ? true : false;
 116      }
 117  
 118      /**
 119      * PHP5 style class constructor.
 120      *
 121      * @param int $courseid
 122      */
 123      function __construct($courseid) {
 124          $this->tinymce($courseid);
 125      }
 126  
 127      /**
 128      * Checks configuration key validity.
 129      * @param string $key Configuration key to check.
 130      * @return bool Returns true if key is valid. Otherwise false is returned.
 131      */
 132      function __is_valid_key($key) {
 133  
 134          if ( is_array($key) ) {
 135              return false;
 136          }
 137  
 138          if ( strstr($key, "theme_advanced_") ) { // Search in theme keys.
 139  
 140              foreach ( $this->tinythemekeys as $value ) {
 141                  if ( strstr($key, "<1-n>") ) {
 142                      $value = preg_replace("/<(.*)>/", "([0-9]+)", $value);
 143                  } else {
 144                      $value = preg_replace("/<(.*)>/", "([a-z0-9]+)", $value);
 145                  }
 146  
 147                  if ( preg_match("/^(". $value .")$/i", $key) ) {
 148                      return true;
 149                  }
 150              }
 151  
 152          } else {
 153              if ( in_array($key, $this->tinyconfkeys) ) {
 154                  return true;
 155              }
 156          }
 157          return false;
 158  
 159      }
 160  
 161      /**
 162      * Sets configuration key and value pairs.
 163      * Passed parameters can be key and value pair or
 164      * an associative array of keys and values.
 165      * @todo code example
 166      */
 167      function setconfig () {
 168  
 169          $numargs = func_num_args();
 170  
 171          if ( $numargs > 2 ) {
 172              $this->error("Too many arguments!");
 173              exit;
 174          }
 175  
 176          if ( $numargs < 1 ) {
 177              $this->error("No arguments passed!");
 178              exit;
 179          }
 180  
 181          switch ( $numargs ) {
 182              case 1: // Must be an array.
 183  
 184                  $arg = func_get_arg(0);
 185                  if ( is_array($arg) ) {
 186                      foreach ( $arg as $key => $value ) {
 187                          if ( !is_string($key) ) {
 188                              $this->error("Array is not associative array!");
 189                              exit;
 190                          }
 191                          if ( !$this->__is_valid_key($key) ) {
 192                              $this->error("Invalid configuration key: '$key'");
 193                          }
 194  
 195                          $this->tinyconf[$key] = $value;
 196                      }
 197                  } else {
 198                      $this->error("Given argument is not an array!!!");
 199                  }
 200  
 201              break;
 202              case 2: // Key, Value pair.
 203  
 204                  $key   = func_get_arg(0);
 205                  $value = func_get_arg(1);
 206  
 207                  if ( !$this->__is_valid_key($key) ) {
 208                      $this->error("Invalid configuration key: $key");
 209                  }
 210                  $this->tinyconf[$key] = $value;
 211  
 212              break;
 213          }
 214  
 215      }
 216  
 217      /**
 218      * For internal usage. Print out configuration arrays.
 219      * @param string $conftype Type of configuration.
 220      * @return void
 221      */
 222      function __printconfig ($conftype='') {
 223  
 224          switch ( $conftype ) {
 225              case 'merge': // New config overrides defaults if found.
 226                  $conf = array_merge($this->defaults,$this->tinyconf);
 227              break;
 228              case 'append': // Append mode leave default value if found.
 229                  $conf = $this->defaults;
 230                  $keys = array_keys($this->defaults);
 231                  foreach ( $this->tinyconf as $key => $value ) {
 232                      if ( in_array($key, $keys) ) {
 233                          continue;
 234                      } else {
 235                          $conf[$key] = $value;
 236                      }
 237                  }
 238              break;
 239              case 'default':
 240                  $conf = $this->defaults;
 241              break;
 242              default:
 243                  $conf = $this->tinyconf;
 244          }
 245  
 246          echo "\n";
 247          echo '<script type="text/javascript">'."\n";
 248          echo '//<![CDATA['."\n";
 249          echo '  tinyMCE.init({'."\n";
 250  
 251          if ( !empty($conf) ) {
 252              $max = count($conf);
 253              $cnt = 1;
 254              foreach ( $conf as $key => $value ) {
 255  
 256                  if ( empty($value) ) {
 257                      continue;
 258                  }
 259  
 260                  if ( $cnt > 1 ) {
 261                      echo ',' ."\n";
 262                  }
 263  
 264                  echo "\t" . $key .' : ';
 265  
 266                  if ( is_bool($value) ) {
 267                      echo ($value) ? 'true' : 'false';
 268                  } else {
 269                      echo '"'. $value .'"';
 270                  }
 271  
 272                  $cnt++;
 273              }
 274          }
 275  
 276          echo '  });'."\n";
 277  
 278          if ( $this->printdialogs ) {
 279              $this->__dialogs();
 280          }
 281          echo '//]]>'."\n";
 282          echo '</script>'."\n";
 283  
 284      }
 285  
 286      /**
 287      * Print out code that start up the editor.
 288      * @param string $conftype Configuration type to print.
 289      */
 290      function starteditor($conftype='default') {
 291          $this->__printconfig($conftype);
 292      }
 293  
 294      /**
 295      * For backward compatibility only.
 296      * @param string $name
 297      * @param string $editorhidesomebuttons
 298      */
 299      function use_html_editor($name='', $editorhidesomebuttons='') {
 300          if ( empty($this->tinyconf) ) {
 301              $this->__printconfig('default');
 302          }
 303  
 304          if ( !empty($this->cfg->editorsrc) ) {
 305              unset($this->cfg->editorsrc);
 306          }
 307  
 308      }
 309  
 310      /**
 311      * Print out needed script for custom dialog which is
 312      * needed to provide access to Moodle's files and folders.
 313      * For internal use only.
 314      */
 315      function __dialogs() {
 316          ?>
 317  
 318          function moodleFileBrowser (field_name, url, type, win) {
 319              Dialog("<?php p($this->cfg->wwwroot) ?>/lib/editor/htmlarea/popups/link.php?id=<?php p($this->courseid) ?>", 470, 400, function (param) {
 320  
 321                  if ( !param ) {
 322                      return false;
 323                  }
 324  
 325                  win.document.forms[0].elements[field_name].value = param;
 326  
 327              },null);
 328          }
 329  
 330          <?php
 331      }
 332  
 333      /**
 334      * Try to generate TinyMCE compatible language string from
 335      * current users language. If not successful return default
 336      * language which is english.
 337      * For internal use only.
 338      */
 339      function __get_language() {
 340  
 341          $tinylangdir = $this->cfg->libdir .'/editor/tinymce/jscripts/tiny_mce/langs';
 342          $currentlanguage = current_language();
 343          $defaultlanguage = 'en';
 344  
 345          if ( !$fp = opendir($tinylangdir) ) {
 346              return $defaultlanguage;
 347              exit;
 348          }
 349  
 350          $languages = array();
 351  
 352          while ( ($file = readdir($fp)) !== false ) {
 353              if ( preg_match("/\.js$/i", $file) ) {
 354                  array_push($languages, basename($file, '.js'));
 355              }
 356          }
 357  
 358          if ( $fp ) {
 359              closedir($fp);
 360          }
 361  
 362          // If language is found in array.
 363          if ( in_array($currentlanguage, $languages) ) {
 364              return $currentlanguage;
 365          }
 366  
 367          // Check if two character country code is found (eg. fi, de, sv etc.)
 368          // then return that.
 369          $currentlanguage = str_replace("_utf8", "", $currentlanguage);
 370  
 371          if ( in_array($currentlanguage, $languages) ) {
 372              return $currentlanguage;
 373          }
 374  
 375          return $defaultlanguage;
 376  
 377      }
 378  
 379  }
 380  
 381  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7