#328948 by sun: Fixed PHP notices when editors are assigned, but not configured.
[project/wysiwyg.git] / wysiwyg.module
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Integrate client-side editors with Drupal.
7 */
8
9 /**
10 * Implementation of hook_menu().
11 */
12 function wysiwyg_menu() {
13 $items = array();
14 $items['admin/settings/wysiwyg/profile'] = array(
15 'title' => 'Wysiwyg',
16 'page callback' => 'wysiwyg_admin',
17 'description' => 'Configure client-side editor profiles.',
18 'access arguments' => array('administer site configuration'),
19 'file' => 'wysiwyg.admin.inc',
20 );
21 return $items;
22 }
23
24 /**
25 * Implementation of hook_theme().
26 */
27 function wysiwyg_theme() {
28 return array(
29 'wysiwyg_profile_overview' => array('arguments' => array('form' => NULL)),
30 'wysiwyg_admin_button_table' => array('arguments' => array('form' => NULL)),
31 );
32 }
33
34 /**
35 * Implementation of hook_help().
36 */
37 function wysiwyg_help($path, $arg) {
38 switch ($path) {
39 case 'admin/settings/wysiwyg/profile':
40 if (empty($arg[4])) {
41 $output = '<p>'. t('A Wysiwyg profile can be associated to an input format. A Wysiwyg profile defines, which client-side editor is loaded, can define what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') .'</p>';
42 return $output;
43 }
44 break;
45 }
46 }
47
48 /**
49 * Implementation of hook_elements().
50 *
51 * Before Drupal 7, there is no way to easily identify form fields that are
52 * input format enabled. This is a workaround: We assign a form #after_build
53 * processing callback that is executed on all forms after they have been
54 * completely built, so that form elements already are in their effective order
55 * and position.
56 *
57 * @see wysiwyg_process_form()
58 *
59 * @todo Remove #wysiwyg_style; the GUI for an editor should be solely handled
60 * via profiles, when profiles are attached to an input format. It makes no
61 * sense to display TinyMCE's simple GUI/theme for the user signature, when
62 * the input format allows users to use advanced HTML and hence, editor
63 * plugins. Fix this here, in wysiwyg_process_element(), and lastly
64 * in wysiwyg_get_editor_config().
65 */
66 function wysiwyg_elements() {
67 $type = array();
68 // @todo Derive editor theme from input format.
69 $type['textarea'] = array('#wysiwyg_style' => 'advanced');
70 $type['form'] = array('#after_build' => array('wysiwyg_process_form'));
71 return $type;
72 }
73
74 /**
75 * Implementation of hook_form_alter().
76 */
77 function wysiwyg_form_alter(&$form, &$form_state) {
78 // Disable 'teaser' textarea.
79 if (isset($form['body_field'])) {
80 unset($form['body_field']['teaser_js']);
81 $form['body_field']['teaser_include'] = array();
82 }
83 }
84
85 /**
86 * Process a textarea for Wysiwyg Editor.
87 *
88 * This way, we can recurse into the form and search for certain, hard-coded
89 * elements that have been added by filter_form(). If an input format selector
90 * or input format guidelines element is found, we assume that the preceding
91 * element is the corresponding textarea and use it's #id for attaching
92 * client-side editors.
93 *
94 * @see wysiwyg_elements(), filter_form()
95 */
96 function wysiwyg_process_form(&$form) {
97 // Iterate over element children; resetting array keys to access last index.
98 if ($children = array_values(element_children($form))) {
99 foreach ($children as $index => $item) {
100 $element = &$form[$item];
101
102 // filter_form() always uses the key 'format'. We need a type-agnostic
103 // match to prevent false positives. Also, there must have been at least
104 // one element on this level.
105 if ($item === 'format' && $index > 0) {
106 // Make sure we either match a input format selector or input format
107 // guidelines (displayed if user has access to one input format only).
108 if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
109 // The element before this element is the target form field.
110 $field = &$form[$children[$index - 1]];
111
112 // Disable #resizable to avoid resizable behavior to hi-jack the UI,
113 // but load the behavior, so the 'none' editor can attach/detach it.
114 $extra_class = '';
115 if (!empty($field['#resizable'])) {
116 // Due to our CSS class parsing, we can add arbitrary parameters
117 // for each input format.
118 $extra_class = ' wysiwyg-resizable-1';
119 $field['#resizable'] = FALSE;
120 drupal_add_js('misc/textarea.js');
121 }
122
123 // Determine the available input formats. The last child element is a
124 // link to "More information about formatting options". When only one
125 // input format is displayed, we also have to remove formatting
126 // guidelines, stored in the child 'format'.
127 $formats = element_children($element);
128 array_pop($formats);
129 unset($formats['format']);
130 foreach ($formats as $format) {
131 // Default to 'none' editor (Drupal's default behaviors).
132 $editor = 'none';
133 $theme = '';
134 // Fetch the profile associated to this input format.
135 $profile = wysiwyg_get_profile($format);
136 if ($profile) {
137 $editor = $profile->editor;
138 // Check editor theme (and reset it if not/no longer available).
139 $theme = wysiwyg_get_editor_themes($profile, $field['#wysiwyg_style']);
140
141 // Add profile settings for this input format.
142 wysiwyg_add_editor_settings($profile, $theme);
143 // Add plugin settings for this input format.
144 wysiwyg_add_plugin_settings($profile);
145
146 $theme = ' wysiwyg-theme-'. $theme;
147 }
148
149 // Use a prefix/suffix for a single input format, or attach to input
150 // format selector radio buttons.
151 if (isset($element['format']['guidelines'])) {
152 $element[$format]['#prefix'] = '<div class="wysiwyg wysiwyg-editor-'. $editor .' wysiwyg-field-'. $field['#id'] . $theme . $extra_class .'">';
153 $element[$format]['#suffix'] = '</div>';
154 }
155 else {
156 if (isset($element[$format]['#attributes']['class'])) {
157 $element[$format]['#attributes']['class'] .= ' ';
158 }
159 else {
160 $element[$format]['#attributes']['class'] = '';
161 }
162 $element[$format]['#attributes']['class'] .= 'wysiwyg wysiwyg-editor-'. $editor .' wysiwyg-field-'. $field['#id'] . $theme . $extra_class;
163 }
164 }
165 }
166 // If this element is 'format', do not recurse further.
167 continue;
168 }
169 // Recurse into children.
170 wysiwyg_process_form($element);
171 }
172 }
173 return $form;
174 }
175
176 /**
177 * Determine the profile to use for a given input format id.
178 *
179 * This function also performs sanity checks for the configured editor in a
180 * profile to ensure that we do not load a malformed editor.
181 *
182 * @param $format
183 * The internal id of an input format.
184 *
185 * @return
186 * A wysiwyg profile.
187 *
188 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
189 */
190 function wysiwyg_get_profile($format) {
191 if ($profile = wysiwyg_load_profile($format)) {
192 if (wysiwyg_load_editor($profile)) {
193 return $profile;
194 }
195 }
196 return FALSE;
197 }
198
199 /**
200 * Load an editor library and initialize basic Wysiwyg settings.
201 *
202 * @param $profile
203 * A wysiwyg editor profile.
204 *
205 * @return
206 * TRUE if the editor has been loaded, FALSE if not.
207 *
208 * @see wysiwyg_get_profile()
209 */
210 function wysiwyg_load_editor($profile) {
211 static $settings_added;
212 static $loaded = array();
213
214 $name = $profile->editor;
215 // Library files must be loaded only once.
216 if (!isset($loaded[$name])) {
217 // Load editor.
218 $editor = wysiwyg_get_editor($name);
219 if ($editor) {
220 // Determine library files to load.
221 // @todo Allow to configure the library/execMode to use.
222 if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
223 $library = $profile->settings['library'];
224 $files = $editor['libraries'][$profile->settings['library']]['files'];
225 }
226 else {
227 // Fallback to the first by default (external libraries can change).
228 $library = key($editor['libraries']);
229 $files = array_shift($editor['libraries']);
230 $files = $files['files'];
231 }
232 foreach ($files as $file) {
233 drupal_add_js($editor['library path'] . '/' . $file);
234 }
235 // Load JavaScript integration files for this editor.
236 $files = array();
237 if (isset($editor['js files'])) {
238 $files = $editor['js files'];
239 }
240 foreach ($files as $file) {
241 drupal_add_js($editor['js path'] . '/' . $file);
242 }
243 // Load CSS stylesheets for this editor.
244 $files = array();
245 if (isset($editor['css files'])) {
246 $files = $editor['css files'];
247 }
248 foreach ($files as $file) {
249 drupal_add_css($editor['css path'] . '/' . $file, 'module', 'screen');
250 }
251
252 $status = wysiwyg_user_get_status($profile);
253 drupal_add_js(array('wysiwyg' => array(
254 'configs' => array($editor['name'] => array()),
255 // @todo Move into profile settings.
256 'showToggle' => isset($profile->settings['show_toggle']) ? $profile->settings['show_toggle'] : TRUE,
257 // @todo http://drupal.org/node/322433
258 'status' => $status,
259 // @todo Move into (global) editor settings.
260 // If JS compression is enabled, at least TinyMCE is unable to determine
261 // its own base path and exec mode since it can't find the script name.
262 'editorBasePath' => base_path() . $editor['library path'],
263 'execMode' => $library,
264 )), 'setting');
265
266 $loaded[$name] = TRUE;
267 }
268 else {
269 $loaded[$name] = FALSE;
270 }
271 }
272
273 // Add basic Wysiwyg settings if any editor has been added.
274 if (!isset($settings_added) && $loaded[$name]) {
275 drupal_add_js(array('wysiwyg' => array(
276 'configs' => array(),
277 'disable' => t('Disable rich-text'),
278 'enable' => t('Enable rich-text'),
279 )), 'setting');
280
281 // Initialize our namespaces in the *header* to do not force editor
282 // integration scripts to check and define Drupal.wysiwyg on its own.
283 drupal_add_js(wysiwyg_get_path('wysiwyg.init.js'), 'core');
284
285 // The 'none' editor is a special editor implementation, allowing us to
286 // attach and detach regular Drupal behaviors just like any other editor.
287 drupal_add_js(wysiwyg_get_path('editors/js/none.js'));
288
289 // Add wysiwyg.js to the footer to ensure it's executed after the
290 // Drupal.settings array has been rendered and populated. Also, since editor
291 // library initialization functions must be loaded first by the browser,
292 // and Drupal.wysiwygInit() must be executed AFTER editors registered
293 // their callbacks and BEFORE Drupal.behaviors are applied, this must come
294 // last.
295 drupal_add_js(wysiwyg_get_path('wysiwyg.js'), 'module', 'footer');
296
297 $settings_added = TRUE;
298 }
299
300 return $loaded[$name];
301 }
302
303 /**
304 * Register a theme.
305 */
306 function wysiwyg_add_editor_settings($profile, $theme) {
307 static $editors = array();
308
309 if (!isset($editors[$profile->editor][$theme])) {
310 $config = wysiwyg_get_editor_config($profile, $theme);
311 drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array($theme => $config)))), 'setting');
312 $editors[$profile->editor][$theme] = TRUE;
313 }
314 }
315
316 /**
317 * Add settings for external plugins.
318 *
319 * @param $profile
320 * A wysiwyg editor profile.
321 */
322 function wysiwyg_add_plugin_settings($profile) {
323 static $plugins_added = array();
324
325 if (!isset($plugins_added[$profile->editor])) {
326 $plugins = array();
327 $editor = wysiwyg_get_editor($profile->editor);
328 // Collect editor plugins provided via hook_wysiwyg_plugin().
329 $info = module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']);
330 // Only keep enabled plugins in this profile.
331 foreach ($info as $plugin => $meta) {
332 if (!isset($profile->settings['buttons'][$plugin])) {
333 unset($info[$plugin]);
334 }
335 }
336
337 if (isset($editor['plugin settings callback']) && function_exists($editor['plugin settings callback'])) {
338 $plugins = $editor['plugin settings callback']($editor, $profile, $info);
339 }
340
341 drupal_add_js(array('wysiwyg' => array('plugins' => array($profile->editor => $plugins))), 'setting');
342
343 $plugins_added[$profile->editor] = TRUE;
344 }
345 }
346
347 /**
348 * Grab the themes available to Wysiwyg Editor.
349 *
350 * Wysiwyg Editor themes control the functionality and buttons that are available to a
351 * user. Themes are only looked for within the default Wysiwyg Editor theme directory.
352 *
353 * @param $profile
354 * A wysiwyg editor profile; passed/altered by reference.
355 * @param $selected_theme
356 * An optional theme name that ought to be used.
357 *
358 * @return
359 * An array of theme names, or a single, checked theme name if $selected_theme
360 * was given.
361 */
362 function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
363 static $themes = array();
364
365 if (!isset($themes[$profile->editor])) {
366 $editor = wysiwyg_get_editor($profile->editor);
367 if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
368 $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
369 }
370 // Fallback to 'default' otherwise.
371 else {
372 $themes[$editor['name']] = array('default');
373 }
374 }
375
376 // Check optional $selected_theme argument, if given.
377 if (isset($selected_theme)) {
378 // If the passed theme name does not exist, use the first available.
379 if (!isset($themes[$profile->editor][$selected_theme])) {
380 $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
381 }
382 }
383
384 return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
385 }
386
387 /**
388 * Return plugin metadata from the plugin registry.
389 *
390 * @param $editor_name
391 * The internal name of an editor to return plugins for.
392 *
393 * @return
394 * An array for each plugin.
395 */
396 function wysiwyg_get_plugins($editor_name) {
397 $plugins = array();
398 if (!empty($editor_name)) {
399 $editor = wysiwyg_get_editor($editor_name);
400 // Add internal editor plugins.
401 if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
402 $plugins = $editor['plugin callback']($editor);
403 }
404 // Load our own plugins.
405 include_once drupal_get_path('module', 'wysiwyg') .'/wysiwyg.plugins.inc';
406
407 // Add editor plugins provided via hook_wysiwyg_plugin().
408 $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
409 }
410 return $plugins;
411 }
412
413 /**
414 * Return an array of initial Wysiwyg Editor config options from the current role.
415 */
416 function wysiwyg_get_editor_config($profile, $theme) {
417 $editor = wysiwyg_get_editor($profile->editor);
418 $settings = array();
419 if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
420 $settings = $editor['settings callback']($editor, $profile->settings, $theme);
421 }
422 return $settings;
423 }
424
425 /**
426 * Load all profiles. Just load one profile if $name is passed in.
427 */
428 function wysiwyg_load_profile($format = '') {
429 static $profiles;
430
431 if (!isset($profiles)) {
432 $profiles = array();
433 $result = db_query('SELECT * FROM {wysiwyg}');
434 while ($profile = db_fetch_object($result)) {
435 $profile->settings = unserialize($profile->settings);
436 $profiles[$profile->format] = $profile;
437 }
438 }
439
440 return ($format && isset($profiles[$format]) ? $profiles[$format] : ($format ? FALSE : $profiles));
441 }
442
443 /**
444 * Implementation of hook_user().
445 */
446 function wysiwyg_user($type, &$edit, &$user, $category = NULL) {
447 if ($type == 'form' && $category == 'account') {
448 // @todo http://drupal.org/node/322433
449 $profile = new stdClass;
450 if (isset($profile->settings['user_choose']) && $profile->settings['user_choose']) {
451 $form['wysiwyg'] = array(
452 '#type' => 'fieldset',
453 '#title' => t('Wysiwyg Editor settings'),
454 '#weight' => 10,
455 '#collapsible' => TRUE,
456 '#collapsed' => TRUE,
457 );
458 $form['wysiwyg']['wysiwyg_status'] = array(
459 '#type' => 'checkbox',
460 '#title' => t('Enable editor by default'),
461 '#default_value' => isset($user->wysiwyg_status) ? $user->wysiwyg_status : (isset($profile->settings['default']) ? $profile->settings['default'] : FALSE),
462 '#return_value' => 1,
463 '#description' => t('If enabled, rich-text editing is enabled by default in textarea fields.'),
464 );
465 return array('wysiwyg' => $form);
466 }
467 }
468 elseif ($type == 'validate' && isset($edit['wysiwyg_status'])) {
469 return array('wysiwyg_status' => $edit['wysiwyg_status']);
470 }
471 }
472
473 function wysiwyg_user_get_status($profile) {
474 global $user;
475
476 if ($profile->settings['user_choose'] && isset($user->wysiwyg_status)) {
477 $status = $user->wysiwyg_status;
478 }
479 else {
480 $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE;
481 }
482
483 return $status;
484 }
485
486 /**
487 * @defgroup wysiwyg_api Wysiwyg API
488 * @{
489 *
490 * @todo Forked from Panels; abstract into a separate API module that allows
491 * contrib modules to define supported include/plugin types.
492 */
493
494 /**
495 * Return library information for a given editor.
496 *
497 * @param $name
498 * The internal name of an editor.
499 *
500 * @return
501 * The library information for the editor, or FALSE if $name is unknown or not
502 * installed properly.
503 */
504 function wysiwyg_get_editor($name) {
505 $editors = wysiwyg_get_all_editors();
506 return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
507 }
508
509 /**
510 * Compile a list holding all supported editors including installed editor version information.
511 */
512 function wysiwyg_get_all_editors() {
513 static $editors;
514
515 if (isset($editors)) {
516 return $editors;
517 }
518
519 $editors = wysiwyg_load_includes('editors', 'editor');
520 foreach ($editors as $editor => $properties) {
521 // Fill in required properties.
522 $editors[$editor] += array(
523 'title' => '',
524 'vendor url' => '',
525 'download url' => '',
526 'editor path' => wysiwyg_get_path($properties['name']),
527 'library path' => wysiwyg_get_path($properties['name']),
528 'libraries' => array(),
529 'version callback' => NULL,
530 'themes callback' => NULL,
531 'settings callback' => NULL,
532 'plugin callback' => NULL,
533 'plugin settings callback' => NULL,
534 'versions' => array(),
535 'js path' => $properties['path'] .'/js',
536 'css path' => $properties['path'] .'/css',
537 );
538 // Check whether library is present.
539 if (!($editors[$editor]['installed'] = file_exists($properties['library path']))) {
540 continue;
541 }
542 // Detect library version.
543 if (function_exists($editors[$editor]['version callback'])) {
544 $editors[$editor]['installed version'] = $editors[$editor]['version callback']($properties);
545 }
546 if (empty($editors[$editor]['installed version'])) {
547 $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
548 $editors[$editor]['installed'] = FALSE;
549 continue;
550 }
551 // Determine to which supported version the installed version maps.
552 ksort($editors[$editor]['versions']);
553 $version = 0;
554 foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
555 if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
556 $version = $supported_version;
557 }
558 }
559 if (!$version) {
560 $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $properties['title']));
561 $editors[$editor]['installed'] = FALSE;
562 continue;
563 }
564 // Apply library version specific definitions and overrides.
565 $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
566 unset($editors[$editor]['versions']);
567 $editors[$editor]['title'] = $editors[$editor]['title'] . ' ' . $editors[$editor]['installed version'];
568 }
569 return $editors;
570 }
571
572 /**
573 * Load include files for wysiwyg implemented by all modules.
574 *
575 * @param $type
576 * The type of includes to search for, can be 'editors'.
577 * @param $hook
578 * The hook name to invoke.
579 * @param $file
580 * An optional include file name without .inc extension to limit the search to.
581 *
582 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
583 */
584 function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
585 // Determine implementations.
586 $directories = wysiwyg_get_directories($type);
587 $directories['wysiwyg_'] = wysiwyg_get_path($type);
588 $file_list = array();
589 foreach ($directories as $module => $path) {
590 $file_list[$module] = drupal_system_listing("$file" . '.inc$', $path, 'name', 0);
591 }
592
593 // Load implementations.
594 $info = array();
595 foreach (array_filter($file_list) as $module => $files) {
596 foreach ($files as $file) {
597 include_once './' . $file->filename;
598 $result = _wysiwyg_process_include('wysiwyg', $module . $file->name, dirname($file->filename), $hook);
599 if (is_array($result)) {
600 $info = array_merge($info, $result);
601 }
602 }
603 }
604 return $info;
605 }
606
607 /**
608 * Helper function to build module/file paths.
609 *
610 * @param $file
611 * A file or directory in a module to return.
612 * @param $base_path
613 * Whether to prefix the resulting path with base_path().
614 * @param $module
615 * The module name to use as prefix.
616 *
617 * @return
618 * The path to the specified file in a module.
619 */
620 function wysiwyg_get_path($file = '', $base_path = FALSE, $module = 'wysiwyg') {
621 $base_path = ($base_path ? base_path() : '');
622 return $base_path . drupal_get_path('module', $module) . '/' . $file;
623 }
624
625 /**
626 * Return a list of directories by modules implementing wysiwyg_include_directory().
627 *
628 * @param $plugintype
629 * The type of a plugin; can be 'editors'.
630 *
631 * @return
632 * An array containing module names suffixed with '_' and their defined
633 * directory.
634 *
635 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
636 */
637 function wysiwyg_get_directories($plugintype) {
638 $directories = array();
639 foreach (module_implements('wysiwyg_include_directory') as $module) {
640 $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
641 if (isset($result) && is_string($result)) {
642 $directories[$module .'_'] = drupal_get_path('module', $module) .'/'. $result;
643 }
644 }
645 return $directories;
646 }
647
648 /**
649 * Process a single hook implementation of a wysiwyg editor.
650 *
651 * @param $module
652 * The module that owns the hook.
653 * @param $identifier
654 * Either the module or 'wysiwyg_' . $file->name
655 * @param $hook
656 * The name of the hook being invoked.
657 */
658 function _wysiwyg_process_include($module, $identifier, $path, $hook) {
659 $function = $identifier . '_' . $hook;
660 if (!function_exists($function)) {
661 return NULL;
662 }
663 $result = $function();
664 if (!isset($result) || !is_array($result)) {
665 return NULL;
666 }
667
668 // Fill in defaults.
669 foreach ($result as $editor => $properties) {
670 $result[$editor]['module'] = $module;
671 $result[$editor]['name'] = $editor;
672 $result[$editor]['path'] = $path;
673 }
674 return $result;
675 }
676
677 /**
678 * @} End of "defgroup wysiwyg_api".
679 */
680