<?php
// This file defines a set of functions and an associative array.
// The key of the array corresponds to a header in the source
// export file and the value of the array item will be used in
// the creation of the output file.
//
// 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 export_conv
	{
		var $currentrecord = array(); //used for buffering to allow uid lines to go first
		var $id;
		//list of all id's
		var $ids = array();
		var $type = 'pdb';

		var $export = array(
			'title'			=> 'Title',
			'first_name'		=> 'First',
			'middle_name'		=> 'Middle',
			'last_name'		=> 'Last',
			'suffix'		=> 'Suffix',
			'org_name'		=> 'Company',
			'department'		=> 'Dept',
			'work_add1'		=> 'Bus. Street',
			'work_add2'		=> 'Bus. St. 2',
			'work_add3'		=> 'Bus. St. 3',
			'work_city'		=> 'Bus. City',
			'work_state'		=> 'Bus. State',
			'work_postal_code'	=> 'Bus. Postal Code',
			'work_country'		=> 'Bus. Country',
			'home_add1'		=> 'Home Street',
			'home_city'		=> 'Home City',
			'home_state'		=> 'Home State',
			'home_postal_code'	=> 'Home Postal Code',
			'home_country'		=> 'Home Country',
			'tel_fax'		=> 'Bus. Fax',
			'tel_work'		=> 'Bus. Phone',
			'tel_msg'		=> "Assistant's Phone",
			'tel_car'		=> 'Car Phone',
			'tel_isdn'		=> 'ISDN',
			'tel_home'		=> 'Home Phone',
			'tel_cell'		=> 'Mobile Phone',
			'tel_pager'		=> 'Pager',
			'ophone'		=> 'Bus. Phone2',
			'bday'			=> 'Birthday',
			'email'			=> 'Email Addr',
			'email_home'		=> 'Email Addr2',
			'url'			=> 'URL',
			'note'			=> 'Notes'
		);

		// This will store the contacts object
		var $contacts = '';

		// Read full list of user's contacts only to get id's for each
		function export_start_file($buffer,$ncat_id='')
		{
			$this->contacts = CreateObject('phpgwapi.contacts');

			$this->id=-1;
			$fields = array('person_id');

			if ($ncat_id)
			{
				$criteria = $this->contacts->criteria_for_index($GLOBALS['phpgw_info']['user']['account_id'], PHPGW_CONTACTS_ALL, $ncat_id);
				$tmp_person = $this->contacts->get_persons($fields, '', '', '', '', '', $criteria);
			}
			else
			{
				$criteria = $this->contacts->criteria_for_index($GLOBALS['phpgw_info']['user']['account_id'], PHPGW_CONTACTS_ALL);
				$tmp_person = $this->contacts->get_persons($fields, '', '', '', '', '', $criteria);
			}

			if(is_array($tmp_person))
			{
				foreach($tmp_person as $data)
				{
					$this->ids[] = $data['person_id'];
				}
			}

			// $ids is now an array of all id's for this user, e.g. $ids[0] = 21, etc...
			// $buffer is still empty
			return $buffer;
		}

		// Read each entry
		function export_start_record($buffer)
		{
			$this->id++;

			$top = $this->contacts->person_complete_data($this->ids[$this->id]);
			
			if(is_array($top['locations']))
			{
				foreach($top['locations'] as $key => $values)
				{
					if($values['type']=='work')
					{
						$work_type = $key;
					}
					elseif($values['type']=='home')
					{
						$home_type = $key;
					}
				}
			}

			if(is_array($top['notes']))
			{
				$note_id = key($top['notes']);
			}

			$record['title'] 		= $top['title'];
			$record['first_name'] 		= $top['first_name'];
			$record['middle_name'] 		= $top['middle_name'];
			$record['last_name'] 		= $top['last_name'];
			$record['suffix'] 		= $top['suffix'];
			$record['org_name'] 		= $top['org_name'];
			$record['department'] 		= $top['department'];
			$record['work_add1'] 		= $top['locations'][$work_type]['add1'];
			$record['work_add2'] 		= $top['locations'][$work_type]['add2'];
			$record['work_add3'] 		= $top['locations'][$work_type]['add3'];
			$record['work_city'] 		= $top['locations'][$work_type]['city'];
			$record['work_state'] 		= $top['locations'][$work_type]['state'];
			$record['work_postal_code']	= $top['locations'][$work_type]['postal_code'];
			$record['work_country'] 	= $top['locations'][$work_type]['country'];
			$record['home_add1'] 		= $top['locations'][$home_type]['add1'];
			$record['home_city'] 		= $top['locations'][$home_type]['city'];
			$record['home_state'] 		= $top['locations'][$home_type]['state'];
			$record['home_postal_code'] 	= $top['locations'][$home_type]['postal_code'];
			$record['home_country'] 	= $top['locations'][$home_type]['country'];
			$record['tel_fax'] 		= $top['comm_media']['work fax'];
			$record['tel_work'] 		= $top['comm_media']['work phone'];
			$record['tel_msg'] 		= $top['comm_media']['msg phone'];
			$record['tel_car'] 		= $top['comm_media']['car phone'];
			$record['tel_isdn'] 		= $top['comm_media']['isdn'];
			$record['tel_home'] 		= $top['comm_media']['home phone'];
			$record['tel_cell'] 		= $top['comm_media']['mobile (cell) phone'];
			$record['tel_pager'] 		= $top['comm_media']['pager'];
			$record['ophone'] 		= $top['comm_media']['voice phone'];
			$record['bday'] 		= $top['comm_media']['birthday'];
			$record['email'] 		= $top['comm_media']['work email'];
			$record['email_home'] 		= $top['comm_media']['home email'];
			$record['url'] 			= $top['comm_media']['website'];
			$record['note'] 		= $top['notes'][$note_id]['text'];

			$this->currentrecord = $record;
			return $buffer;
		}

		// Read each attribute, populate buffer array
		// name/value are the fields from the export array above
		function export_new_attrib($buffer,$name,$value)
		{
			if ($this->export[$name])
			{
				$buffer[$this->id][$this->export[$name]] = $value;
				//echo '<br />'.$this->id.' - '.$this->export[$name].': '.$buffer[$this->id][$this->export[$name]];
			}
			return $buffer;
		}

		// Tack on some extra values - none for this file
		function export_end_record($buffer)
		{
			return $buffer;
		}

		// Parse it all into a string
		function export_end_file($buffer)
		{
			reset($this->ids);

			for ($i=0;$i<count($this->ids);$i++)
			{
				$j = $i + 1;
				reset($this->export);
				$entries .= "#" . $j . ":" . $buffer[$i]['n_given'] . $buffer[$i]['n_family'] . "\r\n";
				while (list($name,$value)=each($this->export))
				{
					$entries .= $value . ":\t" . $buffer[$i][$value] . "\n";
				}
				$entries .= "\r\n";
			}

			$buffer = $entries;
			$pdb =  CreateObject('addressbook.pdb');
			$pdb->fetch($buffer, 'phpgw Contacts', 'phpgw.pdb');
			return $buffer;
		}
	}
?>
