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