#667848 by TwoD, kaakuu: Fixed FCKeditor is not properly detached in IE.
[project/wysiwyg.git] / wysiwyg.admin.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Integrate Wysiwyg editors into Drupal.
7 */
8
9 /**
10 * Form builder for Wysiwyg profile form.
11 */
12 function wysiwyg_profile_form($form_state, $profile) {
13 // Merge in defaults.
14 $profile = (array) $profile;
15 $profile += array(
16 'format' => 0,
17 'editor' => '',
18 );
19 if (empty($profile['settings'])) {
20 $profile['settings'] = array();
21 }
22 $profile['settings'] += array(
23 'default' => TRUE,
24 'user_choose' => FALSE,
25 'show_toggle' => TRUE,
26 'theme' => 'advanced',
27 'language' => 'en',
28 'access' => 1,
29 'access_pages' => "node/*\nuser/*\ncomment/*",
30 'buttons' => array(),
31 'toolbar_loc' => 'top',
32 'toolbar_align' => 'left',
33 'path_loc' => 'bottom',
34 'resizing' => TRUE,
35 // Also available, but buggy in TinyMCE 2.x: blockquote,code,dt,dd,samp.
36 'block_formats' => 'p,address,pre,h2,h3,h4,h5,h6,div',
37 'verify_html' => TRUE,
38 'preformatted' => FALSE,
39 'convert_fonts_to_spans' => TRUE,
40 'remove_linebreaks' => TRUE,
41 'apply_source_formatting' => FALSE,
42 'paste_auto_cleanup_on_paste' => FALSE,
43 'css_setting' => 'theme',
44 'css_path' => NULL,
45 'css_classes' => NULL,
46 );
47 $profile = (object) $profile;
48
49 $formats = filter_formats();
50 $editor = wysiwyg_get_editor($profile->editor);
51 drupal_set_title(t('%editor profile for %format', array('%editor' => $editor['title'], '%format' => $formats[$profile->format]->name)));
52
53 $form = array();
54 $form['format'] = array('#type' => 'value', '#value' => $profile->format);
55 $form['input_format'] = array('#type' => 'value', '#value' => $formats[$profile->format]->name);
56 $form['editor'] = array('#type' => 'value', '#value' => $profile->editor);
57
58 $form['basic'] = array(
59 '#type' => 'fieldset',
60 '#title' => t('Basic setup'),
61 '#collapsible' => TRUE,
62 '#collapsed' => TRUE,
63 );
64
65 $form['basic']['default'] = array(
66 '#type' => 'checkbox',
67 '#title' => t('Enabled by default'),
68 '#default_value' => $profile->settings['default'],
69 '#return_value' => 1,
70 '#description' => t('The default editor state for users having access to this profile. Users are able to override this state if the next option is enabled.'),
71 );
72
73 $form['basic']['user_choose'] = array(
74 '#type' => 'checkbox',
75 '#title' => t('Allow users to choose default'),
76 '#default_value' => $profile->settings['user_choose'],
77 '#return_value' => 1,
78 '#description' => t('If allowed, users will be able to choose their own editor default state in their user account settings.'),
79 );
80
81 $form['basic']['show_toggle'] = array(
82 '#type' => 'checkbox',
83 '#title' => t('Show <em>enable/disable rich text</em> toggle link'),
84 '#default_value' => $profile->settings['show_toggle'],
85 '#return_value' => 1,
86 '#description' => t('Whether or not to show the <em>enable/disable rich text</em> toggle link below a textarea. If disabled, the user setting or global default is used (see above).'),
87 );
88
89 $form['basic']['theme'] = array(
90 '#type' => 'hidden',
91 '#value' => $profile->settings['theme'],
92 );
93
94 $form['basic']['language'] = array(
95 '#type' => 'select',
96 '#title' => t('Language'),
97 '#default_value' => $profile->settings['language'],
98 '#options' => drupal_map_assoc(array('ar', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'fr_ca', 'he', 'hu', 'is', 'it', 'ja', 'ko', 'nb', 'nl', 'nn', 'pl', 'pt', 'pt_br', 'ru', 'ru_KOI8-R', 'ru_UTF-8', 'si', 'sk', 'sv', 'th', 'tr', 'uk', 'zh_cn', 'zh_tw', 'zh_tw_utf8')),
99 '#description' => t('The language to use for the editor interface. Language codes are based on the <a href="http://www.loc.gov/standards/iso639-2/englangn.html">ISO-639-2</a> format.'),
100 );
101
102 $form['buttons'] = array(
103 '#type' => 'fieldset',
104 '#title' => t('Buttons and plugins'),
105 '#collapsible' => TRUE,
106 '#collapsed' => TRUE,
107 '#tree' => TRUE,
108 '#theme' => 'wysiwyg_admin_button_table',
109 );
110
111 $plugins = wysiwyg_get_plugins($profile->editor);
112 // Generate the button list.
113 foreach ($plugins as $name => $meta) {
114 if (isset($meta['buttons']) && is_array($meta['buttons'])) {
115 foreach ($meta['buttons'] as $button => $title) {
116 $icon = '';
117 if (!empty($meta['path'])) {
118 // @todo Button icon locations are different in editors, editor versions,
119 // and contrib/custom plugins (like Image Assist, f.e.).
120 $img_src = $meta['path'] . "/images/$name.gif";
121 // Handle plugins that have more than one button.
122 if (!file_exists($img_src)) {
123 $img_src = $meta['path'] . "/images/$button.gif";
124 }
125 $icon = file_exists($img_src) ? '<img src="' . base_path() . $img_src . '" title="' . $button . '" style="border: 1px solid grey; vertical-align: middle;" />' : '';
126 }
127 $title = (isset($meta['url']) ? l($title, $meta['url'], array('target' => '_blank')) : $title);
128 $title = (!empty($icon) ? $icon . ' ' . $title : $title);
129 $form['buttons'][$name][$button] = array(
130 '#type' => 'checkbox',
131 '#title' => $title,
132 '#default_value' => !empty($profile->settings['buttons'][$name][$button]) ? $profile->settings['buttons'][$name][$button] : FALSE,
133 );
134 }
135 }
136 else if (isset($meta['extensions']) && is_array($meta['extensions'])) {
137 foreach ($meta['extensions'] as $extension => $title) {
138 $form['buttons'][$name][$extension] = array(
139 '#type' => 'checkbox',
140 '#title' => isset($meta['url']) ? l($title, $meta['url'], array('target' => '_blank')) : $title,
141 '#default_value' => !empty($profile->settings['buttons'][$name][$extension]) ? $profile->settings['buttons'][$name][$extension] : FALSE,
142 );
143 }
144 }
145 }
146
147 $form['appearance'] = array(
148 '#type' => 'fieldset',
149 '#title' => t('Editor appearance'),
150 '#collapsible' => TRUE,
151 '#collapsed' => TRUE,
152 );
153
154 $form['appearance']['toolbar_loc'] = array(
155 '#type' => 'select',
156 '#title' => t('Toolbar location'),
157 '#default_value' => $profile->settings['toolbar_loc'],
158 '#options' => array('bottom' => t('Bottom'), 'top' => t('Top')),
159 '#description' => t('This option controls whether the editor toolbar is displayed above or below the editing area.'),
160 );
161
162 $form['appearance']['toolbar_align'] = array(
163 '#type' => 'select',
164 '#title' => t('Button alignment'),
165 '#default_value' => $profile->settings['toolbar_align'],
166 '#options' => array('center' => t('Center'), 'left' => t('Left'), 'right' => t('Right')),
167 '#description' => t('This option controls the alignment of icons in the editor toolbar.'),
168 );
169
170 $form['appearance']['path_loc'] = array(
171 '#type' => 'select',
172 '#title' => t('Path location'),
173 '#default_value' => $profile->settings['path_loc'],
174 '#options' => array('none' => t('Hide'), 'top' => t('Top'), 'bottom' => t('Bottom')),
175 '#description' => t('Where to display the path to HTML elements (i.e. <code>body > table > tr > td</code>).'),
176 );
177
178 $form['appearance']['resizing'] = array(
179 '#type' => 'checkbox',
180 '#title' => t('Enable resizing button'),
181 '#default_value' => $profile->settings['resizing'],
182 '#return_value' => 1,
183 '#description' => t('This option gives you the ability to enable/disable the resizing button. If enabled, the Path location toolbar must be set to "Top" or "Bottom" in order to display the resize icon.'),
184 );
185
186 $form['output'] = array(
187 '#type' => 'fieldset',
188 '#title' => t('Cleanup and output'),
189 '#collapsible' => TRUE,
190 '#collapsed' => TRUE,
191 );
192
193 $form['output']['verify_html'] = array(
194 '#type' => 'checkbox',
195 '#title' => t('Verify HTML'),
196 '#default_value' => $profile->settings['verify_html'],
197 '#return_value' => 1,
198 '#description' => t('If enabled, potentially malicious code like <code>&lt;HEAD&gt;</code> tags will be removed from HTML contents.'),
199 );
200
201 $form['output']['preformatted'] = array(
202 '#type' => 'checkbox',
203 '#title' => t('Preformatted'),
204 '#default_value' => $profile->settings['preformatted'],
205 '#return_value' => 1,
206 '#description' => t('If enabled, the editor will insert TAB characters on tab and preserve other whitespace characters just like a PRE element in HTML does.'),
207 );
208
209 $form['output']['convert_fonts_to_spans'] = array(
210 '#type' => 'checkbox',
211 '#title' => t('Convert &lt;font&gt; tags to styles'),
212 '#default_value' => $profile->settings['convert_fonts_to_spans'],
213 '#return_value' => 1,
214 '#description' => t('If enabled, HTML tags declaring the font size, font family, font color and font background color will be replaced by inline CSS styles.'),
215 );
216
217 $form['output']['remove_linebreaks'] = array(
218 '#type' => 'checkbox',
219 '#title' => t('Remove linebreaks'),
220 '#default_value' => $profile->settings['remove_linebreaks'],
221 '#return_value' => 1,
222 '#description' => t('If enabled, the editor will remove most linebreaks from contents. Disabling this option could avoid conflicts with other input filters.'),
223 );
224
225 $form['output']['apply_source_formatting'] = array(
226 '#type' => 'checkbox',
227 '#title' => t('Apply source formatting'),
228 '#default_value' => $profile->settings['apply_source_formatting'],
229 '#return_value' => 1,
230 '#description' => t('If enabled, the editor will re-format the HTML source code. Disabling this option could avoid conflicts with other input filters.'),
231 );
232
233 $form['output']['paste_auto_cleanup_on_paste'] = array(
234 '#type' => 'checkbox',
235 '#title' => t('Force cleanup on standard paste'),
236 '#default_value' => $profile->settings['paste_auto_cleanup_on_paste'],
237 '#return_value' => 1,
238 '#description' => t('If enabled, the default paste function (CTRL-V or SHIFT-INS) behaves like the "paste from word" plugin function.'),
239 );
240
241 $form['css'] = array(
242 '#type' => 'fieldset',
243 '#title' => t('CSS'),
244 '#collapsible' => TRUE,
245 '#collapsed' => TRUE,
246 );
247
248 $form['css']['block_formats'] = array(
249 '#type' => 'textfield',
250 '#title' => t('Block formats'),
251 '#default_value' => $profile->settings['block_formats'],
252 '#size' => 40,
253 '#maxlength' => 250,
254 '#description' => t('Comma separated list of HTML block formats. Possible values: <code>@format-list</code>.', array('@format-list' => 'p,h1,h2,h3,h4,h5,h6,div,blockquote,address,pre,code,dt,dd')),
255 );
256
257 $form['css']['css_setting'] = array(
258 '#type' => 'select',
259 '#title' => t('Editor CSS'),
260 '#default_value' => $profile->settings['css_setting'],
261 '#options' => array('theme' => t('Use theme CSS'), 'self' => t('Define CSS'), 'none' => t('Editor default CSS')),
262 '#description' => t('Defines the CSS to be used in the editor area.<br />Use theme CSS - loads stylesheets from current site theme.<br/>Define CSS - enter path for stylesheet files below.<br />Editor default CSS - uses default stylesheets from editor.'),
263 );
264
265 $form['css']['css_path'] = array(
266 '#type' => 'textfield',
267 '#title' => t('CSS path'),
268 '#default_value' => $profile->settings['css_path'],
269 '#size' => 40,
270 '#maxlength' => 255,
271 '#description' => t('If "Define CSS" was selected above, enter path to a CSS file or a list of CSS files separated by a comma.') . '<br />' . t('Available tokens: <code>%b</code> (base path, eg: <code>/</code>), <code>%t</code> (path to theme, eg: <code>themes/garland</code>)') . '<br />' . t('Example:') . ' css/editor.css,/themes/garland/style.css,%b%t/style.css,http://example.com/external.css',
272 );
273
274 $form['css']['css_classes'] = array(
275 '#type' => 'textarea',
276 '#title' => t('CSS classes'),
277 '#default_value' => $profile->settings['css_classes'],
278 '#description' => t('Optionally define CSS classes for the "Font style" dropdown list.<br />Enter one class on each line in the format: !format. Example: !example<br />If left blank, CSS classes are automatically imported from all loaded stylesheet(s).', array('!format' => '<code>[title]=[class]</code>', '!example' => 'My heading=header1')),
279 );
280
281 $form['submit'] = array(
282 '#type' => 'submit',
283 '#value' => t('Save'),
284 );
285
286 return $form;
287 }
288
289 /**
290 * Submit callback for Wysiwyg profile form.
291 *
292 * @see wysiwyg_profile_form()
293 */
294 function wysiwyg_profile_form_submit($form, &$form_state) {
295 $values = $form_state['values'];
296 if (isset($values['buttons'])) {
297 // Store only enabled buttons for each plugin.
298 foreach ($values['buttons'] as $plugin => $buttons) {
299 $values['buttons'][$plugin] = array_filter($values['buttons'][$plugin]);
300 }
301 // Store only enabled plugins.
302 $values['buttons'] = array_filter($values['buttons']);
303 }
304 // Remove input format name.
305 $format = $values['format'];
306 $input_format = $values['input_format'];
307 $editor = $values['editor'];
308 unset($values['format'], $values['input_format'], $values['editor']);
309
310 // Remove FAPI values.
311 // @see system_settings_form_submit()
312 unset($values['submit'], $values['form_id'], $values['op'], $values['form_token'], $values['form_build_id']);
313
314 // Insert new profile data.
315 db_query("UPDATE {wysiwyg} SET settings = '%s' WHERE format = %d", serialize($values), $format);
316
317 drupal_set_message(t('Wysiwyg profile for %format has been saved.', array('%format' => $input_format)));
318
319 $form_state['redirect'] = 'admin/settings/wysiwyg';
320 }
321
322 /**
323 * Layout for the buttons in the Wysiwyg Editor profile form.
324 */
325 function theme_wysiwyg_admin_button_table($form) {
326 $buttons = array();
327
328 // Flatten forms array.
329 foreach (element_children($form) as $name) {
330 foreach (element_children($form[$name]) as $button) {
331 $buttons[] = drupal_render($form[$name][$button]);
332 }
333 }
334
335 // Split checkboxes into rows with 3 columns.
336 $total = count($buttons);
337 $rows = array();
338 for ($i = 0; $i < $total; $i++) {
339 $row = array();
340 $row[] = array('data' => $buttons[$i]);
341 if (isset($buttons[++$i])) {
342 $row[] = array('data' => $buttons[$i]);
343 }
344 if (isset($buttons[++$i])) {
345 $row[] = array('data' => $buttons[$i]);
346 }
347 $rows[] = $row;
348 }
349
350 $output = theme('table', array(), $rows, array('width' => '100%'));
351
352 return $output;
353 }
354
355 /**
356 * Display overview of setup Wysiwyg Editor profiles; menu callback.
357 */
358 function wysiwyg_profile_overview() {
359 include_once './includes/install.inc';
360 $form = array();
361
362 // Check which wysiwyg editors are installed.
363 $editors = wysiwyg_get_all_editors();
364 $count = count($editors);
365 $status = array();
366 $options = array('' => t('No editor'));
367
368 foreach ($editors as $name => $editor) {
369 $status[$name] = array(
370 'severity' => (isset($editor['error']) ? REQUIREMENT_ERROR : ($editor['installed'] ? REQUIREMENT_OK : REQUIREMENT_INFO)),
371 'title' => t('<a href="!vendor-url">@editor</a> (<a href="!download-url">Download</a>)', array('!vendor-url' => $editor['vendor url'], '@editor' => $editor['title'], '!download-url' => $editor['download url'])),
372 'value' => (isset($editor['installed version']) ? $editor['installed version'] : t('Not installed.')),
373 'description' => (isset($editor['error']) ? $editor['error'] : ''),
374 );
375 if ($editor['installed']) {
376 $options[$name] = $editor['title'] . (isset($editor['installed version']) ? ' ' . $editor['installed version'] : '');
377 }
378 else {
379 // Build on-site installation instructions.
380 // @todo Setup $library in wysiwyg_load_editor() already.
381 $library = (isset($editor['library']) ? $editor['library'] : key($editor['libraries']));
382 $targs = array(
383 '@editor-path' => $editor['editor path'],
384 '@library-filepath' => $editor['library path'] . '/' . (isset($editor['libraries'][$library]['files'][0]) ? $editor['libraries'][$library]['files'][0] : key($editor['libraries'][$library]['files'])),
385 );
386 $instructions = '<p>' . t('Extract the archive and copy its contents into a new folder in the following location:<br /><code>@editor-path</code>', $targs) . '</p>';
387 $instructions .= '<p>' . t('So the actual library can be found at:<br /><code>@library-filepath</code>', $targs) . '</p>';
388
389 $status[$name]['description'] .= $instructions;
390 $count--;
391 }
392 // In case there is an error, always show installation instructions.
393 if (isset($editor['error'])) {
394 $show_instructions = TRUE;
395 }
396 }
397 if (!$count) {
398 $show_instructions = TRUE;
399 }
400 $form['status'] = array(
401 '#type' => 'fieldset',
402 '#title' => t('Installation instructions'),
403 '#collapsible' => TRUE,
404 '#collapsed' => !isset($show_instructions),
405 '#description' => (!$count ? t('There are no editor libraries installed currently. The following list contains a list of currently supported editors:') : ''),
406 '#weight' => 10,
407 );
408 $form['status']['report'] = array('#type' => 'markup', '#value' => theme('status_report', $status));
409
410 if (!$count) {
411 return $form;
412 }
413
414 $formats = filter_formats();
415 $profiles = wysiwyg_profile_load_all();
416 $form['formats'] = array(
417 '#type' => 'fieldset',
418 '#title' => t('Wysiwyg profiles'),
419 '#description' => t('Once an editor has been associated with an input format, the editor association cannot be changed without first deleting the profile and then creating a new one. Delete a profile by clicking on the "delete" link and afterwards, set up a new profile as usual.'),
420 '#tree' => TRUE,
421 );
422
423 $enable_save = FALSE;
424 foreach ($formats as $id => $format) {
425 $form['formats'][$id]['name'] = array(
426 '#value' => check_plain($format->name),
427 );
428 // Only display editor selection for associated input formats to avoid
429 // confusion about disabled selection.
430 if (isset($profiles[$id]) && !empty($profiles[$id]->editor)) {
431 $form['formats'][$id]['editor'] = array(
432 '#type' => 'markup',
433 '#value' => $options[$profiles[$id]->editor],
434 '#id' => "edit-editor-$id",
435 );
436 }
437 else {
438 $form['formats'][$id]['editor'] = array(
439 '#type' => 'select',
440 '#default_value' => '',
441 '#options' => $options,
442 '#id' => "edit-editor-$id",
443 );
444 $enable_save = TRUE;
445 }
446 if (isset($profiles[$id]) && !empty($profiles[$id]->editor)) {
447 $form['formats'][$id]['edit'] = array(
448 '#value' => l(t('Edit'), "admin/settings/wysiwyg/profile/$id/edit"),
449 );
450 $form['formats'][$id]['delete'] = array(
451 '#value' => l(t('Delete'), "admin/settings/wysiwyg/profile/$id/delete"),
452 );
453 }
454 }
455
456 // Submitting the form when no editors can be selected causes errors.
457 if ($enable_save) {
458 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
459 }
460 return $form;
461 }
462
463 /**
464 * Return HTML for the Wysiwyg profile overview form.
465 */
466 function theme_wysiwyg_profile_overview($form) {
467 if (!isset($form['formats'])) {
468 return;
469 }
470 $output = '';
471 $header = array(t('Input format'), t('Editor'), array('data' => t('Operations'), 'colspan' => 2));
472 $rows = array();
473 foreach (element_children($form['formats']) as $item) {
474 $format = &$form['formats'][$item];
475 $rows[] = array(
476 drupal_render($format['name']),
477 drupal_render($format['editor']),
478 isset($format['edit']) ? drupal_render($format['edit']) : '',
479 isset($format['delete']) ? drupal_render($format['delete']) : '',
480 );
481 }
482 $form['formats']['#children'] = theme('table', $header, $rows);
483 $output .= drupal_render($form);
484 return $output;
485 }
486
487 /**
488 * Submit callback for Wysiwyg profile overview form.
489 */
490 function wysiwyg_profile_overview_submit($form, &$form_state) {
491 foreach ($form_state['values']['formats'] as $format => $values) {
492 db_query("UPDATE {wysiwyg} SET editor = '%s' WHERE format = %d", $values['editor'], $format);
493 if (!db_affected_rows()) {
494 db_query("INSERT INTO {wysiwyg} (format, editor) VALUES (%d, '%s')", $format, $values['editor']);
495 }
496 }
497 }
498
499 /**
500 * Delete editor profile confirmation form.
501 */
502 function wysiwyg_profile_delete_confirm(&$form_state, $profile) {
503 $formats = filter_formats();
504 $format = $formats[$profile->format];
505 $form['format'] = array('#type' => 'value', '#value' => $format);
506 return confirm_form(
507 $form,
508 t('Are you sure you want to remove the profile for %name?', array('%name' => $format->name)),
509 'admin/settings/wysiwyg',
510 t('This action cannot be undone.'), t('Remove'), t('Cancel')
511 );
512 }
513
514 /**
515 * Submit callback for Wysiwyg profile delete form.
516 *
517 * @see wysiwyg_profile_delete_confirm()
518 */
519 function wysiwyg_profile_delete_confirm_submit($form, &$form_state) {
520 $format = $form_state['values']['format'];
521 wysiwyg_profile_delete($format->format);
522 drupal_set_message(t('Wysiwyg profile for %name has been deleted.', array('%name' => $format->name)));
523 $form_state['redirect'] = 'admin/settings/wysiwyg';
524 }
525
526 /**
527 * Remove a profile from the database.
528 */
529 function wysiwyg_profile_delete($format) {
530 db_query("DELETE FROM {wysiwyg} WHERE format = %d", $format);
531 }
532