HtmlEditor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * @param string $name
  18. * @param string $elementLabel HTML editor label
  19. * @param array $attributes Attributes for the textarea
  20. * @param array $config Optional configuration settings for the online editor.
  21. *
  22. */
  23. public function __construct(
  24. $name = null,
  25. $elementLabel = null,
  26. $attributes = [],
  27. $config = []
  28. ) {
  29. if (empty($name)) {
  30. return false;
  31. }
  32. parent::__construct($name, $elementLabel, $attributes);
  33. $this->_persistantFreeze = true;
  34. $this->_type = 'html_editor';
  35. //$editor = Container::getHtmlEditor();
  36. $editor = new CkEditor();
  37. if ($editor) {
  38. $this->editor = $editor;
  39. $this->editor->setName($name);
  40. $this->editor->processConfig($config);
  41. }
  42. }
  43. /**
  44. * Return the HTML editor in HTML
  45. * @return string
  46. */
  47. public function toHtml()
  48. {
  49. $value = $this->getValue();
  50. if ($this->editor) {
  51. if ($this->editor->getConfigAttribute('fullPage')) {
  52. if (strlen(trim($value)) == 0) {
  53. // TODO: To be considered whether here to be added DOCTYPE,
  54. // language and character set declarations.
  55. $value = '<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. if ($styleCss) {
  65. $style = true;
  66. } else {
  67. $style = false;
  68. }
  69. return $this->buildEditor($style);
  70. }
  71. }
  72. /**
  73. * Returns the html area content in HTML
  74. * @return string
  75. */
  76. public function getFrozenHtml()
  77. {
  78. return $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. $this->editor->value = $this->getValue();
  90. $this->editor->setName($this->getName());
  91. if ($style == true) {
  92. $result = $this->editor->createHtmlStyle();
  93. } else {
  94. $result = $this->editor->createHtml();
  95. }
  96. }
  97. return $result;
  98. }
  99. }