<?php

/**
 * <pre>
 * Invision Power Services
 * WYSIWYG editor field type abstraction
 * Last Updated: $Date: 2009-08-23 21:54:28 -0400 (Sun, 23 Aug 2009) $
 * </pre>
 *
 * @author 		$Author: bfarber $
 * @copyright	(c) 2001 - 2009 Invision Power Services, Inc.
 * @license		http://www.invisionpower.com/community/board/license.html
 * @package		IP.Content
 * @link		http://www.invisionpower.com
 * @since		2nd Sept 2009
 * @version		$Revision: 67 $
 */

if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly.";
	exit();
}

class fields_editor
{
	/**#@+
	 * Registry objects
	 *
	 * @access	protected
	 * @var		object
	 */	
	protected $registry;
	protected $DB;
	protected $settings;
	protected $request;
	protected $lang;
	protected $member;
	protected $memberData;
	protected $caches;
	protected $cache;
	/**#@-*/
	
	/**
	 * Error string stored from last process
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $error		= '';
	
	/**
	 * Differences library
	 *
	 * @access	protected
	 * @var		object
	 */
	protected $differences;
	
	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	object		Registry
	 * @return	void
	 */
	public function __construct( ipsRegistry $registry )
	{
		/* Make object */
		$this->registry		= $registry;
		$this->DB			= $this->registry->DB();
		$this->settings		=& $this->registry->fetchSettings();
		$this->request		=& $this->registry->fetchRequest();
		$this->lang			= $this->registry->getClass('class_localization');
		$this->member		= $this->registry->member();
		$this->memberData	=& $this->registry->member()->fetchMemberData();
		$this->cache		= $this->registry->cache();
		$this->caches		=& $this->registry->cache()->fetchCaches();

		//-----------------------------------------
		// Set some global bbcode properties
		//-----------------------------------------
		
		IPSText::getTextClass('bbcode')->parse_nl2br		= 1;
		IPSText::getTextClass('bbcode')->parse_smilies		= 1;
		IPSText::getTextClass('bbcode')->parse_bbcode		= 1;
		IPSText::getTextClass('bbcode')->parsing_section	= 'global';
	}
	
	/**
	 * Return default field types
	 *
	 * @access	public
	 * @param	string		Table name
	 * @return	bool
	 */
	public function getTypes()
	{
		return array(
					array( 'editor', $this->lang->words['field_type__editor'] ),
					);
	}
	
	/**
	 * Get an ACP field
	 *
	 * @access	public
	 * @param	array		Field data
	 * @param	mixed		Default value
	 * @return	string		HTML field
	 */
	public function getAcpField( $field, $default='' )
	{
		$id			= $field['field_id'];
		$type		= $field['field_type'];
		$options	= $field['field_extra'];
		
		if( $type == 'editor' )
		{
			if( $default )
			{
				IPSText::getTextClass('bbcode')->parse_html			= $field['field_html'];

				if ( IPSText::getTextClass('editor')->method == 'rte' )
				{
					$default = IPSText::getTextClass('bbcode')->convertForRTE( $default );
				}
				else
				{
					$default = IPSText::getTextClass('bbcode')->preEditParse( $default );
				}
			}

			return IPSText::getTextClass('editor')->showEditor( $default, 'field_' . $id );
		}
		
		return '';
	}
	
	/**
	 * Get a public field
	 *
	 * @access	public
	 * @param	array		Field data
	 * @param	mixed		Default value
	 * @return	string		HTML field
	 */
	public function getPublicField( $field, $default='' )
	{
		$id			= $field['field_id'];
		$type		= $field['field_type'];
		$options	= $field['field_extra'];
		
		if( $type == 'editor' )
		{
			if( $default )
			{
				IPSText::getTextClass('bbcode')->parse_html			= $field['field_html'];

				if ( IPSText::getTextClass('editor')->method == 'rte' )
				{
					$default = IPSText::getTextClass('bbcode')->convertForRTE( $default );
				}
				else
				{
					$default = IPSText::getTextClass('bbcode')->preEditParse( $default );
				}
			}

			return IPSText::getTextClass('editor')->showEditor( $default, 'field_' . $id );
		}
		
		return '';
	}
	
	/**
	 * Get error, if set
	 *
	 * @access	public
	 * @return	mixed		Error lang key, or false
	 */
	public function getError()
	{
		return $this->error ? $this->error : false;
	}
	
