Completed
Push — master ( 0adf57...cd9ffe )
by Julito
10:56
created

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

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