<?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 = 'ldif';

		var $export = array(
			'contact_id'	=> 'employeeNumber',
			'uid'		=> 'uid',
			'full_name_cn'	=> 'cn',
			'full_name_sn'	=> 'sn',
			'first_name'	=> 'displayName',
			'last_name'	=> 'givenName',
			'initials'	=> 'initials',
			'org_name'	=> 'o',
			'title'		=> 'title',
			'department'	=> 'ou',
			'street'	=> 'street',
			'address'	=> 'postalAddress',
			'city'		=> 'l',
			'state'		=> 'st',
			'postal_code'	=> 'postalCode',
			'email'		=> 'mail',
			'home_phone'	=> 'homePhone',
			'work_phone'	=> 'telephoneNumber',
			'pager'		=> 'pager',
			'isdn'		=> 'internationaliSDNNumber',
			'fax'		=> 'facsimileTelephoneNumber',
			'mobile_phone'	=> 'mobile',
			'car_ phone'	=> 'telephoneNumber',
			'website'	=> 'labeledURI'
		);

		// 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['preferred']=='Y')
					{
						$loc_id = $key;
					}
				}
			}


			$record['contact_id'] 	= $top['contact_id'];
			$record['uid'] 		= $top['contact_id'].':'.$top['full_name'];
			$record['full_name_cn'] = $top['full_name'];
			$record['full_name_sn'] = $top['full_name'];
			$record['first_name'] 	= $top['first_name'];
			$record['last_name'] 	= $top['last_name'];
			$record['initials'] 	= $top['initials'];
			$record['org_name'] 	= $top['org_name'];
			$record['title'] 	= $top['title'];
			$record['department'] 	= $top['department'];
			$record['street'] 	= $top['locations'][$loc_id]['add1'] .
						  ($top['locations'][$loc_id]['add2']?' '.$top['locations'][$loc_id]['add2']:'') .
						  ($top['locations'][$loc_id]['add3']?' '.$top['locations'][$loc_id]['add3']:'');
			$record['address'] 	= $top['locations'][$loc_id]['add1'] .
						  ($top['locations'][$loc_id]['add2']?' '.$top['locations'][$loc_id]['add2']:'') .
						  ($top['locations'][$loc_id]['add3']?' '.$top['locations'][$loc_id]['add3']:'');
			$record['city'] 	= $top['locations'][$loc_id]['city'];
			$record['state'] 	= $top['locations'][$loc_id]['state'];
			$record['postal_code'] 	= $top['locations'][$loc_id]['postal_code'];
			$record['email'] 	= $top['comm_media']['work email']?$top['comm_media']['work email']:$top['comm_media']['home email'];
			$record['home_phone'] 	= $top['comm_media']['home phone'];
			$record['work_phone'] 	= $top['comm_media']['work phone'];
			$record['pager'] 	= $top['comm_media']['pager'];
			$record['isdn'] 	= $top['comm_media']['isdn'];
			$record['fax'] 		= $top['comm_media']['work fax']?$top['comm_media']['work fax']:$top['comm_media']['home fax'];
			$record['mobile_phone'] = $top['comm_media']['mobile (cell) phone'];
			$record['car_phone'] 	= $top['comm_media']['car phone'];
			$record['website'] 	= $top['comm_media']['website'];

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

		// Read each attribute, populate buffer
		// name/value are the fields from the export array above
		function export_new_attrib($buffer,$name,$value)
		{
			if ($this->export[$name] && ($value != '') )
			{
				$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
		function export_end_record($buffer)
		{
			if ($GLOBALS['phpgw_info']['server']['ldap_contact_context'])
			{
				$context = $GLOBALS['phpgw_info']['server']['ldap_contact_context'];
			}
			$time = gettimeofday();
			$cn = ereg_replace(',','',$buffer[$this->id]['cn']);
			$buffer[$this->id]['dn']  = 'uid='.$buffer[$this->id]['uid'].','.$context;
// 			$buffer[$this->id]['description'] = ereg_replace("\r\n",';',$buffer[$this->id]['description']);
			//echo '<br />'.$this->id.' - '.$buffer[$this->id]['dn'];
			return $buffer;
		}

		function export_end_file($buffer)
		{
			reset($this->ids);
			for ($i=0;$i<count($this->ids);$i++)
			{
				$entries .= 'dn: '.$buffer[$i]['dn'] . "\n";
				reset($this->export);
				while (list($name,$value)=each($this->export))
				{
					if (($value != 'dn') && !empty($buffer[$i][$value]))
					{
						$tmp = ereg_replace(',','',$buffer[$i][$value]);
						$entries .= $value . ': ' . $tmp . "\n";
					}
				}
				$entries .= 'objectClass: person' . "\n";
				$entries .= 'objectClass: organizationalPerson' . "\n";
				$entries .= 'objectClass: inetOrgPerson' . "\n";
				$entries .= "\n";
			}
			$buffer = $entries;
			return $buffer;
		}
	}
?>
