#679056 by TwoD: Fixed patch for pressing enter in autocomplete, new jQuery API.
[project/wysiwyg.git] / wysiwyg.module
CommitLineData
14b92488 1<?php
2// $Id$
3
4/**
5 * @file
3eaaa5aa 6 * Integrates client-side editors with Drupal.
14b92488 7 */
8
9/**
5d5542b7 10 * Implements hook_entity_info().
11 */
12function wysiwyg_entity_info() {
13 $types['wysiwyg_profile'] = array(
14 'label' => t('Wysiwyg profile'),
15 'base table' => 'wysiwyg',
16 'controller class' => 'WysiwygProfileController',
17 'fieldable' => FALSE,
18 // When loading all entities, DrupalDefaultEntityController::load() ignores
19 // its static cache. Therefore, wysiwyg_profile_load_all() implements a
20 // custom static cache.
21 'static cache' => FALSE,
22 'entity keys' => array(
23 'id' => 'format',
24 ),
25 );
26 return $types;
27}
28
29/**
30 * Controller class for Wysiwyg profiles.
31 */
32class WysiwygProfileController extends DrupalDefaultEntityController {
33 /**
34 * Overrides DrupalDefaultEntityController::attachLoad().
35 */
36 function attachLoad(&$queried_entities, $revision_id = FALSE) {
37 // Unserialize the profile settings.
38 foreach ($queried_entities as $key => $record) {
39 $queried_entities[$key]->settings = unserialize($record->settings);
40 }
41 // Call the default attachLoad() method.
42 parent::attachLoad($queried_entities, $revision_id);
43 }
44}
45
46/**
eb714832 47 * Implementation of hook_menu().
48 */
49function wysiwyg_menu() {
150bc86b 50 $items['admin/config/content/wysiwyg'] = array(
51 'title' => 'Wysiwyg profiles',
117ad234 52 'page callback' => 'drupal_get_form',
53 'page arguments' => array('wysiwyg_profile_overview'),
54 'description' => 'Configure client-side editors.',
55 'access arguments' => array('administer filters'),
56 'file' => 'wysiwyg.admin.inc',
57 );
150bc86b 58 $items['admin/config/content/wysiwyg/profile'] = array(
59 'title' => 'List',
117ad234 60 'type' => MENU_DEFAULT_LOCAL_TASK,
61 );
150bc86b 62 $items['admin/config/content/wysiwyg/profile/%wysiwyg_profile/edit'] = array(
117ad234 63 'title' => 'Edit',
64 'page callback' => 'drupal_get_form',
150bc86b 65 'page arguments' => array('wysiwyg_profile_form', 5),
af8246de 66 'access arguments' => array('administer filters'),
eb714832 67 'file' => 'wysiwyg.admin.inc',
150bc86b 68 'tab_root' => 'admin/config/content/wysiwyg/profile',
69 'tab_parent' => 'admin/config/content/wysiwyg/profile/%wysiwyg_profile',
117ad234 70 'type' => MENU_LOCAL_TASK,
71 );
150bc86b 72 $items['admin/config/content/wysiwyg/profile/%wysiwyg_profile/delete'] = array(
117ad234 73 'title' => 'Remove',
74 'page callback' => 'drupal_get_form',
150bc86b 75 'page arguments' => array('wysiwyg_profile_delete_confirm', 5),
117ad234 76 'access arguments' => array('administer filters'),
77 'file' => 'wysiwyg.admin.inc',
150bc86b 78 'tab_root' => 'admin/config/content/wysiwyg/profile',
79 'tab_parent' => 'admin/config/content/wysiwyg/profile/%wysiwyg_profile',
117ad234 80 'type' => MENU_LOCAL_TASK,
81 'weight' => 10,
eb714832 82 );
6e91d64e 83 $items['wysiwyg/%'] = array(
84 'page callback' => 'wysiwyg_dialog',
85 'page arguments' => array(1),
86 'access arguments' => array('access content'),
87 'type' => MENU_CALLBACK,
88 'file' => 'wysiwyg.dialog.inc',
89 );
eb714832 90 return $items;
91}
92
93/**
94 * Implementation of hook_theme().
6e91d64e 95 *
96 * @see drupal_common_theme(), common.inc
97 * @see template_preprocess_page(), theme.inc
eb714832 98 */
99function wysiwyg_theme() {
100 return array(
150bc86b 101 'wysiwyg_profile_overview' => array(
6935a55c 102 'render element' => 'form',
150bc86b 103 ),
104 'wysiwyg_admin_button_table' => array(
6935a55c 105 'render element' => 'form',
150bc86b 106 ),
6e91d64e 107 'wysiwyg_dialog_page' => array(
6935a55c 108 'variables' => array('content' => NULL, 'show_messages' => TRUE),
6e91d64e 109 'file' => 'wysiwyg.dialog.inc',
110 'template' => 'wysiwyg-dialog-page',
111 ),
eb714832 112 );
113}
114
115/**
116 * Implementation of hook_help().
117 */
118function wysiwyg_help($path, $arg) {
119 switch ($path) {
150bc86b 120 case 'admin/config/content/wysiwyg':
fddd288d 121 $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>';
117ad234 122 return $output;
eb714832 123 }
124}
125
126/**
eb714832 127 * Implementation of hook_form_alter().
128 */
129function wysiwyg_form_alter(&$form, &$form_state) {
715d4b42 130 // Teaser splitter is unconditionally removed and NOT supported.
eb714832 131 if (isset($form['body_field'])) {
132 unset($form['body_field']['teaser_js']);
14b92488 133 }
14b92488 134}
135
136/**
fe2b726f 137 * Implements hook_element_info_alter().
138 */
139function wysiwyg_element_info_alter(&$types) {
140 $types['text_format']['#pre_render'][] = 'wysiwyg_pre_render_text_format';
141}
142
143/**
144 * Process a text format widget to load and attach editors.
14b92488 145 *
fe2b726f 146 * The element's #id is used as reference to attach client-side editors.
eb714832 147 */
fe2b726f 148function wysiwyg_pre_render_text_format($element) {
149 // filter_process_format() copies properties to the expanded 'value' child
80e4748a 150 // element. Skip this text format widget, if it contains no 'format' or when
151 // the current user does not have access to edit the value.
152 if (!isset($element['format']) || !empty($element['value']['#disabled'])) {
fe2b726f 153 return $element;
154 }
47b858d0 155 // Allow modules to programmatically enforce no client-side editor by setting
156 // the #wysiwyg property to FALSE.
157 if (isset($element['#wysiwyg']) && !$element['#wysiwyg']) {
158 return $element;
159 }
eb714832 160
fe2b726f 161 $format_field = &$element['format'];
162 $field = &$element['value'];
163 $settings = array(
164 'field' => $field['#id'],
165 );
150bc86b 166
fe2b726f 167 // If this textarea is #resizable and we will load at least one
168 // editor, then only load the behavior and let the 'none' editor
169 // attach/detach it to avoid hi-jacking the UI. Due to our CSS class
170 // parsing, we can add arbitrary parameters for each input format.
171 // The #resizable property will be removed below, if at least one
172 // profile has been loaded.
173 $resizable = 0;
174 if (!empty($field['#resizable'])) {
175 $resizable = 1;
176 drupal_add_js('misc/textarea.js');
177 }
178 // Determine the available text formats.
179 foreach ($format_field['format']['#options'] as $format_id => $format_name) {
180 $format = 'format' . $format_id;
181 // Initialize default settings, defaulting to 'none' editor.
a271ac7a 182 $settings[$format] = array(
183 'editor' => 'none',
184 'status' => 1,
185 'toggle' => 1,
186 'resizable' => $resizable,
187 );
907a53b6 188
fe2b726f 189 // Fetch the profile associated to this text format.
190 $profile = wysiwyg_get_profile($format_id);
191 if ($profile) {
192 $loaded = TRUE;
193 $settings[$format]['editor'] = $profile->editor;
194 $settings[$format]['status'] = (int) wysiwyg_user_get_status($profile);
195 if (isset($profile->settings['show_toggle'])) {
196 $settings[$format]['toggle'] = (int) $profile->settings['show_toggle'];
907a53b6 197 }
fe2b726f 198 // Check editor theme (and reset it if not/no longer available).
199 $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : ''));
907a53b6 200
fe2b726f 201 // Add plugin settings (first) for this text format.
202 wysiwyg_add_plugin_settings($profile);
203 // Add profile settings for this text format.
204 wysiwyg_add_editor_settings($profile, $theme);
eb714832 205 }
206 }
fe2b726f 207 // Use a hidden element for a single text format.
208 if (!$format_field['format']['#access']) {
a271ac7a 209 $format_field['wysiwyg'] = array(
fe2b726f 210 '#type' => 'hidden',
211 '#name' => $format_field['format']['#name'],
fe2b726f 212 '#value' => $format_id,
a271ac7a 213 '#attributes' => array(
214 'id' => $format_field['format']['#id'],
215 'class' => array('wysiwyg'),
216 ),
fe2b726f 217 );
a271ac7a 218 $format_field['wysiwyg']['#attached']['js'][] = array(
219 'data' => array(
220 'wysiwyg' => array(
221 'triggers' => array(
222 $format_field['format']['#id'] => $settings,
223 ),
224 ),
225 ),
fe2b726f 226 'type' => 'setting',
227 );
228 }
229 // Otherwise, attach to text format selector.
230 else {
231 $format_field['format']['#attributes']['class'][] = 'wysiwyg';
232 $format_field['format']['#attached']['js'][] = array(
a271ac7a 233 'data' => array(
234 'wysiwyg' => array(
235 'triggers' => array(
236 $format_field['format']['#id'] => $settings,
237 ),
238 ),
239 ),
fe2b726f 240 'type' => 'setting',
241 );
242 }
243
244 // If we loaded at least one editor, then the 'none' editor will
245 // handle resizable textareas instead of core.
246 if (isset($loaded) && $resizable) {
247 $field['#resizable'] = FALSE;
248 }
249
250 return $element;
eb714832 251}
252
253/**
254 * Determine the profile to use for a given input format id.
14b92488 255 *
eb714832 256 * This function also performs sanity checks for the configured editor in a
257 * profile to ensure that we do not load a malformed editor.
14b92488 258 *
eb714832 259 * @param $format
260 * The internal id of an input format.
14b92488 261 *
eb714832 262 * @return
263 * A wysiwyg profile.
264 *
265 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
14b92488 266 */
eb714832 267function wysiwyg_get_profile($format) {
117ad234 268 if ($profile = wysiwyg_profile_load($format)) {
eb714832 269 if (wysiwyg_load_editor($profile)) {
270 return $profile;
271 }
272 }
273 return FALSE;
14b92488 274}
275
276/**
eb714832 277 * Load an editor library and initialize basic Wysiwyg settings.
278 *
279 * @param $profile
280 * A wysiwyg editor profile.
14b92488 281 *
eb714832 282 * @return
283 * TRUE if the editor has been loaded, FALSE if not.
14b92488 284 *
eb714832 285 * @see wysiwyg_get_profile()
286 */
287function wysiwyg_load_editor($profile) {
288 static $settings_added;
289 static $loaded = array();
290
81a2754f 291 $name = $profile->editor;
eb714832 292 // Library files must be loaded only once.
293 if (!isset($loaded[$name])) {
294 // Load editor.
295 $editor = wysiwyg_get_editor($name);
296 if ($editor) {
297 // Determine library files to load.
298 // @todo Allow to configure the library/execMode to use.
299 if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
300 $library = $profile->settings['library'];
a3d9c868 301 $files = $editor['libraries'][$library]['files'];
eb714832 302 }
303 else {
a3d9c868 304 // Fallback to the first defined library by default (external libraries can change).
eb714832 305 $library = key($editor['libraries']);
306 $files = array_shift($editor['libraries']);
307 $files = $files['files'];
308 }
46b43486 309 foreach ($files as $file => $options) {
310 if (is_array($options)) {
150bc86b 311 $options += array('type' => 'file', 'scope' => 'header', 'defer' => FALSE, 'cache' => TRUE, 'preprocess' => TRUE);
6935a55c 312 drupal_add_js($editor['library path'] . '/' . $file, $options);
46b43486 313 }
314 else {
315 drupal_add_js($editor['library path'] . '/' . $options);
316 }
eb714832 317 }
a3d9c868 318 // If editor defines an additional load callback, invoke it.
319 // @todo Isn't the settings callback sufficient?
320 if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
321 $editor['load callback']($editor, $library);
322 }
eb714832 323 // Load JavaScript integration files for this editor.
a09d7c1c 324 $files = array();
eb714832 325 if (isset($editor['js files'])) {
326 $files = $editor['js files'];
327 }
328 foreach ($files as $file) {
81a2754f 329 drupal_add_js($editor['js path'] . '/' . $file);
eb714832 330 }
a09d7c1c 331 // Load CSS stylesheets for this editor.
332 $files = array();
333 if (isset($editor['css files'])) {
334 $files = $editor['css files'];
335 }
336 foreach ($files as $file) {
17473a9a 337 drupal_add_css($editor['css path'] . '/' . $file);
a09d7c1c 338 }
eb714832 339
eb714832 340 drupal_add_js(array('wysiwyg' => array(
ef86e943
HD
341 'configs' => array($editor['name'] => array('global' => array(
342 // @todo Move into (global) editor settings.
343 // If JS compression is enabled, at least TinyMCE is unable to determine
344 // its own base path and exec mode since it can't find the script name.
345 'editorBasePath' => base_path() . $editor['library path'],
346 'execMode' => $library,
347 ))),
eb714832 348 )), 'setting');
349
350 $loaded[$name] = TRUE;
351 }
352 else {
353 $loaded[$name] = FALSE;
354 }
355 }
356
357 // Add basic Wysiwyg settings if any editor has been added.
358 if (!isset($settings_added) && $loaded[$name]) {
359 drupal_add_js(array('wysiwyg' => array(
360 'configs' => array(),
150bc86b 361 'plugins' => array(),
eb714832 362 'disable' => t('Disable rich-text'),
363 'enable' => t('Enable rich-text'),
364 )), 'setting');
365
c18af709 366 $path = drupal_get_path('module', 'wysiwyg');
eb714832 367 // Initialize our namespaces in the *header* to do not force editor
368 // integration scripts to check and define Drupal.wysiwyg on its own.
621f1ceb 369 drupal_add_js($path . '/wysiwyg.init.js', array('group' => JS_LIBRARY));
eb714832 370
371 // The 'none' editor is a special editor implementation, allowing us to
372 // attach and detach regular Drupal behaviors just like any other editor.
c18af709 373 drupal_add_js($path . '/editors/js/none.js');
eb714832 374
375 // Add wysiwyg.js to the footer to ensure it's executed after the
376 // Drupal.settings array has been rendered and populated. Also, since editor
377 // library initialization functions must be loaded first by the browser,
7623c9ce 378 // and Drupal.wysiwygInit() must be executed AFTER editors registered
379 // their callbacks and BEFORE Drupal.behaviors are applied, this must come
eb714832 380 // last.
150bc86b 381 drupal_add_js($path . '/wysiwyg.js', array('scope' => 'footer'));
eb714832 382
eb714832 383 $settings_added = TRUE;
384 }
385
386 return $loaded[$name];
387}
388
389/**
e16a9972 390 * Add editor settings for a given input format.
14b92488 391 */
eb714832 392function wysiwyg_add_editor_settings($profile, $theme) {
e16a9972 393 static $formats = array();
eb714832 394
e16a9972 395 if (!isset($formats[$profile->format])) {
eb714832 396 $config = wysiwyg_get_editor_config($profile, $theme);
e16a9972 397 // drupal_to_js() does not properly convert numeric array keys, so we need
398 // to use a string instead of the format id.
150bc86b 399 if ($config) {
400 drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format' . $profile->format => $config)))), 'setting');
401 }
e16a9972 402 $formats[$profile->format] = TRUE;
eb714832 403 }
14b92488 404}
405
406/**
eb714832 407 * Add settings for external plugins.
55557fd1 408 *
409 * Plugins can be used in multiple profiles, but not necessarily in all. Because
410 * of that, we need to process plugins for each profile, even if most of their
411 * settings are not stored per profile.
412 *
413 * Implementations of hook_wysiwyg_plugin() may execute different code for each
414 * editor. Therefore, we have to invoke those implementations for each editor,
415 * but process the resulting plugins separately for each profile.
416 *
417 * Drupal plugins differ to native plugins in that they have plugin-specific
418 * definitions and settings, which need to be processed only once. But they are
419 * also passed to the editor to prepare settings specific to the editor.
420 * Therefore, we load and process the Drupal plugins only once, and hand off the
421 * effective definitions for each profile to the editor.
422 *
eb714832 423 * @param $profile
424 * A wysiwyg editor profile.
55557fd1 425 *
426 * @todo Rewrite wysiwyg_process_form() to build a registry of effective
427 * profiles in use, so we can process plugins in multiple profiles in one shot
428 * and simplify this entire function.
eb714832 429 */
430function wysiwyg_add_plugin_settings($profile) {
c574d4c1 431 static $plugins = array();
432 static $processed_plugins = array();
86ef143a 433 static $processed_formats = array();
434
435 // Each input format must only processed once.
c574d4c1 436 // @todo ...as long as we do not have multiple profiles per format.
86ef143a 437 if (isset($processed_formats[$profile->format])) {
438 return;
439 }
c574d4c1 440 $processed_formats[$profile->format] = TRUE;
6e91d64e 441
442 $editor = wysiwyg_get_editor($profile->editor);
eb714832 443
c574d4c1 444 // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
445 // and Drupal plugins provided via hook_wysiwyg_include_directory().
446 if (!array_key_exists($editor['name'], $plugins)) {
447 $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
6e91d64e 448 }
eb714832 449
c574d4c1 450 // Nothing to do, if there are no plugins.
451 if (empty($plugins[$editor['name']])) {
6e91d64e 452 return;
eb714832 453 }
6e91d64e 454
c574d4c1 455 // Determine name of proxy plugin for Drupal plugins.
456 $proxy = (isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '');
457
458 // Process native editor plugins.
459 if (isset($editor['plugin settings callback'])) {
460 // @todo Require PHP 5.1 in 3.x and use array_intersect_key().
461 $profile_plugins_native = array();
462 foreach ($plugins[$editor['name']] as $plugin => $meta) {
463 // Skip Drupal plugins (handled below).
464 if ($plugin === $proxy) {
465 continue;
55557fd1 466 }
c574d4c1 467 // Only keep native plugins that are enabled in this profile.
468 if (isset($profile->settings['buttons'][$plugin])) {
469 $profile_plugins_native[$plugin] = $meta;
6e91d64e 470 }
471 }
c574d4c1 472 // Invoke the editor's plugin settings callback, so it can populate the
473 // settings for native external plugins with required values.
474 $settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native);
54a65ca8 475
150bc86b 476 if ($settings_native) {
477 drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('native' => $settings_native)))), 'setting');
478 }
6e91d64e 479 }
6e91d64e 480
c574d4c1 481 // Process Drupal plugins.
482 if ($proxy && isset($editor['proxy plugin settings callback'])) {
483 $profile_plugins_drupal = array();
484 foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
485 if (isset($profile->settings['buttons'][$proxy][$plugin])) {
486 // JavaScript and plugin-specific settings for Drupal plugins must be
487 // loaded and processed only once. Plugin information is cached
488 // statically to pass it to the editor's proxy plugin settings callback.
489 if (!isset($processed_plugins[$proxy][$plugin])) {
490 $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;
491 // Load the Drupal plugin's JavaScript.
54a65ca8 492 drupal_add_js($meta['js path'] . '/' . $meta['js file']);
c574d4c1 493 // Add plugin-specific settings.
494 if (isset($meta['settings'])) {
495 drupal_add_js(array('wysiwyg' => array('plugins' => array('drupal' => array($plugin => $meta['settings'])))), 'setting');
496 }
497 }
498 else {
499 $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
500 }
501 }
502 }
503 // Invoke the editor's proxy plugin settings callback, so it can populate
504 // the settings for Drupal plugins with custom, required values.
505 $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);
54a65ca8 506
150bc86b 507 if ($settings_drupal) {
508 drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('drupal' => $settings_drupal)))), 'setting');
509 }
c574d4c1 510 }
eb714832 511}
512
513/**
e16a9972 514 * Retrieve available themes for an editor.
14b92488 515 *
e16a9972 516 * Editor themes control the visual presentation of an editor.
14b92488 517 *
eb714832 518 * @param $profile
519 * A wysiwyg editor profile; passed/altered by reference.
520 * @param $selected_theme
521 * An optional theme name that ought to be used.
14b92488 522 *
eb714832 523 * @return
524 * An array of theme names, or a single, checked theme name if $selected_theme
525 * was given.
14b92488 526 */
eb714832 527function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
528 static $themes = array();
529
81a2754f 530 if (!isset($themes[$profile->editor])) {
531 $editor = wysiwyg_get_editor($profile->editor);
eb714832 532 if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
533 $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
534 }
535 // Fallback to 'default' otherwise.
536 else {
537 $themes[$editor['name']] = array('default');
538 }
14b92488 539 }
eb714832 540
541 // Check optional $selected_theme argument, if given.
542 if (isset($selected_theme)) {
543 // If the passed theme name does not exist, use the first available.
78e31859 544 if (!in_array($selected_theme, $themes[$profile->editor])) {
81a2754f 545 $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
eb714832 546 }
547 }
548
81a2754f 549 return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
14b92488 550}
551
552/**
eb714832 553 * Return plugin metadata from the plugin registry.
14b92488 554 *
eb714832 555 * @param $editor_name
556 * The internal name of an editor to return plugins for.
14b92488 557 *
eb714832 558 * @return
559 * An array for each plugin.
560 */
561function wysiwyg_get_plugins($editor_name) {
562 $plugins = array();
563 if (!empty($editor_name)) {
564 $editor = wysiwyg_get_editor($editor_name);
565 // Add internal editor plugins.
566 if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
567 $plugins = $editor['plugin callback']($editor);
568 }
eb714832 569 // Add editor plugins provided via hook_wysiwyg_plugin().
570 $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
6e91d64e 571 // Add API plugins provided by Drupal modules.
572 // @todo We need to pass the filepath to the plugin icon for Drupal plugins.
573 if (isset($editor['proxy plugin'])) {
574 $plugins += $editor['proxy plugin'];
575 $proxy = key($editor['proxy plugin']);
576 foreach (wysiwyg_get_all_plugins() as $plugin_name => $info) {
577 $plugins[$proxy]['buttons'][$plugin_name] = $info['title'];
578 }
579 }
eb714832 580 }
581 return $plugins;
582}
583
584/**
e16a9972 585 * Return an array of initial editor settings for a Wysiwyg profile.
eb714832 586 */
587function wysiwyg_get_editor_config($profile, $theme) {
81a2754f 588 $editor = wysiwyg_get_editor($profile->editor);
eb714832 589 $settings = array();
590 if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
591 $settings = $editor['settings callback']($editor, $profile->settings, $theme);
d1a1511b 592
593 // Allow other modules to alter the editor settings for this format.
594 $context = array('editor' => $editor, 'profile' => $profile, 'theme' => $theme);
595 drupal_alter('wysiwyg_editor_settings', $settings, $context);
eb714832 596 }
597 return $settings;
598}
599
600/**
5708da35 601 * Retrieve stylesheets for HTML/IFRAME-based editors.
602 *
603 * This assumes that the content editing area only needs stylesheets defined
604 * for the scope 'theme'.
605 *
606 * @return
607 * An array containing CSS files, including proper base path.
608 */
609function wysiwyg_get_css() {
610 static $files;
611
612 if (isset($files)) {
613 return $files;
614 }
a0313372 615 // In node form previews, the theme has not been initialized yet.
150bc86b 616 if (!empty($_POST)) {
617 drupal_theme_initialize();
618 }
a0313372 619
5708da35 620 $files = array();
150bc86b 621 foreach (drupal_add_css() as $filepath => $info) {
68d4032b 622 if ($info['group'] >= CSS_THEME && $info['media'] != 'print') {
150bc86b 623 if (file_exists($filepath)) {
624 $files[] = base_path() . $filepath;
5708da35 625 }
626 }
627 }
628 return $files;
629}
630
631/**
5d5542b7 632 * Loads a profile for a given text format.
633 *
634 * Since there are commonly not many text formats, and each text format-enabled
635 * form element will possibly have to load every single profile, all existing
636 * profiles are loaded and cached once to reduce the amount of database queries.
117ad234 637 */
638function wysiwyg_profile_load($format) {
5d5542b7 639 $profiles = wysiwyg_profile_load_all();
117ad234 640 return (isset($profiles[$format]) ? $profiles[$format] : FALSE);
641}
642
643/**
5d5542b7 644 * Loads all profiles.
eb714832 645 */
117ad234 646function wysiwyg_profile_load_all() {
5d5542b7 647 // entity_load(..., FALSE) does not re-use its own static cache upon
648 // repetitive calls, so a custom static cache is required.
649 // @see wysiwyg_entity_info()
650 $profiles = &drupal_static(__FUNCTION__);
eb714832 651
eb714832 652 if (!isset($profiles)) {
5d5542b7 653 // Additional database cache to support alternative caches like memcache.
654 if ($cached = cache_get('wysiwyg_profiles')) {
655 $profiles = $cached->data;
656 }
657 else {
658 $profiles = entity_load('wysiwyg_profile', FALSE);
659 cache_set('wysiwyg_profiles', $profiles);
eb714832 660 }
661 }
662
117ad234 663 return $profiles;
eb714832 664}
665
666/**
5d5542b7 667 * Deletes a profile from the database.
668 */
669function wysiwyg_profile_delete($format) {
670 db_delete('wysiwyg')
671 ->condition('format', $format)
672 ->execute();
673}
674
675/**
676 * Clear all Wysiwyg profile caches.
677 */
678function wysiwyg_profile_cache_clear() {
679 entity_get_controller('wysiwyg_profile')->resetCache();
680 drupal_static_reset('wysiwyg_profile_load_all');
681 cache_clear_all('wysiwyg_profiles', 'cache');
682}
683
684/**
c600ff4f 685 * Implements hook_form_FORM_ID_alter().
eb714832 686 */
c600ff4f 687function wysiwyg_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
688 $account = $form['#user'];
689 $user_formats = filter_formats($account);
690 $options = array();
691 $options_default = array();
692 foreach (wysiwyg_profile_load_all() as $format => $profile) {
693 // Only show profiles that have user_choose enabled.
694 if (!empty($profile->settings['user_choose']) && isset($user_formats[$format])) {
695 $options[$format] = check_plain($user_formats[$format]->name);
696 if (wysiwyg_user_get_status($profile, $account)) {
697 $options_default[] = $format;
698 }
eb714832 699 }
700 }
c600ff4f 701 if (!empty($options)) {
702 $form['wysiwyg']['wysiwyg_status'] = array(
703 '#type' => 'checkboxes',
704 '#title' => t('Text formats enabled for rich-text editing'),
705 '#options' => $options,
706 '#default_value' => $options_default,
707 );
eb714832 708 }
709}
710
c600ff4f 711/**
712 * Implements hook_user_insert().
713 *
714 * Wysiwyg's user preferences are normally not exposed on the user registration
715 * form, but in case they are manually altered in, we invoke
716 * wysiwyg_user_update() accordingly.
717 */
718function wysiwyg_user_insert(&$edit, $account, $category) {
719 wysiwyg_user_update($edit, $account, $category);
720}
721
722/**
723 * Implements hook_user_update().
724 */
725function wysiwyg_user_update(&$edit, $account, $category) {
726 if (isset($edit['wysiwyg_status'])) {
727 db_delete('wysiwyg_user')
728 ->condition('uid', $account->uid)
729 ->execute();
730 $query = db_insert('wysiwyg_user')
731 ->fields(array('uid', 'format', 'status'));
732 foreach ($edit['wysiwyg_status'] as $format => $status) {
733 $query->values(array(
734 'uid' => $account->uid,
735 'format' => $format,
736 'status' => (int) (bool) $status,
737 ));
738 }
739 $query->execute();
740 }
741}
742
743function wysiwyg_user_get_status($profile, $account = NULL) {
eb714832 744 global $user;
eb714832 745
c600ff4f 746 if (!isset($account)) {
747 $account = $user;
748 }
749
750 // Default wysiwyg editor status information is only required on forms, so we
751 // do not pre-emptively load and attach this information on every user_load().
752 if (!isset($account->wysiwyg_status)) {
753 $account->wysiwyg_status = db_query("SELECT format, status FROM {wysiwyg_user} WHERE uid = :uid", array(
754 ':uid' => $account->uid,
755 ))->fetchAllKeyed();
756 }
757
758 if (!empty($profile->settings['user_choose']) && isset($account->wysiwyg_status[$profile->format])) {
759 $status = $account->wysiwyg_status[$profile->format];
eb714832 760 }
761 else {
9e8e526a 762 $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE;
eb714832 763 }
764
c600ff4f 765 return (bool) $status;
eb714832 766}
767
768/**
769 * @defgroup wysiwyg_api Wysiwyg API
770 * @{
14b92488 771 *
eb714832 772 * @todo Forked from Panels; abstract into a separate API module that allows
773 * contrib modules to define supported include/plugin types.
774 */
775
776/**
777 * Return library information for a given editor.
14b92488 778 *
eb714832 779 * @param $name
780 * The internal name of an editor.
14b92488 781 *
eb714832 782 * @return
783 * The library information for the editor, or FALSE if $name is unknown or not
784 * installed properly.
14b92488 785 */
eb714832 786function wysiwyg_get_editor($name) {
787 $editors = wysiwyg_get_all_editors();
788 return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
789}
790
791/**
792 * Compile a list holding all supported editors including installed editor version information.
793 */
794function wysiwyg_get_all_editors() {
795 static $editors;
796
797 if (isset($editors)) {
798 return $editors;
14b92488 799 }
eb714832 800
801 $editors = wysiwyg_load_includes('editors', 'editor');
802 foreach ($editors as $editor => $properties) {
803 // Fill in required properties.
804 $editors[$editor] += array(
805 'title' => '',
806 'vendor url' => '',
807 'download url' => '',
c18af709 808 'editor path' => wysiwyg_get_path($editors[$editor]['name']),
809 'library path' => wysiwyg_get_path($editors[$editor]['name']),
eb714832 810 'libraries' => array(),
811 'version callback' => NULL,
812 'themes callback' => NULL,
813 'settings callback' => NULL,
814 'plugin callback' => NULL,
815 'plugin settings callback' => NULL,
816 'versions' => array(),
c18af709 817 'js path' => $editors[$editor]['path'] . '/js',
818 'css path' => $editors[$editor]['path'] . '/css',
eb714832 819 );
820 // Check whether library is present.
c18af709 821 if (!($editors[$editor]['installed'] = file_exists($editors[$editor]['library path']))) {
eb714832 822 continue;
823 }
824 // Detect library version.
825 if (function_exists($editors[$editor]['version callback'])) {
c18af709 826 $editors[$editor]['installed version'] = $editors[$editor]['version callback']($editors[$editor]);
eb714832 827 }
828 if (empty($editors[$editor]['installed version'])) {
829 $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
830 $editors[$editor]['installed'] = FALSE;
831 continue;
832 }
833 // Determine to which supported version the installed version maps.
834 ksort($editors[$editor]['versions']);
835 $version = 0;
836 foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
837 if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
838 $version = $supported_version;
14b92488 839 }
eb714832 840 }
841 if (!$version) {
c18af709 842 $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $editors[$editor]['title']));
eb714832 843 $editors[$editor]['installed'] = FALSE;
844 continue;
845 }
846 // Apply library version specific definitions and overrides.
847 $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
848 unset($editors[$editor]['versions']);
eb714832 849 }
850 return $editors;
851}
852
853/**
6e91d64e 854 * Invoke hook_wysiwyg_plugin() in all modules.
855 */
856function wysiwyg_get_all_plugins() {
857 static $plugins;
858
859 if (isset($plugins)) {
860 return $plugins;
861 }
862
863 $plugins = wysiwyg_load_includes('plugins', 'plugin');
864 foreach ($plugins as $name => $properties) {
865 $plugin = &$plugins[$name];
866 // Fill in required/default properties.
867 $plugin += array(
868 'title' => $plugin['name'],
869 'vendor url' => '',
870 'js path' => $plugin['path'] . '/' . $plugin['name'],
871 'js file' => $plugin['name'] . '.js',
872 'css path' => $plugin['path'] . '/' . $plugin['name'],
873 'css file' => $plugin['name'] . '.css',
874 'icon path' => $plugin['path'] . '/' . $plugin['name'] . '/images',
875 'icon file' => $plugin['name'] . '.png',
876 'dialog path' => $plugin['name'],
877 'dialog settings' => array(),
878 'settings callback' => NULL,
879 'settings form callback' => NULL,
880 );
881 // Fill in default settings.
882 $plugin['settings'] += array(
883 'path' => base_path() . $plugin['path'] . '/' . $plugin['name'],
884 );
885 // Check whether library is present.
886 if (!($plugin['installed'] = file_exists($plugin['js path'] . '/' . $plugin['js file']))) {
887 continue;
888 }
889 }
890 return $plugins;
891}
892
893/**
eb714832 894 * Load include files for wysiwyg implemented by all modules.
895 *
896 * @param $type
897 * The type of includes to search for, can be 'editors'.
898 * @param $hook
899 * The hook name to invoke.
900 * @param $file
901 * An optional include file name without .inc extension to limit the search to.
902 *
903 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
904 */
905function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
906 // Determine implementations.
907 $directories = wysiwyg_get_directories($type);
ef25c211 908 $directories['wysiwyg'] = drupal_get_path('module', 'wysiwyg') . '/' . $type;
eb714832 909 $file_list = array();
910 foreach ($directories as $module => $path) {
150bc86b 911 $file_list[$module] = drupal_system_listing("/{$file}.inc\$/", $path, 'name', 0);
eb714832 912 }
913
914 // Load implementations.
915 $info = array();
916 foreach (array_filter($file_list) as $module => $files) {
917 foreach ($files as $file) {
150bc86b 918 include_once './' . $file->uri;
919 $result = _wysiwyg_process_include($module, $module . '_' . $file->name, dirname($file->uri), $hook);
eb714832 920 if (is_array($result)) {
921 $info = array_merge($info, $result);
14b92488 922 }
eb714832 923 }
14b92488 924 }
eb714832 925 return $info;
926}
14b92488 927
eb714832 928/**
c18af709 929 * Helper function to build paths to libraries.
eb714832 930 *
c18af709 931 * @param $library
932 * The external library name to return the path for.
eb714832 933 * @param $base_path
934 * Whether to prefix the resulting path with base_path().
eb714832 935 *
936 * @return
c18af709 937 * The path to the specified library.
938 *
939 * @ingroup libraries
940 */
941function wysiwyg_get_path($library, $base_path = FALSE) {
942 static $libraries;
943
944 if (!isset($libraries)) {
945 $libraries = wysiwyg_get_libraries();
946 }
947 if (!isset($libraries[$library])) {
948 // Most often, external libraries can be shared across multiple sites.
949 return 'sites/all/libraries/' . $library;
950 }
951
952 $path = ($base_path ? base_path() : '');
953 $path .= $libraries[$library];
954
955 return $path;
956}
957
958/**
959 * Return an array of library directories.
960 *
961 * Returns an array of library directories from the all-sites directory
962 * (i.e. sites/all/libraries/), the profiles directory, and site-specific
963 * directory (i.e. sites/somesite/libraries/). The returned array will be keyed
964 * by the library name. Site-specific libraries are prioritized over libraries
965 * in the default directories. That is, if a library with the same name appears
966 * in both the site-wide directory and site-specific directory, only the
967 * site-specific version will be listed.
968 *
969 * @return
970 * A list of library directories.
971 *
972 * @ingroup libraries
eb714832 973 */
c18af709 974function wysiwyg_get_libraries() {
975 global $profile;
976
977 // When this function is called during Drupal's initial installation process,
978 // the name of the profile that is about to be installed is stored in the
979 // global $profile variable. At all other times, the regular system variable
980 // contains the name of the current profile, and we can call variable_get()
981 // to determine the profile.
982 if (!isset($profile)) {
983 $profile = variable_get('install_profile', 'default');
984 }
985
986 $directory = 'libraries';
987 $searchdir = array();
988 $config = conf_path();
989
990 // The 'profiles' directory contains pristine collections of modules and
991 // themes as organized by a distribution. It is pristine in the same way
992 // that /modules is pristine for core; users should avoid changing anything
993 // there in favor of sites/all or sites/<domain> directories.
994 if (file_exists("profiles/$profile/$directory")) {
995 $searchdir[] = "profiles/$profile/$directory";
996 }
997
998 // Always search sites/all/*.
54a65ca8 999 $searchdir[] = 'sites/all/' . $directory;
c18af709 1000
1001 // Also search sites/<domain>/*.
1002 if (file_exists("$config/$directory")) {
1003 $searchdir[] = "$config/$directory";
1004 }
1005
1006 // Retrieve list of directories.
1007 // @todo Core: Allow to scan for directories.
1008 $directories = array();
1009 $nomask = array('CVS');
1010 foreach ($searchdir as $dir) {
1011 if (is_dir($dir) && $handle = opendir($dir)) {
1012 while (FALSE !== ($file = readdir($handle))) {
1013 if (!in_array($file, $nomask) && $file[0] != '.') {
1014 if (is_dir("$dir/$file")) {
1015 $directories[$file] = "$dir/$file";
1016 }
1017 }
1018 }
1019 closedir($handle);
1020 }
1021 }
1022
1023 return $directories;
eb714832 1024}
14b92488 1025
1026/**
eb714832 1027 * Return a list of directories by modules implementing wysiwyg_include_directory().
14b92488 1028 *
eb714832 1029 * @param $plugintype
1030 * The type of a plugin; can be 'editors'.
1031 *
1032 * @return
1033 * An array containing module names suffixed with '_' and their defined
1034 * directory.
14b92488 1035 *
eb714832 1036 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
14b92488 1037 */
eb714832 1038function wysiwyg_get_directories($plugintype) {
1039 $directories = array();
1040 foreach (module_implements('wysiwyg_include_directory') as $module) {
1041 $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
1042 if (isset($result) && is_string($result)) {
ef25c211 1043 $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
eb714832 1044 }
14b92488 1045 }
eb714832 1046 return $directories;
14b92488 1047}
1048
eb714832 1049/**
1050 * Process a single hook implementation of a wysiwyg editor.
1051 *
1052 * @param $module
1053 * The module that owns the hook.
1054 * @param $identifier
1055 * Either the module or 'wysiwyg_' . $file->name
1056 * @param $hook
1057 * The name of the hook being invoked.
1058 */
1059function _wysiwyg_process_include($module, $identifier, $path, $hook) {
1060 $function = $identifier . '_' . $hook;
1061 if (!function_exists($function)) {
1062 return NULL;
1063 }
1064 $result = $function();
1065 if (!isset($result) || !is_array($result)) {
1066 return NULL;
1067 }
1068
1069 // Fill in defaults.
1070 foreach ($result as $editor => $properties) {
1071 $result[$editor]['module'] = $module;
1072 $result[$editor]['name'] = $editor;
1073 $result[$editor]['path'] = $path;
1074 }
1075 return $result;
1076}
1077
1078/**
1079 * @} End of "defgroup wysiwyg_api".
1080 */