html_editor.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'HTML/QuickForm/textarea.php';
  4. require_once api_get_path(LIBRARY_PATH) . 'fckeditor/fckeditor.php';
  5. /**
  6. * A html editor field to use with QuickForm
  7. */
  8. class HTML_QuickForm_html_editor extends HTML_QuickForm_textarea {
  9. /**
  10. * Full page
  11. */
  12. var $fullPage;
  13. var $fck_editor;
  14. /**
  15. * Class constructor
  16. * @param string HTML editor name/id
  17. * @param string HTML editor label
  18. * @param string Attributes for the textarea
  19. * @param array $editor_config Optional configuration settings for the online editor.
  20. */
  21. function HTML_QuickForm_html_editor($elementName = null, $elementLabel = null, $attributes = null, $config = null) {
  22. // The global variable $fck_attribute has been deprecated. It stays here for supporting old external code.
  23. global $fck_attribute;
  24. HTML_QuickForm_element :: HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  25. $this->_persistantFreeze = true;
  26. $this->_type = 'html_editor';
  27. $this->fullPage = false;
  28. $name = $this->getAttribute('name');
  29. $this->fck_editor = new FCKeditor($name);
  30. $this->fck_editor->ToolbarSet = $fck_attribute['ToolbarSet'];
  31. $this->fck_editor->Width = !empty($fck_attribute['Width']) ? $fck_attribute['Width'] : '990';
  32. $this->fck_editor->Height = !empty($fck_attribute['Height']) ? $fck_attribute['Height'] : '400';
  33. //We get the optionnals config parameters in $fck_attribute array
  34. $this->fck_editor->Config = !empty($fck_attribute['Config']) ? $fck_attribute['Config'] : array();
  35. // This is an alternative (a better) way to pass configuration data to the editor.
  36. if (is_array($config)) {
  37. foreach ($config as $key => $value) {
  38. $this->fck_editor->Config[$key] = $config[$key];
  39. }
  40. if (isset($config['ToolbarSet'])) {
  41. $this->fck_editor->ToolbarSet = $config['ToolbarSet'];
  42. }
  43. if (isset($config['Width'])) {
  44. $this->fck_editor->Width = $config['Width'];
  45. }
  46. if (isset($config['Height'])) {
  47. $this->fck_editor->Height = $config['Height'];
  48. }
  49. if (isset($config['FullPage'])) {
  50. $this->fullPage = is_bool($config['FullPage']) ? $config['FullPage'] : ($config['FullPage'] === 'true');
  51. }
  52. }
  53. }
  54. /**
  55. * Check if the browser supports FCKeditor
  56. *
  57. * @access public
  58. * @return boolean
  59. */
  60. function browserSupported() {
  61. return FCKeditor :: IsCompatible();
  62. }
  63. /**
  64. * Return the HTML editor in HTML
  65. * @return string
  66. */
  67. function toHtml() {
  68. $value = $this->getValue();
  69. if ($this->fullPage) {
  70. if (strlen(trim($value)) == 0) {
  71. // TODO: To be considered whether here to be added DOCTYPE, language and character set declarations.
  72. $value = '<html><head><title></title><style type="text/css" media="screen, projection">/*<![CDATA[*/body{font-family: arial, verdana, helvetica, sans-serif;font-size: 12px;}/*]]>*/</style></head><body></body></html>';
  73. $this->setValue($value);
  74. }
  75. }
  76. if ($this->_flagFrozen) {
  77. return $this->getFrozenHtml();
  78. } else {
  79. return $this->build_FCKeditor();
  80. }
  81. }
  82. /**
  83. * Returns the htmlarea content in HTML
  84. * @return string
  85. */
  86. function getFrozenHtml() {
  87. return $this->getValue();
  88. }
  89. /**
  90. * Build this element using FCKeditor
  91. */
  92. function build_FCKeditor() {
  93. if (!FCKeditor :: IsCompatible()) {
  94. return parent::toHTML();
  95. }
  96. $this->fck_editor->Value = $this->getValue();
  97. $result = $this->fck_editor->CreateHtml();
  98. if (isset($this->fck_editor->Config['LoadAsciiMath'])) {
  99. if (isset($_SESSION['ascii_math_loaded']) &&
  100. $_SESSION['ascii_math_loaded'] == false
  101. ) {
  102. $result .= $this->fck_editor->Config['LoadAsciiMath'];
  103. $_SESSION['ascii_math_loaded'] = true;
  104. }
  105. }
  106. //Add a link to open the allowed html tags window
  107. //$result .= '<small><a href="#" onclick="MyWindow=window.open('."'".api_get_path(WEB_CODE_PATH)."help/allowed_html_tags.php?fullpage=". ($this->fullPage ? '1' : '0')."','MyWindow','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=500,height=600,left=200,top=20'".'); return false;">'.get_lang('AllowedHTMLTags').'</a></small>';
  108. return $result;
  109. }
  110. }