#489156 by sun: Removed orphan global 'showToggle' JS setting.
[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['admin/settings/wysiwyg'] = array(
14 'title' => 'Wysiwyg',
15 'page callback' => 'drupal_get_form',
16 'page arguments' => array('wysiwyg_profile_overview'),
17 'description' => 'Configure client-side editors.',
18 'access arguments' => array('administer filters'),
19 'file' => 'wysiwyg.admin.inc',
20 );
21 $items['admin/settings/wysiwyg/profile'] = array(
22 'title' => 'Profiles',
23 'type' => MENU_DEFAULT_LOCAL_TASK,
24 );
25 $items['admin/settings/wysiwyg/profile/%wysiwyg_profile/edit'] = array(
26 'title' => 'Edit',
27 'page callback' => 'drupal_get_form',
28 'page arguments' => array('wysiwyg_profile_form', 4),
29 'access arguments' => array('administer filters'),
30 'file' => 'wysiwyg.admin.inc',
31 'tab_root' => 'admin/settings/wysiwyg/profile',
32 'tab_parent' => 'admin/settings/wysiwyg/profile/%wysiwyg_profile',
33 'type' => MENU_LOCAL_TASK,
34 );
35 $items['admin/settings/wysiwyg/profile/%wysiwyg_profile/delete'] = array(
36 'title' => 'Remove',
37 'page callback' => 'drupal_get_form',
38 'page arguments' => array('wysiwyg_profile_delete_confirm', 4),
39 'access arguments' => array('administer filters'),
40 'file' => 'wysiwyg.admin.inc',
41 'tab_root' => 'admin/settings/wysiwyg/profile',
42 'tab_parent' => 'admin/settings/wysiwyg/profile/%wysiwyg_profile',
43 'type' => MENU_LOCAL_TASK,
44 'weight' => 10,
45 );
46 $items['wysiwyg/%'] = array(
47 'page callback' => 'wysiwyg_dialog',
48 'page arguments' => array(1),
49 'access arguments' => array('access content'),
50 'type' => MENU_CALLBACK,
51 'file' => 'wysiwyg.dialog.inc',
52 );
53 return $items;
54 }
55
56 /**
57 * Implementation of hook_theme().
58 *
59 * @see drupal_common_theme(), common.inc
60 * @see template_preprocess_page(), theme.inc
61 */
62 function wysiwyg_theme() {
63 return array(
64 'wysiwyg_profile_overview' => array('arguments' => array('form' => NULL)),
65 'wysiwyg_admin_button_table' => array('arguments' => array('form' => NULL)),
66 'wysiwyg_dialog_page' => array(
67 'arguments' => array('content' => NULL, 'show_messages' => TRUE),
68 'file' => 'wysiwyg.dialog.inc',
69 'template' => 'wysiwyg-dialog-page',
70 ),
71 );
72 }
73
74 /**
75 * Implementation of hook_help().
76 */
77 function wysiwyg_help($path, $arg) {
78 switch ($path) {
79 case 'admin/settings/wysiwyg':
80 $output = '<p>' . t('A Wysiwyg profile is associated with an input format. A Wysiwyg profile defines which client-side editor is loaded with a particular input format, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') . '</p>';
81 return $output;
82 }
83 }
84
85 /**
86 * Implementation of hook_form_alter().
87 *
88 * Before Drupal 7, there is no way to easily identify form fields that are
89 * input format enabled. As a workaround, we assign a form #after_build
90 * processing callback that is executed on all forms after they have been
91 * completely built, so form elements are in their effective order
92 * and position already.
93 *
94 * @see wysiwyg_process_form()
95 */
96 function wysiwyg_form_alter(&$form, &$form_state) {
97 $form['#after_build'][] = 'wysiwyg_process_form';
98 // Teaser splitter is unconditionally removed and NOT supported.
99 if (isset($form['body_field'])) {
100 unset($form['body_field']['teaser_js']);
101 }
102 }
103
104 /**
105 * Process a textarea for Wysiwyg Editor.
106 *
107 * This way, we can recurse into the form and search for certain, hard-coded
108 * elements that have been added by filter_form(). If an input format selector
109 * or input format guidelines element is found, we assume that the preceding
110 * element is the corresponding textarea and use it's #id for attaching
111 * client-side editors.
112 *
113 * @see wysiwyg_elements(), filter_form()
114 */
115 function wysiwyg_process_form(&$form) {
116 // Iterate over element children; resetting array keys to access last index.
117 if ($children = array_values(element_children($form))) {
118 foreach ($children as $index => $item) {
119 $element = &$form[$item];
120
121 // filter_form() always uses the key 'format'. We need a type-agnostic
122 // match to prevent false positives. Also, there must have been at least
123 // one element on this level.
124 if ($item === 'format' && $index > 0) {
125 // Make sure we either match a input format selector or input format
126 // guidelines (displayed if user has access to one input format only).
127 if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
128 // The element before this element is the target form field.
129 $field = &$form[$children[$index - 1]];
130
131 // Disable #resizable to avoid resizable behavior to hi-jack the UI,
132 // but load the behavior, so the 'none' editor can attach/detach it.
133 $extra_class = '';
134 if (!empty($field['#resizable'])) {
135 // Due to our CSS class parsing, we can add arbitrary parameters
136 // for each input format.
137 $extra_class = ' wysiwyg-resizable-1';
138 $field['#resizable'] = FALSE;
139 drupal_add_js('misc/textarea.js');
140 }
141
142 // Determine the available input formats. The last child element is a
143 // link to "More information about formatting options". When only one
144 // input format is displayed, we also have to remove formatting
145 // guidelines, stored in the child 'format'.
146 $formats = element_children($element);
147 array_pop($formats);
148 if (($key = array_search('format', $formats)) !== FALSE) {
149 unset($formats[$key]);
150 }
151 foreach ($formats as $format) {
152 // Default to 'none' editor (Drupal's default behaviors).
153 $editor = 'none';
154 $status = 1;
155 $toggle = 1;
156 // Fetch the profile associated to this input format.
157 $profile = wysiwyg_get_profile($format);
158 if ($profile) {
159 $editor = $profile->editor;
160 $status = (int) wysiwyg_user_get_status($profile);
161 if (isset($profile->settings['show_toggle'])) {
162 $toggle = (int) $profile->settings['show_toggle'];
163 }
164 // Check editor theme (and reset it if not/no longer available).
165 $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : ''));
166
167 // Add plugin settings (first) for this input format.
168 wysiwyg_add_plugin_settings($profile);
169 // Add profile settings for this input format.
170 wysiwyg_add_editor_settings($profile, $theme);
171 }
172
173 // Use a prefix/suffix for a single input format, or attach to input
174 // format selector radio buttons.
175 if (isset($element['format']['guidelines'])) {
176 $element['format']['guidelines']['#prefix'] = '<div class="wysiwyg wysiwyg-format-' . $format . ' wysiwyg-editor-' . $editor . ' wysiwyg-field-' . $field['#id'] . ' wysiwyg-status-' . $status . ' wysiwyg-toggle-' . $toggle . $extra_class . '">';
177 $element['format']['guidelines']['#suffix'] = '</div>';
178 // Edge-case: Default format contains no input filters.
179 if (empty($element['format']['guidelines']['#value'])) {
180 $element['format']['guidelines']['#value'] = ' ';
181 }
182 }
183 else {
184 $element[$format]['#attributes']['class'] = (isset($element[$format]['#attributes']['class']) ? $element[$format]['#attributes']['class'] . ' ' : '');
185 $element[$format]['#attributes']['class'] .= 'wysiwyg wysiwyg-format-' . $format . ' wysiwyg-editor-' . $editor . ' wysiwyg-field-' . $field['#id'] . ' wysiwyg-status-' . $status . ' wysiwyg-toggle-' . $toggle . $extra_class;
186 }
187 }
188 }
189 // If this element is 'format', do not recurse further.
190 continue;
191 }
192 // Recurse into children.
193 wysiwyg_process_form($element);
194 }
195 }
196 return $form;
197 }
198
199 /**
200 * Determine the profile to use for a given input format id.
201 *
202 * This function also performs sanity checks for the configured editor in a
203 * profile to ensure that we do not load a malformed editor.
204 *
205 * @param $format
206 * The internal id of an input format.
207 *
208 * @return
209 * A wysiwyg profile.
210 *
211 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
212 */
213 function wysiwyg_get_profile($format) {
214 if ($profile = wysiwyg_profile_load($format)) {
215 if (wysiwyg_load_editor($profile)) {
216 return $profile;
217 }
218 }
219 return FALSE;
220 }
221
222 /**
223 * Load an editor library and initialize basic Wysiwyg settings.
224 *
225 * @param $profile
226 * A wysiwyg editor profile.
227 *
228 * @return
229 * TRUE if the editor has been loaded, FALSE if not.
230 *
231 * @see wysiwyg_get_profile()
232 */
233 function wysiwyg_load_editor($profile) {
234 static $settings_added;
235 static $loaded = array();
236
237 $name = $profile->editor;
238 // Library files must be loaded only once.
239 if (!isset($loaded[$name])) {
240 // Load editor.
241 $editor = wysiwyg_get_editor($name);
242 if ($editor) {
243 // Determine library files to load.
244 // @todo Allow to configure the library/execMode to use.
245 if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
246 $library = $profile->settings['library'];
247 $files = $editor['libraries'][$library]['files'];
248 }
249 else {
250 // Fallback to the first defined library by default (external libraries can change).
251 $library = key($editor['libraries']);
252 $files = array_shift($editor['libraries']);
253 $files = $files['files'];
254 }
255 foreach ($files as $file => $options) {
256 if (is_array($options)) {
257 $options += array('type' => 'module', 'scope' => 'header', 'defer' => FALSE, 'cache' => TRUE, 'preprocess' => TRUE);
258 drupal_add_js($editor['library path'] . '/' . $file, $options['type'], $options['scope'], $options['defer'], $options['cache'], $options['preprocess']);
259 }
260 else {
261 drupal_add_js($editor['library path'] . '/' . $options);
262 }
263 }
264 // If editor defines an additional load callback, invoke it.
265 // @todo Isn't the settings callback sufficient?
266 if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
267 $editor['load callback']($editor, $library);
268 }
269 // Load JavaScript integration files for this editor.
270 $files = array();
271 if (isset($editor['js files'])) {
272 $files = $editor['js files'];
273 }
274 foreach ($files as $file) {
275 drupal_add_js($editor['js path'] . '/' . $file);
276 }
277 // Load CSS stylesheets for this editor.
278 $files = array();
279 if (isset($editor['css files'])) {
280 $files = $editor['css files'];
281 }
282 foreach ($files as $file) {
283 drupal_add_css($editor['css path'] . '/' . $file);
284 }
285
286 drupal_add_js(array('wysiwyg' => array(
287 'configs' => array($editor['name'] => array()),
288 // @todo Move into (global) editor settings.
289 // If JS compression is enabled, at least TinyMCE is unable to determine
290 // its own base path and exec mode since it can't find the script name.
291 'editorBasePath' => base_path() . $editor['library path'],
292 'execMode' => $library,
293 )), 'setting');
294
295 $loaded[$name] = TRUE;
296 }
297 else {
298 $loaded[$name] = FALSE;
299 }
300 }
301
302 // Add basic Wysiwyg settings if any editor has been added.
303 if (!isset($settings_added) && $loaded[$name]) {
304 drupal_add_js(array('wysiwyg' => array(
305 'configs' => array(),
306 'disable' => t('Disable rich-text'),
307 'enable' => t('Enable rich-text'),
308 )), 'setting');
309
310 $path = drupal_get_path('module', 'wysiwyg');
311 // Initialize our namespaces in the *header* to do not force editor
312 // integration scripts to check and define Drupal.wysiwyg on its own.
313 drupal_add_js($path . '/wysiwyg.init.js', 'core');
314
315 // The 'none' editor is a special editor implementation, allowing us to
316 // attach and detach regular Drupal behaviors just like any other editor.
317 drupal_add_js($path . '/editors/js/none.js');
318
319 // Add wysiwyg.js to the footer to ensure it's executed after the
320 // Drupal.settings array has been rendered and populated. Also, since editor
321 // library initialization functions must be loaded first by the browser,
322 // and Drupal.wysiwygInit() must be executed AFTER editors registered
323 // their callbacks and BEFORE Drupal.behaviors are applied, this must come
324 // last.
325 drupal_add_js($path . '/wysiwyg.js', 'module', 'footer');
326
327 $settings_added = TRUE;
328 }
329
330 return $loaded[$name];
331 }
332
333 /**
334 * Add editor settings for a given input format.
335 */
336 function wysiwyg_add_editor_settings($profile, $theme) {
337 static $formats = array();
338
339 if (!isset($formats[$profile->format])) {
340 $config = wysiwyg_get_editor_config($profile, $theme);
341 // drupal_to_js() does not properly convert numeric array keys, so we need
342 // to use a string instead of the format id.
343 drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format' . $profile->format => $config)))), 'setting');
344 $formats[$profile->format] = TRUE;
345 }
346 }
347
348 /**
349 * Add settings for external plugins.
350 *
351 * Plugins can be used in multiple profiles, but not necessarily in all. Because
352 * of that, we need to process plugins for each profile, even if most of their
353 * settings are not stored per profile.
354 *
355 * Implementations of hook_wysiwyg_plugin() may execute different code for each
356 * editor. Therefore, we have to invoke those implementations for each editor,
357 * but process the resulting plugins separately for each profile.
358 *
359 * Drupal plugins differ to native plugins in that they have plugin-specific
360 * definitions and settings, which need to be processed only once. But they are
361 * also passed to the editor to prepare settings specific to the editor.
362 * Therefore, we load and process the Drupal plugins only once, and hand off the
363 * effective definitions for each profile to the editor.
364 *
365 * @param $profile
366 * A wysiwyg editor profile.
367 *
368 * @todo Rewrite wysiwyg_process_form() to build a registry of effective
369 * profiles in use, so we can process plugins in multiple profiles in one shot
370 * and simplify this entire function.
371 */
372 function wysiwyg_add_plugin_settings($profile) {
373 static $plugins = array();
374 static $processed_plugins = array();
375 static $processed_formats = array();
376
377 // Each input format must only processed once.
378 // @todo ...as long as we do not have multiple profiles per format.
379 if (isset($processed_formats[$profile->format])) {
380 return;
381 }
382 $processed_formats[$profile->format] = TRUE;
383
384 $editor = wysiwyg_get_editor($profile->editor);
385
386 // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
387 // and Drupal plugins provided via hook_wysiwyg_include_directory().
388 if (!array_key_exists($editor['name'], $plugins)) {
389 $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
390 }
391
392 // Nothing to do, if there are no plugins.
393 if (empty($plugins[$editor['name']])) {
394 return;
395 }
396
397 // Determine name of proxy plugin for Drupal plugins.
398 $proxy = (isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '');
399
400 // Process native editor plugins.
401 if (isset($editor['plugin settings callback'])) {
402 // @todo Require PHP 5.1 in 3.x and use array_intersect_key().
403 $profile_plugins_native = array();
404 foreach ($plugins[$editor['name']] as $plugin => $meta) {
405 // Skip Drupal plugins (handled below).
406 if ($plugin === $proxy) {
407 continue;
408 }
409 // Only keep native plugins that are enabled in this profile.
410 if (isset($profile->settings['buttons'][$plugin])) {
411 $profile_plugins_native[$plugin] = $meta;
412 }
413 }
414 // Invoke the editor's plugin settings callback, so it can populate the
415 // settings for native external plugins with required values.
416 $settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native);
417
418 drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('native' => $settings_native)))), 'setting');
419 }
420
421 // Process Drupal plugins.
422 if ($proxy && isset($editor['proxy plugin settings callback'])) {
423 $profile_plugins_drupal = array();
424 foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
425 if (isset($profile->settings['buttons'][$proxy][$plugin])) {
426 // JavaScript and plugin-specific settings for Drupal plugins must be
427 // loaded and processed only once. Plugin information is cached
428 // statically to pass it to the editor's proxy plugin settings callback.
429 if (!isset($processed_plugins[$proxy][$plugin])) {
430 $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;
431 // Load the Drupal plugin's JavaScript.
432 drupal_add_js($meta['js path'] . '/' . $meta['js file']);
433 // Add plugin-specific settings.
434 if (isset($meta['settings'])) {
435 drupal_add_js(array('wysiwyg' => array('plugins' => array('drupal' => array($plugin => $meta['settings'])))), 'setting');
436 }
437 }
438 else {
439 $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
440 }
441 }
442 }
443 // Invoke the editor's proxy plugin settings callback, so it can populate
444 // the settings for Drupal plugins with custom, required values.
445 $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);
446
447 drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('drupal' => $settings_drupal)))), 'setting');
448 }
449 }
450
451 /**
452 * Retrieve available themes for an editor.
453 *
454 * Editor themes control the visual presentation of an editor.
455 *
456 * @param $profile
457 * A wysiwyg editor profile; passed/altered by reference.
458 * @param $selected_theme
459 * An optional theme name that ought to be used.
460 *
461 * @return
462 * An array of theme names, or a single, checked theme name if $selected_theme
463 * was given.
464 */
465 function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
466 static $themes = array();
467
468 if (!isset($themes[$profile->editor])) {
469 $editor = wysiwyg_get_editor($profile->editor);
470 if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
471 $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
472 }
473 // Fallback to 'default' otherwise.
474 else {
475 $themes[$editor['name']] = array('default');
476 }
477 }
478
479 // Check optional $selected_theme argument, if given.
480 if (isset($selected_theme)) {
481 // If the passed theme name does not exist, use the first available.
482 if (!in_array($selected_theme, $themes[$profile->editor])) {
483 $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
484 }
485 }
486
487 return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
488 }
489
490 /**
491 * Return plugin metadata from the plugin registry.
492 *
493 * @param $editor_name
494 * The internal name of an editor to return plugins for.
495 *
496 * @return
497 * An array for each plugin.
498 */
499 function wysiwyg_get_plugins($editor_name) {
500 $plugins = array();
501 if (!empty($editor_name)) {
502 $editor = wysiwyg_get_editor($editor_name);
503 // Add internal editor plugins.
504 if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
505 $plugins = $editor['plugin callback']($editor);
506 }
507 // Add editor plugins provided via hook_wysiwyg_plugin().
508 $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
509 // Add API plugins provided by Drupal modules.
510 // @todo We need to pass the filepath to the plugin icon for Drupal plugins.
511 if (isset($editor['proxy plugin'])) {
512 $plugins += $editor['proxy plugin'];
513 $proxy = key($editor['proxy plugin']);
514 foreach (wysiwyg_get_all_plugins() as $plugin_name => $info) {
515 $plugins[$proxy]['buttons'][$plugin_name] = $info['title'];
516 }
517 }
518 }
519 return $plugins;
520 }
521
522 /**
523 * Return an array of initial editor settings for a Wysiwyg profile.
524 */
525 function wysiwyg_get_editor_config($profile, $theme) {
526 $editor = wysiwyg_get_editor($profile->editor);
527 $settings = array();
528 if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
529 $settings = $editor['settings callback']($editor, $profile->settings, $theme);
530 }
531 return $settings;
532 }
533
534 /**
535 * Retrieve stylesheets for HTML/IFRAME-based editors.
536 *
537 * This assumes that the content editing area only needs stylesheets defined
538 * for the scope 'theme'.
539 *
540 * @return
541 * An array containing CSS files, including proper base path.
542 */
543 function wysiwyg_get_css() {
544 static $files;
545
546 if (isset($files)) {
547 return $files;
548 }
549 // In node form previews, the theme has not been initialized yet.
550 init_theme();
551
552 $files = array();
553 foreach (drupal_add_css() as $media => $css) {
554 if ($media != 'print') {
555 foreach ($css['theme'] as $filepath => $preprocess) {
556 if (file_exists($filepath)) {
557 $files[] = base_path() . $filepath;
558 }
559 }
560 }
561 }
562 return $files;
563 }
564
565 /**
566 * Load profile for a given input format.
567 */
568 function wysiwyg_profile_load($format) {
569 static $profiles;
570
571 if (!isset($profiles) || !array_key_exists($format, $profiles)) {
572 $result = db_query('SELECT format, editor, settings FROM {wysiwyg} WHERE format = %d', $format);
573 while ($profile = db_fetch_object($result)) {
574 $profile->settings = unserialize($profile->settings);
575 $profiles[$profile->format] = $profile;
576 }
577 }
578
579 return (isset($profiles[$format]) ? $profiles[$format] : FALSE);
580 }
581
582 /**
583 * Load all profiles.
584 */
585 function wysiwyg_profile_load_all() {
586 static $profiles;
587
588 if (!isset($profiles)) {
589 $profiles = array();
590 $result = db_query('SELECT format, editor, settings FROM {wysiwyg}');
591 while ($profile = db_fetch_object($result)) {
592 $profile->settings = unserialize($profile->settings);
593 $profiles[$profile->format] = $profile;
594 }
595 }
596
597 return $profiles;
598 }
599
600 /**
601 * Implementation of hook_user().
602 */
603 function wysiwyg_user($type, &$edit, &$user, $category = NULL) {
604 if ($type == 'form' && $category == 'account') {
605 // @todo http://drupal.org/node/322433
606 $profile = new stdClass;
607 if (isset($profile->settings['user_choose']) && $profile->settings['user_choose']) {
608 $form['wysiwyg'] = array(
609 '#type' => 'fieldset',
610 '#title' => t('Wysiwyg Editor settings'),
611 '#weight' => 10,
612 '#collapsible' => TRUE,
613 '#collapsed' => TRUE,
614 );
615 $form['wysiwyg']['wysiwyg_status'] = array(
616 '#type' => 'checkbox',
617 '#title' => t('Enable editor by default'),
618 '#default_value' => isset($user->wysiwyg_status) ? $user->wysiwyg_status : (isset($profile->settings['default']) ? $profile->settings['default'] : FALSE),
619 '#return_value' => 1,
620 '#description' => t('If enabled, rich-text editing is enabled by default in textarea fields.'),
621 );
622 return array('wysiwyg' => $form);
623 }
624 }
625 elseif ($type == 'validate' && isset($edit['wysiwyg_status'])) {
626 return array('wysiwyg_status' => $edit['wysiwyg_status']);
627 }
628 }
629
630 function wysiwyg_user_get_status($profile) {
631 global $user;
632
633 if (!empty($profile->settings['user_choose']) && isset($user->wysiwyg_status)) {
634 $status = $user->wysiwyg_status;
635 }
636 else {
637 $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE;
638 }
639
640 return $status;
641 }
642
643 /**
644 * @defgroup wysiwyg_api Wysiwyg API
645 * @{
646 *
647 * @todo Forked from Panels; abstract into a separate API module that allows
648 * contrib modules to define supported include/plugin types.
649 */
650
651 /**
652 * Return library information for a given editor.
653 *
654 * @param $name
655 * The internal name of an editor.
656 *
657 * @return
658 * The library information for the editor, or FALSE if $name is unknown or not
659 * installed properly.
660 */
661 function wysiwyg_get_editor($name) {
662 $editors = wysiwyg_get_all_editors();
663 return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
664 }
665
666 /**
667 * Compile a list holding all supported editors including installed editor version information.
668 */
669 function wysiwyg_get_all_editors() {
670 static $editors;
671
672 if (isset($editors)) {
673 return $editors;
674 }
675
676 $editors = wysiwyg_load_includes('editors', 'editor');
677 foreach ($editors as $editor => $properties) {
678 // Fill in required properties.
679 $editors[$editor] += array(
680 'title' => '',
681 'vendor url' => '',
682 'download url' => '',
683 'editor path' => wysiwyg_get_path($editors[$editor]['name']),
684 'library path' => wysiwyg_get_path($editors[$editor]['name']),
685 'libraries' => array(),
686 'version callback' => NULL,
687 'themes callback' => NULL,
688 'settings callback' => NULL,
689 'plugin callback' => NULL,
690 'plugin settings callback' => NULL,
691 'versions' => array(),
692 'js path' => $editors[$editor]['path'] . '/js',
693 'css path' => $editors[$editor]['path'] . '/css',
694 );
695 // Check whether library is present.
696 if (!($editors[$editor]['installed'] = file_exists($editors[$editor]['library path']))) {
697 continue;
698 }
699 // Detect library version.
700 if (function_exists($editors[$editor]['version callback'])) {
701 $editors[$editor]['installed version'] = $editors[$editor]['version callback']($editors[$editor]);
702 }
703 if (empty($editors[$editor]['installed version'])) {
704 $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
705 $editors[$editor]['installed'] = FALSE;
706 continue;
707 }
708 // Determine to which supported version the installed version maps.
709 ksort($editors[$editor]['versions']);
710 $version = 0;
711 foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
712 if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
713 $version = $supported_version;
714 }
715 }
716 if (!$version) {
717 $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $editors[$editor]['title']));
718 $editors[$editor]['installed'] = FALSE;
719 continue;
720 }
721 // Apply library version specific definitions and overrides.
722 $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
723 unset($editors[$editor]['versions']);
724 }
725 return $editors;
726 }
727
728 /**
729 * Invoke hook_wysiwyg_plugin() in all modules.
730 */
731 function wysiwyg_get_all_plugins() {
732 static $plugins;
733
734 if (isset($plugins)) {
735 return $plugins;
736 }
737
738 $plugins = wysiwyg_load_includes('plugins', 'plugin');
739 foreach ($plugins as $name => $properties) {
740 $plugin = &$plugins[$name];
741 // Fill in required/default properties.
742 $plugin += array(
743 'title' => $plugin['name'],
744 'vendor url' => '',
745 'js path' => $plugin['path'] . '/' . $plugin['name'],
746 'js file' => $plugin['name'] . '.js',
747 'css path' => $plugin['path'] . '/' . $plugin['name'],
748 'css file' => $plugin['name'] . '.css',
749 'icon path' => $plugin['path'] . '/' . $plugin['name'] . '/images',
750 'icon file' => $plugin['name'] . '.png',
751 'dialog path' => $plugin['name'],
752 'dialog settings' => array(),
753 'settings callback' => NULL,
754 'settings form callback' => NULL,
755 );
756 // Fill in default settings.
757 $plugin['settings'] += array(
758 'path' => base_path() . $plugin['path'] . '/' . $plugin['name'],
759 );
760 // Check whether library is present.
761 if (!($plugin['installed'] = file_exists($plugin['js path'] . '/' . $plugin['js file']))) {
762 continue;
763 }
764 }
765 return $plugins;
766 }
767
768 /**
769 * Load include files for wysiwyg implemented by all modules.
770 *
771 * @param $type
772 * The type of includes to search for, can be 'editors'.
773 * @param $hook
774 * The hook name to invoke.
775 * @param $file
776 * An optional include file name without .inc extension to limit the search to.
777 *
778 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
779 */
780 function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
781 // Determine implementations.
782 $directories = wysiwyg_get_directories($type);
783 $directories['wysiwyg'] = drupal_get_path('module', 'wysiwyg') . '/' . $type;
784 $file_list = array();
785 foreach ($directories as $module => $path) {
786 $file_list[$module] = drupal_system_listing("$file" . '.inc$', $path, 'name', 0);
787 }
788
789 // Load implementations.
790 $info = array();
791 foreach (array_filter($file_list) as $module => $files) {
792 foreach ($files as $file) {
793 include_once './' . $file->filename;
794 $result = _wysiwyg_process_include($module, $module . '_' . $file->name, dirname($file->filename), $hook);
795 if (is_array($result)) {
796 $info = array_merge($info, $result);
797 }
798 }
799 }
800 return $info;
801 }
802
803 /**
804 * Helper function to build paths to libraries.
805 *
806 * @param $library
807 * The external library name to return the path for.
808 * @param $base_path
809 * Whether to prefix the resulting path with base_path().
810 *
811 * @return
812 * The path to the specified library.
813 *
814 * @ingroup libraries
815 */
816 function wysiwyg_get_path($library, $base_path = FALSE) {
817 static $libraries;
818
819 if (!isset($libraries)) {
820 $libraries = wysiwyg_get_libraries();
821 }
822 if (!isset($libraries[$library])) {
823 // Most often, external libraries can be shared across multiple sites.
824 return 'sites/all/libraries/' . $library;
825 }
826
827 $path = ($base_path ? base_path() : '');
828 $path .= $libraries[$library];
829
830 return $path;
831 }
832
833 /**
834 * Return an array of library directories.
835 *
836 * Returns an array of library directories from the all-sites directory
837 * (i.e. sites/all/libraries/), the profiles directory, and site-specific
838 * directory (i.e. sites/somesite/libraries/). The returned array will be keyed
839 * by the library name. Site-specific libraries are prioritized over libraries
840 * in the default directories. That is, if a library with the same name appears
841 * in both the site-wide directory and site-specific directory, only the
842 * site-specific version will be listed.
843 *
844 * @return
845 * A list of library directories.
846 *
847 * @ingroup libraries
848 */
849 function wysiwyg_get_libraries() {
850 global $profile;
851
852 // When this function is called during Drupal's initial installation process,
853 // the name of the profile that is about to be installed is stored in the
854 // global $profile variable. At all other times, the regular system variable
855 // contains the name of the current profile, and we can call variable_get()
856 // to determine the profile.
857 if (!isset($profile)) {
858 $profile = variable_get('install_profile', 'default');
859 }
860
861 $directory = 'libraries';
862 $searchdir = array();
863 $config = conf_path();
864
865 // The 'profiles' directory contains pristine collections of modules and
866 // themes as organized by a distribution. It is pristine in the same way
867 // that /modules is pristine for core; users should avoid changing anything
868 // there in favor of sites/all or sites/<domain> directories.
869 if (file_exists("profiles/$profile/$directory")) {
870 $searchdir[] = "profiles/$profile/$directory";
871 }
872
873 // Always search sites/all/*.
874 $searchdir[] = 'sites/all/' . $directory;
875
876 // Also search sites/<domain>/*.
877 if (file_exists("$config/$directory")) {
878 $searchdir[] = "$config/$directory";
879 }
880
881 // Retrieve list of directories.
882 // @todo Core: Allow to scan for directories.
883 $directories = array();
884 $nomask = array('CVS');
885 foreach ($searchdir as $dir) {
886 if (is_dir($dir) && $handle = opendir($dir)) {
887 while (FALSE !== ($file = readdir($handle))) {
888 if (!in_array($file, $nomask) && $file[0] != '.') {
889 if (is_dir("$dir/$file")) {
890 $directories[$file] = "$dir/$file";
891 }
892 }
893 }
894 closedir($handle);
895 }
896 }
897
898 return $directories;
899 }
900
901 /**
902 * Return a list of directories by modules implementing wysiwyg_include_directory().
903 *
904 * @param $plugintype
905 * The type of a plugin; can be 'editors'.
906 *
907 * @return
908 * An array containing module names suffixed with '_' and their defined
909 * directory.
910 *
911 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
912 */
913 function wysiwyg_get_directories($plugintype) {
914 $directories = array();
915 foreach (module_implements('wysiwyg_include_directory') as $module) {
916 $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
917 if (isset($result) && is_string($result)) {
918 $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
919 }
920 }
921 return $directories;
922 }
923
924 /**
925 * Process a single hook implementation of a wysiwyg editor.
926 *
927 * @param $module
928 * The module that owns the hook.
929 * @param $identifier
930 * Either the module or 'wysiwyg_' . $file->name
931 * @param $hook
932 * The name of the hook being invoked.
933 */
934 function _wysiwyg_process_include($module, $identifier, $path, $hook) {
935 $function = $identifier . '_' . $hook;
936 if (!function_exists($function)) {
937 return NULL;
938 }
939 $result = $function();
940 if (!isset($result) || !is_array($result)) {
941 return NULL;
942 }
943
944 // Fill in defaults.
945 foreach ($result as $editor => $properties) {
946 $result[$editor]['module'] = $module;
947 $result[$editor]['name'] = $editor;
948 $result[$editor]['path'] = $path;
949 }
950 return $result;
951 }
952
953 /**
954 * @} End of "defgroup wysiwyg_api".
955 */
956