<?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(
			'title'		=> 'title', 
			'first_name'	=> 'givenname',
			'last_name'	=> 'sn',
			'full_name'	=> 'cn',
			'org_name'	=> 'o',
			'department'	=> 'ou',
			'add1'      	=> 'streetaddress',
			'city'    	=> 'locality',
			'state'      	=> 'st',
			'postal_code'  	=> 'postalcode',
			'country'	=> 'countryname',
			'tel_work'	=> 'telephonenumber',
			'tel_home'      => 'homephone',
			'tel_fax'       => 'facsimiletelephonenumber',
			'ophone'        => 'xmozillaanyphone',
			'tel_cell'      => 'cellphone',
			'note'          => 'description',
			'ophone'        => 'ophone',
			'tel_pager'     => 'pagerphone',
			'email'         => 'mail',
			'url'           => 'homeurl',
		);

		// 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->read_single_entry($this->ids[$this->id],$this->qfields);
			//$this->currentrecord = $top[0];
			$record = $this->contacts->person_complete_data($this->ids[$this->id]);

			if(is_array($record['locations']))
			{
				foreach($record['locations'] as $key => $values)
				{
					if($values['preferred']=='Y')
					{
						$loc_id = $key;
					}
				}
			}

			if(is_array($record['notes']))
			{
				$note_id = key($record['notes']);
			}
			
			$currentrecord['title'] 	= $record['title'];
			$currentrecord['first_name'] 	= $record['first_name'];
			$currentrecord['last_name'] 	= $record['last_name'];
			$currentrecord['full_name'] 	= $record['full_name'];
			$currentrecord['org_name'] 	= $record['org_name'];
			$currentrecord['department'] 	= $record['department'];
			$currentrecord['add1'] 		= $record['locations'][$loc_id]['add1'] .
							  ($record['locations'][$loc_id]['add2']?' '.$record['locations'][$loc_id]['add2']:'') .
							  ($record['locations'][$loc_id]['add3']?' '.$record['locations'][$loc_id]['add3']:'');
			$currentrecord['city'] 		= $record['locations'][$loc_id]['city'];
			$currentrecord['state'] 	= $record['locations'][$loc_id]['state'];
			$currentrecord['postal_code'] 	= $record['locations'][$loc_id]['postal_code'];
			$currentrecord['country'] 	= $record['locations'][$loc_id]['country'];
			$currentrecord['note'] 		= $record['notes'][$note_id]['text'];
			$currentrecord['tel_work'] 	= $record['comm_media']['work phone'];
			$currentrecord['tel_home'] 	= $record['comm_media']['home phone'];
			$currentrecord['tel_fax'] 	= $record['comm_media']['work fax'];
			$currentrecord['ophone'] 	= $record['comm_media']['voice phone'];
			$currentrecord['tel_cell'] 	= $record['comm_media']['mobile (cell) phone'];
			$currentrecord['ophone'] 	= $record['comm_media']['voice phone'];
			$currentrecord['tel_pager'] 	= $record['comm_media']['pager'];
			$currentrecord['email'] 	= $record['comm_media']['work email'];
			$currentrecord['url'] 		= $record['comm_media']['website'];
			$this->currentrecord = $currentrecord;
			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])
			{
				if (strstr($value,"\n"))
				{
					$value = ': '.base64_encode($value);
				}
				else
				{
					 $value = ' '.$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)
		{
			$buffer[$this->id]['dn']                          = 'cn='.$buffer[$this->id]['cn'].',mail='.$buffer[$this->id]['mail'];
			$buffer[$this->id]['xmozillauseconferenceserver'] = '0';
			$buffer[$this->id]['xmozillanickname']            = '';
			$buffer[$this->id]['xmozillausehtmlmail']         = 'False';
			if ($buffer[$this->id]['ophone'])
			{
				$buffer[$this->id]['xmozillaanyphone']        = $buffer[$this->id]['ophone'];
			}
			else
			{
				$buffer[$this->id]['xmozillaanyphone']        = $buffer[$this->id]['telephonenumber'];
			}
			//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')
					{
						$entries .= $value . ":" . $buffer[$i][$value] . "\n";
					}
				}
				$entries .= 'xmozillauseconferenceserver: ' . $buffer[$i]['xmozillauseconferenceserver'] . "\n";
				$entries .= 'xmozillanickname: '            . $buffer[$i]['xmozillanickname'] . "\n";
				$entries .= 'xmozillausehtmlmail: '         . $buffer[$i]['xmozillausehtmlmail'] . "\n";
				$entries .= 'xmozillaanyphone: '            . $buffer[$i]['xmozillaanyphone'] . "\n";
				$entries .= 'objectClass: person' . "\n";
				$entries .= 'objectClass: account' . "\n";
				$entries .= 'objectClass: organizationalPerson' . "\n";
				$entries .= 'objectClass: posixAccount' . "\n";
				$entries .= 'objectClass: inetOrgPerson' . "\n";
				$entries .= 'objectClass: shadowAccount' . "\n";
				$entries .= "\n";
			}
			$buffer = $entries;
			return $buffer;
		}
	}
?>
