File: htmlarea.class.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (366 lines) | stat: -rw-r--r-- 12,862 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php // $Id: htmlarea.class.php,v 1.1 2006/03/04 15:24:13 julmis Exp $

/**
 * This file contains the htmlarea subclass for moodle editorObject.
 *
 * @author Janne Mikkonen
 * @version  $Id: htmlarea.class.php,v 1.1 2006/03/04 15:24:13 julmis Exp $
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package editorObject
 */
class htmlarea extends editorObject {

    /**
    * Configuration array to hold configuration data.
    * @var array $htmlareaconf
    */
    var $htmlareaconf = array();

    /**
    * Configuration keys array to store possible configuration keys.
    * @var array $htmlareaconfkeys
    */
    var $htmlareaconfkeys = array("width","height","statusBar","undoSteps","undoTimeout",
                                  "sizeIncludesToolbar","fullPage","pageStyle","killWordOnPaste",
                                  "toolbar","fontname","fontsize","formatblock","customSelects");

    /**
    * An array to store valid value types that can
    * be passed to specific configuration key.
    * @var array $htmlareaconfkeytypes
    */
    var $htmlareaconfkeytypes = array('width' => 'string', 'height' => 'string', 'statusBar' => 'bool',
                                      'undoSteps' => 'int', 'undoTimeout' => 'int',
                                      'sizeIncludeToolbar' => 'bool', 'fullPage' => 'bool',
                                      'pageStyle' => 'string', 'killWordOnPaste' => 'bool',
                                      'toolbar' => 'array', 'fontname' => 'assoc', 'fontsize' => 'assoc',
                                      'formatblock' => 'assoc', 'customSelects' => 'array');

    /**
    * Array of default configuration set via editor settings.
    * @var array $defaults
    */
    var $defaults = array();

    /**
    * PHP4 style class constructor.
    *
    * @param int $courseid Courseid.
    */
    function htmlarea($courseid) {
        parent::editorObject();
        $this->courseid = clean_param($courseid, PARAM_INT);

        $pagestyle  = 'body {';
        $pagestyle .= !empty($this->cfg->editorbackgroundcolor) ?
                             ' background-color: '. $this->cfg->editorbackgroundcolor .'; ' : '';
        $pagestyle .= !empty($this->cfg->editorfontfamily) ?
                             ' font-family: '. $this->cfg->editorfontfamily .';' : '';
        $pagestyle .= !empty($this->cfg->editorfontsize) ?
                             ' font-size: '. $this->cfg->editorfontsize .';' : '';
        $pagestyle .= '}';

        $this->defaults['pageStyle'] = $pagestyle;
        $this->defaults['killWordOnPaste'] = !empty($this->cfg->editorkillword) ? true : false;

        $fontlist = isset($this->cfg->editorfontlist) ? explode(';', $this->cfg->editorfontlist) : array();
        $fonts = array();
        foreach ( $fontlist as $fontline ) {
            if ( !empty($fontline) ) {
                list($fontkey, $fontvalue) = split(":", $fontline);
                $fonts[$fontkey] = $fontvalue;
            }
        }
        $this->defaults['fontname'] = $fonts;
        $this->defaults['hideSomeButtons'] = !empty($this->cfg->editorhidebuttons) ?
                                                    ' '. $this->cfg->editorhidebuttons .' ' : '';

    }

    /**
    * PHP5 style class constructor.
    * @param int $courseid Course id.
    */
    function __construct($courseid) {
        $this->htmlarea($courseid);
    }

    /**
    * Check if passed configuration key is valid.
    * @param string $key
    * @return bool Return true if key is valid and false if it's not.
    */
    function __is_valid_key($key) {
        if ( in_array($key, $this->htmlareaconfkeys) ) {
            return true;
        }
        return false;
    }

