#737318 by dboune: Fixed CKEditor default skin depends on filesystem order.
[project/wysiwyg.git] / editors / ckeditor.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Editor integration functions for CKEditor.
7 */
8
9 /**
10 * Plugin implementation of hook_editor().
11 */
12 function wysiwyg_ckeditor_editor() {
13 $editor['ckeditor'] = array(
14 'title' => 'CKEditor',
15 'vendor url' => 'http://ckeditor.com',
16 'download url' => 'http://ckeditor.com/download',
17 'libraries' => array(
18 '' => array(
19 'title' => 'Default',
20 'files' => array(
21 'ckeditor.js' => array('preprocess' => FALSE),
22 ),
23 ),
24 'src' => array(
25 'title' => 'Source',
26 'files' => array(
27 'ckeditor_source.js' => array('preprocess' => FALSE),
28 ),
29 ),
30 ),
31 'version callback' => 'wysiwyg_ckeditor_version',
32 'themes callback' => 'wysiwyg_ckeditor_themes',
33 'settings callback' => 'wysiwyg_ckeditor_settings',
34 'plugin callback' => 'wysiwyg_ckeditor_plugins',
35 'plugin settings callback' => 'wysiwyg_ckeditor_plugin_settings',
36 'proxy plugin' => array(
37 'drupal' => array(
38 'load' => TRUE,
39 'proxy' => TRUE,
40 ),
41 ),
42 'proxy plugin settings callback' => 'wysiwyg_ckeditor_proxy_plugin_settings',
43 'versions' => array(
44 '3.0.0.3665' => array(
45 'js files' => array('ckeditor-3.0.js'),
46 ),
47 ),
48 );
49 return $editor;
50 }
51
52 /**
53 * Detect editor version.
54 *
55 * @param $editor
56 * An array containing editor properties as returned from hook_editor().
57 *
58 * @return
59 * The installed editor version.
60 */
61 function wysiwyg_ckeditor_version($editor) {
62 $library = $editor['library path'] . '/ckeditor.js';
63 if (!file_exists($library)) {
64 return;
65 }
66 $library = fopen($library, 'r');
67 $max_lines = 8;
68 while ($max_lines && $line = fgets($library, 140)) {
69 // version:'CKEditor 3.0 SVN',revision:'3665'
70 // version:'3.0 RC',revision:'3753'
71 // version:'3.0.1',revision:'4391'
72 if (preg_match('@version:\'(?:CKEditor )?([\d\.]+)(?:.+revision:\'([\d]+))?@', $line, $version)) {
73 fclose($library);
74 // Version numbers need to have three parts since 3.0.1.
75 $version[1] = preg_replace('/^(\d+)\.(\d+)$/', '${1}.${2}.0', $version[1]);
76 return $version[1] . '.' . $version[2];
77 }
78 $max_lines--;
79 }
80 fclose($library);
81 }
82
83 /**
84 * Determine available editor themes or check/reset a given one.
85 *
86 * @param $editor
87 * A processed hook_editor() array of editor properties.
88 * @param $profile
89 * A wysiwyg editor profile.
90 *
91 * @return
92 * An array of theme names. The first returned name should be the default
93 * theme name.
94 */
95 function wysiwyg_ckeditor_themes($editor, $profile) {
96 // @todo Skins are not themes but this will do for now.
97 $path = $editor['library path'] . '/skins/';
98 if (file_exists($path) && ($dir_handle = opendir($path))) {
99 $themes = array();
100 while ($file = readdir($dir_handle)) {
101 if (is_dir($path . $file) && substr($file, 0, 1) != '.' && $file != 'CVS') {
102 $themes[] = $file;
103 }
104 }
105 closedir($dir_handle);
106 natcasesort($themes);
107 return !empty($themes) ? $themes : array('default');
108 }
109 else {
110 return array('default');
111 }
112 }
113
114 /**
115 * Return runtime editor settings for a given wysiwyg profile.
116 *
117 * @param $editor
118 * A processed hook_editor() array of editor properties.
119 * @param $config
120 * An array containing wysiwyg editor profile settings.
121 * @param $theme
122 * The name of a theme/GUI/skin to use.
123 *
124 * @return
125 * A settings array to be populated in
126 * Drupal.settings.wysiwyg.configs.{editor}
127 */
128 function wysiwyg_ckeditor_settings($editor, $config, $theme) {
129 $settings = array(
130 'baseHref' => $GLOBALS['base_url'] . '/',
131 'width' => '100%',
132 // For better compatibility with smaller textareas.
133 'resize_minWidth' => 450,
134 'height' => 420,
135 // @todo Do not use skins as themes and add separate skin handling.
136 'theme' => 'default',
137 'skin' => !empty($theme) ? $theme : 'kama',
138 // By default, CKEditor converts most characters into HTML entities. Since
139 // it does not support a custom definition, but Drupal supports Unicode, we
140 // disable at least the additional character sets. CKEditor always converts
141 // XML default characters '&', '<', '>'.
142 // @todo Check whether completely disabling ProcessHTMLEntities is an option.
143 'entities_latin' => FALSE,
144 'entities_greek' => FALSE,
145 );
146
147 // Add HTML block format settings; common block formats are already predefined
148 // by CKEditor.
149 if (isset($config['block_formats'])) {
150 $block_formats = explode(',', drupal_strtolower($config['block_formats']));
151 $predefined_formats = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'pre', 'address', 'div');
152 foreach (array_diff($block_formats, $predefined_formats) as $tag) {
153 $tag = trim($tag);
154 $settings["format_$tag"] = array('element' => $tag);
155 }
156 $settings['format_tags'] = implode(';', $block_formats);
157 }
158
159 if (isset($config['apply_source_formatting'])) {
160 $settings['apply_source_formatting'] = $config['apply_source_formatting'];
161 }
162
163 if (isset($config['css_setting'])) {
164 // Versions below 3.0.1 could only handle one stylesheet.
165 if (version_compare($editor['installed version'], '3.0.1.4391', '<')) {
166 if ($config['css_setting'] == 'theme') {
167 $settings['contentsCss'] = reset(wysiwyg_get_css());
168 }
169 elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
170 $settings['contentsCss'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
171 }
172 }
173 else {
174 if ($config['css_setting'] == 'theme') {
175 $settings['contentsCss'] = wysiwyg_get_css();
176 }
177 elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
178 $settings['contentsCss'] = explode(',', strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme())));
179 }
180 }
181 }
182
183 if (isset($config['language'])) {
184 $settings['language'] = $config['language'];
185 }
186 if (isset($config['resizing'])) {
187 $settings['resize_enabled'] = $config['resizing'];
188 }
189 if (isset($config['toolbar_loc'])) {
190 $settings['toolbarLocation'] = $config['toolbar_loc'];
191 }
192
193 if (!empty($config['buttons'])) {
194 $extra_plugins = array();
195 $settings['toolbar'] = array();
196 $plugins = wysiwyg_get_plugins($editor['name']);
197 foreach ($config['buttons'] as $plugin => $buttons) {
198 foreach ($buttons as $button => $enabled) {
199 // Iterate separately over buttons and extensions properties.
200 foreach (array('buttons', 'extensions') as $type) {
201 // Skip unavailable plugins.
202 if (!isset($plugins[$plugin][$type][$button])) {
203 continue;
204 }
205 // Add buttons.
206 if ($type == 'buttons') {
207 $settings['toolbar'][] = $button;
208 }
209 // Add external Drupal plugins to the list of extensions.
210 if ($type == 'buttons' && !empty($plugins[$plugin]['proxy'])) {
211 $extra_plugins[] = $button;
212 }
213 // Add external plugins to the list of extensions.
214 elseif ($type == 'buttons' && empty($plugins[$plugin]['internal'])) {
215 $extra_plugins[] = $plugin;
216 }
217 // Add internal buttons that also need to be loaded as extension.
218 elseif ($type == 'buttons' && !empty($plugins[$plugin]['load'])) {
219 $extra_plugins[] = $plugin;
220 }
221 // Add plain extensions.
222 elseif ($type == 'extensions' && !empty($plugins[$plugin]['load'])) {
223 $extra_plugins[] = $plugin;
224 }
225 // Allow plugins to add or override global configuration settings.
226 if (!empty($plugins[$plugin]['options'])) {
227 $settings = array_merge($settings, $plugins[$plugin]['options']);
228 }
229 }
230 }
231 }
232 if (!empty($extra_plugins)) {
233 $settings['extraPlugins'] = implode(',', $extra_plugins);
234 }
235 // For now, all buttons are placed into one row.
236 if (!empty($settings['toolbar'])) {
237 $settings['toolbar'] = array($settings['toolbar']);
238 }
239 }
240
241 return $settings;
242 }
243
244 /**
245 * Build a JS settings array of native external plugins that need to be loaded separately.
246 */
247 function wysiwyg_ckeditor_plugin_settings($editor, $profile, $plugins) {
248 $settings = array();
249 foreach ($plugins as $name => $plugin) {
250 // Register all plugins that need to be loaded.
251 if (!empty($plugin['load'])) {
252 $settings[$name] = array();
253 // Add path for native external plugins.
254 if (empty($plugin['internal']) && isset($plugin['path'])) {
255 $settings[$name]['path'] = base_path() . $plugin['path'];
256 }
257 // Force native internal plugins to use the standard path.
258 else {
259 $settings[$name]['path'] = base_path() . $editor['library path'] . '/plugins/' . $name . '/';
260 }
261 if (!empty($plugin['filename'])) {
262 $settings[$name]['fileName'] = $plugin['filename'];
263 }
264 }
265 }
266 return $settings;
267 }
268
269 /**
270 * Build a JS settings array for Drupal plugins loaded via the proxy plugin.
271 */
272 function wysiwyg_ckeditor_proxy_plugin_settings($editor, $profile, $plugins) {
273 $settings = array();
274 foreach ($plugins as $name => $plugin) {
275 // Populate required plugin settings.
276 $settings[$name] = $plugin['dialog settings'] + array(
277 'title' => $plugin['title'],
278 'icon' => base_path() . $plugin['icon path'] . '/' . $plugin['icon file'],
279 'iconTitle' => $plugin['icon title'],
280 // @todo These should only be set if the plugin defined them.
281 'css' => base_path() . $plugin['css path'] . '/' . $plugin['css file'],
282 );
283 }
284 return $settings;
285 }
286
287 /**
288 * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
289 */
290 function wysiwyg_ckeditor_plugins($editor) {
291 $plugins = array(
292 'default' => array(
293 'buttons' => array(
294 'Bold' => t('Bold'), 'Italic' => t('Italic'), 'Underline' => t('Underline'),
295 'Strike' => t('Strike-through'),
296 'JustifyLeft' => t('Align left'), 'JustifyCenter' => t('Align center'), 'JustifyRight' => t('Align right'), 'JustifyBlock' => t('Justify'),
297 'BulletedList' => t('Bullet list'), 'NumberedList' => t('Numbered list'),
298 'Outdent' => t('Outdent'), 'Indent' => t('Indent'),
299 'Undo' => t('Undo'), 'Redo' => t('Redo'),
300 'Link' => t('Link'), 'Unlink' => t('Unlink'), 'Anchor' => t('Anchor'),
301 'Image' => t('Image'),
302 'TextColor' => t('Forecolor'), 'BGColor' => t('Backcolor'),
303 'Superscript' => t('Superscript'), 'Subscript' => t('Subscript'),
304 'Blockquote' => t('Blockquote'), 'Source' => t('Source code'),
305 'HorizontalRule' => t('Horizontal rule'),
306 'Cut' => t('Cut'), 'Copy' => t('Copy'), 'Paste' => t('Paste'),
307 'PasteText' => t('Paste Text'), 'PasteFromWord' => t('Paste from Word'),
308 'ShowBlocks' => t('Show blocks'),
309 'RemoveFormat' => t('Remove format'),
310 'SpecialChar' => t('Character map'),
311 'Format' => t('HTML block format'), 'Font' => t('Font'), 'FontSize' => t('Font size'), 'Styles' => t('Font style'),
312 'Table' => t('Table'),
313 'SelectAll' => t('Select all'), 'Find' => t('Search'), 'Replace' => t('Replace'),
314 'Flash' => t('Flash'), 'Smiley' => t('Smiley'),
315 'CreateDiv' => t('Div container'),
316 'Maximize' => t('Maximize'),
317 'SpellChecker' => t('Check spelling'), 'Scayt' => t('Check spelling as you type'),
318 'About' => t('About'),
319 ),
320 'internal' => TRUE,
321 ),
322 );
323
324 if (version_compare($editor['installed version'], '3.1.0.4885', '<')) {
325 unset($plugins['default']['buttons']['CreateDiv']);
326 }
327 return $plugins;
328 }
329