<?php

/**
 * <pre>
 * Invision Power Services
 * Member type-ahead lookup field
 * 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		18 Nov 2009
 * @version		$Revision: 67 $
 */

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

class fields_member
{
	/**#@+
	 * 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		= '';

	/**
	 * 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();
	}
	
	/**
	 * Return default field types
	 *
	 * @access	public
	 * @param	string		Table name
	 * @return	bool
	 */
	public function getTypes()
	{
		return array(
					array( 'member', $this->lang->words['field_type__member'] ),
					);
	}
	
	/**
	 * 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 == 'member' )
		{
			$return	= '';

			$return	.= $this->registry->output->formInput( 'field_' . $id, $default, 'field_' . $id, 30, 'text' ) . 
				"<script type='text/javascript'>
					document.observe('dom:loaded', function(){
						if( $('field_{$id}') )
						{
							var autoComplete = new ipb.Autocomplete( $('field_{$id}'), { multibox: false, url: acp.autocompleteUrl, templates: { wrap: acp.autocompleteWrap, item: acp.autocompleteItem } } );
						}
					});
				</script>";

			return $return;
		}
		
		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 == 'member' )
		{
			$return	= '';

			$return	.= "<input type='text' class='input_text' name='field_{$id}' id='field_{$id}' value='{$default}' />
				<script type='text/javascript'>
					document.observe('dom:loaded', function(){
						if( $('field_{$id}') )
						{
							var autoComplete = new ipb.Autocomplete( $('field_{$id}'), { multibox: false, url: ipb.vars['base_url'] + 'app=core&module=ajax&section=findnames&do=get-member-names&secure_key=' + ipb.vars['secure_hash'] + '&name=', templates: { wrap: ipb.templates['autocomplete_wrap'], item: ipb.templates['autocomplete_item'] } } );
						}
					});
				</script>";

			return $return;
		}
		
		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'] == 'member' )
		{
			$value	= trim( $this->request['field_' . $field['field_id'] ] );
			
			if( $field['field_required'] AND !trim($value) )
			{
				$this->error	= sprintf( $this->lang->words['dbfield_required'], $field['field_name'] );

				return $value;
			}
			
			//-----------------------------------------
			// Verify display name
			//-----------------------------------------
			
			$_check	= array();
			
			if( $value )
			{
				$_check	= IPSMember::load( $value, 'core', 'displayname' );
			}
			
			if( $field['field_required'] )
			{
				if( !$_check['member_id'] )
				{
					$this->error	= sprintf( $this->lang->words['dbfield_mem_invalid'], $field['field_name'] );
				}
	
				if( !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 )
	{
		return $record['field_' . $field['field_id'] ];
	}
	
	/**
	 * 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'] == 'member' )
		{
			if( $search )
			{
				return '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 )
	{
		return $this->getDifferenceReport( $current, $previous );
	}
}