Passed
Push — master ( c3cb05...1bd367 )
by Julito
09:55
created

Editor::formatTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor;
5
6
use Symfony\Component\Routing\RouterInterface;
7
use Symfony\Component\Translation\Translator;
8
use Symfony\Component\Translation\TranslatorInterface;
9
10
/**
11
 * Class Editor.
12
 *
13
 * @package Chamilo\CoreBundle\Component\Editor
14
 */
15
class Editor
16
{
17
    /**
18
     * Name of the instance.
19
     *
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     * Name of the toolbar to load.
26
     *
27
     * @var string
28
     */
29
    public $toolbarSet;
30
31
    /**
32
     * Initial value.
33
     *
34
     * @var string
35
     */
36
    public $value;
37
38
    /**
39
     * @var array
40
     */
41
    public $config;
42
43
    /** @var Translator */
44
    public $translator;
45
46
    /** @var RouterInterface */
47
    public $urlGenerator;
48
49
    /** @var \Template */
50
    public $template;
51
52
    /**
53
     * Editor constructor.
54
     *
55
     * @param TranslatorInterface $translator
56
     * @param RouterInterface     $urlGenerator
57
     */
58
    public function __construct(
59
        TranslatorInterface $translator,
60
        RouterInterface $urlGenerator
61
    ) {
62
        $this->toolbarSet = 'Basic';
63
        $this->value = '';
64
        $this->config = [];
65
        $this->setConfigAttribute('width', '100%');
66
        $this->setConfigAttribute('height', '200');
67
        $this->setConfigAttribute('fullPage', false);
68
        $this->translator = $translator;
69
        $this->urlGenerator = $urlGenerator;
70
        //$this->course = $course;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getName()
77
    {
78
        return $this->name;
79
    }
80
81
    /**
82
     * @param string $name
83
     */
84
    public function setName($name)
85
    {
86
        $this->name = $name;
87
    }
88
89
    /**
90
     * Return the HTML code required to run editor.
91
     *
92
     * @return string
93
     */
94
    public function createHtml()
95
    {
96
        $html = '<textarea id="'.$this->getName().'" name="'.$this->getName().'">'.$this->value.'</textarea>';
97
98
        return $html;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    /*public function editorReplace()
105
    {
106
        $toolbar = new Toolbar($this->urlGenerator, $this->toolbarSet, $this->config);
107
        $toolbar->setLanguage($this->getLocale());
108
        $config = $toolbar->getConfig();
109
        $javascript = $this->toJavascript($config);
110
111
        $html = "<script>
112
           CKEDITOR.replace('".$this->name."',
113
               $javascript
114
           );
115
116
           </script>";
117
        return $html;
118
    }*/
119
120
    /**
121
     * @param string $key
122
     * @param mixed  $value
123
     */
124
    public function setConfigAttribute($key, $value)
125
    {
126
        $this->config[$key] = $value;
127
    }
128
129
    /**
130
     * @param string $key
131
     *
132
     * @return mixed
133
     */
134
    public function getConfigAttribute($key)
135
    {
136
        return $this->config[$key] ?? null;
137
    }
138
139
    /**
140
     * @param array $config
141
     */
142
    public function processConfig($config)
143
    {
144
        if (is_array($config)) {
145
            foreach ($config as $key => $value) {
146
                switch ($key) {
147
                    case 'ToolbarSet':
148
                        $this->toolbarSet = $value;
149
                        break;
150
                    case 'Config':
151
                        $this->processConfig($value);
152
                        break;
153
                    case 'width':
154
                    case 'Width':
155
                        $this->setConfigAttribute('width', $value);
156
                        break;
157
                    case 'height':
158
                    case 'Height':
159
                        $this->setConfigAttribute('height', $value);
160
                        break;
161
                    case 'FullPage':
162
                    case 'fullPage':
163
                        $this->setConfigAttribute('fullPage', $value);
164
                        break;
165
                    default:
166
                        $this->setConfigAttribute($key, $value);
167
                        break;
168
                }
169
            }
170
        }
171
    }
172
173
    /**
174
     * @return string
175
     */
176
    public function getEditorStandAloneTemplate()
177
    {
178
        return 'javascript/editor/elfinder_standalone.tpl';
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    public function getLocale()
185
    {
186
        return api_get_language_isocode();
187
    }
188
189
    /**
190
     * Converts a PHP variable into its Javascript equivalent.
191
     * The code of this method has been "borrowed" from the function drupal_to_js() within the Drupal CMS.
192
     *
193
     * @param mixed $var The variable to be converted into Javascript syntax
194
     *
195
     * @return string Returns a string
196
     *                Note: This function is similar to json_encode(),
197
     *                in addition it produces HTML-safe strings, i.e. with <, > and & escaped.
198
     *
199
     * @see http://drupal.org/
200
     */
201
    protected function toJavascript($var)
202
    {
203
        switch (gettype($var)) {
204
            case 'boolean':
205
                return $var ? 'true' : 'false'; // Lowercase necessary!
206
            case 'integer':
207
            case 'double':
208
                return (string) $var;
209
            case 'resource':
210
            case 'string':
211
                return '"'.str_replace(
212
                    ["\r", "\n", "<", ">", "&"],
213
                    ['\r', '\n', '\x3c', '\x3e', '\x26'],
214
                    addslashes($var)
0 ignored issues
show
Bug introduced by
It seems like $var can also be of type resource; however, parameter $str of addslashes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

214
                    addslashes(/** @scrutinizer ignore-type */ $var)
Loading history...
215
                ).'"';
216
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
217
            case 'array':
218
                // Arrays in JSON can't be associative. If the array is empty or if it
219
                // has sequential whole number keys starting with 0, it's not associative
220
                // so we can go ahead and convert it as an array.
221
                if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
222
                    $output = [];
223
                    foreach ($var as $v) {
224
                        $output[] = $this->toJavascript($v);
225
                    }
226
227
                    return '[ '.implode(', ', $output).' ]';
228
                }
229
                //no break
230
            case 'object':
231
                // Otherwise, fall through to convert the array as an object.
232
                $output = [];
233
                foreach ($var as $k => $v) {
234
                    $output[] = $this->toJavascript(strval($k)).': '.$this->toJavascript($v);
235
                }
236
237
                return '{ '.implode(', ', $output).' }';
238
                break;
239
            default:
240
                return 'null';
241
        }
242
    }
243
}
244