    /**
    * Check if passed value's type is valid.
    * @param string $key Configuration key name.
    * @param mixed $value Configuration value.
    * @return bool Returns true if value is valid type and false if it's not.
    */
    function __is_valid_value_type($key, $value) {

        if ( !empty($this->htmlareaconfkeytypes[$key]) ) {
            $keytype = $this->htmlareaconfkeytypes[$key];

            switch ( $keytype ) {
                case 'bool':
                    if ( is_bool($value) ) {
                        return true;
                    }
                break;
                case 'string':
                    if ( is_string($value) ) {
                        return true;
                    }
                break;
                case 'int':
                    if ( is_int($value) ) {
                        return true;
                    }
                break;
                case 'array':
                    if ( is_array($value) ) {
                        return true;
                    }
                break;
                case 'assoc':
                    if ( is_array($value) ) {
                        // Check first key.
                        $key = key($value);
                        if ( preg_match("/[a-z]+/i", $key) ) {
                            return true;
                        }
                    }
                break;
                default:
            }
        }
        return false;
    }

    /**
    * Sets configuration key and value pairs.
    * Passed parameters can be key and value pair or
    * an associative array of keys and values.
    * @todo code example
    */
    function setconfig() {

        $numargs = func_num_args();

        switch ( $numargs ) {
            case 1: // Must be an array.
                $args = func_get_arg(0);
                if ( !is_array($args) ) {
                    $this->error("Passed argument is not an array!!!");
                }
                foreach ( $args as $key => $value ) {
                    if ( !preg_match("/[a-z]+/i", $key) && !$this->__is_valid_key($key) ) {
                        $this->error("Invalid configuration key!!!");
                    }
                    if ( $this->__is_valid_value_type($key, $value) ) {
                        $this->htmlareaconf[$key] = $value;
                    } else {
                        $this->error("Invalid key, value pair!!!");
                    }
                }
            break;
            case 2: // Must be key, value pair.
                $key   = func_get_arg(0);
                $value = func_get_arg(1);
                if ( empty($key) or !isset($value) ) {
                    $this->error("Empty key or value passed!!!");
                }
                if ( !preg_match("/[a-z]+/i", $key) ) {
                    $this->error("Configuration key must be a string!!");
                }

                if ( !$this->__is_valid_key($key) ) {
                    $this->error("Invalid configuration key!!!");
                }

                if ( $this->__is_valid_value_type($key, $value) ) {
                    $this->htmlareaconf[$key] = $value;
                } else {
                    $this->error("Invalid key, value pair!!!");
                }
            break;
            default:
                if ( $numargs > 2 ) {
                    $this->error("Too many arguments!!!");
                }
                if ( $numargs < 1 ) {
                    $this->error("No arguments passed!!!");
                }
        }
    }

