html_editor.php 5.0 KB

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