1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory CKEditor package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\CKEditorBundle\Renderer; |
13
|
|
|
|
14
|
|
|
use Ivory\JsonBuilder\JsonBuilder; |
15
|
|
|
use Symfony\Component\Asset\Packages; |
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
17
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
19
|
|
|
use Symfony\Component\Templating\EngineInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class CKEditorRenderer implements CKEditorRendererInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var JsonBuilder |
28
|
|
|
*/ |
29
|
|
|
private $jsonBuilder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RouterInterface |
33
|
|
|
*/ |
34
|
|
|
private $router; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Packages |
38
|
|
|
*/ |
39
|
|
|
private $packages; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var EngineInterface |
43
|
|
|
*/ |
44
|
|
|
private $templating; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
private $locale; |
50
|
|
|
|
51
|
770 |
|
public function __construct( |
52
|
|
|
JsonBuilder $jsonBuilder, |
53
|
|
|
RouterInterface $router, |
54
|
|
|
RequestStack $requestStack, |
55
|
|
|
Packages $packages = null, |
56
|
|
|
EngineInterface $templating = null, |
57
|
|
|
$locale = null |
58
|
|
|
) { |
59
|
770 |
|
$this->jsonBuilder = $jsonBuilder; |
60
|
770 |
|
$this->router = $router; |
61
|
770 |
|
$this->packages = $packages; |
62
|
770 |
|
$this->templating = $templating; |
63
|
770 |
|
$request = $requestStack->getMasterRequest(); |
64
|
770 |
|
if ($request && null !== $request->getLocale()) { |
65
|
20 |
|
$this->locale = $request->getLocale(); |
66
|
16 |
|
} else { |
67
|
750 |
|
$this->locale = $locale; |
68
|
|
|
} |
69
|
770 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
100 |
|
public function renderBasePath($basePath) |
75
|
|
|
{ |
76
|
100 |
|
return $this->fixPath($basePath); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
90 |
|
public function renderJsPath($jsPath) |
83
|
|
|
{ |
84
|
90 |
|
return $this->fixPath($jsPath); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
610 |
|
public function renderWidget($id, array $config, array $options = []) |
91
|
|
|
{ |
92
|
610 |
|
$config = $this->fixConfigLanguage($config); |
93
|
610 |
|
$config = $this->fixConfigContentsCss($config); |
94
|
610 |
|
$config = $this->fixConfigFilebrowsers( |
95
|
610 |
|
$config, |
96
|
610 |
|
isset($options['filebrowsers']) ? $options['filebrowsers'] : [] |
97
|
488 |
|
); |
98
|
|
|
|
99
|
610 |
|
$autoInline = isset($options['auto_inline']) && !$options['auto_inline'] |
100
|
494 |
|
? 'CKEDITOR.disableAutoInline = true;'."\n" |
101
|
610 |
|
: null; |
102
|
|
|
|
103
|
610 |
|
$builder = $this->jsonBuilder->reset()->setValues($config); |
104
|
610 |
|
$this->fixConfigEscapedValues($builder, $config); |
105
|
|
|
|
106
|
610 |
|
$widget = sprintf( |
107
|
610 |
|
'CKEDITOR.%s("%s", %s);', |
108
|
610 |
|
isset($options['inline']) && $options['inline'] ? 'inline' : 'replace', |
109
|
610 |
|
$id, |
110
|
610 |
|
$this->fixConfigConstants($builder->build()) |
111
|
488 |
|
); |
112
|
|
|
|
113
|
610 |
|
if (isset($options['input_sync']) && $options['input_sync']) { |
114
|
30 |
|
$variable = 'ivory_ckeditor_'.$id; |
115
|
30 |
|
$widget = 'var '.$variable.' = '.$widget."\n"; |
116
|
|
|
|
117
|
30 |
|
return $autoInline.$widget.$variable.'.on(\'change\', function() { '.$variable.'.updateElement(); });'; |
118
|
|
|
} |
119
|
|
|
|
120
|
580 |
|
return $autoInline.$widget; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
110 |
|
public function renderDestroy($id) |
127
|
|
|
{ |
128
|
110 |
|
return sprintf( |
129
|
|
|
'if (CKEDITOR.instances["%1$s"]) { '. |
130
|
88 |
|
'CKEDITOR.instances["%1$s"].destroy(true); '. |
131
|
88 |
|
'delete CKEDITOR.instances["%1$s"]; '. |
132
|
110 |
|
'}', |
133
|
22 |
|
$id |
134
|
88 |
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
40 |
|
public function renderPlugin($name, array $plugin) |
141
|
|
|
{ |
142
|
40 |
|
return sprintf( |
143
|
40 |
|
'CKEDITOR.plugins.addExternal("%s", "%s", "%s");', |
144
|
40 |
|
$name, |
145
|
40 |
|
$this->fixPath($plugin['path']), |
146
|
40 |
|
$plugin['filename'] |
147
|
32 |
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
30 |
|
public function renderStylesSet($name, array $stylesSet) |
154
|
|
|
{ |
155
|
30 |
|
return sprintf( |
156
|
|
|
'if (CKEDITOR.stylesSet.get("%1$s") === null) { '. |
157
|
24 |
|
'CKEDITOR.stylesSet.add("%1$s", %2$s); '. |
158
|
30 |
|
'}', |
159
|
30 |
|
$name, |
160
|
30 |
|
$this->jsonBuilder->reset()->setValues($stylesSet)->build() |
161
|
24 |
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
60 |
|
public function renderTemplate($name, array $template) |
168
|
|
|
{ |
169
|
60 |
|
if (isset($template['imagesPath'])) { |
170
|
60 |
|
$template['imagesPath'] = $this->fixPath($template['imagesPath']); |
171
|
48 |
|
} |
172
|
|
|
|
173
|
60 |
|
if (isset($template['templates'])) { |
174
|
60 |
|
foreach ($template['templates'] as &$rawTemplate) { |
|
|
|
|
175
|
60 |
|
if (isset($rawTemplate['template'])) { |
176
|
20 |
|
$rawTemplate['html'] = $this->templating->render( |
177
|
20 |
|
$rawTemplate['template'], |
178
|
20 |
|
isset($rawTemplate['template_parameters']) ? $rawTemplate['template_parameters'] : [] |
179
|
16 |
|
); |
180
|
16 |
|
} |
181
|
|
|
|
182
|
60 |
|
unset($rawTemplate['template']); |
183
|
60 |
|
unset($rawTemplate['template_parameters']); |
184
|
48 |
|
} |
185
|
48 |
|
} |
186
|
|
|
|
187
|
60 |
|
return sprintf( |
188
|
60 |
|
'CKEDITOR.addTemplates("%s", %s);', |
189
|
60 |
|
$name, |
190
|
60 |
|
$this->jsonBuilder->reset()->setValues($template)->build() |
191
|
48 |
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $config |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
610 |
|
private function fixConfigLanguage(array $config) |
200
|
|
|
{ |
201
|
610 |
|
if (!isset($config['language']) && ($language = $this->locale) !== null) { |
202
|
40 |
|
$config['language'] = $language; |
203
|
32 |
|
} |
204
|
|
|
|
205
|
610 |
|
if (isset($config['language'])) { |
206
|
60 |
|
$config['language'] = strtolower(str_replace('_', '-', $config['language'])); |
207
|
48 |
|
} |
208
|
|
|
|
209
|
610 |
|
return $config; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param array $config |
214
|
|
|
* |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
610 |
|
private function fixConfigContentsCss(array $config) |
218
|
|
|
{ |
219
|
610 |
|
if (isset($config['contentsCss'])) { |
220
|
80 |
|
$cssContents = (array) $config['contentsCss']; |
221
|
|
|
|
222
|
80 |
|
$config['contentsCss'] = []; |
223
|
80 |
|
foreach ($cssContents as $cssContent) { |
224
|
80 |
|
$config['contentsCss'][] = $this->fixPath($cssContent); |
225
|
64 |
|
} |
226
|
64 |
|
} |
227
|
|
|
|
228
|
610 |
|
return $config; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param array $config |
233
|
|
|
* @param array $filebrowsers |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
610 |
|
private function fixConfigFilebrowsers(array $config, array $filebrowsers) |
238
|
|
|
{ |
239
|
610 |
|
$filebrowsers = array_unique(array_merge([ |
240
|
610 |
|
'Browse', |
241
|
488 |
|
'FlashBrowse', |
242
|
488 |
|
'ImageBrowse', |
243
|
488 |
|
'ImageBrowseLink', |
244
|
488 |
|
'Upload', |
245
|
488 |
|
'FlashUpload', |
246
|
488 |
|
'ImageUpload', |
247
|
610 |
|
], $filebrowsers)); |
248
|
|
|
|
249
|
610 |
|
foreach ($filebrowsers as $filebrowser) { |
250
|
610 |
|
$fileBrowserKey = 'filebrowser'.$filebrowser; |
251
|
610 |
|
$handler = $fileBrowserKey.'Handler'; |
252
|
610 |
|
$url = $fileBrowserKey.'Url'; |
253
|
610 |
|
$route = $fileBrowserKey.'Route'; |
254
|
610 |
|
$routeParameters = $fileBrowserKey.'RouteParameters'; |
255
|
610 |
|
$routeType = $fileBrowserKey.'RouteType'; |
256
|
|
|
|
257
|
610 |
|
if (isset($config[$handler])) { |
258
|
70 |
|
$config[$url] = $config[$handler]($this->router); |
259
|
610 |
|
} elseif (isset($config[$route])) { |
260
|
140 |
|
$config[$url] = $this->router->generate( |
261
|
140 |
|
$config[$route], |
262
|
140 |
|
isset($config[$routeParameters]) ? $config[$routeParameters] : [], |
263
|
140 |
|
isset($config[$routeType]) ? $config[$routeType] : UrlGeneratorInterface::ABSOLUTE_PATH |
264
|
112 |
|
); |
265
|
112 |
|
} |
266
|
|
|
|
267
|
610 |
|
unset($config[$handler]); |
268
|
610 |
|
unset($config[$route]); |
269
|
610 |
|
unset($config[$routeParameters]); |
270
|
610 |
|
unset($config[$routeType]); |
271
|
488 |
|
} |
272
|
|
|
|
273
|
610 |
|
return $config; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param JsonBuilder $builder |
278
|
|
|
* @param array $config |
279
|
|
|
*/ |
280
|
610 |
|
private function fixConfigEscapedValues(JsonBuilder $builder, array $config) |
281
|
|
|
{ |
282
|
610 |
|
if (isset($config['protectedSource'])) { |
283
|
10 |
|
foreach ($config['protectedSource'] as $key => $value) { |
284
|
10 |
|
$builder->setValue(sprintf('[protectedSource][%s]', $key), $value, false); |
285
|
8 |
|
} |
286
|
8 |
|
} |
287
|
|
|
|
288
|
|
|
$escapedValueKeys = [ |
289
|
610 |
|
'stylesheetParser_skipSelectors', |
290
|
488 |
|
'stylesheetParser_validSelectors', |
291
|
488 |
|
]; |
292
|
|
|
|
293
|
610 |
|
foreach ($escapedValueKeys as $escapedValueKey) { |
294
|
610 |
|
if (isset($config[$escapedValueKey])) { |
295
|
138 |
|
$builder->setValue(sprintf('[%s]', $escapedValueKey), $config[$escapedValueKey], false); |
296
|
16 |
|
} |
297
|
488 |
|
} |
298
|
610 |
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string $json |
302
|
|
|
* |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
610 |
|
private function fixConfigConstants($json) |
306
|
|
|
{ |
307
|
610 |
|
return preg_replace('/"(CKEDITOR\.[A-Z_]+)"/', '$1', $json); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param string $path |
312
|
|
|
* |
313
|
|
|
* @return string |
314
|
|
|
*/ |
315
|
250 |
|
private function fixPath($path) |
316
|
|
|
{ |
317
|
250 |
|
if ($this->packages === null) { |
318
|
|
|
return $path; |
319
|
|
|
} |
320
|
|
|
|
321
|
250 |
|
$url = $this->packages->getUrl($path); |
322
|
|
|
|
323
|
250 |
|
if (substr($path, -1) === '/' && ($position = strpos($url, '?')) !== false) { |
324
|
40 |
|
$url = substr($url, 0, $position); |
325
|
32 |
|
} |
326
|
|
|
|
327
|
250 |
|
return $url; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|