Passed
Push — master ( df064e...3f6262 )
by Angel Fernando Quiroz
07:57 queued 39s
created

HtmlEditor::getTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 30
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Component\Editor\Editor;
6
use Chamilo\CoreBundle\Framework\Container;
7
8
/**
9
 * A html editor field to use with QuickForm.
10
 */
11
class HtmlEditor extends HTML_QuickForm_textarea
12
{
13
    /** @var Editor */
14
    public $editor;
15
16
    /**
17
     * Full page.
18
     */
19
    public $fullPage;
20
21
    /**
22
     * @param string       $name
23
     * @param string|array $label      HTML editor  label
24
     * @param array        $attributes Attributes for the textarea
25
     * @param array        $config     optional configuration settings for the online editor
26
     */
27
    public function __construct(
28
        $name,
29
        $label = null,
30
        $attributes = [],
31
        $config = []
32
    ) {
33
        if (empty($name)) {
34
            throw new \Exception('Name is required');
35
        }
36
37
        parent::__construct($name, $label, $attributes);
38
        $id = $this->getAttribute('id');
39
        $this->_persistantFreeze = true;
40
        $this->_type = 'html_editor';
41
42
        $editor = Container::getHtmlEditor();
43
        if ($editor) {
0 ignored issues
show
introduced by
$editor is of type Chamilo\CoreBundle\Component\Editor\Editor, 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 (0 == strlen(trim($value))) {
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
    public function getFrozenHtml(): string
87
    {
88
        return Security::remove_XSS($this->getValue());
0 ignored issues
show
Bug Best Practice introduced by
The expression return Security::remove_XSS($this->getValue()) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91
    public function buildEditor(bool $style = false): string
92
    {
93
        $result = '';
94
        if ($this->editor) {
95
            $value = $this->getCleanValue();
96
            $this->editor->setName($this->getName());
97
            $this->editor->setTextareaId($this->getAttribute('id'));
98
            if ($style) {
99
                $result = $this->editor->createHtmlStyle($value);
0 ignored issues
show
introduced by
The method createHtmlStyle() does not exist on Chamilo\CoreBundle\Component\Editor\Editor. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

99
                /** @scrutinizer ignore-call */ 
100
                $result = $this->editor->createHtmlStyle($value);
Loading history...
100
            } else {
101
                $result = $this->editor->createHtml($value);
0 ignored issues
show
introduced by
The method createHtml() does not exist on Chamilo\CoreBundle\Component\Editor\Editor. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

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

101
                /** @scrutinizer ignore-call */ 
102
                $result = $this->editor->createHtml($value);
Loading history...
102
            }
103
        }
104
105
        return $result;
106
    }
107
108
    public function getTemplate(string $layout): string
109
    {
110
        if (FormValidator::LAYOUT_HORIZONTAL === $layout) {
111
            return '
112
                <div class="field">
113
                    <div class="p-float-label">
114
                        <div class="html-editor-container">
115
                            {element}
116
                            {icon}
117
                        </div>
118
                        <label {label-for}>
119
                            <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
120
                            {label}
121
                        </label>
122
                    </div>
123
                    <!-- BEGIN label_2 -->
124
                        <small>{label_2}</small>
125
                    <!-- END label_2 -->
126
127
                     <!-- BEGIN label_3 -->
128
                        <small>{label_3}</small>
129
                    <!-- END label_3 -->
130
131
                    <!-- BEGIN error -->
132
                        <small class="p-error">{error}</small>
133
                    <!-- END error -->
134
                </div>';
135
        }
136
137
        return parent::getTemplate($layout);
138
    }
139
}
140