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