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