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\DependencyInjection\ContainerInterface; |
16
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class CKEditorRenderer implements CKEditorRendererInterface |
22
|
|
|
{ |
23
|
|
|
/** @var \Ivory\JsonBuilder\JsonBuilder */ |
24
|
|
|
private $jsonBuilder; |
25
|
|
|
|
26
|
|
|
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates a CKEditor renderer. |
31
|
|
|
* |
32
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container. |
33
|
|
|
*/ |
34
|
1392 |
|
public function __construct(ContainerInterface $container) |
35
|
|
|
{ |
36
|
1392 |
|
$this->jsonBuilder = new JsonBuilder(); |
37
|
1392 |
|
$this->container = $container; |
38
|
1392 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
170 |
|
public function renderBasePath($basePath) |
44
|
|
|
{ |
45
|
170 |
|
return $this->fixPath($this->fixUrl($basePath)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
153 |
|
public function renderJsPath($jsPath) |
52
|
|
|
{ |
53
|
153 |
|
return $this->fixUrl($jsPath); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
1069 |
|
public function renderWidget($id, array $config, array $options = array()) |
60
|
|
|
{ |
61
|
1069 |
|
$config = $this->fixConfigLanguage($config); |
62
|
1069 |
|
$config = $this->fixConfigContentsCss($config); |
63
|
1069 |
|
$config = $this->fixConfigFilebrowsers( |
64
|
1006 |
|
$config, |
65
|
1069 |
|
isset($options['filebrowsers']) ? $options['filebrowsers'] : array() |
66
|
1006 |
|
); |
67
|
|
|
|
68
|
1069 |
|
$this->jsonBuilder |
69
|
1069 |
|
->reset() |
70
|
1069 |
|
->setValues($config); |
71
|
|
|
|
72
|
1069 |
|
$this->fixConfigEscapedValues($config); |
73
|
|
|
|
74
|
1069 |
|
$autoInline = isset($options['auto_inline']) && !$options['auto_inline'] |
75
|
1009 |
|
? 'CKEDITOR.disableAutoInline = true;'.PHP_EOL |
76
|
1069 |
|
: null; |
77
|
|
|
|
78
|
1069 |
|
$widget = sprintf( |
79
|
1069 |
|
'CKEDITOR.%s("%s", %s);', |
80
|
1069 |
|
isset($options['inline']) && $options['inline'] ? 'inline' : 'replace', |
81
|
1006 |
|
$id, |
82
|
1069 |
|
$this->fixConfigConstants($this->jsonBuilder->build()) |
83
|
1006 |
|
); |
84
|
|
|
|
85
|
1069 |
|
if (isset($options['input_sync']) && $options['input_sync']) { |
86
|
51 |
|
$variable = 'ivory_ckeditor_'.$id; |
87
|
51 |
|
$widget = 'var '.$variable.' = '.$widget.PHP_EOL; |
88
|
|
|
|
89
|
51 |
|
return $autoInline.$widget.$variable.'.on(\'change\', function() { '.$variable.'.updateElement(); });'; |
90
|
|
|
} |
91
|
|
|
|
92
|
1018 |
|
return $autoInline.$widget; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
187 |
|
public function renderDestroy($id) |
99
|
|
|
{ |
100
|
187 |
|
return sprintf('if (CKEDITOR.instances["%s"]) { delete CKEDITOR.instances["%s"]; }', $id, $id); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
68 |
|
public function renderPlugin($name, array $plugin) |
107
|
|
|
{ |
108
|
68 |
|
return sprintf( |
109
|
68 |
|
'CKEDITOR.plugins.addExternal("%s", "%s", "%s");', |
110
|
64 |
|
$name, |
111
|
68 |
|
$this->fixPath($this->fixUrl($plugin['path'])), |
112
|
68 |
|
$plugin['filename'] |
113
|
64 |
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
51 |
|
public function renderStylesSet($name, array $stylesSet) |
120
|
|
|
{ |
121
|
51 |
|
$this->jsonBuilder |
122
|
51 |
|
->reset() |
123
|
51 |
|
->setValues($stylesSet); |
124
|
|
|
|
125
|
51 |
|
return sprintf( |
126
|
51 |
|
'if (CKEDITOR.stylesSet.get("%s") === null) { CKEDITOR.stylesSet.add("%s", %s); }', |
127
|
48 |
|
$name, |
128
|
48 |
|
$name, |
129
|
51 |
|
$this->jsonBuilder->build() |
130
|
48 |
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
136 |
|
public function renderTemplate($name, array $template) |
137
|
|
|
{ |
138
|
136 |
|
if (isset($template['imagesPath'])) { |
139
|
136 |
|
$template['imagesPath'] = $this->fixPath($this->fixUrl($template['imagesPath'])); |
140
|
128 |
|
} |
141
|
|
|
|
142
|
136 |
|
if (isset($template['templates'])) { |
143
|
136 |
|
foreach ($template['templates'] as &$rawTemplate) { |
|
|
|
|
144
|
136 |
|
if (isset($rawTemplate['template'])) { |
145
|
68 |
|
$rawTemplate['html'] = $this->getTemplating()->render( |
146
|
68 |
|
$rawTemplate['template'], |
147
|
68 |
|
isset($rawTemplate['template_parameters']) ? $rawTemplate['template_parameters'] : array() |
148
|
64 |
|
); |
149
|
64 |
|
} |
150
|
|
|
|
151
|
136 |
|
unset($rawTemplate['template']); |
152
|
136 |
|
unset($rawTemplate['template_parameters']); |
153
|
128 |
|
} |
154
|
128 |
|
} |
155
|
|
|
|
156
|
136 |
|
$this->jsonBuilder |
157
|
136 |
|
->reset() |
158
|
136 |
|
->setValues($template); |
159
|
|
|
|
160
|
136 |
|
return sprintf('CKEDITOR.addTemplates("%s", %s);', $name, $this->jsonBuilder->build()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Fixes the config language. |
165
|
|
|
* |
166
|
|
|
* @param array $config The config. |
167
|
|
|
* |
168
|
|
|
* @return array The fixed config. |
169
|
|
|
*/ |
170
|
1069 |
|
private function fixConfigLanguage(array $config) |
171
|
|
|
{ |
172
|
1069 |
|
if (!isset($config['language']) && ($language = $this->getLanguage()) !== null) { |
173
|
100 |
|
$config['language'] = $language; |
174
|
94 |
|
} |
175
|
|
|
|
176
|
1069 |
|
if (isset($config['language'])) { |
177
|
134 |
|
$config['language'] = strtolower(str_replace('_', '-', $config['language'])); |
178
|
126 |
|
} |
179
|
|
|
|
180
|
1069 |
|
return $config; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Fixes the config contents css. |
185
|
|
|
* |
186
|
|
|
* @param array $config The config. |
187
|
|
|
* |
188
|
|
|
* @return array The fixed config. |
189
|
|
|
*/ |
190
|
1069 |
|
private function fixConfigContentsCss(array $config) |
191
|
|
|
{ |
192
|
1069 |
|
if (isset($config['contentsCss'])) { |
193
|
136 |
|
$cssContents = (array) $config['contentsCss']; |
194
|
|
|
|
195
|
136 |
|
$config['contentsCss'] = array(); |
196
|
136 |
|
foreach ($cssContents as $cssContent) { |
197
|
136 |
|
$config['contentsCss'][] = $this->fixPath($this->fixUrl($cssContent)); |
198
|
128 |
|
} |
199
|
128 |
|
} |
200
|
|
|
|
201
|
1069 |
|
return $config; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Fixes the config filebrowsers. |
206
|
|
|
* |
207
|
|
|
* @param array $config The config. |
208
|
|
|
* @param array $filebrowsers The filebrowsers. |
209
|
|
|
* |
210
|
|
|
* @return array The fixed config. |
211
|
|
|
*/ |
212
|
1069 |
|
private function fixConfigFilebrowsers(array $config, array $filebrowsers) |
213
|
|
|
{ |
214
|
1069 |
|
$filebrowsers = array_unique(array_merge(array( |
215
|
1069 |
|
'Browse', |
216
|
1006 |
|
'FlashBrowse', |
217
|
1006 |
|
'ImageBrowse', |
218
|
1006 |
|
'ImageBrowseLink', |
219
|
1006 |
|
'Upload', |
220
|
1006 |
|
'FlashUpload', |
221
|
1006 |
|
'ImageUpload', |
222
|
1006 |
|
), $filebrowsers)); |
223
|
|
|
|
224
|
1069 |
|
foreach ($filebrowsers as $filebrowser) { |
225
|
1069 |
|
$fileBrowserKey = 'filebrowser'.$filebrowser; |
226
|
1069 |
|
$handler = $fileBrowserKey.'Handler'; |
227
|
1069 |
|
$url = $fileBrowserKey.'Url'; |
228
|
1069 |
|
$route = $fileBrowserKey.'Route'; |
229
|
1069 |
|
$routeParameters = $fileBrowserKey.'RouteParameters'; |
230
|
1069 |
|
$routeAbsolute = $fileBrowserKey.'RouteAbsolute'; |
231
|
|
|
|
232
|
1069 |
|
if (isset($config[$handler])) { |
233
|
119 |
|
$config[$url] = $config[$handler]($this->getRouter()); |
234
|
1069 |
|
} elseif (isset($config[$route])) { |
235
|
238 |
|
$config[$url] = $this->getRouter()->generate( |
236
|
238 |
|
$config[$route], |
237
|
238 |
|
isset($config[$routeParameters]) ? $config[$routeParameters] : array(), |
238
|
238 |
|
$this->fixRoutePath(!isset($config[$routeAbsolute]) || $config[$routeAbsolute]) |
|
|
|
|
239
|
224 |
|
); |
240
|
224 |
|
} |
241
|
|
|
|
242
|
1069 |
|
unset($config[$handler]); |
243
|
1069 |
|
unset($config[$route]); |
244
|
1069 |
|
unset($config[$routeParameters]); |
245
|
1069 |
|
unset($config[$routeAbsolute]); |
246
|
1006 |
|
} |
247
|
|
|
|
248
|
1069 |
|
return $config; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Fixes the config escaped values and sets them on the json builder. |
253
|
|
|
* |
254
|
|
|
* @param array $config The config. |
255
|
|
|
*/ |
256
|
1069 |
|
private function fixConfigEscapedValues(array $config) |
257
|
|
|
{ |
258
|
1069 |
|
if (isset($config['protectedSource'])) { |
259
|
17 |
|
foreach ($config['protectedSource'] as $key => $value) { |
260
|
17 |
|
$this->jsonBuilder->setValue(sprintf('[protectedSource][%s]', $key), $value, false); |
261
|
16 |
|
} |
262
|
16 |
|
} |
263
|
|
|
|
264
|
|
|
$escapedValueKeys = array( |
265
|
1069 |
|
'stylesheetParser_skipSelectors', |
266
|
1006 |
|
'stylesheetParser_validSelectors', |
267
|
1006 |
|
); |
268
|
|
|
|
269
|
1069 |
|
foreach ($escapedValueKeys as $escapedValueKey) { |
270
|
1069 |
|
if (isset($config[$escapedValueKey])) { |
271
|
95 |
|
$this->jsonBuilder->setValue(sprintf('[%s]', $escapedValueKey), $config[$escapedValueKey], false); |
272
|
32 |
|
} |
273
|
1006 |
|
} |
274
|
1069 |
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Fixes the config constants. |
278
|
|
|
* |
279
|
|
|
* @param string $json The json config. |
280
|
|
|
* |
281
|
|
|
* @return string The fixed config. |
282
|
|
|
*/ |
283
|
1069 |
|
private function fixConfigConstants($json) |
284
|
|
|
{ |
285
|
1069 |
|
return preg_replace('/"(CKEDITOR\.[A-Z_]+)"/', '$1', $json); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Fixes a path. |
290
|
|
|
* |
291
|
|
|
* @param string $path The path. |
292
|
|
|
* |
293
|
|
|
* @return string The fixed path. |
294
|
|
|
*/ |
295
|
442 |
|
private function fixPath($path) |
296
|
|
|
{ |
297
|
442 |
|
if (($position = strpos($path, '?')) !== false) { |
298
|
170 |
|
return substr($path, 0, $position); |
299
|
|
|
} |
300
|
|
|
|
301
|
306 |
|
return $path; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Fixes an url. |
306
|
|
|
* |
307
|
|
|
* @param string $url The url. |
308
|
|
|
* |
309
|
|
|
* @return string The fixed url. |
310
|
|
|
*/ |
311
|
459 |
|
private function fixUrl($url) |
312
|
|
|
{ |
313
|
459 |
|
$assetsHelper = $this->getAssetsHelper(); |
314
|
|
|
|
315
|
459 |
|
return $assetsHelper !== null ? $assetsHelper->getUrl($url) : $url; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param bool $routePath |
320
|
|
|
* |
321
|
|
|
* @return int|bool |
322
|
|
|
*/ |
323
|
238 |
|
private function fixRoutePath($routePath) |
324
|
|
|
{ |
325
|
238 |
|
if ($routePath) { |
326
|
238 |
|
return defined('Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_PATH') |
327
|
238 |
|
? UrlGeneratorInterface::ABSOLUTE_PATH |
328
|
238 |
|
: true; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return defined('Symfony\Component\Routing\Generator\UrlGeneratorInterface::RELATIVE_PATH') |
332
|
|
|
? UrlGeneratorInterface::RELATIVE_PATH |
333
|
|
|
: false; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Gets the locale. |
338
|
|
|
* |
339
|
|
|
* @return string|null The locale. |
340
|
|
|
*/ |
341
|
1035 |
|
private function getLanguage() |
342
|
|
|
{ |
343
|
1035 |
|
if (($request = $this->getRequest()) !== null) { |
344
|
66 |
|
return $request->getLocale(); |
345
|
|
|
} |
346
|
|
|
|
347
|
969 |
|
if ($this->container->hasParameter($parameter = 'locale')) { |
348
|
34 |
|
return $this->container->getParameter($parameter); |
349
|
|
|
} |
350
|
935 |
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Gets the request. |
354
|
|
|
* |
355
|
|
|
* @return \Symfony\Component\HttpFoundation\Request|null The request. |
356
|
|
|
*/ |
357
|
1035 |
|
private function getRequest() |
358
|
|
|
{ |
359
|
1035 |
|
if ($this->container->has($service = 'request_stack')) { |
360
|
32 |
|
return $this->container->get($service)->getMasterRequest(); |
361
|
|
|
} |
362
|
|
|
|
363
|
1003 |
|
if ($this->container->has($service = 'request')) { |
364
|
34 |
|
return $this->container->get($service); |
365
|
|
|
} |
366
|
969 |
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Gets the templating engine. |
370
|
|
|
* |
371
|
|
|
* @return \Symfony\Component\Templating\EngineInterface|\Twig_Environment The templating engine. |
372
|
|
|
*/ |
373
|
68 |
|
private function getTemplating() |
374
|
|
|
{ |
375
|
68 |
|
return $this->container->has($templating = 'templating') |
376
|
66 |
|
? $this->container->get($templating) |
377
|
68 |
|
: $this->container->get('twig'); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Gets the assets helper. |
382
|
|
|
* |
383
|
|
|
* @return \Symfony\Component\Asset\Packages|\Symfony\Component\Templating\Helper\CoreAssetsHelper|null The assets helper. |
384
|
|
|
*/ |
385
|
459 |
|
private function getAssetsHelper() |
386
|
|
|
{ |
387
|
459 |
|
return $this->container->get('assets.packages', ContainerInterface::NULL_ON_INVALID_REFERENCE); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Gets the router. |
392
|
|
|
* |
393
|
|
|
* @return \Symfony\Component\Routing\RouterInterface The router. |
394
|
|
|
*/ |
395
|
357 |
|
private function getRouter() |
396
|
|
|
{ |
397
|
357 |
|
return $this->container->get('router'); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|