	/**
	 * Process the input and return normalized value to store
	 *
	 * @access	public
	 * @param	array 		Field data
	 * @return	string		Data to store in DB
	 */
	public function processInput( $field )
	{
		$value	= '';
		
		if( $field['field_type'] == 'editor' )
		{
			$value	= IPSText::getTextClass( 'editor' )->processRawPost( 'field_' . $field['field_id'] );

			IPSText::getTextClass('bbcode')->parse_html			= $field['field_html'];
			
			$value	= IPSText::getTextClass('bbcode')->preDbParse( $value );
			$test	= IPSText::getTextClass('bbcode')->preDisplayParse( $value );
			
			if ( IPSText::getTextClass( 'bbcode' )->error != "" )
			{
				$this->error	= $this->lang->words[ IPSText::getTextClass( 'bbcode' )->error ];
			}
			
			if( $field['field_max_length'] AND strlen(trim($value)) > $field['field_max_length'] )
			{
				$this->error	= sprintf( $this->lang->words['dbfield_too_long'], $field['field_name'] );
			}
			
			if( $field['field_required'] AND !trim($value) )
			{
				$this->error	= sprintf( $this->lang->words['dbfield_required'], $field['field_name'] );
			}
			
			return $value;
		}

		return '';
	}
	
	/**
	 * Process input after data has been saved to database
	 *
	 * @access	public
	 * @param	array 		Field data
	 * @return	bool		Error or not
	 */
	public function postProcessInput( $field, $record_id=0 )
	{
		return false;
	}
	
	/**
	 * Record deletion callback
	 *
	 * @access	public
	 * @param	array 		Field data
	 * @param	array		Record data
	 * @return	bool		Handled successfully or not
	 */
	public function postProcessDelete( $field, $record )
	{
		return false;
	}
	
	/**
	 * Process the field and return a display value
	 *
	 * @access	public
	 * @param	array 		Field data
	 * @param	array		Record data
	 * @param	int			Number of characters to truncate at (0 means no truncating)
	 * @return	string		Data to display
	 */
	public function getFieldValue( $field, $record=array(), $truncate=0 )
	{
		$fieldValue	= $record['field_' . $field['field_id'] ];
		
		if( !$fieldValue )
		{
			return '';
		}
				
		if( $field['field_type'] == 'editor' )
		{
			if( $truncate )
			{
				if( IPSText::mbstrlen( strip_tags( $fieldValue ) ) > $truncate )
				{
					return IPSText::truncate( IPSText::getTextClass('bbcode')->stripAllTags( strip_tags( $fieldValue ) ), $truncate );
				}
				else
				{
					IPSText::getTextClass('bbcode')->parse_html			= $field['field_html'];

					return IPSText::getTextClass('bbcode')->preDisplayParse( $fieldValue );
				}
			}
			else
			{
				IPSText::getTextClass('bbcode')->parse_html			= $field['field_html'];

				return IPSText::getTextClass('bbcode')->preDisplayParse( $fieldValue );
			}
		}

		return '';
	}
	
	/**
	 * Produce where clause based on search string
	 *
	 * @access	public
	 * @param	array 		Field data
	 * @param	string		Supplid value
	 * @return	string		Data to display
	 */
	public function getSearchWhere( $field, $search='' )
	{
		if( !$search )
		{
			return '';
		}

		if( $field['field_type'] == 'editor' )
		{
			$search	= strtolower($search);
			return 'LOWER(field_' . $field['field_id'] . ") LIKE '%{$search}%'";
		}

		return '';
	}
	
	/**
	 * Get a true difference report
	 *
	 * @access	public
	 * @param	string		Current text
	 * @param	string		Previous text
	 * @param	string		HTML to display
	 */
	public function getDifferenceReport( $current, $previous )
	{
		if( !is_object($this->differences) )
		{
			//-----------------------------------------
			// Get Diff library
			//-----------------------------------------
			
			require_once( IPS_KERNEL_PATH . 'classDifference.php' );
			$this->differences         = new classDifference();
			$this->differences->method = 'PHP';
		}
		
		$result	= $this->differences->getDifferences( IPSText::br2nl( $previous ), IPSText::br2nl( $current ) );
		
		if( $this->differences->diff_found )
		{
			$result	= str_replace( "\n", "<br>", $result );
			$result	= str_replace( "&gt;&lt;", "&gt;\n&lt;", $result );
			$result	= preg_replace( "#(?<!(\<del|\<ins)) {1}(?!:style)#i", "&nbsp;", $result );
			$result	= str_replace( "\t", "&nbsp; &nbsp; ", $result );
			//$result	= str_replace( "&lt;br&nbsp;/&gt;", "<br />", $result );
		}
		else
		{
			$result	= $current;
		}
		
		return $result;
	}
	
	/**
	 * Compare two versions of a particular field
	 *
	 * @access	public
	 * @param	array 		Field data
	 * @param	string		Current data in the field
	 * @param	string		Previous data in the field
	 * @return	string		HTML revisions differences
	 */
	public function compareRevision( $field, $current, $previous )
	{
		if( $field['field_type'] == 'editor' )
		{
			return $this->getDifferenceReport( $current, $previous );
		}

		return '';
	}
}