You are here

function fckeditor_process_textarea in FCKeditor - WYSIWYG HTML editor 5

Same name and namespace in other branches
  1. 5.2 fckeditor.module \fckeditor_process_textarea()
  2. 6.2 fckeditor.module \fckeditor_process_textarea()
  3. 6 fckeditor.module \fckeditor_process_textarea()

This function create the HTML objects required for the FCKeditor

Parameters

$element: A fully populated form elment to add the editor to

Return value

The same $element with extra FCKeditor markup and initialization

File

./fckeditor.module, line 302
FCKeditor Module for Drupal 5.x

Code

function fckeditor_process_textarea($element) {
  $exclude = preg_split("/[\\s,]+/", strip_tags(variable_get("fckeditor_exclude", '')));
  $toggle = variable_get('fckeditor_exclude_toggle', '0');
  switch ($toggle) {
    case 1:

      // normal inclusion based on the id
      $enabled = fckeditor_idsearch($element['#id'], $exclude);
      break;
    case 2:

      // Path exclusion
      $enabled = true;

      // defaults to enabled
      // This bizarre bit of magic courtesy of block.module
      $path = drupal_get_path_alias($_GET['q']);
      $regexp = '/^(' . preg_replace(array(
        '/(\\r\\n?|\\n)/',
        '/\\\\\\*/',
        '/(^|\\|)\\\\<front\\\\>($|\\|)/',
      ), array(
        '|',
        '.*',
        '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
      ), preg_quote(variable_get("fckeditor_exclude", ''), '/')) . ')$/';
      if (preg_match($regexp, $path)) {
        $enabled = false;

        // but if in excluded paths disable it again
      }
      break;
    case 3:

      // Path inclusion
      // This bizarre bit of magic courtesy of block.module
      $path = drupal_get_path_alias($_GET['q']);
      $regexp = '/^(' . preg_replace(array(
        '/(\\r\\n?|\\n)/',
        '/\\\\\\*/',
        '/(^|\\|)\\\\<front\\\\>($|\\|)/',
      ), array(
        '|',
        '.*',
        '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
      ), preg_quote(variable_get("fckeditor_exclude", ''), '/')) . ')$/';
      if (preg_match($regexp, $path)) {
        $enabled = true;
      }
      break;
    case 0:
    default:

      // normal exclusion based on the id
      $enabled = !fckeditor_idsearch($element['#id'], $exclude);
      break;
  }
  if ($element['#rows'] > variable_get('fckeditor_minimum_rows', 5) && $enabled) {

    // only replace textarea when it has enough rows and it is enabled
    // setting some variables
    $module_drupal_path = drupal_get_path('module', 'fckeditor');
    $module_full_path = base_path() . $module_drupal_path;

    // get the default drupal files path
    $files_path = base_path() . file_directory_path();

    // '-' in a javascript id will not work
    $js_id = 'oFCKeditor_' . str_replace('-', '_', $element['#id']);

    // configured in settings
    $width = variable_get("fckeditor_width", '100%');

    // sensible default for small toolbars
    $height = $element['#rows'] * 14 + 100;

    // nessecary because FCKeditor interferes with resize script
    $element['#resizable'] = FALSE;
    drupal_add_js($module_drupal_path . '/fckeditor/fckeditor.js');
    if (user_access('use advanced fckeditor')) {
      $toolbar = variable_get("fckeditor_advanced_toolbar", 'DrupalFull');
      $height += 100;

      // sensible default for admin toolbars
    }
    else {
      $toolbar = variable_get("fckeditor_default_toolbar", 'DrupalBasic');
    }
    $element['#suffix'] .= "\n<script type=\"text/javascript\">\nvar " . $js_id . " = new FCKeditor( '" . $element['#id'] . "' );\n" . $js_id . ".BasePath\t= '" . $module_full_path . "/fckeditor/';\n" . $js_id . ".Config['CustomConfigurationsPath'] = '" . $module_full_path . "/fckeditor.config.js';\n" . $js_id . ".ToolbarSet = '" . $toolbar . "';\n" . $js_id . ".Height = '" . $height . "';\n" . $js_id . ".Width = '" . $width . "';\n";

    // integrate IMCE if it exists and is prefered
    if (function_exists('imce_integrate') && variable_get('imce_settings_fck', 0)) {
      imce_integrate('fck');
      $advanced_uploads = 0;
      $basic_uploads = 0;
    }
    else {
      $advanced_uploads = variable_get("fckeditor_upload_advanced", '0');
      $basic_uploads = variable_get("fckeditor_upload_basic", '0');
    }

    // add code for filebrowser for users that have access
    if (user_access('allow fckeditor file uploads') == 1) {
      if ($advanced_uploads) {
        $element['#suffix'] .= $js_id . ".Config['LinkBrowserURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Connector=connectors/php/connector.php&ServerPath=" . $files_path . "';\n  " . $js_id . ".Config['ImageBrowserURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&ServerPath=" . $files_path . "';\n  " . $js_id . ".Config['FlashBrowserURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/php/connector.php&ServerPath=" . $files_path . "';\n";
      }
      else {
        $element['#suffix'] .= $js_id . ".Config['LinkBrowser'] = false;\n" . $js_id . ".Config['ImageBrowser'] = false;\n" . $js_id . ".Config['FlashBrowser'] = false;\n";
      }
      if ($basic_uploads) {
        $element['#suffix'] .= $js_id . ".Config['LinkUploadURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/upload/php/upload.php';\n  " . $js_id . ".Config['ImageUploadURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/upload/php/upload.php';\n  " . $js_id . ".Config['FlashUploadURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/upload/php/upload.php';\n";
      }
      else {
        $element['#suffix'] .= $js_id . ".Config['LinkUpload'] = false;\n" . $js_id . ".Config['ImageUpload'] = false;\n" . $js_id . ".Config['FlashUpload'] = false;\n";
      }
    }
    else {
      $element['#suffix'] .= $js_id . ".Config['LinkBrowser'] = false;\n" . $js_id . ".Config['ImageBrowser'] = false;\n" . $js_id . ".Config['FlashBrowser'] = false;\n" . $js_id . ".Config['LinkUpload'] = false;\n" . $js_id . ".Config['ImageUpload'] = false;\n" . $js_id . ".Config['FlashUpload'] = false;\n";
    }

    // add custom xml stylesheet if it exists
    $theme_path = path_to_theme();
    if (file_exists($theme_path . '/fckstyles.xml')) {
      $styles_xml_path = base_path() . $theme_path . '/fckstyles.xml';
      $element['#suffix'] .= $js_id . ".Config['StylesXmlPath'] = '" . $styles_xml_path . "';\n";
    }

    // add custom stylesheet if configured
    // lets hope it exists but we'll leave that to the site admin
    $custom_style = variable_get("fckeditor_stylesheet", "");
    if (!empty($custom_style) && is_string($custom_style)) {
      $element['#suffix'] .= $js_id . ".Config['EditorAreaCSS'] = '" . $custom_style . "';";
    }
    $element['#suffix'] .= "</script>\n";
    if (variable_get('fckeditor_popup', '0')) {

      // Add the script file with the popup open function.
      drupal_add_js($module_drupal_path . '/fckeditor.popup.js');
      $element['#suffix'] .= " <span class=\"fckeditor_popuplink\">(<a href=\"#\" onclick=\"FCKeditor_OpenPopup('" . $module_full_path . "/fckeditor.popup.html?var=" . $js_id . "&el=" . $element['#id'] . "');return false;\">" . t('Open rich editor') . "</a>)</span>";
    }
    else {

      // if no popup mode, add the editor initialization to the footer
      // this obviously needs print($closure) in page.tpl.php
      drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {' . $js_id . '.ReplaceTextarea();});}', 'inline', 'footer');
    }
  }

  // display the field id for administrators
  if (user_access('administer site configuration')) {
    $element['#suffix'] .= '<div class="textarea-identifier description">The ID for for <a href="' . url('admin/settings/fckeditor') . '">excluding or including</a> this element is: ' . $element['#id'] . ' - the path is: ' . $_GET['q'] . '</div>';
  }
  return $element;
}