#585932 by sun: Ported to Drupal 7.
[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, &$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)), PASS_THROUGH);
52
53 $form['format'] = array('#type' => 'value', '#value' => $profile->format);
54 $form['input_format'] = array('#type' => 'value', '#value' => $formats[$profile->format]->name);
55 $form['editor'] = array('#type' => 'value', '#value' => $profile->editor);
56
57 $form['basic'] = array(
58 '#type' => 'fieldset',
59 '#title' => t('Basic setup'),
60 '#collapsible' => TRUE,
61 '#collapsed' => TRUE,
62 );
63
64 $form['basic']['default'] = array(
65 '#type' => 'checkbox',
66 '#title' => t('Enabled by default'),
67 '#default_value' => $profile->settings['default'],
68 '#return_value' => 1,
69 '#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.'),
70 );
71
72 $form['basic']['user_choose'] = array(
73 '#type' => 'checkbox',
74 '#title' => t('Allow users to choose default'),
75 '#default_value' => $profile->settings['user_choose'],
76 '#return_value' => 1,
77 '#description' => t('If allowed, users will be able to choose their own editor default state in their user account settings.'),
78 );
79
80 $form['basic']['show_toggle'] = array(
81 '#type' => 'checkbox',
82 '#title' => t('Show <em>enable/disable rich text</em> toggle link'),
83 '#default_value' => $profile->settings['show_toggle'],
84 '#return_value' => 1,
85 '#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).'),
86 );
87
88 $form['basic']['theme'] = array(
89 '#type' => 'hidden',
90 '#value' => $profile->settings['theme'],
91 );
92
93 $form['basic']['language'] = array(
94 '#type' => 'select',
95 '#title' => t('Language'),
96 '#default_value' => $profile->settings['language'],
97 '#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', 'zh_cn', 'zh_tw', 'zh_tw_utf8')),
98 '#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.'),
99 );
100
101 $form['buttons'] = array(
102 '#type' => 'fieldset',
103 '#title' => t('Buttons and plugins'),
104 '#collapsible' => TRUE,
105 '#collapsed' => TRUE,
106 '#tree' => TRUE,
107 '#theme' => 'wysiwyg_admin_button_table',
108 );
109
110 $plugins = wysiwyg_get_plugins($profile->editor);
111 // Generate the button list.
112 foreach ($plugins as $name => $meta) {
113 if (isset($meta['buttons']) && is_array($meta['buttons'])) {
114 foreach ($meta['buttons'] as $button => $title) {
115 $icon = '';
116 if (!empty($meta['path'])) {
117 // @todo Button icon locations are different in editors, editor versions,
118 // and contrib/custom plugins (like Image Assist, f.e.).
119 $img_src = $meta['path'] . "/images/$name.gif";
120 // Handle plugins that have more than one button.
121 if (!file_exists($img_src)) {
122 $img_src = $meta['path'] . "/images/$button.gif";
123 }
124 $icon = file_exists($img_src) ? '<img src="' . base_path() . $img_src . '" title="' . $button . '" style="border: 1px solid grey; vertical-align: middle;" />' : '';
125 }
126 $title = (isset($meta['url']) ? l($title, $meta['url'], array('target' => '_blank')) : $title);
127 $title = (!empty($icon) ? $icon . ' ' . $title : $title);
128 $form['buttons'][$name][$button] = array(
129 '#type' => 'checkbox',
130 '#title' => $title,
131 '#default_value' => !empty($profile->settings['buttons'][$name][$button]) ? $profile->settings['buttons'][$name][$button] : FALSE,
132 );
133 }
134 }
135 else if (isset($meta['extensions']) && is_array($meta['extensions'])) {
136 foreach ($meta['extensions'] as $extension => $title) {
137 $form['buttons'][$name][$extension] = array(
138 '#type' => 'checkbox',
139 '#title' => isset($meta['url']) ? l($title, $meta['url'], array('target' => '_blank')) : $title,
140 '#default_value' => !empty($profile->settings['buttons'][$name][$extension]) ? $profile->settings['buttons'][$name][$extension] : FALSE,
141 );
142 }
143 }
144 }
145
146 $form['appearance'] = array(
147 '#type' => 'fieldset',
148 '#title' => t('Editor appearance'),
149 '#collapsible' => TRUE,
150 '#collapsed' => TRUE,
151 );
152
153 $form['appearance']['toolbar_loc'] = array(
154 '#type' => 'select',
155 '#title' => t('Toolbar location'),
156 '#default_value' => $profile->settings['toolbar_loc'],
157 '#options' => array('bottom' => t('Bottom'), 'top' => t('Top')),
158 '#description' => t('This option controls whether the editor toolbar is displayed above or below the editing area.'),
159 );
160
161 $form['appearance']['toolbar_align'] = array(
162 '#type' => 'select',
163 '#title' => t('Button alignment'),
164 '#default_value' => $profile->settings['toolbar_align'],
165 '#options' => array('center' => t('Center'), 'left' => t('Left'), 'right' => t('Right')),
166 '#description' => t('This option controls the alignment of icons in the editor toolbar.'),
167 );
168
169 $form['appearance']['path_loc'] = array(
170 '#type' => 'select',
171 '#title' => t('Path location'),
172 '#default_value' => $profile->settings['path_loc'],
173 '#options' => array('none' => t('Hide'), 'top' => t('Top'), 'bottom' => t('Bottom')),
174 '#description' => t('Where to display the path to HTML elements (i.e. <code>body > table > tr > td</code>).'),
175 );
176
177 $form['appearance']['resizing'] = array(
178 '#type' => 'checkbox',
179 '#title' => t('Enable resizing button'),
180 '#default_value' => $profile->settings['resizing'],
181 '#return_value' => 1,
182 '#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.'),
183 );
184
185 $form['output'] = array(
186 '#type' => 'fieldset',
187 '#title' => t('Cleanup and output'),
188 '#collapsible' => TRUE,
189 '#collapsed' => TRUE,
190 );
191
192 $form['output']['verify_html'] = array(
193 '#type' => 'checkbox',
194 '#title' => t('Verify HTML'),
195 '#default_value' => $profile->settings['verify_html'],
196 '#return_value' => 1,
197 '#description' => t('If enabled, potentially malicious code like <code>&lt;HEAD&gt;</code> tags will be removed from HTML contents.'),
198 );
199
200 $form['output']['preformatted'] = array(
201 '#type' => 'checkbox',
202 '#title' => t('Preformatted'),
203 '#default_value' => $profile->settings['preformatted'],
204 '#return_value' => 1,
205 '#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.'),
206 );
207
208 $form['output']['convert_fonts_to_spans'] = array(
209 '#type' => 'checkbox',
210 '#title' => t('Convert &lt;font&gt; tags to styles'),
211 '#default_value' => $profile->settings['convert_fonts_to_spans'],
212 '#return_value' => 1,
213 '#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.'),
214 );
215
216 $form['output']['remove_linebreaks'] = array(
217 '#type' => 'checkbox',
218 '#title' => t('Remove linebreaks'),
219 '#default_value' => $profile->settings['remove_linebreaks'],
220 '#return_value' => 1,
221 '#description' => t('If enabled, the editor will remove most linebreaks from contents. Disabling this option could avoid conflicts with other input filters.'),
222 );
223
224 $form['output']['apply_source_formatting'] = array(
225 '#type' => 'checkbox',
226 '#title' => t('Apply source formatting'),
227 '#default_value' => $profile->settings['apply_source_formatting'],
228 '#return_value' => 1,
229 '#description' => t('If enabled, the editor will re-format the HTML source code. Disabling this option could avoid conflicts with other input filters.'),
230 );
231
232 $form['output']['paste_auto_cleanup_on_paste'] = array(
233 '#type' => 'checkbox',
234 '#title' => t('Force cleanup on standard paste'),
235 '#default_value' => $profile->settings['paste_auto_cleanup_on_paste'],
236 '#return_value' => 1,
237 '#description' => t('If enabled, the default paste function (CTRL-V or SHIFT-INS) behaves like the "paste from word" plugin function.'),
238 );
239
240 $form['css'] = array(
241 '#type' => 'fieldset',
242 '#title' => t('CSS'),
243 '#collapsible' => TRUE,
244 '#collapsed' => TRUE,
245 );
246
247 $form['css']['block_formats'] = array(
248 '#type' => 'textfield',
249 '#title' => t('Block formats'),
250 '#default_value' => $profile->settings['block_formats'],
251 '#size' => 40,
252 '#maxlength' => 250,
253 '#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')),
254 );
255
256 $form['css']['css_setting'] = array(
257 '#type' => 'select',
258 '#title' => t('Editor CSS'),
259 '#default_value' => $profile->settings['css_setting'],
260 '#options' => array('theme' => t('Use theme CSS'), 'self' => t('Define CSS'), 'none' => t('Editor default CSS')),
261 '#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.'),
262 );
263
264 $form['css']['css_path'] = array(
265 '#type' => 'textfield',
266 '#title' => t('CSS path'),
267 '#default_value' => $profile->settings['css_path'],
268 '#size' => 40,
269 '#maxlength' => 255,
270 '#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',
271 );
272
273 $form['css']['css_classes'] = array(
274 '#type' => 'textarea',
275 '#title' => t('CSS classes'),
276 '#default_value' => $profile->settings['css_classes'],
277 '#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')),
278 );
279
280 $form['submit'] = array(
281 '#type' => 'submit',
282 '#value' => t('Save'),
283 );
284
285 return $form;
286 }
287
288 /**
289 * Submit callback for Wysiwyg profile form.
290 *
291 * @see wysiwyg_profile_form()
292 */
293 function wysiwyg_profile_form_submit($form, &$form_state) {
294 $values = $form_state['values'];
295 if (isset($values['buttons'])) {
296 // Store only enabled buttons for each plugin.
297 foreach ($values['buttons'] as $plugin => $buttons) {
298 $values['buttons'][$plugin] = array_filter($values['buttons'][$plugin]);
299 }
300 // Store only enabled plugins.
301 $values['buttons'] = array_filter($values['buttons']);
302 }
303 // Remove input format name.
304 $format = $values['format'];
305 $input_format = $values['input_format'];
306 $editor = $values['editor'];
307 unset($values['format'], $values['input_format'], $values['editor']);
308
309 // Remove FAPI values.
310 // @see system_settings_form_submit()
311 unset($values['submit'], $values['form_id'], $values['op'], $values['form_token'], $values['form_build_id']);
312
313 // Insert new profile data.
314 db_query("UPDATE {wysiwyg} SET settings = :settings WHERE format = :format", array(':settings' => serialize($values), ':format' => $format));
315
316 drupal_set_message(t('Wysiwyg profile for %format has been saved.', array('%format' => $input_format)));
317
318 $form_state['redirect'] = 'admin/config/content/wysiwyg';
319 }
320
321 /**
322 * Layout for the buttons in the Wysiwyg Editor profile form.
323 */
324 function theme_wysiwyg_admin_button_table($form) {
325 $buttons = array();
326
327 // Flatten forms array.
328 foreach (element_children($form) as $name) {
329 foreach (element_children($form[$name]) as $button) {
330 $buttons[] = drupal_render($form[$name][$button]);
331 }
332 }
333
334 // Split checkboxes into rows with 3 columns.
335 $total = count($buttons);
336 $rows = array();
337 for ($i = 0; $i < $total; $i++) {
338 $row = array();
339 $row[] = array('data' => $buttons[$i]);
340 if (isset($buttons[++$i])) {
341 $row[] = array('data' => $buttons[$i]);
342 }
343 if (isset($buttons[++$i])) {
344 $row[] = array('data' => $buttons[$i]);
345 }
346 $rows[] = $row;
347 }
348
349 $output = theme('table', array(), $rows, array('width' => '100%'));
350
351 return $output;
352 }
353
354 /**
355 * Display overview of setup Wysiwyg Editor profiles; menu callback.
356 */
357 function wysiwyg_profile_overview($form, &$form_state) {
358 include_once './includes/install.inc';
359
360 // Check which wysiwyg editors are installed.
361 $editors = wysiwyg_get_all_editors();
362 $count = count($editors);
363 $status = array();
364 $options = array('' => t('No editor'));
365
366 foreach ($editors as $name => $editor) {
367 $status[$name] = array(
368 'severity' => (isset($editor['error']) ? REQUIREMENT_ERROR : ($editor['installed'] ? REQUIREMENT_OK : REQUIREMENT_INFO)),
369 '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'])),
370 'value' => (isset($editor['installed version']) ? $editor['installed version'] : t('Not installed.')),
371 'description' => (isset($editor['error']) ? $editor['error'] : ''),
372 );
373 if ($editor['installed']) {
374 $options[$name] = $editor['title'] . (isset($editor['installed version']) ? ' ' . $editor['installed version'] : '');
375 }
376 else {
377 // Build on-site installation instructions.
378 // @todo Setup $library in wysiwyg_load_editor() already.
379 $library = (isset($editor['library']) ? $editor['library'] : key($editor['libraries']));
380 $targs = array(
381 '@editor-path' => $editor['editor path'],
382 '@library-filepath' => $editor['library path'] . '/' . $editor['libraries'][$library]['files'][0],
383 );
384 $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>';
385 $instructions .= '<p>' . t('So the actual library can be found at:<br /><code>@library-filepath</code>', $targs) . '</p>';
386
387 $status[$name]['description'] .= $instructions;
388 $count--;
389 }
390 // In case there is an error, always show installation instructions.
391 if (isset($editor['error'])) {
392 $show_instructions = TRUE;
393 }
394 }
395 if (!$count) {
396 $show_instructions = TRUE;
397 }
398 $form['status'] = array(
399 '#type' => 'fieldset',
400 '#title' => t('Installation instructions'),
401 '#collapsible' => TRUE,
402 '#collapsed' => !isset($show_instructions),
403 '#description' => (!$count ? t('There are no editor libraries installed currently. The following list contains a list of currently supported editors:') : ''),
404 '#weight' => 10,
405 );
406 $form['status']['report'] = array('#markup' => theme('status_report', $status));
407
408 if (!$count) {
409 return $form;
410 }
411
412 $formats = filter_formats();
413 $profiles = wysiwyg_profile_load_all();
414 $form['formats'] = array(
415 '#type' => 'item',
416 '#description' => t('To assign a different editor to a text format, click "delete" to remove the existing first.'),
417 '#tree' => TRUE,
418 );
419 foreach ($formats as $id => $format) {
420 $form['formats'][$id]['name'] = array(
421 '#markup' => check_plain($format->name),
422 );
423 // Only display editor selection for associated input formats to avoid
424 // confusion about disabled selection.
425 if (isset($profiles[$id]) && !empty($profiles[$id]->editor)) {
426 $form['formats'][$id]['editor'] = array(
427 '#markup' => $options[$profiles[$id]->editor],
428 );
429 }
430 else {
431 $form['formats'][$id]['editor'] = array(
432 '#type' => 'select',
433 '#default_value' => '',
434 '#options' => $options,
435 );
436 }
437 if (isset($profiles[$id]) && !empty($profiles[$id]->editor)) {
438 $form['formats'][$id]['edit'] = array(
439 '#markup' => l(t('Edit'), "admin/config/content/wysiwyg/profile/$id/edit"),
440 );
441 $form['formats'][$id]['delete'] = array(
442 '#markup' => l(t('Delete'), "admin/config/content/wysiwyg/profile/$id/delete"),
443 );
444 }
445 }
446
447 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
448 return $form;
449 }
450
451 /**
452 * Return HTML for the Wysiwyg profile overview form.
453 */
454 function theme_wysiwyg_profile_overview($form) {
455 if (!isset($form['formats'])) {
456 return;
457 }
458 $output = '';
459 $header = array(t('Input format'), t('Editor'), array('data' => t('Operations'), 'colspan' => 2));
460 $rows = array();
461 foreach (element_children($form['formats']) as $item) {
462 $format = &$form['formats'][$item];
463 $rows[] = array(
464 drupal_render($format['name']),
465 drupal_render($format['editor']),
466 isset($format['edit']) ? drupal_render($format['edit']) : '',
467 isset($format['delete']) ? drupal_render($format['delete']) : '',
468 );
469 }
470 $form['formats']['table']['#markup'] = theme('table', $header, $rows);
471 $output .= drupal_render_children($form);
472 return $output;
473 }
474
475 /**
476 * Submit callback for Wysiwyg profile overview form.
477 */
478 function wysiwyg_profile_overview_submit($form, &$form_state) {
479 foreach ($form_state['values']['formats'] as $format => $values) {
480 db_merge('wysiwyg')
481 ->key(array('format' => $format))
482 ->fields(array(
483 'editor' => $values['editor'],
484 ))
485 ->execute();
486 }
487 }
488
489 /**
490 * Delete editor profile confirmation form.
491 */
492 function wysiwyg_profile_delete_confirm($form, &$form_state, $profile) {
493 $formats = filter_formats();
494 $format = $formats[$profile->format];
495 $form['format'] = array('#type' => 'value', '#value' => $format);
496 return confirm_form(
497 $form,
498 t('Are you sure you want to remove the profile for %name?', array('%name' => $format->name)),
499 'admin/config/content/wysiwyg',
500 t('This action cannot be undone.'), t('Remove'), t('Cancel')
501 );
502 }
503
504 /**
505 * Submit callback for Wysiwyg profile delete form.
506 *
507 * @see wysiwyg_profile_delete_confirm()
508 */
509 function wysiwyg_profile_delete_confirm_submit($form, &$form_state) {
510 $format = $form_state['values']['format'];
511 wysiwyg_profile_delete($format->format);
512 drupal_set_message(t('Wysiwyg profile for %name has been deleted.', array('%name' => $format->name)));
513 $form_state['redirect'] = 'admin/config/content/wysiwyg';
514 }
515
516 /**
517 * Remove a profile from the database.
518 */
519 function wysiwyg_profile_delete($format) {
520 db_delete('wysiwyg')
521 ->condition('format', $format)
522 ->execute();
523 }
524