/[drupal]/contributions/modules/wysiwyg/wysiwyg.module
ViewVC logotype

Contents of /contributions/modules/wysiwyg/wysiwyg.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2