HtmlEditor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Component\Editor\CkEditor\CkEditor;
  4. /**
  5. * A html editor field to use with QuickForm.
  6. */
  7. class HtmlEditor extends HTML_QuickForm_textarea
  8. {
  9. /** @var \Chamilo\CoreBundle\Component\Editor\Editor */
  10. public $editor;
  11. /**
  12. * Full page.
  13. */
  14. public $fullPage;
  15. /**
  16. * Class Constructor.
  17. *
  18. * @param string $name
  19. * @param string|array $label HTML editor label
  20. * @param array $attributes Attributes for the textarea
  21. * @param array $config optional configuration settings for the online editor
  22. */
  23. public function __construct(
  24. $name,
  25. $label = null,
  26. $attributes = [],
  27. $config = []
  28. ) {
  29. if (empty($name)) {
  30. throw new \Exception('Name is required');
  31. }
  32. parent::__construct($name, $label, $attributes);
  33. $this->_persistantFreeze = true;
  34. $this->_type = 'html_editor';
  35. $editor = new CkEditor();
  36. if ($editor) {
  37. $this->editor = $editor;
  38. $this->editor->setName($name);
  39. $this->editor->processConfig($config);
  40. }
  41. }
  42. /**
  43. * Return the HTML editor in HTML.
  44. *
  45. * @return string
  46. */
  47. public function toHtml()
  48. {
  49. if ($this->editor) {
  50. if ($this->editor->getConfigAttribute('fullPage')) {
  51. $value = $this->getValue();
  52. if (strlen(trim($value)) == 0) {
  53. // TODO: To be considered whether here to add
  54. // language and character set declarations.
  55. $value = '<!DOCTYPE html><html><head><title></title></head><body></body></html>';
  56. $this->setValue($value);
  57. }
  58. }
  59. }
  60. if ($this->isFrozen()) {
  61. return $this->getFrozenHtml();
  62. } else {
  63. $styleCss = $this->editor->getConfigAttribute('style');
  64. $style = false;
  65. if ($styleCss) {
  66. $style = true;
  67. }
  68. return $this->buildEditor($style);
  69. }
  70. }
  71. /**
  72. * Returns the html area content in HTML.
  73. *
  74. * @return string
  75. */
  76. public function getFrozenHtml()
  77. {
  78. return Security::remove_XSS($this->getValue());
  79. }
  80. /**
  81. * @param bool $style
  82. *
  83. * @return string
  84. */
  85. public function buildEditor($style = false)
  86. {
  87. $result = '';
  88. if ($this->editor) {
  89. $value = $this->getCleanValue();
  90. $this->editor->setName($this->getName());
  91. if ($style === true) {
  92. $result = $this->editor->createHtmlStyle($value);
  93. } else {
  94. $result = $this->editor->createHtml($value);
  95. }
  96. }
  97. return $result;
  98. }
  99. }