| 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 | */ |
| 12 | function wysiwyg_menu() { |
| 13 | $items = array(); |
| 14 | $items['admin/settings/wysiwyg/profile'] = array( |
| 15 | 'title' => 'Wysiwyg', |
| 16 | 'page callback' => 'wysiwyg_admin', |
| 17 | 'description' => 'Configure client-side editor profiles.', |
| af8246de |
18 | 'access arguments' => array('administer filters'), |
| eb714832 |
19 | 'file' => 'wysiwyg.admin.inc', |
| 20 | ); |
| 21 | return $items; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Implementation of hook_theme(). |
| 26 | */ |
| 27 | function wysiwyg_theme() { |
| 28 | return array( |
| 81a2754f |
29 | 'wysiwyg_profile_overview' => array('arguments' => array('form' => NULL)), |
| 30 | 'wysiwyg_admin_button_table' => array('arguments' => array('form' => NULL)), |
| eb714832 |
31 | ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Implementation of hook_help(). |
| 36 | */ |
| 37 | function wysiwyg_help($path, $arg) { |
| 38 | switch ($path) { |
| 39 | case 'admin/settings/wysiwyg/profile': |
| c06ba055 |
40 | if (empty($arg[4])) { |
| 81a2754f |
41 | $output = '<p>'. t('A Wysiwyg profile can be associated to an input format. A Wysiwyg profile defines, which client-side editor is loaded, can define what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') .'</p>'; |
| c06ba055 |
42 | return $output; |
| 43 | } |
| 44 | break; |
| eb714832 |
45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| eb714832 |
49 | * Implementation of hook_form_alter(). |
| 33fd8bc9 |
50 | * |
| 51 | * Before Drupal 7, there is no way to easily identify form fields that are |
| 52 | * input format enabled. As a workaround, we assign a form #after_build |
| 53 | * processing callback that is executed on all forms after they have been |
| 54 | * completely built, so form elements are in their effective order |
| 55 | * and position already. |
| 56 | * |
| 57 | * @see wysiwyg_process_form() |
| eb714832 |
58 | */ |
| 59 | function wysiwyg_form_alter(&$form, &$form_state) { |
| 33fd8bc9 |
60 | $form['#after_build'][] = 'wysiwyg_process_form'; |
| eb714832 |
61 | // Disable 'teaser' textarea. |
| 62 | if (isset($form['body_field'])) { |
| 63 | unset($form['body_field']['teaser_js']); |
| 64 | $form['body_field']['teaser_include'] = array(); |
| 14b92488 |
65 | } |
| 14b92488 |
66 | } |
| 67 | |
| 68 | /** |
| eb714832 |
69 | * Process a textarea for Wysiwyg Editor. |
| 70 | * |
| 71 | * This way, we can recurse into the form and search for certain, hard-coded |
| 72 | * elements that have been added by filter_form(). If an input format selector |
| 73 | * or input format guidelines element is found, we assume that the preceding |
| 74 | * element is the corresponding textarea and use it's #id for attaching |
| 75 | * client-side editors. |
| 14b92488 |
76 | * |
| eb714832 |
77 | * @see wysiwyg_elements(), filter_form() |
| 78 | */ |
| 79 | function wysiwyg_process_form(&$form) { |
| 80 | // Iterate over element children; resetting array keys to access last index. |
| 81 | if ($children = array_values(element_children($form))) { |
| 82 | foreach ($children as $index => $item) { |
| 83 | $element = &$form[$item]; |
| 84 | |
| 85 | // filter_form() always uses the key 'format'. We need a type-agnostic |
| 86 | // match to prevent false positives. Also, there must have been at least |
| 87 | // one element on this level. |
| 88 | if ($item === 'format' && $index > 0) { |
| 89 | // Make sure we either match a input format selector or input format |
| 90 | // guidelines (displayed if user has access to one input format only). |
| 91 | if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) { |
| 92 | // The element before this element is the target form field. |
| 93 | $field = &$form[$children[$index - 1]]; |
| 94 | |
| 95 | // Disable #resizable to avoid resizable behavior to hi-jack the UI, |
| 96 | // but load the behavior, so the 'none' editor can attach/detach it. |
| 97 | $extra_class = ''; |
| 98 | if (!empty($field['#resizable'])) { |
| 99 | // Due to our CSS class parsing, we can add arbitrary parameters |
| 100 | // for each input format. |
| 101 | $extra_class = ' wysiwyg-resizable-1'; |
| 102 | $field['#resizable'] = FALSE; |
| 103 | drupal_add_js('misc/textarea.js'); |
| 104 | } |
| 105 | |
| 106 | // Determine the available input formats. The last child element is a |
| 107 | // link to "More information about formatting options". When only one |
| 108 | // input format is displayed, we also have to remove formatting |
| 109 | // guidelines, stored in the child 'format'. |
| 110 | $formats = element_children($element); |
| 111 | array_pop($formats); |
| 1aabb694 |
112 | if (($key = array_search('format', $formats)) !== FALSE) { |
| 113 | unset($formats[$key]); |
| 114 | } |
| eb714832 |
115 | foreach ($formats as $format) { |
| 116 | // Default to 'none' editor (Drupal's default behaviors). |
| 117 | $editor = 'none'; |
| c3dc7f9a |
118 | $status = 1; |
| eb714832 |
119 | // Fetch the profile associated to this input format. |
| 120 | $profile = wysiwyg_get_profile($format); |
| 121 | if ($profile) { |
| 81a2754f |
122 | $editor = $profile->editor; |
| c3dc7f9a |
123 | $status = (int)wysiwyg_user_get_status($profile); |
| eb714832 |
124 | // Check editor theme (and reset it if not/no longer available). |
| e16a9972 |
125 | $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : '')); |
| eb714832 |
126 | |
| 127 | // Add profile settings for this input format. |
| 128 | wysiwyg_add_editor_settings($profile, $theme); |
| 129 | // Add plugin settings for this input format. |
| 130 | wysiwyg_add_plugin_settings($profile); |
| eb714832 |
131 | } |
| 132 | |
| 133 | // Use a prefix/suffix for a single input format, or attach to input |
| 134 | // format selector radio buttons. |
| 135 | if (isset($element['format']['guidelines'])) { |
| c3dc7f9a |
136 | $element['format']['guidelines']['#prefix'] = '<div class="wysiwyg wysiwyg-format-'. $format .' wysiwyg-editor-'. $editor .' wysiwyg-field-'. $field['#id'] .' wysiwyg-status-'. $status . $extra_class .'">'; |
| 1aabb694 |
137 | $element['format']['guidelines']['#suffix'] = '</div>'; |
| 138 | // Edge-case: Default format contains no input filters. |
| 139 | if (empty($element['format']['guidelines']['#value'])) { |
| 140 | $element['format']['guidelines']['#value'] = ' '; |
| 141 | } |
| eb714832 |
142 | } |
| 143 | else { |
| 144 | if (isset($element[$format]['#attributes']['class'])) { |
| 145 | $element[$format]['#attributes']['class'] .= ' '; |
| 146 | } |
| 147 | else { |
| 148 | $element[$format]['#attributes']['class'] = ''; |
| 149 | } |
| c3dc7f9a |
150 | $element[$format]['#attributes']['class'] .= 'wysiwyg wysiwyg-format-'. $format .' wysiwyg-editor-'. $editor .' wysiwyg-field-'. $field['#id'] .' wysiwyg-status-'. $status . $extra_class; |
| eb714832 |
151 | } |
| 152 | } |
| 153 | } |
| 154 | // If this element is 'format', do not recurse further. |
| 155 | continue; |
| 156 | } |
| 157 | // Recurse into children. |
| 158 | wysiwyg_process_form($element); |
| 159 | } |
| 160 | } |
| 161 | return $form; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Determine the profile to use for a given input format id. |
| 14b92488 |
166 | * |
| eb714832 |
167 | * This function also performs sanity checks for the configured editor in a |
| 168 | * profile to ensure that we do not load a malformed editor. |
| 14b92488 |
169 | * |
| eb714832 |
170 | * @param $format |
| 171 | * The internal id of an input format. |
| 14b92488 |
172 | * |
| eb714832 |
173 | * @return |
| 174 | * A wysiwyg profile. |
| 175 | * |
| 176 | * @see wysiwyg_load_editor(), wysiwyg_get_editor() |
| 14b92488 |
177 | */ |
| eb714832 |
178 | function wysiwyg_get_profile($format) { |
| 81a2754f |
179 | if ($profile = wysiwyg_load_profile($format)) { |
| eb714832 |
180 | if (wysiwyg_load_editor($profile)) { |
| 181 | return $profile; |
| 182 | } |
| 183 | } |
| 184 | return FALSE; |
| 14b92488 |
185 | } |
| 186 | |
| 187 | /** |
| eb714832 |
188 | * Load an editor library and initialize basic Wysiwyg settings. |
| 189 | * |
| 190 | * @param $profile |
| 191 | * A wysiwyg editor profile. |
| 14b92488 |
192 | * |
| eb714832 |
193 | * @return |
| 194 | * TRUE if the editor has been loaded, FALSE if not. |
| 14b92488 |
195 | * |
| eb714832 |
196 | * @see wysiwyg_get_profile() |
| 197 | */ |
| 198 | function wysiwyg_load_editor($profile) { |
| 199 | static $settings_added; |
| 200 | static $loaded = array(); |
| 201 | |
| 81a2754f |
202 | $name = $profile->editor; |
| eb714832 |
203 | // Library files must be loaded only once. |
| 204 | if (!isset($loaded[$name])) { |
| 205 | // Load editor. |
| 206 | $editor = wysiwyg_get_editor($name); |
| 207 | if ($editor) { |
| 208 | // Determine library files to load. |
| 209 | // @todo Allow to configure the library/execMode to use. |
| 210 | if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) { |
| 211 | $library = $profile->settings['library']; |
| 212 | $files = $editor['libraries'][$profile->settings['library']]['files']; |
| 213 | } |
| 214 | else { |
| 215 | // Fallback to the first by default (external libraries can change). |
| 216 | $library = key($editor['libraries']); |
| 217 | $files = array_shift($editor['libraries']); |
| 218 | $files = $files['files']; |
| 219 | } |
| 220 | foreach ($files as $file) { |
| 221 | drupal_add_js($editor['library path'] . '/' . $file); |
| 222 | } |
| 223 | // Load JavaScript integration files for this editor. |
| a09d7c1c |
224 | $files = array(); |
| eb714832 |
225 | if (isset($editor['js files'])) { |
| 226 | $files = $editor['js files']; |
| 227 | } |
| 228 | foreach ($files as $file) { |
| 81a2754f |
229 | drupal_add_js($editor['js path'] . '/' . $file); |
| eb714832 |
230 | } |
| a09d7c1c |
231 | // Load CSS stylesheets for this editor. |
| 232 | $files = array(); |
| 233 | if (isset($editor['css files'])) { |
| 234 | $files = $editor['css files']; |
| 235 | } |
| 236 | foreach ($files as $file) { |
| 237 | drupal_add_css($editor['css path'] . '/' . $file, 'module', 'screen'); |
| 238 | } |
| eb714832 |
239 | |
| eb714832 |
240 | drupal_add_js(array('wysiwyg' => array( |
| 241 | 'configs' => array($editor['name'] => array()), |
| 7623c9ce |
242 | // @todo Move into profile settings. |
| 9e8e526a |
243 | 'showToggle' => isset($profile->settings['show_toggle']) ? $profile->settings['show_toggle'] : TRUE, |
| 7623c9ce |
244 | // @todo Move into (global) editor settings. |
| eb714832 |
245 | // If JS compression is enabled, at least TinyMCE is unable to determine |
| 246 | // its own base path and exec mode since it can't find the script name. |
| 247 | 'editorBasePath' => base_path() . $editor['library path'], |
| 248 | 'execMode' => $library, |
| 249 | )), 'setting'); |
| 250 | |
| 251 | $loaded[$name] = TRUE; |
| 252 | } |
| 253 | else { |
| 254 | $loaded[$name] = FALSE; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Add basic Wysiwyg settings if any editor has been added. |
| 259 | if (!isset($settings_added) && $loaded[$name]) { |
| 260 | drupal_add_js(array('wysiwyg' => array( |
| 261 | 'configs' => array(), |
| 262 | 'disable' => t('Disable rich-text'), |
| 263 | 'enable' => t('Enable rich-text'), |
| 264 | )), 'setting'); |
| 265 | |
| 266 | // Initialize our namespaces in the *header* to do not force editor |
| 267 | // integration scripts to check and define Drupal.wysiwyg on its own. |
| 81a2754f |
268 | drupal_add_js(wysiwyg_get_path('wysiwyg.init.js'), 'core'); |
| eb714832 |
269 | |
| 270 | // The 'none' editor is a special editor implementation, allowing us to |
| 271 | // attach and detach regular Drupal behaviors just like any other editor. |
| 81a2754f |
272 | drupal_add_js(wysiwyg_get_path('editors/js/none.js')); |
| eb714832 |
273 | |
| 274 | // Add wysiwyg.js to the footer to ensure it's executed after the |
| 275 | // Drupal.settings array has been rendered and populated. Also, since editor |
| 276 | // library initialization functions must be loaded first by the browser, |
| 7623c9ce |
277 | // and Drupal.wysiwygInit() must be executed AFTER editors registered |
| 278 | // their callbacks and BEFORE Drupal.behaviors are applied, this must come |
| eb714832 |
279 | // last. |
| 280 | drupal_add_js(wysiwyg_get_path('wysiwyg.js'), 'module', 'footer'); |
| 281 | |
| eb714832 |
282 | $settings_added = TRUE; |
| 283 | } |
| 284 | |
| 285 | return $loaded[$name]; |
| 286 | } |
| 287 | |
| 288 | /** |
| e16a9972 |
289 | * Add editor settings for a given input format. |
| 14b92488 |
290 | */ |
| eb714832 |
291 | function wysiwyg_add_editor_settings($profile, $theme) { |
| e16a9972 |
292 | static $formats = array(); |
| eb714832 |
293 | |
| e16a9972 |
294 | if (!isset($formats[$profile->format])) { |
| eb714832 |
295 | $config = wysiwyg_get_editor_config($profile, $theme); |
| e16a9972 |
296 | // drupal_to_js() does not properly convert numeric array keys, so we need |
| 297 | // to use a string instead of the format id. |
| 298 | drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format'. $profile->format => $config)))), 'setting'); |
| 299 | $formats[$profile->format] = TRUE; |
| eb714832 |
300 | } |
| 14b92488 |
301 | } |
| 302 | |
| 303 | /** |
| eb714832 |
304 | * Add settings for external plugins. |
| 305 | * |
| 306 | * @param $profile |
| 307 | * A wysiwyg editor profile. |
| 308 | */ |
| 309 | function wysiwyg_add_plugin_settings($profile) { |
| 310 | static $plugins_added = array(); |
| 311 | |
| 81a2754f |
312 | if (!isset($plugins_added[$profile->editor])) { |
| eb714832 |
313 | $plugins = array(); |
| 81a2754f |
314 | $editor = wysiwyg_get_editor($profile->editor); |
| eb714832 |
315 | // Collect editor plugins provided via hook_wysiwyg_plugin(). |
| 316 | $info = module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']); |
| 317 | // Only keep enabled plugins in this profile. |
| 318 | foreach ($info as $plugin => $meta) { |
| 319 | if (!isset($profile->settings['buttons'][$plugin])) { |
| 320 | unset($info[$plugin]); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (isset($editor['plugin settings callback']) && function_exists($editor['plugin settings callback'])) { |
| 325 | $plugins = $editor['plugin settings callback']($editor, $profile, $info); |
| 326 | } |
| 327 | |
| 81a2754f |
328 | drupal_add_js(array('wysiwyg' => array('plugins' => array($profile->editor => $plugins))), 'setting'); |
| eb714832 |
329 | |
| 81a2754f |
330 | $plugins_added[$profile->editor] = TRUE; |
| eb714832 |
331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| e16a9972 |
335 | * Retrieve available themes for an editor. |
| 14b92488 |
336 | * |
| e16a9972 |
337 | * Editor themes control the visual presentation of an editor. |
| 14b92488 |
338 | * |
| eb714832 |
339 | * @param $profile |
| 340 | * A wysiwyg editor profile; passed/altered by reference. |
| 341 | * @param $selected_theme |
| 342 | * An optional theme name that ought to be used. |
| 14b92488 |
343 | * |
| eb714832 |
344 | * @return |
| 345 | * An array of theme names, or a single, checked theme name if $selected_theme |
| 346 | * was given. |
| 14b92488 |
347 | */ |
| eb714832 |
348 | function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) { |
| 349 | static $themes = array(); |
| 350 | |
| 81a2754f |
351 | if (!isset($themes[$profile->editor])) { |
| 352 | $editor = wysiwyg_get_editor($profile->editor); |
| eb714832 |
353 | if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) { |
| 354 | $themes[$editor['name']] = $editor['themes callback']($editor, $profile); |
| 355 | } |
| 356 | // Fallback to 'default' otherwise. |
| 357 | else { |
| 358 | $themes[$editor['name']] = array('default'); |
| 359 | } |
| 14b92488 |
360 | } |
| eb714832 |
361 | |
| 362 | // Check optional $selected_theme argument, if given. |
| 363 | if (isset($selected_theme)) { |
| 364 | // If the passed theme name does not exist, use the first available. |
| 81a2754f |
365 | if (!isset($themes[$profile->editor][$selected_theme])) { |
| 366 | $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0]; |
| eb714832 |
367 | } |
| 368 | } |
| 369 | |
| 81a2754f |
370 | return isset($selected_theme) ? $selected_theme : $themes[$profile->editor]; |
| 14b92488 |
371 | } |
| 372 | |
| 373 | /** |
| eb714832 |
374 | * Return plugin metadata from the plugin registry. |
| 14b92488 |
375 | * |
| eb714832 |
376 | * @param $editor_name |
| 377 | * The internal name of an editor to return plugins for. |
| 14b92488 |
378 | * |
| eb714832 |
379 | * @return |
| 380 | * An array for each plugin. |
| 381 | */ |
| 382 | function wysiwyg_get_plugins($editor_name) { |
| 383 | $plugins = array(); |
| 384 | if (!empty($editor_name)) { |
| 385 | $editor = wysiwyg_get_editor($editor_name); |
| 386 | // Add internal editor plugins. |
| 387 | if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) { |
| 388 | $plugins = $editor['plugin callback']($editor); |
| 389 | } |
| 390 | // Load our own plugins. |
| 391 | include_once drupal_get_path('module', 'wysiwyg') .'/wysiwyg.plugins.inc'; |
| 392 | |
| 393 | // Add editor plugins provided via hook_wysiwyg_plugin(). |
| 394 | $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version'])); |
| 395 | } |
| 396 | return $plugins; |
| 397 | } |
| 398 | |
| 399 | /** |
| e16a9972 |
400 | * Return an array of initial editor settings for a Wysiwyg profile. |
| eb714832 |
401 | */ |
| 402 | function wysiwyg_get_editor_config($profile, $theme) { |
| 81a2754f |
403 | $editor = wysiwyg_get_editor($profile->editor); |
| eb714832 |
404 | $settings = array(); |
| 405 | if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) { |
| 406 | $settings = $editor['settings callback']($editor, $profile->settings, $theme); |
| 407 | } |
| 408 | return $settings; |
| 409 | } |
| 410 | |
| 411 | /** |
| eb714832 |
412 | * Load all profiles. Just load one profile if $name is passed in. |
| 413 | */ |
| 81a2754f |
414 | function wysiwyg_load_profile($format = '') { |
| eb714832 |
415 | static $profiles; |
| 416 | |
| eb714832 |
417 | if (!isset($profiles)) { |
| 418 | $profiles = array(); |
| 81a2754f |
419 | $result = db_query('SELECT * FROM {wysiwyg}'); |
| eb714832 |
420 | while ($profile = db_fetch_object($result)) { |
| 421 | $profile->settings = unserialize($profile->settings); |
| 81a2754f |
422 | $profiles[$profile->format] = $profile; |
| eb714832 |
423 | } |
| 424 | } |
| 425 | |
| 81a2754f |
426 | return ($format && isset($profiles[$format]) ? $profiles[$format] : ($format ? FALSE : $profiles)); |
| eb714832 |
427 | } |
| 428 | |
| 429 | /** |
| 430 | * Implementation of hook_user(). |
| 431 | */ |
| 432 | function wysiwyg_user($type, &$edit, &$user, $category = NULL) { |
| 81a2754f |
433 | if ($type == 'form' && $category == 'account') { |
| 434 | // @todo http://drupal.org/node/322433 |
| 435 | $profile = new stdClass; |
| eb714832 |
436 | if (isset($profile->settings['user_choose']) && $profile->settings['user_choose']) { |
| 437 | $form['wysiwyg'] = array( |
| 438 | '#type' => 'fieldset', |
| 439 | '#title' => t('Wysiwyg Editor settings'), |
| 440 | '#weight' => 10, |
| 441 | '#collapsible' => TRUE, |
| 442 | '#collapsed' => TRUE, |
| 443 | ); |
| 444 | $form['wysiwyg']['wysiwyg_status'] = array( |
| 445 | '#type' => 'checkbox', |
| 446 | '#title' => t('Enable editor by default'), |
| 447 | '#default_value' => isset($user->wysiwyg_status) ? $user->wysiwyg_status : (isset($profile->settings['default']) ? $profile->settings['default'] : FALSE), |
| 448 | '#return_value' => 1, |
| 449 | '#description' => t('If enabled, rich-text editing is enabled by default in textarea fields.'), |
| 450 | ); |
| 451 | return array('wysiwyg' => $form); |
| 452 | } |
| 453 | } |
| 81a2754f |
454 | elseif ($type == 'validate' && isset($edit['wysiwyg_status'])) { |
| eb714832 |
455 | return array('wysiwyg_status' => $edit['wysiwyg_status']); |
| 456 | } |
| 457 | } |
| 458 | |
| eb714832 |
459 | function wysiwyg_user_get_status($profile) { |
| 460 | global $user; |
| eb714832 |
461 | |
| 81a2754f |
462 | if ($profile->settings['user_choose'] && isset($user->wysiwyg_status)) { |
| eb714832 |
463 | $status = $user->wysiwyg_status; |
| 464 | } |
| 465 | else { |
| 9e8e526a |
466 | $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE; |
| eb714832 |
467 | } |
| 468 | |
| 469 | return $status; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * @defgroup wysiwyg_api Wysiwyg API |
| 474 | * @{ |
| 14b92488 |
475 | * |
| eb714832 |
476 | * @todo Forked from Panels; abstract into a separate API module that allows |
| 477 | * contrib modules to define supported include/plugin types. |
| 478 | */ |
| 479 | |
| 480 | /** |
| 481 | * Return library information for a given editor. |
| 14b92488 |
482 | * |
| eb714832 |
483 | * @param $name |
| 484 | * The internal name of an editor. |
| 14b92488 |
485 | * |
| eb714832 |
486 | * @return |
| 487 | * The library information for the editor, or FALSE if $name is unknown or not |
| 488 | * installed properly. |
| 14b92488 |
489 | */ |
| eb714832 |
490 | function wysiwyg_get_editor($name) { |
| 491 | $editors = wysiwyg_get_all_editors(); |
| 492 | return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Compile a list holding all supported editors including installed editor version information. |
| 497 | */ |
| 498 | function wysiwyg_get_all_editors() { |
| 499 | static $editors; |
| 500 | |
| 501 | if (isset($editors)) { |
| 502 | return $editors; |
| 14b92488 |
503 | } |
| eb714832 |
504 | |
| 505 | $editors = wysiwyg_load_includes('editors', 'editor'); |
| 506 | foreach ($editors as $editor => $properties) { |
| 507 | // Fill in required properties. |
| 508 | $editors[$editor] += array( |
| 509 | 'title' => '', |
| 510 | 'vendor url' => '', |
| 511 | 'download url' => '', |
| 512 | 'editor path' => wysiwyg_get_path($properties['name']), |
| 513 | 'library path' => wysiwyg_get_path($properties['name']), |
| 514 | 'libraries' => array(), |
| 515 | 'version callback' => NULL, |
| 516 | 'themes callback' => NULL, |
| 517 | 'settings callback' => NULL, |
| 518 | 'plugin callback' => NULL, |
| 519 | 'plugin settings callback' => NULL, |
| 520 | 'versions' => array(), |
| 521 | 'js path' => $properties['path'] .'/js', |
| 522 | 'css path' => $properties['path'] .'/css', |
| 523 | ); |
| 524 | // Check whether library is present. |
| 525 | if (!($editors[$editor]['installed'] = file_exists($properties['library path']))) { |
| 526 | continue; |
| 527 | } |
| 528 | // Detect library version. |
| 529 | if (function_exists($editors[$editor]['version callback'])) { |
| 530 | $editors[$editor]['installed version'] = $editors[$editor]['version callback']($properties); |
| 531 | } |
| 532 | if (empty($editors[$editor]['installed version'])) { |
| 533 | $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title'])); |
| 534 | $editors[$editor]['installed'] = FALSE; |
| 535 | continue; |
| 536 | } |
| 537 | // Determine to which supported version the installed version maps. |
| 538 | ksort($editors[$editor]['versions']); |
| 539 | $version = 0; |
| 540 | foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) { |
| 541 | if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) { |
| 542 | $version = $supported_version; |
| 14b92488 |
543 | } |
| eb714832 |
544 | } |
| 545 | if (!$version) { |
| cac2b421 |
546 | $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $properties['title'])); |
| eb714832 |
547 | $editors[$editor]['installed'] = FALSE; |
| 548 | continue; |
| 549 | } |
| 550 | // Apply library version specific definitions and overrides. |
| 551 | $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]); |
| 552 | unset($editors[$editor]['versions']); |
| eb714832 |
553 | } |
| 554 | return $editors; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Load include files for wysiwyg implemented by all modules. |
| 559 | * |
| 560 | * @param $type |
| 561 | * The type of includes to search for, can be 'editors'. |
| 562 | * @param $hook |
| 563 | * The hook name to invoke. |
| 564 | * @param $file |
| 565 | * An optional include file name without .inc extension to limit the search to. |
| 566 | * |
| 567 | * @see wysiwyg_get_directories(), _wysiwyg_process_include() |
| 568 | */ |
| 569 | function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) { |
| 570 | // Determine implementations. |
| 571 | $directories = wysiwyg_get_directories($type); |
| 572 | $directories['wysiwyg_'] = wysiwyg_get_path($type); |
| 573 | $file_list = array(); |
| 574 | foreach ($directories as $module => $path) { |
| 575 | $file_list[$module] = drupal_system_listing("$file" . '.inc$', $path, 'name', 0); |
| 576 | } |
| 577 | |
| 578 | // Load implementations. |
| 579 | $info = array(); |
| 580 | foreach (array_filter($file_list) as $module => $files) { |
| 581 | foreach ($files as $file) { |
| 582 | include_once './' . $file->filename; |
| 583 | $result = _wysiwyg_process_include('wysiwyg', $module . $file->name, dirname($file->filename), $hook); |
| 584 | if (is_array($result)) { |
| 585 | $info = array_merge($info, $result); |
| 14b92488 |
586 | } |
| eb714832 |
587 | } |
| 14b92488 |
588 | } |
| eb714832 |
589 | return $info; |
| 590 | } |
| 14b92488 |
591 | |
| eb714832 |
592 | /** |
| 593 | * Helper function to build module/file paths. |
| 594 | * |
| 595 | * @param $file |
| 596 | * A file or directory in a module to return. |
| 597 | * @param $base_path |
| 598 | * Whether to prefix the resulting path with base_path(). |
| 599 | * @param $module |
| 600 | * The module name to use as prefix. |
| 601 | * |
| 602 | * @return |
| 603 | * The path to the specified file in a module. |
| 604 | */ |
| 605 | function wysiwyg_get_path($file = '', $base_path = FALSE, $module = 'wysiwyg') { |
| 606 | $base_path = ($base_path ? base_path() : ''); |
| 607 | return $base_path . drupal_get_path('module', $module) . '/' . $file; |
| 608 | } |
| 14b92488 |
609 | |
| 610 | /** |
| eb714832 |
611 | * Return a list of directories by modules implementing wysiwyg_include_directory(). |
| 14b92488 |
612 | * |
| eb714832 |
613 | * @param $plugintype |
| 614 | * The type of a plugin; can be 'editors'. |
| 615 | * |
| 616 | * @return |
| 617 | * An array containing module names suffixed with '_' and their defined |
| 618 | * directory. |
| 14b92488 |
619 | * |
| eb714832 |
620 | * @see wysiwyg_load_includes(), _wysiwyg_process_include() |
| 14b92488 |
621 | */ |
| eb714832 |
622 | function wysiwyg_get_directories($plugintype) { |
| 623 | $directories = array(); |
| 624 | foreach (module_implements('wysiwyg_include_directory') as $module) { |
| 625 | $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype); |
| 626 | if (isset($result) && is_string($result)) { |
| 627 | $directories[$module .'_'] = drupal_get_path('module', $module) .'/'. $result; |
| 628 | } |
| 14b92488 |
629 | } |
| eb714832 |
630 | return $directories; |
| 14b92488 |
631 | } |
| 632 | |
| eb714832 |
633 | /** |
| 634 | * Process a single hook implementation of a wysiwyg editor. |
| 635 | * |
| 636 | * @param $module |
| 637 | * The module that owns the hook. |
| 638 | * @param $identifier |
| 639 | * Either the module or 'wysiwyg_' . $file->name |
| 640 | * @param $hook |
| 641 | * The name of the hook being invoked. |
| 642 | */ |
| 643 | function _wysiwyg_process_include($module, $identifier, $path, $hook) { |
| 644 | $function = $identifier . '_' . $hook; |
| 645 | if (!function_exists($function)) { |
| 646 | return NULL; |
| 647 | } |
| 648 | $result = $function(); |
| 649 | if (!isset($result) || !is_array($result)) { |
| 650 | return NULL; |
| 651 | } |
| 652 | |
| 653 | // Fill in defaults. |
| 654 | foreach ($result as $editor => $properties) { |
| 655 | $result[$editor]['module'] = $module; |
| 656 | $result[$editor]['name'] = $editor; |
| 657 | $result[$editor]['path'] = $path; |
| 658 | } |
| 659 | return $result; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * @} End of "defgroup wysiwyg_api". |
| 664 | */ |
| 665 | |