4 * This file contains the htmlarea subclass for moodle editorObject.
6 * @author Janne Mikkonen
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package editorObject
11 class htmlarea
extends editorObject
{
14 * Configuration array to hold configuration data.
15 * @var array $htmlareaconf
17 var $htmlareaconf = array();
20 * Configuration keys array to store possible configuration keys.
21 * @var array $htmlareaconfkeys
23 var $htmlareaconfkeys = array("width","height","statusBar","undoSteps","undoTimeout",
24 "sizeIncludesToolbar","fullPage","pageStyle","killWordOnPaste",
25 "toolbar","fontname","fontsize","formatblock","customSelects");
28 * An array to store valid value types that can
29 * be passed to specific configuration key.
30 * @var array $htmlareaconfkeytypes
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');
40 * Array of default configuration set via editor settings.
41 * @var array $defaults
43 var $defaults = array();
46 * PHP4 style class constructor.
48 * @param int $courseid Courseid.
50 function htmlarea($courseid) {
51 parent
::editorObject();
52 $this->courseid
= clean_param($courseid, PARAM_INT
);
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
.';' : '';
63 $this->defaults
['pageStyle'] = $pagestyle;
64 $this->defaults
['killWordOnPaste'] = !empty($this->cfg
->editorkillword
) ?
true : false;
66 $fontlist = isset($this->cfg
->editorfontlist
) ?
explode(';', $this->cfg
->editorfontlist
) : array();
68 foreach ( $fontlist as $fontline ) {
69 if ( !empty($fontline) ) {
70 list($fontkey, $fontvalue) = split(":", $fontline);
71 $fonts[$fontkey] = $fontvalue;
74 $this->defaults
['fontname'] = $fonts;
75 $this->defaults
['hideSomeButtons'] = !empty($this->cfg
->editorhidebuttons
) ?
76 ' '. $this->cfg
->editorhidebuttons
.' ' : '';
81 * PHP5 style class constructor.
82 * @param int $courseid Course id.
84 function __construct($courseid) {
85 $this->htmlarea($courseid);
89 * Check if passed configuration key is valid.
91 * @return bool Return true if key is valid and false if it's not.
93 function __is_valid_key($key) {
94 if ( in_array($key, $this->htmlareaconfkeys
) ) {
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.
106 function __is_valid_value_type($key, $value) {
108 if ( !empty($this->htmlareaconfkeytypes
[$key]) ) {
109 $keytype = $this->htmlareaconfkeytypes
[$key];
111 switch ( $keytype ) {
113 if ( is_bool($value) ) {
118 if ( is_string($value) ) {
123 if ( is_int($value) ) {
128 if ( is_array($value) ) {
133 if ( is_array($value) ) {
136 if ( preg_match("/[a-z]+/i", $key) ) {
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.
153 function setconfig() {
155 $numargs = func_num_args();
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!!!");
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!!!");
167 if ( $this->__is_valid_value_type($key, $value) ) {
168 $this->htmlareaconf
[$key] = $value;
170 $this->error("Invalid key, value pair!!!");
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!!!");
180 if ( !preg_match("/[a-z]+/i", $key) ) {
181 $this->error("Configuration key must be a string!!");
184 if ( !$this->__is_valid_key($key) ) {
185 $this->error("Invalid configuration key!!!");
188 if ( $this->__is_valid_value_type($key, $value) ) {
189 $this->htmlareaconf
[$key] = $value;
191 $this->error("Invalid key, value pair!!!");
195 if ( $numargs > 2 ) {
196 $this->error("Too many arguments!!!");
198 if ( $numargs < 1 ) {
199 $this->error("No arguments passed!!!");
205 * For internal usage. Print out configuration arrays.
206 * @param string $conftype Type of configuration.
209 function __printconfig($conftype='') {
212 $assocs = array('fontname','fontsize','formatblocks');
214 switch( $conftype ) {
215 case 'merge': // New config overrides defaults if found.
216 $conf = array_merge($this->defaults
,$this->htmlareaconf
);
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) ) {
225 $conf[$key] = $value;
229 case 'default': // Use only default config.
230 $conf = $this->defaults
;
232 default: // Print what's in htmlareaconf.
233 $conf = $this->htmlareaconf
;
237 echo '<script type="text/javascript" defer="defer">'."\n";
238 echo '//<![CDATA['."\n";
239 echo ' var config = new HTMLArea.Config();'."\n";
241 foreach ( $conf as $key => $value ) {
243 if ( empty($value) ) {
247 echo ' config.'. $key .' = ';
248 if ( is_bool($value) ) {
249 echo $value ?
"true;\n" : "false;\n";
250 } else if ( in_array($key, $assocs) ) {
254 foreach ( $value as $key => $value ) {
258 echo "\t\"$key\" : \"$value\"";
263 } else if ( $key == 'toolbar' ) {
264 // toolbar is array of arrays.
266 $max = count($conf['toolbar']);
268 foreach ( $conf['toolbar'] as $row ) {
270 $count = count($row);
271 for ( $i = 0; $i < $count; $i++
) {
275 if ( $i != 0 && ($i %
4) == 0 ) {
278 echo '"'. $row[$i] .'"';
287 echo "\t" . '];'. "\n";
290 echo '"'. $value .'"'. "\n";
294 if ( !empty($this->cfg
->editorspelling
) && !empty($this->cfg
->aspellpath
) ) {
296 $this->print_speller_code(true);
300 echo ' HTMLArea.replaceAll(config);'."\n";
302 echo '</script>'."\n";
307 * Print out code that start up the editor.
308 * @param string $conftype Configuration type to print.
310 function starteditor($configtype='') {
311 $this->__printconfig($configtype);
315 * For backward compatibility only.
316 * @param string $name
317 * @param string $editorhidesomebuttons
319 function use_html_editor ( $name='', $editorhidebuttons='' ) {
321 if ( !empty($editorhidesomebuttons) ) {
322 $this->defaults
['hideSomeButtons'] = $editorhidesomebuttons;
326 $this->starteditor('default');
328 $this->starteditor('default');
331 if ( !empty($this->cfg
->editorsrc
) ) {
332 unset($this->cfg
->editorsrc
);
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()
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";
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";
365 echo '</script>'."\n";