[ Index ]

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

title

Body

[close]

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

   1  <?php // $Id: htmlarea.class.php,v 1.4.2.1 2008/05/02 03:31:50 scyrma Exp $
   2  
   3  /**
   4   * This file contains the htmlarea subclass for moodle editorObject.
   5   *
   6   * @author Janne Mikkonen
   7   * @version  $Id: htmlarea.class.php,v 1.4.2.1 2008/05/02 03:31:50 scyrma Exp $
   8   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   9   * @package editorObject
  10   */
  11  class htmlarea extends editorObject {
  12  
  13      /**
  14      * Configuration array to hold configuration data.
  15      * @var array $htmlareaconf
  16      */
  17      var $htmlareaconf = array();
  18  
  19      /**
  20      * Configuration keys array to store possible configuration keys.
  21      * @var array $htmlareaconfkeys
  22      */
  23      var $htmlareaconfkeys = array("width","height","statusBar","undoSteps","undoTimeout",
  24                                    "sizeIncludesToolbar","fullPage","pageStyle","killWordOnPaste",
  25                                    "toolbar","fontname","fontsize","formatblock","customSelects");
  26  
  27      /**
  28      * An array to store valid value types that can
  29      * be passed to specific configuration key.
  30      * @var array $htmlareaconfkeytypes
  31      */
  32      var $htmlareaconfkeytypes = array('width' => 'string', 'height' => 'string', 'statusBar' => 'bool',
  33                                        'undoSteps' => 'int', 'undoTimeout' => 'int',
  34                                        'sizeIncludeToolbar' => 'bool', 'fullPage' => 'bool',
  35                                        'pageStyle' => 'string', 'killWordOnPaste' => 'bool',
  36                                        'toolbar' => 'array', 'fontname' => 'assoc', 'fontsize' => 'assoc',
  37                                        'formatblock' => 'assoc', 'customSelects' => 'array');
  38  
  39      /**
  40      * Array of default configuration set via editor settings.
  41      * @var array $defaults
  42      */
  43      var $defaults = array();
  44  
  45      /**
  46      * PHP4 style class constructor.
  47      *
  48      * @param int $courseid Courseid.
  49      */
  50      function htmlarea($courseid) {
  51          parent::editorObject();
  52          $this->courseid = clean_param($courseid, PARAM_INT);
  53  
  54          $pagestyle  = 'body {';
  55          $pagestyle .= !empty($this->cfg->editorbackgroundcolor) ?
  56                               ' background-color: '. $this->cfg->editorbackgroundcolor .'; ' : '';
  57          $pagestyle .= !empty($this->cfg->editorfontfamily) ?
  58                               ' font-family: '. $this->cfg->editorfontfamily .';' : '';
  59          $pagestyle .= !empty($this->cfg->editorfontsize) ?
  60                               ' font-size: '. $this->cfg->editorfontsize .';' : '';
  61          $pagestyle .= '}';
  62  
  63          $this->defaults['pageStyle'] = $pagestyle;
  64          $this->defaults['killWordOnPaste'] = !empty($this->cfg->editorkillword) ? true : false;
  65  
  66          $fontlist = isset($this->cfg->editorfontlist) ? explode(';', $this->cfg->editorfontlist) : array();
  67          $fonts = array();
  68          foreach ( $fontlist as $fontline ) {
  69              if ( !empty($fontline) ) {
  70                  list($fontkey, $fontvalue) = split(":", $fontline);
  71                  $fonts[$fontkey] = $fontvalue;
  72              }
  73          }
  74          $this->defaults['fontname'] = $fonts;
  75          $this->defaults['hideSomeButtons'] = !empty($this->cfg->editorhidebuttons) ?
  76                                                      ' '. $this->cfg->editorhidebuttons .' ' : '';
  77  
  78      }
  79  
  80      /**
  81      * PHP5 style class constructor.
  82      * @param int $courseid Course id.
  83      */
  84      function __construct($courseid) {
  85          $this->htmlarea($courseid);
  86      }
  87  
  88      /**
  89      * Check if passed configuration key is valid.
  90      * @param string $key
  91      * @return bool Return true if key is valid and false if it's not.
  92      */
  93      function __is_valid_key($key) {
  94          if ( in_array($key, $this->htmlareaconfkeys) ) {
  95              return true;
  96          }
  97          return false;
  98      }
  99  
 100      /**
 101      * Check if passed value's type is valid.
 102      * @param string $key Configuration key name.
 103      * @param mixed $value Configuration value.
 104      * @return bool Returns true if value is valid type and false if it's not.
 105      */
 106      function __is_valid_value_type($key, $value) {
 107  
 108          if ( !empty($this->htmlareaconfkeytypes[$key]) ) {
 109              $keytype = $this->htmlareaconfkeytypes[$key];
 110  
 111              switch ( $keytype ) {
 112                  case 'bool':
 113                      if ( is_bool($value) ) {
 114                          return true;
 115                      }
 116                  break;
 117                  case 'string':
 118                      if ( is_string($value) ) {
 119                          return true;
 120                      }
 121                  break;
 122                  case 'int':
 123                      if ( is_int($value) ) {
 124                          return true;
 125                      }
 126                  break;
 127                  case 'array':
 128                      if ( is_array($value) ) {
 129                          return true;
 130                      }
 131                  break;
 132                  case 'assoc':
 133                      if ( is_array($value) ) {
 134                          // Check first key.
 135                          $key = key($value);
 136                          if ( preg_match("/[a-z]+/i", $key) ) {
 137                              return true;
 138                          }
 139                      }
 140                  break;
 141                  default:
 142              }
 143          }
 144          return false;
 145      }
 146  
 147      /**
 148      * Sets configuration key and value pairs.
 149      * Passed parameters can be key and value pair or
 150      * an associative array of keys and values.
 151      * @todo code example
 152      */
 153      function setconfig() {
 154  
 155          $numargs = func_num_args();
 156  
 157          switch ( $numargs ) {
 158              case 1: // Must be an array.
 159                  $args = func_get_arg(0);
 160                  if ( !is_array($args) ) {
 161                      $this->error("Passed argument is not an array!!!");
 162                  }
 163                  foreach ( $args as $key => $value ) {
 164                      if ( !preg_match("/[a-z]+/i", $key) && !$this->__is_valid_key($key) ) {
 165                          $this->error("Invalid configuration key!!!");
 166                      }
 167                      if ( $this->__is_valid_value_type($key, $value) ) {
 168                          $this->htmlareaconf[$key] = $value;
 169                      } else {
 170                          $this->error("Invalid key, value pair!!!");
 171                      }
 172                  }
 173              break;
 174              case 2: // Must be key, value pair.
 175                  $key   = func_get_arg(0);
 176                  $value = func_get_arg(1);
 177                  if ( empty($key) or !isset($value) ) {
 178                      $this->error("Empty key or value passed!!!");
 179                  }
 180                  if ( !preg_match("/[a-z]+/i", $key) ) {
 181                      $this->error("Configuration key must be a string!!");
 182                  }
 183  
 184                  if ( !$this->__is_valid_key($key) ) {
 185                      $this->error("Invalid configuration key!!!");
 186                  }
 187  
 188                  if ( $this->__is_valid_value_type($key, $value) ) {
 189                      $this->htmlareaconf[$key] = $value;
 190                  } else {
 191                      $this->error("Invalid key, value pair!!!");
 192                  }
 193              break;
 194              default:
 195                  if ( $numargs > 2 ) {
 196                      $this->error("Too many arguments!!!");
 197                  }
 198                  if ( $numargs < 1 ) {
 199                      $this->error("No arguments passed!!!");
 200                  }
 201          }
 202      }
 203  
 204      /**
 205      * For internal usage. Print out configuration arrays.
 206      * @param string $conftype Type of configuration.
 207      * @return void
 208      */
 209      function __printconfig($conftype='') {
 210  
 211          $conf = NULL;
 212          $assocs = array('fontname','fontsize','formatblocks');
 213  
 214          switch( $conftype ) {
 215              case 'merge': // New config overrides defaults if found.
 216                  $conf = array_merge($this->defaults,$this->htmlareaconf);
 217              break;
 218              case 'append': // Append mode leave default value if found.
 219                  $conf = $this->defaults;
 220                  $keys = array_keys($this->defaults);
 221                  foreach ( $this->htmlareaconf as $key => $value ) {
 222                      if ( in_array($key, $keys) ) {
 223                          continue;
 224                      } else {
 225                          $conf[$key] = $value;
 226                      }
 227                  }
 228              break;
 229              case 'default': // Use only default config.
 230                  $conf = $this->defaults;
 231              break;
 232              default: // Print what's in htmlareaconf.
 233                  $conf = $this->htmlareaconf;
 234          }
 235  
 236          echo "\n";
 237          echo '<script type="text/javascript" defer="defer">'."\n";
 238          echo '//<![CDATA['."\n";
 239          echo '    var config = new HTMLArea.Config();'."\n";
 240  
 241          foreach ( $conf as $key => $value ) {
 242  
 243              if ( empty($value) ) {
 244                  continue;
 245              }
 246  
 247              echo '    config.'. $key .' = ';
 248              if ( is_bool($value) ) {
 249                  echo $value ? "true;\n" : "false;\n";
 250              } else if ( in_array($key, $assocs) ) {
 251  
 252                  echo '{'."\n";
 253                  $cnt = 1;
 254                  foreach ( $value as $key => $value ) {
 255                      if ( $cnt > 1 ) {
 256                          echo ",\n";
 257                      }
 258                      echo "\t\"$key\" : \"$value\"";
 259                      $cnt++;
 260                  }
 261                  echo ' };'."\n";
 262  
 263              } else if ( $key == 'toolbar' ) {
 264                  // toolbar is array of arrays.
 265                  echo '['."\n";
 266                  $max = count($conf['toolbar']);
 267                  $cnt = 1;
 268                  foreach ( $conf['toolbar'] as $row ) {
 269                      echo "\t" . '[ ';
 270                      $count = count($row);
 271                      for ( $i = 0; $i < $count; $i++ ) {
 272                          if ( $i > 0 ) {
 273                              echo ',';
 274                          }
 275                          if ( $i != 0 && ($i % 4) == 0 ) {
 276                              echo "\n\t";
 277                          }
 278                          echo '"'. $row[$i] .'"';
 279                      }
 280                      if ( $cnt < $max ) {
 281                          echo " ],\n";
 282                      } else {
 283                          echo " ]\n";
 284                      }
 285                      $cnt++;
 286                  }
 287                  echo "\t" . '];'. "\n";
 288  
 289              } else {
 290                  echo '"'. $value .'"'. "\n";
 291              }
 292          }
 293  
 294          if ( !empty($this->cfg->editorspelling) && !empty($this->cfg->aspellpath) ) {
 295              echo "\n";
 296              $this->print_speller_code(true);
 297              echo "\n";
 298          }
 299  
 300          echo '    HTMLArea.replaceAll(config);'."\n";
 301          echo '//]]>'."\n";
 302          echo '</script>'."\n";
 303  
 304      }
 305  
 306      /**
 307      * Print out code that start up the editor.
 308      * @param string $conftype Configuration type to print.
 309      */
 310      function starteditor($configtype='') {
 311          $this->__printconfig($configtype);
 312      }
 313  
 314      /**
 315      * For backward compatibility only.
 316      * @param string $name
 317      * @param string $editorhidesomebuttons
 318      */
 319      function use_html_editor ( $name='', $editorhidebuttons='' ) {
 320  
 321          if ( !empty($editorhidesomebuttons) ) {
 322              $this->defaults['hideSomeButtons'] = $editorhidesomebuttons;
 323          }
 324  
 325          if (empty($name)) {
 326              $this->starteditor('default');
 327          } else {
 328              $this->starteditor('default');
 329          }
 330  
 331          if ( !empty($this->cfg->editorsrc) ) {
 332              unset($this->cfg->editorsrc);
 333          }
 334  
 335      }
 336  
 337      /**
 338      * Prints out needed code for spellchecking.
 339      * @param bool $usehtmleditor
 340      * @todo Deprecated? see lib/weblib.php::print_speller_code()
 341      * @see lib/weblib.php::print_speller_code()
 342      */
 343      function print_speller_code ($usehtmleditor=false) {
 344          echo "\n".'<script type="text/javascript">'."\n";
 345          echo '//<![CDATA['."\n";
 346          if (!$usehtmleditor) {
 347              echo 'function openSpellChecker() {'."\n";
 348              echo "\tvar speller = new spellChecker();\n";
 349              echo "\tspeller.popUpUrl = \"" . $this->cfg->httpswwwroot ."/lib/speller/spellchecker.html\";\n";
 350              echo "\tspeller.spellCheckScript = \"". $this->cfg->httpswwwroot ."/lib/speller/server-scripts/spellchecker.php\";\n";
 351              echo "\tspeller.spellCheckAll();\n";
 352              echo '}'."\n";
 353          } else {
 354              echo "\n\tfunction spellClickHandler(editor, buttonId) {\n";
 355              echo "\t\teditor._textArea.value = editor.getHTML();\n";
 356              echo "\t\tvar speller = new spellChecker( editor._textArea );\n";
 357              echo "\t\tspeller.popUpUrl = \"" . $this->cfg->httpswwwroot ."/lib/speller/spellchecker.html\";\n";
 358              echo "\t\tspeller.spellCheckScript = \"". $this->cfg->httpswwwroot ."/lib/speller/server-scripts/spellchecker.php\";\n";
 359              echo "\t\tspeller._moogle_edit=1;\n";
 360              echo "\t\tspeller._editor=editor;\n";
 361              echo "\t\tspeller.openChecker();\n\t";
 362              echo '}'."\n";
 363          }
 364          echo '//]]>'."\n";
 365          echo '</script>'."\n";
 366  
 367      }
 368  
 369  }
 370  ?>


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