Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
yui_editor.module 9.45 KiB
<?php
/**
 * @file
 * yui_editor Module for Drupal 5.x
 *
 * This module allows Drupal to replace textarea fields with YUI editor.
 */


/*
 * Implementation of hook_menu()
 */
function yui_editor_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/yui_editor',
      'title' => t('YUI editor settings'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'yui_editor_settings_form',
      'access' => user_access('administer site configuration'),
      'description' => t("View/Modify YUI editor settings"));
    $items[] = array(
      'path' => 'yui_editor/image_upload',
      'callback' => 'yui_editor_image_upload',
      'access' => user_access('upload files'),
      'type' => MENU_CALLBACK
    );
  }

  return $items;
}


/*
 * implementation of hook_perm().
 */
function yui_editor_perm() {
  $array = array('Access YUI editor');

  return $array;
}


/*
 *The settings page.
 */
function yui_editor_settings_form() {
  $form['yui_editor_include'] = array(
    '#type' => 'textarea',
    '#title' => t('Paths'),
    '#description' => t('Define where should YUI replace the body textarea, each path on a new line. You can use wildcards (*). (Leave blank for every form)'),
    '#default_value' => variable_get('yui_editor_include', ''),
    '#rows' => 5,
    '#cols' => 60);
  $form['yui_editor_ids'] = array(
    '#type' => 'textarea',
    '#title' => t('IDs'),
    '#description' => t('Define which textareas do you want the editor to replace, write the ID of each textarea on a seperate line. (if empty the editor will only replace the body textarea)'),
    '#default_value' => variable_get('yui_editor_ids', ''),
    '#rows' => 5,
    '#cols' => 20);
  $form['yui_editor_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => variable_get('yui_editor_title', ''),
    '#description' => t('The title to give the editor'));
  $form['yui_editor_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#default_value' => variable_get('yui_editor_height', '250px'),
    '#description' => t('The height of the editor (i.e. 250px)'));
  $form['yui_editor_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#default_value' => variable_get('yui_editor_width', '100%'),
    '#description' => t('The width of the editor (i.e. 100%)'));
  $form['yui_editor_markup'] = array(
    '#type' => 'textfield',
    '#title' => t('Markup'),
    '#default_value' => variable_get('yui_editor_markup', 'xhtml'),
    '#description' => t('Set the YUI editor markup'));
  $form['yui_editor_button_profile'] = array(
    '#type' => 'select',
    '#title' => t('Button profile'),
    '#options' => array('yui_editor_toolbar_default.js' => 'Default', 'yui_editor_toolbar_no_style.js' => 'No Style Tags'),
    '#default_value' => variable_get('yui_editor_button_profile', 'yui_editor_toolbar_default.js'),
    '#description' => t('Select the appropriate button profile'));
  $form['yui_editor_titlebar'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show titlebar'),
    '#default_value' => variable_get('yui_editor_titlebar', 1),
    '#description' => t('Toggle the YUI editor titlebar'));
  $form['yui_editor_img_upload'] = array(
    '#type' => 'checkbox',
    '#title' => t('Image upload'),
    '#default_value' => variable_get('yui_editor_img_upload', 1),
    '#description' => t('Allow users to upload images directly from the editor for insertion into the editor. (Note: Users must also have \'upload files\' permission set for this functionality to be made available.'));
  $form['yui_editor_resize'] = array(
    '#type' => 'checkbox',
    '#title' => t('Editor resize'),
    '#default_value' => variable_get('yui_editor_resize', 0),
    '#description' => t('Allow user to resize the editor'));
  $form['yui_editor_plaintext'] = array(
    '#type' => 'checkbox',
    '#title' => t('Plaintext button'),
    '#default_value' => variable_get('yui_editor_plaintext', 0),
    '#description' => t('Allow user to switch between plain text area and YUI editor'));
  $form['yui_editor_coder'] = array(
    '#type' => 'checkbox',
    '#title' => t('Coder button'),
    '#default_value' => variable_get('yui_editor_coder', 0),
    '#description' => t('Add the code viewing button to the toolbar. Note: This does not work with the Plaintext button. Choose one of them only.'));
  $form['yui_editor_collapse'] = array(
    '#type' => 'checkbox',
    '#title' => t('Collapsible titlebar'),
    '#default_value' => variable_get('yui_editor_collapse', 0),
    '#description' => t('Allow user to collapse the titlebar'));
  $form['yui_editor_draggable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Draggable titlebar'),
    '#default_value' => variable_get('yui_editor_draggable', 0),
    '#description' => t('Allow user to drag the titlebar'));
  $form['yui_editor_button_type'] = array(
    '#type' => 'select',
    '#title' => t('Button type'),
    '#options' => array('simple' => 'Simple', 'advanced' => 'Advanced'),
    '#default_value' => variable_get('yui_editor_button_type', 'simple'),
    '#description' => t('Select the appropriate button profile'));
  $form['yui_editor_dom'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show DOM'),
    '#default_value' => variable_get('yui_editor_dom', 1),
    '#description' => t('Toggle the YUI editor DOM display (at the bottom of the editor)'));
  return system_settings_form($form);
}

