<?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' => 'title', 
			'First Name' => 'first_name',
			'Middle Name' => 'middle_name',
			'Last Name' => 'last_name',
			'Suffix' => 'suffix',
			'Company' => 'organizations',  //objectclass: organization
			'Department' => 'departament', //objectclass: organizationalPerson
			'Job Title' => 'title', //objectclass: organizationalPerson
			'Business Street' => 'addr_one_add1',
			'Business Street 2' => 'addr_one_add2',
			'Business Street 3' => 'addr_one_add3',
			'Business City' => 'addr_one_city',
			'Business State' => 'addr_one_state',
			'Business Postal Code' => 'addr_one_postal_code',
			'Business Country' => 'addr_one_country',
			'Home Street' => 'addr_two_add1',
			'Home City' => 'addr_two_city',
			'Home State' => 'addr_two_state',
			'Home Postal Code' => 'addr_two_postal_code',
			'Home Country' => 'addr_two_country',
			'Home Street 2' => 'addr_two_add2',
			'Home Street 3' => 'addr_two_add3',
			'Other Street' => 'addr_three_add1',
			'Other City' => 'addr_three_add2',
			'Other State' => 'addr_three_add3',
			'Other Postal Code' => 'addr_three_postal_code',
			'Other Country' => 'addr_three_country',
			"Assistant's Phone" => 'comm_phone_msg',
			'Business Fax' => 'comm_fax_work',
			'Business Phone' => 'comm_phone_work',
			'Business Phone 2' => 'comm_phone_work_two',
			'Callback' => '',
			'Car Phone' => 'comm_mobile_car',
			'Company Main Phone' => 'org_comm_phone_work',
			'Home Fax' => 'comm_fax_home',
			'Home Phone' => 'comm_phone_home',
			'Home Phone 2' => 'comm_phone_home_two', //This will make another homePhone entry
			'ISDN' => 'comm_phone_isdn',
			'Mobile Phone' => 'come_mobile_cell', //newPilotPerson
			'Other Fax' => 'com_fax_other',
			'Other Phone' => 'comm_phone_other_two',
			'Pager' => 'comm_phone_pager',
			'Primary Phone' => '',
			'Radio Phone' => '',
			'TTY/TDD Phone' => '',
			'Telex' => '', //organization
			'Account' => '',
			'Anniversary' => '',
			"Assistant's Name" => '', //newPilotPerson
			'Billing Information' => '',
			'Birthday' => 'birthday',
			'Categories' => 'categories_list', 
			'Children' => '',
			'Directory Server' => '',
			'E-mail Address' => 'comm_email_work',
			'E-mail Display Name' => 'Display name',
			'E-mail 2 Address' => 'comm_email_home',
			'E-mail 2 Display Name' => '',
			'E-mail 3 Address' => 'comm_email_other', //add another...
			'E-mail 3 Display Name' => '',
			'Gender' => '',
			'Government ID Number' => '',
			'Hobby' => '',
			'Initials' => 'initials',
			'Internet Free Busy' => '',
			'Keywords' => '',
			'Language' => '',
			'Location' => '',
			"Manager's Name" => '',
			'Mileage' => '',
			'Notes' => 'note',
			'Office Location' => 'org_addr_one_add1',
			'Organizational ID Number' => '',
			'PO Box' => 'addr_four_add1',
			'Priority' => '',
			'Private Profession' => '',
			'Referred By' => '',
			'Sensitivity' => '',
			'Spouse' => '',
			'User 1' => '',
			'User 2' => '',
			'User 3' => '',
			'User 4' => '',
			'Web Page' => 'url'
		);

		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>'; 
			for ($i=1;$i<=count($buffer);$i++)
			{
				while ( list($name,$value) = @each($buffer[$i]) )
				{
					//echo '<br>'.$i.': '.$name.' => '.$value;
					$entry[$i][$name] = $value;
				}
				$entry[$i]['email_type']      = 'INTERNET';
				$entry[$i]['email_home_type'] = 'INTERNET';
				$entry[$i]['adr_one_type']    = 'intl';
				$entry[$i]['adr_two_type']    = 'intl';
				//echo '<br>';
				$contacts->add($GLOBALS['phpgw_info']['user']['account_id'],$entry[$i],$access,$cat_id);
			}
			$num = $i - 1;
			return lang('Successfully imported x records into your addressbook.',$num);
		}
	}
?>
