<?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.
	include_once(PHPGW_INCLUDE_ROOT.'/addressbook/inc/class.addressbook_importer.inc.php');
	class import_conv extends addressbook_importer
	{
		var $currentrecord = array(); //used for buffering to allow uid lines to go first
		var $id;
		var $type = 'csv';

		var $import = array(
			'Title' 		=> array('person', 'title'), 
			'First Name' 		=> array('person', 'first_name'),
			'Middle Name' 		=> array('person', 'middle_name'),
			'Last Name' 		=> array('person', 'last_name'),
			'Suffix' 		=> array('person', 'suffix'),
			'Company' 		=> '',  //objectclass: organization
			'Department' 		=> array('person', 'department'), //objectclass: organizationalPerson
			'Job Title' 		=> array('person', 'title'), //objectclass: organizationalPerson
			'Business Street'	=> array('location', 'add1', 'work'),
			'Business Street 2' 	=> array('location', 'add2', 'work'),
			'Business Street 3' 	=> array('location', 'add3', 'work'),
			'Business City' 	=> array('location', 'city', 'work'),
			'Business State' 	=> array('location', 'state', 'work'),
			'Business Postal Code' 	=> array('location', 'postal_code', 'work'),
			'Business Country' 	=> array('location', 'country', 'work'),
			'Home Street' 		=> array('location', 'add1', 'home'),
			'Home City' 		=> array('location', 'city', 'home'),
			'Home State' 		=> array('location', 'state', 'home'),
			'Home Postal Code' 	=> array('location', 'postal_code', 'home'),
			'Home Country' 		=> array('location', 'country', 'home'),
			'Home Street 2' 	=> array('location', 'add2', 'home'),
			'Home Street 3' 	=> array('location', 'add3', 'home'),
			'Other Street' 		=> '',
			'Other City' 		=> '',
			'Other State' 		=> '',
			'Other Postal Code' 	=> '',
			'Other Country' 	=> '',
			"Assistant's Phone" 	=> array('comms', 'msg phone'),
			'Business Fax' 		=> array('comms', 'work fax'),
			'Business Phone' 	=> array('comms', 'work phone'),
			'Business Phone 2' 	=> array('comms', 'bbs'),
			'Callback' 		=> '',
			'Car Phone' 		=> array('comms', 'car phone'),
			'Company Main Phone' 	=> '',
			'Home Fax' 		=> array('comms', 'home fax'),
			'Home Phone' 		=> array('comms', 'home phone'),
			'Home Phone 2' 		=> array('comms', 'modem'), //This will make another homePhone entry
			'ISDN' 			=> array('comms', 'isdn'),
			'Mobile Phone' 		=> array('comms', 'mobile (cell) phone'), //newPilotPerson
			'Other Fax' 		=> array('comms', 'work fax'),
			'Other Phone' 		=> array('comms', 'video'),
			'Pager' 		=> array('comms', 'pager'),
			'Primary Phone' 	=> '',
			'Radio Phone' 		=> '',
			'TTY/TDD Phone' 	=> '',
			'Telex' 		=> '', //organization
			'Account' 		=> '',
			'Anniversary' 		=> '',
			"Assistant's Name" 	=> '', //newPilotPerson
			'Billing Information' 	=> '',
			'Birthday' 		=> array('person', 'birthday'),
			'Categories' 		=> array('person', 'categories'), 
			'Children' 		=> '',
			'Directory Server' 	=> '',
			'E-mail Address' 	=> array('comms', 'work email'),
			'E-mail Display Name' 	=> '',
			'E-mail 2 Address' 	=> array('comms', 'home email'),
			'E-mail 2 Display Name' => '',
			'E-mail 3 Address' 	=> '', //add another...
			'E-mail 3 Display Name' => '',
			'Gender' 		=> '',
			'Government ID Number' 	=> '',
			'Hobby' 		=> '',
			'Initials' 		=> array('person', 'initials'),
			'Internet Free Busy' 	=> '',
			'Keywords' 		=> '',
			'Language' 		=> '',
			'Location' 		=> '',
			"Manager's Name" 	=> '',
			'Mileage' 		=> '',
			'Notes' 		=> array('notes', 'note', 'general'),
			'Office Location' 	=> '',
			'Organizational ID Number' => '',
			'PO Box' 		=> '',
			'Priority' 		=> '',
			'Private Profession' 	=> '',
			'Referred By' 		=> '',
			'Sensitivity' 		=> '',
			'Spouse' 		=> '',
			'User 1' 		=> '',
			'User 2' 		=> '',
			'User 3' 		=> '',
			'User 4' 		=> '',
			'Web Page' 		=> array('comms', 'website')
		);

		function import_start_file($buffer)
		{
		}

		function import_start_record($buffer)
		{
		}

		function import_new_attrib($buffer,$name,$value)
		{
			$value = trim($value);
			$value = str_replace('\n','<br />',$value);
			$value = str_replace('\r','',$value);

			$method = $name[0];
			$element = $name[1];
			$args = $name[2];
			
			$this->$method($element, $value, $args);
		}

		function import_end_record($buffer)
		{
			$this->record_set[] = $this->record;
			$this->record = array();
		}

		function import_end_file($buffer,$access='private',$cat_id=0)
		{
 			$contacts = CreateObject('phpgwapi.contacts');

			$num = count($this->record_set);
			foreach($this->record_set as $contact)
			{
				$contact['categories'] = array($cat_id);
				$contact['access'] = $access;
				$contacts->contact_import($contact);
			}
			return lang('Successfully imported %1 records into your addressbook.',$num);
		}
	}
?>
