Passed
Push — 1.11.x ( 7f75d7...beb077 )
by Angel Fernando Quiroz
09:59
created

HtmlEditor::buildEditor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
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) {
0 ignored issues
show
introduced by
$editor is of type Chamilo\CoreBundle\Compo...ditor\CkEditor\CkEditor, thus it always evaluated to true.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method createHtmlStyle() does not exist on Chamilo\CoreBundle\Component\Editor\Editor. Did you maybe mean createHtml()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
                /** @scrutinizer ignore-call */ 
107
                $result = $this->editor->createHtmlStyle($value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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