<?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_-_Hungary 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(
			'Cm' => 'n_prefix',
			'Utnv' => 'n_given',
			'Kzps' => 'n_middle',
			'Vezetknv' => 'n_family',
			'Uttag' => 'n_suffix',
			'Cg' => 'org_name',  //objectclass: organization
			'Osztly' => 'org_unit', //objectclass: organizationalPerson
			'Beoszts' => 'title', //objectclass: organizationalPerson
			'Mukahely cme' => 'adr_one_street',
			'2. vllalati utcacm' => 'address2',
			'3. vllalati utcacm' => 'address3',
			'Vros (hivatal)' => 'adr_one_locality',
			'Megye (hivatal)' => 'adr_one_region',
			'Munkahely irnytszm' => 'adr_one_postalcode',
			'Orszg (hivatal)' => 'adr_one_countryname',
			'Lakcm' => 'adr_two_street',
			'Vros (laks)' => 'adr_two_locality',
			'Megye (laks)' => 'adr_two_region',
			'Irnytszm (laks)' => 'adr_two_postalcode',
			'Orszg (laks)' => 'adr_two_countryname',
			'2. otthoni utcacm' => '',
			'3. otthoni utcacm' => '',
			'Ms utcacm' => '',
			'Ms vros' => '',
			'Ms llam' => '',
			'Ms irnytszm' => '',
			'Ms orszg' => '',
			"Titkr telefonszma" => 'tel_msg',
			'Hivatali fax' => 'tel_fax',
			'Hivatali telefon' => 'tel_work',
			'Msik hivatali telefon' => 'ophone',
			'Visszahvs' => '',
			'Auttelefon' => 'tel_car',
			'Cg fvonala' => '',
			'Otthoni fax' => '',
			'Otthoni telefon' => 'tel_home',
			'Msik otthoni telefon' => '', //This will make another homePhone entry
			'ISDN' => 'tel_isdn',
			'Mobiltelefon' => 'tel_cell', //newPilotPerson
			'Egyb fax' => '',
			'Egyb telefon' => '',
			'Szemlyhv' => 'tel_pager',
			'Elsdleges telefon' => '',
			'Rditelefon' => '',
			'TTY/TDD telefon' => '',
			'Telex' => '', //organization
			'Cmkiszolgl' => '',
			'vfordul' => '',
			"Titkr neve" => '', //newPilotPerson
			'Szmlaadatok' => '',
			'Szletsnap' => 'bday',
			'Kategrik' => '',
			'Gyerekek' => '',
			'Cmkiszolgl' => '',
			'Elektronikus levlcm' => 'email',
			'Elektronikus levlhez megjelentend nv' => '',
			'2. elektronikus levlcm' => 'email_home',
			'2. elektronikus levlhez megjelentend nv' => '',
			'3. elektronikus levlcm' => '', //add another...
			'3. elektronikus levlhez megjelentend nv' => '',
			'Nem' => '',
			'Kormnyzati azonost' => '',
			'Hobbi' => '',
			'Monogram' => '',
			'Elfoglaltsg kzzttele az Interneten' => '',
			'Kulcsszavak' => '',
			'Nyelv' => '',
			'Hely' => '',
			"Felettes neve" => '',
			'Tvolsg' => '',
			'Feljegyzsek' => 'note',
			'Iroda helye' => '',
			'Szervezeti azonost' => '',
			'Egyb cm, postafik' => '',
			'Priorits' => '',
			'Magnjelleg' => '',
			'Referencia' => '',
			'Sensitivity' => '',
			'Hzastrs' => '',
			'Felhasznli 1' => '',
			'Felhasznli 2' => '',
			'Felhasznli 3' => '',
			'Felhasznli 4' => '',
			'Weblap' => '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_family'] . ' ' . $entry[$i]['n_given'];
				//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);
		}
	}
?>
