PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/editor/htmlarea/htmlarea.class.php

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