HtmlEditor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  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. $id = $this->getAttribute('id');
  34. //var_dump($id);
  35. $this->_persistantFreeze = true;
  36. $this->_type = 'html_editor';
  37. $editor = Container::getHtmlEditor();
  38. if ($editor) {
  39. $this->editor = $editor;
  40. $this->editor->setTextareaId($id);
  41. $this->editor->setName($name);
  42. $this->editor->processConfig($config);
  43. }
  44. }
  45. /**
  46. * Return the HTML editor in HTML.
  47. *
  48. * @return string
  49. */
  50. public function toHtml()
  51. {
  52. if ($this->editor) {
  53. if ($this->editor->getConfigAttribute('fullPage')) {
  54. $value = $this->getValue();
  55. if (strlen(trim($value)) == 0) {
  56. // TODO: To be considered whether here to add
  57. // language and character set declarations.
  58. $value = '<!DOCTYPE html><html><head><title></title></head><body></body></html>';
  59. $this->setValue($value);
  60. }
  61. }
  62. }
  63. if ($this->isFrozen()) {
  64. return $this->getFrozenHtml();
  65. } else {
  66. $styleCss = $this->editor->getConfigAttribute('style');
  67. $style = false;
  68. if ($styleCss) {
  69. $style = true;
  70. }
  71. return $this->buildEditor($style);
  72. }
  73. }
  74. /**
  75. * Returns the html area content in HTML.
  76. *
  77. * @return string
  78. */
  79. public function getFrozenHtml()
  80. {
  81. return Security::remove_XSS($this->getValue());
  82. }
  83. /**
  84. * @param bool $style
  85. *
  86. * @return string
  87. */
  88. public function buildEditor($style = false)
  89. {
  90. $result = '';
  91. if ($this->editor) {
  92. $value = $this->getCleanValue();
  93. $this->editor->setName($this->getName());
  94. $this->editor->setTextareaId($this->getAttribute('id'));
  95. if ($style === true) {
  96. $result = $this->editor->createHtmlStyle($value);
  97. } else {
  98. $result = $this->editor->createHtml($value);
  99. }
  100. }
  101. return $result;
  102. }
  103. }