    /**
    * For internal usage. Print out configuration arrays.
    * @param string $conftype Type of configuration.
    * @return void
    */
    function __printconfig($conftype='') {

        $conf = NULL;
        $assocs = array('fontname','fontsize','formatblocks');

        switch( $conftype ) {
            case 'merge': // New config overrides defaults if found.
                $conf = array_merge($this->defaults,$this->htmlareaconf);
            break;
            case 'append': // Append mode leave default value if found.
                $conf = $this->defaults;
                $keys = array_keys($this->defaults);
                foreach ( $this->htmlareaconf as $key => $value ) {
                    if ( in_array($key, $keys) ) {
                        continue;
                    } else {
                        $conf[$key] = $value;
                    }
                }
            break;
            case 'default': // Use only default config.
                $conf = $this->defaults;
            break;
            default: // Print what's in htmlareaconf.
                $conf = $this->htmlareaconf;
        }

        echo "\n";
        echo '<script language="javascript" type="text/javascript" defer="defer">'."\n";
        echo '<!--'."\n";
        echo '    var config = new HTMLArea.Config();'."\n";

        foreach ( $conf as $key => $value ) {

            if ( empty($value) ) {
                continue;
            }

            echo '    config.'. $key .' = ';
            if ( is_bool($value) ) {
                echo $value ? "true;\n" : "false;\n";
            } else if ( in_array($key, $assocs) ) {

                echo '{'."\n";
                $cnt = 1;
                foreach ( $value as $key => $value ) {
                    if ( $cnt > 1 ) {
                        echo ",\n";
                    }
                    echo "\t\"$key\" : \"$value\"";
                    $cnt++;
                }
                echo ' };'."\n";

            } else if ( $key == 'toolbar' ) {
                // toolbar is array of arrays.
                echo '['."\n";
                $max = count($conf['toolbar']);
                $cnt = 1;
                foreach ( $conf['toolbar'] as $row ) {
                    echo "\t" . '[ ';
                    $count = count($row);
                    for ( $i = 0; $i < $count; $i++ ) {
                        if ( $i > 0 ) {
                            echo ',';
                        }
                        if ( $i != 0 && ($i % 4) == 0 ) {
                            echo "\n\t";
                        }
                        echo '"'. $row[$i] .'"';
                    }
                    if ( $cnt < $max ) {
                        echo " ],\n";
                    } else {
                        echo " ]\n";
                    }
                    $cnt++;
                }
                echo "\t" . '];'. "\n";

            } else {
                echo '"'. $value .'"'. "\n";
            }
        }

        if ( !empty($this->cfg->editorspelling) && !empty($this->cfg->aspellpath) ) {
            echo "\n";
            $this->print_speller_code(true);
            echo "\n";
        }

        echo '    HTMLArea.replaceAll(config);'."\n";
        echo '// done hiding -->'."\n";
        echo '</script>'."\n";

    }

    /**
    * Print out code that start up the editor.
    * @param string $conftype Configuration type to print.
    */
    function starteditor($configtype='') {
        $this->__printconfig($configtype);
    }

    /**
    * For backward compatibility only.
    * @param string $name
    * @param string $editorhidesomebuttons
    */
    function use_html_editor ( $name='', $editorhidebuttons='' ) {

        if ( !empty($editorhidesomebuttons) ) {
            $this->defaults['hideSomeButtons'] = $editorhidesomebuttons;
        }

        if (empty($name)) {
            $this->starteditor('default');
        } else {
            $this->starteditor('default');
        }

        if ( !empty($this->cfg->editorsrc) ) {
            unset($this->cfg->editorsrc);
        }

    }

    /**
    * Prints out needed code for spellchecking.
    * @param bool $usehtmleditor
    */
    function print_speller_code ($usehtmleditor=false) {

        if(!$usehtmleditor) {
            echo "\n".'<script language="javascript" type="text/javascript">'."\n";
            echo 'function openSpellChecker() {'."\n";
            echo "\tvar speller = new spellChecker();\n";
            echo "\tspeller.popUpUrl = \"" . $this->cfg->wwwroot ."/lib/speller/spellchecker.html\";\n";
            echo "\tspeller.spellCheckScript = \"". $this->cfg->wwwroot ."/lib/speller/server-scripts/spellchecker.php\";\n";
            echo "\tspeller.spellCheckAll();\n";
            echo '}'."\n";
            echo '</script>'."\n";
        } else {
            echo "\n\tfunction spellClickHandler(editor, buttonId) {\n";
            echo "\t\teditor._textArea.value = editor.getHTML();\n";
            echo "\t\tvar speller = new spellChecker( editor._textArea );\n";
            echo "\t\tspeller.popUpUrl = \"" . $this->cfg->wwwroot ."/lib/speller/spellchecker.html\";\n";
            echo "\t\tspeller.spellCheckScript = \"". $this->cfg->wwwroot ."/lib/speller/server-scripts/spellchecker.php\";\n";
            echo "\t\tspeller._moogle_edit=1;\n";
            echo "\t\tspeller._editor=editor;\n";
            echo "\t\tspeller.openChecker();\n\t";
            echo '}'."\n";
        }
    }

}
?>