<?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.

  /* $Id: Import_from_Outlook_-_Italiano 18716 2005-07-03 14:15:23Z milosch $ */

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

		var $import = array(
			'Titolo' => 'n_prefix',
			'Nome' => 'n_given',
			'Secondo nome' => 'n_middle',
			'Cognome' => 'n_family',
			'Titolo straniero' => 'n_suffix',
			'Societ' => 'org_name',  //objectclass: organization
			'Reparto' => 'org_unit', //objectclass: organizationalPerson
			'Posizione' => 'title', //objectclass: organizationalPerson
			'Via (uff.)' => 'adr_one_street',
			'Via (uff.) 2' => 'address2',
			'Via (uff.) 3' => 'address3',
			'Citt (uff.)' => 'adr_one_locality',
			'Provincia (uff.)' => 'adr_one_region',
			'CAP (uff.)' => 'adr_one_postalcode',
			'Paese (uff.)' => 'adr_one_countryname',
			'Via (ab.)' => 'adr_two_street',
			'Citt (ab.)' => 'adr_two_locality',
			'Provincia (ab.)' => 'adr_two_region',
			'CAP (ab.)' => 'adr_two_postalcode',
			'Paese (ab.)' => 'adr_two_countryname',
			'Via (ab.) 2' => '',
			'Via (ab.) 3' => '',
			'Altra via' => '',
			'Altra via 2' => '',
			'Altra via 3' => '',
			'Altra citt' => '',
			'Altra provincia' => '',
			'Altro CAP' => '',
			'Altro paese' => '',
			"Telefono assistente" => 'tel_msg',
			'Fax (uff.)' => 'tel_fax',
			'Ufficio' => 'tel_work',
			'Ufficio 2' => 'ophone',
			'Richiamata automatica' => '',
			'Telefono auto' => 'tel_car',
			'Telefono principale societ' => '',
			'Fax (ab.)' => '',
			'Abitazione' => 'tel_home',
			'Abitazione 2' => '', //This will make another homePhone entry
			'ISDN' => 'tel_isdn',
			'Cellulare' => 'tel_cell', //newPilotPerson
			'Altro fax' => '',
			'Altro telefono' => '',
			'Cercapersone' => 'tel_pager',
			'Telefono principale' => '',
			'Radiotelefono' => '',
			'Telefono TTY/TDD' => '',
			'Telex' => '', //organization
			'Account' => '',
			'Anniversario' => '',
			'Nome assistente' => '', //newPilotPerson
			'Dati fatturazione' => '',
			'Compleanno' => 'bday',
			'Categorie' => '',
			'Figli' => '',
			'Server di elenchi in linea' => '',
			'Indirizzo posta elettronica' => 'email',
			'Nome visualizzato posta elettronica' => '',
			'Indirizzo posta elettronica 2' => 'email_home',
			'Nome visualizzato posta elettronica 2' => '',
			'Indirizzo posta elettronica 3' => '', //add another...
			'Nome visualizzato posta elettronica 3' => '',
			'Sesso' => '',
			'Cod. Fisc./P. IVA' => '',
			'Hobby' => '',
			'Iniziali' => '',
			'Disponibilit Internet' => '',
			'Parole chiave' => '',
			'Lingua' => '',
			'Luogo' => '',
			'Tipo posta elettronica' => '',
			'Tipo posta elettronica 2' => '',
			'Tipo posta elettronica 3' => '',
			'Privato' => '',
			'Sesso' => '',
			'Ubicazione ufficio' => '',
			'Indirizzo (ab.) - Casella postale' => '',
			'Nome manager' => '',
			'Indennit trasferta' => '',
			'Notes' => 'note',
			'Indirizzo (uff.) - Casella postale' => '',
			'Numero ID organizzativo' => '',
			'Altro indirizzo - Casella postale' => '',
			'Priorit' => '',
			'Professione' => '',
			'Presentato da' => '',
			'Riservatezza' => '',
			'Nome coniuge' => '',
			'Utente 1' => '',
			'Utente 2' => '',
			'Utente 3' => '',
			'Utente 4' => '',
			'Pagina Web' => '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';
				$entry[$i]['fn'] = $entry[$i]['n_given'] . ' ' . $entry[$i]['n_family'];
				//echo '<br>';
				$contacts->add($GLOBALS['egw_info']['user']['account_id'],$entry[$i],$access,$cat_id);
			}
			$num = $i - 1;
			return lang('Successfully imported %1 records into your addressbook.',$num);
		}
	}
?>
