html_editor.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'HTML/QuickForm/textarea.php';
  4. /**
  5. * A html editor field to use with QuickForm
  6. */
  7. class HTML_QuickForm_html_editor extends HTML_QuickForm_textarea {
  8. /**
  9. * Full page
  10. */
  11. var $fullPage;
  12. var $fck_editor;
  13. /**
  14. * if true it show the fckeditor as usual if false it shows a textarea
  15. * @var bool
  16. */
  17. public $richEditorStatus = true;
  18. public function getRichEditorStatus() {
  19. return $this->richEditorStatus;
  20. }
  21. public function setRichEditorStatus($status) {
  22. $this->richEditorStatus = (bool)$status;
  23. }
  24. /**
  25. * Class constructor
  26. * @param string HTML editor name/id
  27. * @param string HTML editor label
  28. * @param string Attributes for the textarea
  29. * @param array $editor_config Optional configuration settings for the online editor.
  30. */
  31. function HTML_QuickForm_html_editor($elementName = null, $elementLabel = null, $attributes = null, $config = null) {
  32. // The global variable $fck_attribute has been deprecated. It stays here for supporting old external code.
  33. global $fck_attribute;
  34. HTML_QuickForm_element :: HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  35. $this->_persistantFreeze = true;
  36. $this->_type = 'html_editor';
  37. $this->fullPage = false;
  38. $name = $this->getAttribute('name');
  39. $this->fck_editor = new FCKeditor($name);
  40. $this->fck_editor->ToolbarSet = $fck_attribute['ToolbarSet'];
  41. $this->fck_editor->Width = !empty($fck_attribute['Width']) ? $fck_attribute['Width'] : '990';
  42. $this->fck_editor->Height = !empty($fck_attribute['Height']) ? $fck_attribute['Height'] : '400';
  43. //We get the optionnals config parameters in $fck_attribute array
  44. $this->fck_editor->Config = !empty($fck_attribute['Config']) ? $fck_attribute['Config'] : array();
  45. // This is an alternative (a better) way to pass configuration data to the editor.
  46. if (is_array($config)) {
  47. foreach ($config as $key => $value) {
  48. $this->fck_editor->Config[$key] = $config[$key];
  49. }
  50. if (isset($config['ToolbarSet'])) {
  51. $this->fck_editor->ToolbarSet = $config['ToolbarSet'];
  52. }
  53. if (isset($config['Width'])) {
  54. $this->fck_editor->Width = $config['Width'];
  55. }
  56. if (isset($config['Height'])) {
  57. $this->fck_editor->Height = $config['Height'];
  58. }
  59. if (isset($config['FullPage'])) {
  60. $this->fullPage = is_bool($config['FullPage']) ? $config['FullPage'] : ($config['FullPage'] === 'true');
  61. }
  62. }
  63. }
  64. /**
  65. * Check if the browser supports FCKeditor
  66. *
  67. * @access public
  68. * @return boolean
  69. */
  70. function browserSupported() {
  71. return FCKeditor :: IsCompatible();
  72. }
  73. /**
  74. * Return the HTML editor in HTML
  75. * @return string
  76. */
  77. function toHtml() {
  78. if ($this->getRichEditorStatus() == false) {
  79. //Fix rows and cols
  80. //$this->_attributes['rows'] = 20;
  81. //$this->_attributes['class'] = 20;
  82. return parent::toHtml();
  83. }
  84. $value = $this->getValue();
  85. if ($this->fullPage) {
  86. if (strlen(trim($value)) == 0) {
  87. // TODO: To be considered whether here to be added DOCTYPE, language and character set declarations.
  88. $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>';
  89. $this->setValue($value);
  90. }
  91. }
  92. if ($this->_flagFrozen) {
  93. return $this->getFrozenHtml();
  94. } else {
  95. return $this->build_FCKeditor();
  96. }
  97. }
  98. /**
  99. * Returns the htmlarea content in HTML
  100. * @return string
  101. */
  102. function getFrozenHtml() {
  103. return $this->getValue();
  104. }
  105. /**
  106. * Build this element using FCKeditor
  107. */
  108. function build_FCKeditor() {
  109. if (!FCKeditor :: IsCompatible()) {
  110. return parent::toHTML();
  111. }
  112. $this->fck_editor->Value = $this->getValue();
  113. $result = $this->fck_editor->CreateHtml();
  114. //Add a link to open the allowed html tags window
  115. //$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>';
  116. return $result;
  117. }
  118. }