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
|
|
|
public function __construct(ContainerInterface $container) |
35
|
|
|
{ |
36
|
|
|
$this->jsonBuilder = new JsonBuilder(); |
37
|
|
|
$this->container = $container; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function renderBasePath($basePath) |
44
|
|
|
{ |
45
|
|
|
return $this->fixPath($this->fixUrl($basePath)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function renderJsPath($jsPath) |
52
|
|
|
{ |
53
|
|
|
return $this->fixUrl($jsPath); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function renderWidget($id, array $config, array $options = array()) |
60
|
|
|
{ |
61
|
|
|
$config = $this->fixConfigLanguage($config); |
62
|
|
|
$config = $this->fixConfigContentsCss($config); |
63
|
|
|
$config = $this->fixConfigFilebrowsers($config); |
64
|
|
|
|
65
|
|
|
$this->jsonBuilder |
66
|
|
|
->reset() |
67
|
|
|
->setValues($config); |
68
|
|
|
|
69
|
|
|
$this->fixConfigEscapedValues($config); |
70
|
|
|
|
71
|
|
|
$autoInline = isset($options['auto_inline']) && !$options['auto_inline'] |
72
|
|
|
? 'CKEDITOR.disableAutoInline = true;'.PHP_EOL |
73
|
|
|
: null; |
74
|
|
|
|
75
|
|
|
$widget = sprintf( |
76
|
|
|
'CKEDITOR.%s("%s", %s);', |
77
|
|
|
isset($options['inline']) && $options['inline'] ? 'inline' : 'replace', |
78
|
|
|
$id, |
79
|
|
|
$this->fixConfigConstants($this->jsonBuilder->build()) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
if (isset($options['input_sync']) && $options['input_sync']) { |
83
|
|
|
$variable = 'ivory_ckeditor_'.$id; |
84
|
|
|
$widget = 'var '.$variable.' = '.$widget.PHP_EOL; |
85
|
|
|
|
86
|
|
|
return $autoInline.$widget.$variable.'.on(\'change\', function() { '.$variable.'.updateElement(); });'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $autoInline.$widget; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function renderDestroy($id) |
96
|
|
|
{ |
97
|
|
|
return sprintf('if (CKEDITOR.instances["%s"]) { delete CKEDITOR.instances["%s"]; }', $id, $id); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function renderPlugin($name, array $plugin) |
104
|
|
|
{ |
105
|
|
|
return sprintf( |
106
|
|
|
'CKEDITOR.plugins.addExternal("%s", "%s", "%s");', |
107
|
|
|
$name, |
108
|
|
|
$this->fixPath($this->fixUrl($plugin['path'])), |
109
|
|
|
$plugin['filename'] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function renderStylesSet($name, array $stylesSet) |
117
|
|
|
{ |
118
|
|
|
$this->jsonBuilder |
119
|
|
|
->reset() |
120
|
|
|
->setValues($stylesSet); |
121
|
|
|
|
122
|
|
|
return sprintf( |
123
|
|
|
'if (CKEDITOR.stylesSet.get("%s") === null) { CKEDITOR.stylesSet.add("%s", %s); }', |
124
|
|
|
$name, |
125
|
|
|
$name, |
126
|
|
|
$this->jsonBuilder->build() |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function renderTemplate($name, array $template) |
134
|
|
|
{ |
135
|
|
|
if (isset($template['imagesPath'])) { |
136
|
|
|
$template['imagesPath'] = $this->fixPath($this->fixUrl($template['imagesPath'])); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->jsonBuilder |
140
|
|
|
->reset() |
141
|
|
|
->setValues($template); |
142
|
|
|
|
143
|
|
|
return sprintf('CKEDITOR.addTemplates("%s", %s);', $name, $this->jsonBuilder->build()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Fixes the config language. |
148
|
|
|
* |
149
|
|
|
* @param array $config The config. |
150
|
|
|
* |
151
|
|
|
* @return array The fixed config. |
152
|
|
|
*/ |
153
|
|
|
private function fixConfigLanguage(array $config) |
154
|
|
|
{ |
155
|
|
|
if (isset($config['language'])) { |
156
|
|
|
$config['language'] = strtolower(str_replace('_', '-', $config['language'])); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $config; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Fixes the config contents css. |
164
|
|
|
* |
165
|
|
|
* @param array $config The config. |
166
|
|
|
* |
167
|
|
|
* @return array The fixed config. |
168
|
|
|
*/ |
169
|
|
|
private function fixConfigContentsCss(array $config) |
170
|
|
|
{ |
171
|
|
|
if (isset($config['contentsCss'])) { |
172
|
|
|
$cssContents = (array) $config['contentsCss']; |
173
|
|
|
|
174
|
|
|
$config['contentsCss'] = array(); |
175
|
|
|
foreach ($cssContents as $cssContent) { |
176
|
|
|
$config['contentsCss'][] = $this->fixPath($this->fixUrl($cssContent)); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $config; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Fixes the config filebrowsers. |
185
|
|
|
* |
186
|
|
|
* @param array $config The config. |
187
|
|
|
* |
188
|
|
|
* @return array The fixed config. |
189
|
|
|
*/ |
190
|
|
|
private function fixConfigFilebrowsers(array $config) |
191
|
|
|
{ |
192
|
|
|
$keys = array( |
193
|
|
|
'Browse', |
194
|
|
|
'FlashBrowse', |
195
|
|
|
'ImageBrowse', |
196
|
|
|
'ImageBrowseLink', |
197
|
|
|
'Upload', |
198
|
|
|
'FlashUpload', |
199
|
|
|
'ImageUpload', |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
foreach ($keys as $key) { |
203
|
|
|
$fileBrowserKey = 'filebrowser'.$key; |
204
|
|
|
$handler = $fileBrowserKey.'Handler'; |
205
|
|
|
$url = $fileBrowserKey.'Url'; |
206
|
|
|
$route = $fileBrowserKey.'Route'; |
207
|
|
|
$routeParameters = $fileBrowserKey.'RouteParameters'; |
208
|
|
|
$routeAbsolute = $fileBrowserKey.'RouteAbsolute'; |
209
|
|
|
|
210
|
|
|
if (isset($config[$handler])) { |
211
|
|
|
$config[$url] = $config[$handler]($this->getRouter()); |
212
|
|
|
} elseif (isset($config[$route])) { |
213
|
|
|
$config[$url] = $this->getRouter()->generate( |
214
|
|
|
$config[$route], |
215
|
|
|
isset($config[$routeParameters]) ? $config[$routeParameters] : array(), |
216
|
|
|
isset($config[$routeAbsolute]) |
217
|
|
|
? $config[$routeAbsolute] |
218
|
|
|
: ( |
219
|
|
|
defined('Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_PATH') |
220
|
|
|
? UrlGeneratorInterface::ABSOLUTE_PATH |
221
|
|
|
: false |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
unset($config[$handler]); |
227
|
|
|
unset($config[$route]); |
228
|
|
|
unset($config[$routeParameters]); |
229
|
|
|
unset($config[$routeAbsolute]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $config; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Fixes the config escaped values and sets them on the json builder. |
237
|
|
|
* |
238
|
|
|
* @param array $config The config. |
239
|
|
|
*/ |
240
|
|
|
private function fixConfigEscapedValues(array $config) |
241
|
|
|
{ |
242
|
|
|
if (isset($config['protectedSource'])) { |
243
|
|
|
foreach ($config['protectedSource'] as $key => $value) { |
244
|
|
|
$this->jsonBuilder->setValue(sprintf('[protectedSource][%s]', $key), $value, false); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$escapedValueKeys = array( |
249
|
|
|
'stylesheetParser_skipSelectors', |
250
|
|
|
'stylesheetParser_validSelectors', |
251
|
|
|
); |
252
|
|
|
|
253
|
|
|
foreach ($escapedValueKeys as $escapedValueKey) { |
254
|
|
|
if (isset($config[$escapedValueKey])) { |
255
|
|
|
$this->jsonBuilder->setValue(sprintf('[%s]', $escapedValueKey), $config[$escapedValueKey], false); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Fixes the config constants. |
262
|
|
|
* |
263
|
|
|
* @param string $json The json config. |
264
|
|
|
* |
265
|
|
|
* @return string The fixed config. |
266
|
|
|
*/ |
267
|
|
|
private function fixConfigConstants($json) |
268
|
|
|
{ |
269
|
|
|
return preg_replace('/"(CKEDITOR\.[A-Z_]+)"/', '$1', $json); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Fixes a path. |
274
|
|
|
* |
275
|
|
|
* @param string $path The path. |
276
|
|
|
* |
277
|
|
|
* @return string The fixed path. |
278
|
|
|
*/ |
279
|
|
|
private function fixPath($path) |
280
|
|
|
{ |
281
|
|
|
if (($position = strpos($path, '?')) !== false) { |
282
|
|
|
return substr($path, 0, $position); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $path; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Fixes an url. |
290
|
|
|
* |
291
|
|
|
* @param string $url The url. |
292
|
|
|
* |
293
|
|
|
* @return string The fixed url. |
294
|
|
|
*/ |
295
|
|
|
private function fixUrl($url) |
296
|
|
|
{ |
297
|
|
|
$assetsHelper = $this->getAssetsHelper(); |
298
|
|
|
|
299
|
|
|
if ($assetsHelper !== null) { |
300
|
|
|
$url = $assetsHelper->getUrl($url); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return $url; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Gets the assets helper. |
308
|
|
|
* |
309
|
|
|
* @return \Symfony\Component\Asset\Packages|\Symfony\Component\Templating\Helper\CoreAssetsHelper|null The assets helper. |
310
|
|
|
*/ |
311
|
|
|
private function getAssetsHelper() |
312
|
|
|
{ |
313
|
|
|
return $this->container->get('assets.packages', ContainerInterface::NULL_ON_INVALID_REFERENCE); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Gets the router. |
318
|
|
|
* |
319
|
|
|
* @return \Symfony\Component\Routing\RouterInterface The router. |
320
|
|
|
*/ |
321
|
|
|
private function getRouter() |
322
|
|
|
{ |
323
|
|
|
return $this->container->get('router'); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|