/*
 * Implementation of hook_form_alter()
 */
function yui_editor_form_alter($form_id, &$form) {
  static $called;

  if ($called == TRUE) {
    return;
  }
  $called = TRUE;

  //  Thank you 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("yui_editor_include", ''), '/')) .')$/';
  $yui_editor_include = variable_get("yui_editor_include", "");
  if(preg_match($regexp, $path) or empty($yui_editor_include)) {
    $enabled = TRUE;
  }
  if (!$enabled or !user_access('Access YUI editor')) {
    return;
  }

  $yui_source = variable_get('yui_source','http://yui.yahooapis.com/2.5.1');
  yui_add_css('editor', $yui_source, '/build/menu/assets/skins/sam/menu.css');
  yui_add_css('editor', $yui_source, '/build/button/assets/skins/sam/button.css');
  yui_add_css('editor', $yui_source, '/build/fonts/fonts-min.css');
  yui_add_css('editor', $yui_source, '/build/container/assets/skins/sam/container.css');
  yui_add_css('editor', $yui_source, '/build/editor/assets/skins/sam/editor.css');
  yui_add_css('editor', $yui_source, '/build/resize/assets/skins/sam/resize.css');
  yui_add_js('editor', $yui_source, '/build/yahoo-dom-event/yahoo-dom-event.js');
  yui_add_js('editor', $yui_source, '/build/utilities/utilities.js');
  yui_add_js('editor', $yui_source, '/build/animation/animation-min.js');
  yui_add_js('editor', $yui_source, '/build/element/element-beta-min.js');
  yui_add_js('editor', $yui_source, '/build/container/container-min.js');
  yui_add_js('editor', $yui_source, '/build/menu/menu-min.js');
  yui_add_js('editor', $yui_source, '/build/button/button-min.js');
  yui_add_js('editor', $yui_source, '/build/resize/resize-beta-min.js');
  yui_add_js('editor', $yui_source, '/build/editor/editor-beta-min.js');

  drupal_add_css(drupal_get_path("module", "yui_editor")."/yui_editor.css");
  drupal_add_js(drupal_get_path("module", "yui_editor")."/yui_editor.js");
  drupal_add_js(drupal_get_path("module", "yui_editor")."/toolbars/".variable_get('yui_editor_button_profile', 'yui_editor_toolbar_default.js'));

  $ids = variable_get('yui_editor_ids', '');
  $textareas = array();
  if (!empty($ids)) {
    $textareas = explode("\r\n", $ids);
  }
  else if (! in_array('edit-body', $textareas)) {
    $textareas[] = 'edit-body';
  }

  foreach($textareas as $id) {
    drupal_add_js("if (Drupal.jsEnabled) { $(document).ready(function () { render_editor('".$id."', {".
      "height: '".variable_get('yui_editor_height', '250px')."', ".
      "width: '".variable_get('yui_editor_width', '100%')."', ".
      "markup: '".variable_get('yui_editor_markup', 'xhtml')."', ".
      "resize: '".variable_get('yui_editor_resize', 0)."', ".
      "plaintext: ".variable_get('yui_editor_plaintext', 0).", ".
      "coder: ".variable_get('yui_editor_coder', 0).", ".
      "dom: ".variable_get('yui_editor_dom', 1).", ".
      "collapse: ".variable_get('yui_editor_collapse', 0).", ".
      "draggable: ".variable_get('yui_editor_draggable', 0).", ".
      "buttonType: '".variable_get('yui_editor_button_type', 'simple')."', ".
      "titlebar: ".variable_get('yui_editor_titlebar', 1).", ".
      "imgUpload: ".(user_access('upload files') ? variable_get('yui_editor_img_upload', 1) : 0).", ".
      "title: '".variable_get('yui_editor_title', '')."'".
      "})})}", "inline", "footer");
  }
}

/**
 * Menu-callback for JavaScript-based uploads.
 */
function yui_editor_image_upload() {
  header("content-type: text/html"); // the return type must be text/html
  $response = null;
  $path = file_directory_path();

  //Append trailing slash to path if not there 
  if (! (substr($path, -1) == '/')) {
    $path .= '/';
  }
  $path .= 'images';
  $file = file_save_upload('upload', $path, FILE_EXISTS_REPLACE);
  if (!$file) {
    $response->status = 'Error Reading Uploaded File.';
    print drupal_to_js($response);
    exit;
  }
  
  $response->status = 'UPLOADED';
  $response->image_url = $file->filepath;

  print drupal_to_js($response);
  exit;
}