<?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 = 'sql';
				
		var $export= array();

		// 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++;
			$fields = $this->contacts->person_complete_data($this->ids[$this->id]);

			$contact['contact_id']		= $fields['contact_id'];
			$contact['access']		= $fields['access'];
			$contact['owner']		= $fields['owner'];
			$contact['per_first_name']	= $fields['first_name'];
			$contact['per_last_name']	= $fields['last_name'];
			$contact['per_middle_name']	= $fields['middle_name'];
			$contact['per_suffix']		= $fields['suffix'];
			$contact['per_prefix']		= $fields['prefix'];
			$contact['per_birthday']	= $fields['birthday'];
			$contact['per_pubkey']		= $fields['pubkey'];
			$contact['per_title']		= $fields['title'];
			$contact['per_department']	= $fields['department'];
			$contact['per_initials']	= $fields['initials'];
			$contact['per_sound']		= $fields['sound'];
			$contact['per_active']		= $fields['active'];
			$contact['preferred_org']	= $fields['preferred_org'];
			$contact['preferred_address']	= $fields['preferred_address'];
			$contact['relations']		= $fields['organizations'];
			$contact['categories']		= $fields['categories'];
			$contact['access']		= $fields['access'];

			unset(	$fields['contact_id'],
				$fields['first_name'],
				$fields['last_name'],
				$fields['middle_name'],
				$fields['suffix'],
				$fields['prefix'],
				$fields['birthday'],
				$fields['pubkey'],
				$fields['title'],
				$fields['department'],
				$fields['initials'],
				$fields['sound'],
				$fields['active'],
				$fields['preferred_org'],
				$fields['preferred_address'],
				$fields['organizations'],
				$fields['categories'], 
				$fields['access'],
				$fields['full_name'],
				$fields['owner'],
				$fields['createon'],
				$fields['createby'],
				$fields['modon'],
				$fields['modby'],
				$fields['account_id'],
				$fields['org_name']);

			// Locations info
			if(is_array($fields['locations']))
			{
				foreach($fields['locations'] as $location_input)
					{
						// Go for a _good_ address type
						$addr_type = $this->contacts->search_location_type($location_input['type']);
						if(!empty($addr_type))
						{
							$location['addr_type'] = $addr_type;
						}
						else
						{
							$addr_type = $this->contacts->search_location_type('work');
							if(!empty($addr_type))
							{
								$location['addr_type'] = $addr_type;
							}
							else
							{
								//return PHPGW_CONTACTS_ERROR_LOCATION_TYPE_MISSING;
								return -1;
							}
						}
						$location['addr_add1']		= $location_input['add1'];
						$location['addr_add2']		= $location_input['add2'];
						$location['addr_add3']		= $location_input['add3'];
						$location['addr_city']		= $location_input['city'];
						$location['addr_state']		= $location_input['state'];
						$location['addr_postal_code']	= $location_input['postal_code'];
						$location['addr_country']	= $location_input['country'];
						$location['addr_preferred']	= $location_input['preferred'];
						$locations[]			= $location;
					}
			}
			unset($fields['locations']);

			// Notes
			if(is_array($fields['notes']))
			{
				foreach($fields['notes'] as $note_input)
					{
						$note_type = $this->contacts->search_note_type($note_input['type']);
						if(!empty($note_type))
						{
							$note['note_type'] = $note_type;
						}
						else
						{
							// FIXME: what is the default value for note_type?
							$note_type = $this->contacts->search_note_type('general');
							if(!empty($note_type))
							{
								$note['note_type'] = $note_type;
							}
							else
							{
								//return PHPGW_CONTACTS_ERROR_NOTE_TYPE_MISSING;
								return -1;
							}
						}
					}
				$note['note_text']	= $note_input['note'];
				$notes[]		= $note;
			}
			unset($fields['notes']);

			// Communcation media fields
			if(is_array($fields['comm_media']))
			{
				foreach($fields['comm_media'] as $description_input => $comm_input)
					{
						$description_id = $this->contacts->search_comm_descr($description_input);
						if(!empty($description_id))
						{
							$comm['comm_descr']	= $description_id;
							$comm['comm_data']	= $comm_input;
							$comm_media[]		= $comm;
						}
						else
						{
							// Promote to others
							$fields[$description_input] = $comm_input;
						}
					}
			}
			unset($fields['comm_media']);

			// Other fields
			$others=array();
			if(count($fields) > 0)
			{
				foreach($fields as $field_name => $field_value)
					{
						$other['other_name']	= $field_name;
						$other['other_value']	= $field_value;
						$other['other_owner']	= $GLOBALS['phpgw_info']['user']['account_id'];
						$others[]		= $other;
					}
			}

			$record = array('contact' => $contact,
					'comm_media' => $comm_media,
					'locations' => $locations,
					'categories' => $contact['categories'],
					'others' => $others,
					'relations' => $contact['relations'],
					'notes' => $notes);
			
			$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)
		{
			$buffer[$this->id][$name] = $value;
			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)
		{
			$type = 1;
 			for ($i=0;$i<count($this->ids);$i++)
 			{
				$contact_sql='';
				$person_sql='';
				$comm_sql='';
				$loc_sql='';
				$other_sql='';
				$notes_sql='';
				
				$cid = $buffer[$i]['contact']['contact_id'];

				$contact_table = CreateObject('phpgwapi.contact_central');
				$contact_sql = $contact_table->insert(array('contact_id' => $buffer[$i]['contact']['contact_id'],
									    'owner' => $buffer[$i]['contact']['owner'],
									    'access' => $buffer[$i]['contact']['access'],
									    'cat_id' => $this->contacts->get_categories($buffer[$i]['categories']),
									    'contact_type' => $type), PHPGW_SQL_RETURN_SQL) . "; \n";
				
				$person_sql = $this->contacts->add_person($buffer[$i]['contact'], $cid, PHPGW_SQL_RETURN_SQL) . "; \n";
				
				if(is_array($buffer[$i]['comm_media']))
				{
					foreach($buffer[$i]['comm_media'] as $comm)
					{
						$comm_sql .= $this->contacts->add_communication_media($comm, $cid, PHPGW_SQL_RETURN_SQL) . "; \n";
					}
				}

				if(is_array($buffer[$i]['locations']))
				{
					foreach($buffer[$i]['locations'] as $location)
					{
						$loc_sql .= $this->contacts->add_location($location, $cid, PHPGW_SQL_RETURN_SQL) . "; \n";
					}
				}
			
				if(is_array($buffer[$i]['others']))
				{
					foreach($buffer[$i]['others'] as $other)
					{
						$other_sql .= $this->contacts->add_others($other, $cid, PHPGW_SQL_RETURN_SQL) . "; \n";
					}
				}

				if(is_array($buffer[$i]['notes']))
				{
					foreach($buffer[$i]['notes'] as $note)
					{
						$notes_sql .= $this->contacts->add_notes($notes, $cid, PHPGW_SQL_RETURN_SQL) . "; \n";
					}
				}
				
				$entries .= $contact_sql.$person_sql.$comm_sql.$loc_sql.$other_sql.$notes_sql;
			}
			$buffer = $entries;
			return $buffer;
		}
	}
?>
