<?php
/*
 * FCKeditor Module for Drupal 4.7
 * 
 * This module allows Drupal to replace textarea fields with FCKEditor.
 *
 * This HTML text editor brings to the web many of the powerful functionalities
 * of known desktop editors like Word. It's really  lightweight and doesn't
 * require any kind of installation on the client computer.
 *
 * ----------------------------------------------------------------------------
 *
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2006 Frederico Caldeira Knabben
 * 
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 * 
 * For further information visit:
 * 		http://www.fckeditor.net/
 *
 * ----------------------------------------------------------------------------
 * 
 * @version 1.1
 * @author  Frederico Caldeira Knabben (www.fckeditor.net)
 */

/**
 * Implementation of hook_help
 */
function fckeditor_help( $section = '' ) {
	$output = '';

	switch ( $section ) 
	{
		case 'admin/modules#description' :
			$output = t( 'Enables the usage FCKeditor (WYSIWYG editor) instead of plain text fields.' );
			break;
		
//		case 'admin/help#fckeditor' :
//			$output = t( 'FCKeditor Help' ) ;
//			break ;
	}

	return $output;
}

/**
 * Implementation of hook_perm
 */
function fckeditor_perm() {
  return array( 'use FCKeditor' );
}

/**
 * Implementation of hook_setting().
 */
function fckeditor_settings() {
	// Check if the user has administration access
	if ( !user_access('access administration pages') ) {
		return message_access();
	}

	$form['fckeditor_base_path'] = array(
		'#type' => 'textfield',
		'#title' => t( 'Base Path' ),
		'#description' => t( 'The URL of the FCKeditor installation directory' ),
		'#maxlength' => 255,
		'#required' => TRUE,
		'#default_value' => variable_get('fckeditor_base_path', '/' . fckeditor_basepath())
	);
	
	$form['fckeditor_popup'] = array(
		'#type' => 'radios',
		'#title' => t( 'Integration Mode' ),
		'#required' => TRUE,
		'#options' => array('1' => t('Open Popup Window'), '0' => t('Replace text fields')),
		'#default_value' => variable_get('fckeditor_popup', '1')
	);
	
	return $form;
}

/**
 * Implementation of hook_elements().
 */
function fckeditor_elements()  {
  $type = array();

	if ( user_access('use FCKeditor') ) 
	{    
		$type['textarea'] = array(
			'#process' => array( 'fckeditor_process_textarea' => array() )	// Set our custom field pre-processor.
		);
		
		// Disable the resize handle if replacing the textarea with the editor.
		if ( !variable_get('fckeditor_popup', '1') ) {
			$type['textarea']['#resizable'] = false ;
		}
	}

  return $type;
}

/**
 * Implementation of theme_textarea
 */
function fckeditor_process_textarea( $element ) {
	// Get some setup information.
	$modPath	= drupal_get_path('module', 'fckeditor');
	$basePath	= fckeditor_basepath();
	$popupMode	= variable_get('fckeditor_popup', '1');

	// Add the editor script file to the head.
	drupal_add_js(preg_replace('/^\//', '', $basePath) . 'fckeditor.js');

	// Today we repeat the same code for each textarea. We could do that in
	// other ways, but this implementation leaves doors opened to future
	// changes, like custom configurations for each textarea.

	// Let's give a different var name to each instance. This will be used in
	// the popup.
	$jsVar = 'oFCKeditor_' . str_replace('-', '_', $element['#id']);
 
	// Send the JavaScript code that defines the editor instance.
	$element[ '#suffix' ] .= <<<CODE
<script type="text/javascript">
	var {$jsVar} = new FCKeditor( '{$element['#id']}' ) ;
	{$jsVar}.BasePath	= '$basePath' ;
	{$jsVar}.Config['CustomConfigurationsPath'] = '/{$modPath}/fckeditor.config.js' ;
	{$jsVar}.ToolbarSet = 'Drupal' ;
</script>\n
CODE;

	if ( $popupMode ) {
		// Add the script file with the popup open function.
		drupal_add_js($modPath . '/fckeditor.popup.js');
		$element[ '#title' ] .= " <span class=\"fckeditor_popuplink\">(<a href=\"#\" onclick=\"FCKeditor_OpenPopup('/{$modPath}/fckeditor.popup.html?var={$jsVar}&el={$element['#id']}');return false;\">" . t('Open rich editor') . "</a>)</span>" ;
	} else {		
		$element[ '#suffix' ] .= <<<CODE
<script type="text/javascript">
	{$jsVar}.ReplaceTextarea() ;
</script>\n
CODE;
		// Leave this line or the above CODE block will break the PHP parser.
	}

	return $element;
}

/**
 * Gets the Base Path of the FCKeditor installation.
 */
function fckeditor_basepath() {
	$path = variable_get( 'fckeditor_base_path', false );
	
	if ( !$path ) {
		return '/' . drupal_get_path('module', 'fckeditor') . '/fckeditor/';
	}
	
	return $path;
}

?>