<?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( 
			'' 					=> array('person', 'first_name'),
			'̾' 					=> array('person', 'last_name'),
			'ɾ' 					=> array('person', 'suffix'),
			'̾' 				=> array('person', 'org_name'), //objectclass: organization
			'̾' 				=> array('person', 'department'), //objectclass: organizationalPerson
			'' 					=> array('person', 'title'), //objectclass: organizationalPerson
			'̳ιޤϰ' 	=> array('location', 'country', 'work'),
			'̳͹ֹ' 		=> array('location', 'postal_code', 'work'),
			'̳ƻܸ' 		=> array('location', 'state', 'work'),
			'̳λԶĮ¼' 		=> array('location', 'city', 'work'),
			'̳'			=> array('location', 'add1', 'work'),
			'եξ' 		=> array('location', 'add2', 'work'),
			'' 			=> array('location', 'add1', 'home'),
			'λԶĮ¼' 		=> array('location', 'city', 'home'),
			'ƻܸ' 		=> array('location', 'state', 'home'),
			'͹ֹ' 		=> array('location', 'postal_code', 'home'),
			'ޤϰ' 			=> array('location', 'country', 'home'),
			'̳եå' 		=> array('comms', 'work fax'),
			'ֹ̳' 		=> array('comms', 'work phone'),
			'ݥåȥ٥' 			=> array('comms', 'pager'),
			'ư' 			=> array('comms', 'car phone'),
			'եå' 		=> array('comms', 'home fax'),
			'ֹ :' 		=> array('comms', 'home phone'),
			'ǥ' 				=> array('comms', 'modem'), //This will make another homePhone entry
			'ISDN' 					=> array('comms', 'isdn'),
			'' 				=> array('comms', 'mobile (cell) phone'), //newPilotPerson
			'' 				=> array('person', 'birthday'),
			'ƥ' 				=> array('person', 'categories'), 
			'Żҥ᡼ ɥ쥹' 	=> array('comms', 'work email'),
			'Żҥ᡼ ɥ쥹' 	=> array('comms', 'home email'),
			'˥' 			=> array('person', 'initials'),
			'' 					=> array('notes', 'note', 'general'),
			'ӥͥ Web ڡ' 	=> array('comms', 'website')
		);
		
		function import_conv()
		{
			$this->map = array(
				'first_name'	=> lang('first name'),
				'last_name'		=> lang('last name'),
				'suffix'		=> lang('suffix'),
				'org_name'		=> lang('company'),
				'department'	=> lang('department'),
				'title'			=> lang('title'),
				'country'		=> lang('country'),
				'postal_code'	=> lang('zip code'),
				'state'			=> lang('state'),
				'city'			=> lang('city'),
				'add1'			=> lang('address 1'),
				'add2'			=> lang('address 2'),
				'work fax'		=> lang('work fax'),
				'work phone'	=> lang('work phone'),
				'bbs'			=> lang('bbs'),
				'car phone'		=> lang('car phone'),
				'home fax'		=> lang('home fax'),
				'home phone'	=> lang('home phone'),
				'modem'			=> lang('modem'),
				'isdn'			=> lang('isdn'),
				'mobile (cell) phone'	=> lang('mobile (cell) phone'),
				'birthday'		=> lang('birthday'),
				'categories'	=> lang('categories'),
				'work email'	=> lang('work email'),
				'home email'	=> lang('home email'),
				'initials'		=> lang('initials'),
				'general'		=> lang('general'),
				'website'		=> lang('website'),
				'note'			=> lang('note'),
				'home'			=> lang('own house'),
				'work'			=> lang('work'),
				'general'		=> lang('general')
			);
		}

		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);
			if (isset($_POST['download']) && $_POST['download'] != '')
				$debug_only = True;
			if ($num > 0)
				$html = '<table border="1" cellspacing="0" cellpadding="0" align="center" width="75%" class="th">'."\n";
			foreach($this->record_set as $idx => $contact)
			{
				$records = $this->build_html_data($contact);
				$html .= "\t<tr>\n\t\t<td align=\"center\" width=\"10%\">".sprintf('%d',$idx+1)."</td>\n\t\t<td>\n";
				$html .= "\t\t\t".'<table width="100%" border="0" cellpadding="0" cellspacing="0">'."\n";
				for ($i = 0;$i < count($records); $i++)
				{
					list($col, $value) = each($records[$i]);
					$html .= "\t\t\t\t<tr class=".'"'.($col == 'type' ? 'row_on' : 'row_off').'"'.">\n";
					if ($col == 'type')
					{
						$col = lang($col);
						$value = $this->map[$value] ? $this->map[$value] : $value;
					}
					$html .= "\t\t\t\t\t".'<td align="right" nowrap width="30%" valign="top">'.$GLOBALS['phpgw']->strip_html($col)."</td>\n";
					$html .= "\t\t\t\t\t".'<td align="center" nowrap width="5%" valign="top">'.'=>'."</td>\n";
					$html .= "\t\t\t\t\t".'<td nowrap width="65%">'.nl2br($GLOBALS['phpgw']->strip_html($value))."</td>\n";
					$html .= "\t\t\t\t</tr>\n";
				}
				$html .= "\t\t\t</table>\n";
				$html .= "\t\t\n\t\t</td>\n\t</tr>\n";
				if ($debug_only)
					continue;
				$contact['categories'] = array($cat_id);
				$contact['access'] = $access ? $access : 'public';
				$contact['active'] = 'Y';
				$contact['preferred_org'] = $contact['org_name'];
				unset($contact['org_name']);
				$contacts->contact_import($contact);
			}
			if ($num > 0)
				$html .= "</table>\n<br />";
			$html .= lang('Successfully imported %1 records into your addressbook.',$num);
			return $html;
		}
		
		function build_html_data($contact, $records=array())
		{
			foreach ($contact as $key => $val)
			{
				if (empty($val))
					continue;
				if (is_string($val))
				{
					$map_key = isset($this->map[$key]) ? $this->map[$key] : $key; 
					$records[] = array($map_key => $val);
				}
				elseif (is_array($val))
					$this->build_html_data($val, &$records);
			}
			return $records;
		}
	}
?>
