<?php
// This file defines a set of functions and an associative array.
// The key of the array corresponds to a header in the source
// import file and the value of the array item will be used in
// the creation of the output file.
//
// An exported Outlook file looks like this:
//
// Title<tab>First Name<tab>Middle Name<tab>Last Name<tab>...
// <tab>Patrick<tab><tab>Walsh<tab>...
//
// Where the first line explains each optional field.  This is what
// will be looked up in the key.
//
// The array need not be in any order and any fields not defined will
// not be transferred.  If the val='+', the value will be appended to
// the previous field and any text after the '+' will be appended 
// before the value.  For example, the following would add a comma and
// a space between LastName and FirstName and store it in FullName:
//
//	array('LastName' => 'FullName','FirstName' => '+, ');
//
// Also start with a '#' symbol and a comma separated list will be
// turned into a number of the same entries.

	class import_conv
	{
		var $currentrecord = array(); //used for buffering to allow uid lines to go first
		var $id;
		var $type = 'csv';

		var $import = array(
			'ȥ' => 'title', 
			'' => 'n_given',
			'̾' => 'n_family',
			'ɾ' => 'n_suffix',
			'̾' => 'org_name',  //objectclass: organization
			'̾' => 'org_unit', //objectclass: organizationalPerson
			'̳' => 'adr_one_street',
			'̳λԶĮ¼' => 'adr_one_locality',
			'̳ƻܸ' => 'adr_one_region',
			'̳͹ֹ' => 'adr_one_postalcode',
			'̳ιޤϰ' => 'adr_one_countryname',
			'' => 'adr_two_street',
			'λԶĮ¼' => 'adr_two_locality',
			'ƻܸ' => 'adr_two_region',
			'͹ֹ' => 'adr_two_postalcode',
			'ޤϰ' => 'adr_two_countryname',
			'̳եå' => 'tel_fax',
			'ֹ̳' => 'tel_work',
			'ֹ' => 'tel_home',
			'' => 'tel_cell', //newPilotPerson
			'' => 'bday',
			'Żҥ᡼ ɥ쥹' => 'email',
			'ӥͥ Web ڡ' => 'url',
			'' => 'note'
		);

		function import_start_file($buffer)
		{
			return $buffer;
		}

		function import_start_record($buffer)
		{
			$top=array();
			++$this->id;
			$this->currentrecord = $top;
			return $buffer;
		}

		function import_new_attrib($buffer,$name,$value)
		{
			$value = trim($value);
			$value = str_replace('\n','<BR>',$value);
			$value = str_replace('\r','',$value);
			$this->currentrecord += array($name => $value);

			return $buffer;
		}

		function import_end_record($buffer)
		{
			$buffer[$this->id]='';
			while ( list($name, $value) = each($this->currentrecord))
			{
				$buffer[$this->id][$name] = $value;
				//echo '<br>'.$name.' => '.$value;
			}
			return $buffer;
		}

		function import_end_file($buffer,$access='private',$cat_id=0)
		{
			$contacts = CreateObject('phpgwapi.contacts');
			//echo '<br>';
			$log = '<table border="1" cellspacing="0" cellpadding="0" align="center">'."\n";
			for ($i=1;$i<=count($buffer);$i++)
			{
				$log .= "\t<tr>\n\t\t<td align=center>$i</td>\n\t\t<td>\n";
				$log .= "\t\t\t".'<table width="100%" border="0" cellpadding="0" cellspacing="0">'."\n";
				while ( list($name,$value) = @each($buffer[$i]) )
				{
					//echo '<br>'.$i.': '.$name.' => '.$value;
					$entry[$i][$name] = $value;
					$value = nl2br($value);
					$log .= "\t\t\t\t<tr>\n";
					$log .= "\t\t\t\t\t".'<td align="right" nowrap width="30%">'.$this->display_name($name)."</td>\n";
					$log .= "\t\t\t\t\t".'<td align="center" nowrap width="5%">'.'=>'."</td>\n";
					$log .= "\t\t\t\t\t".'<td nowrap width="65%">'.$value."</td>\n";
					$log .= "\t\t\t\t</tr>\n";
				}
				$log .= "\t\t\t</table>\n";
				$entry[$i]['email_type']      = 'INTERNET';
				$entry[$i]['email_home_type'] = 'INTERNET';
				$entry[$i]['adr_one_type']    = 'intl';
				$entry[$i]['adr_two_type']    = 'intl';
				$entry[$i]['fn'] = $entry[$i]['n_given'] . ' ' . $entry[$i]['n_family'];
				//echo '<br>';
				if (!isset($GLOBALS['HTTP_POST_VARS']['download']) || $GLOBALS['HTTP_POST_VARS']['download'] == '') 
					$contacts->add($GLOBALS['phpgw_info']['user']['account_id'],$entry[$i],$access,$cat_id);
				$log .= "\t\t\n\t\t</td>\n\t</tr>\n";
			}
			$log .= "</table>\n";
			$num = $i - 1;
			$log .= "<p align=center>\n".lang('Successfully imported %1 records into your addressbook.',$num)."</p>\n";
			return $log;
		}
		
		/* this cleans up the fieldnames for display */
		function display_name($column)
		{
			$abc = array(
				'fn'                  => 'full name',
				'sound'               => 'Sound',
				'org_name'            => 'company name',
				'org_unit'            => 'department',
				'title'               => 'title',
				'n_prefix'            => 'prefix',
				'n_given'             => 'first name',
				'n_middle'            => 'middle name',
				'n_family'            => 'last name',
				'n_suffix'            => 'suffix',
				'label'               => 'label',
				'adr_one_street'      => 'business street',
				'adr_one_locality'    => 'business city',
				'adr_one_region'      => 'business state',
				'adr_one_postalcode'  => 'business zip code',
				'adr_one_countryname' => 'business country',
				'adr_one_type'        => 'business address type',
				'adr_two_street'      => 'home street',
				'adr_two_locality'    => 'home city',
				'adr_two_region'      => 'home state',
				'adr_two_postalcode'  => 'home zip code',
				'adr_two_countryname' => 'home country',
				'adr_two_type'        => 'home address type',
				'tz'                  => 'time zone',
				'geo'                 => 'geo',
				'tel_work'            => 'business phone',
				'tel_home'            => 'home phone',
				'tel_voice'           => 'voice phone',
				'tel_msg'             => 'message phone',
				'tel_fax'             => 'fax',
				'tel_pager'           => 'pager',
				'tel_cell'            => 'mobile phone',
				'tel_bbs'             => 'bbs phone',
				'tel_modem'           => 'modem phone',
				'tel_isdn'            => 'isdn phone',
				'tel_car'             => 'car phone',
				'tel_video'           => 'video phone',
				'tel_prefer'          => 'preferred phone',
				'email'               => 'business email',
				'email_type'          => 'business email type',
				'email_home'          => 'home email',
				'email_home_type'     => 'home email type',
				'address2'            => 'address line 2',
				'address3'            => 'address line 3',
				'ophone'              => 'Other Phone',
				'bday'                => 'birthday',
				'url'                 => 'url',
				'pubkey'              => 'public key',
				'note'                => 'notes'
			);

			if ($abc[$column])
			{
				return lang($abc[$column]);
			}
			return;
		}
	}
?>
