<?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.
//
// 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.

	class export_conv
	{
		var $id;
		var $type = 'csv';

		var $export = array(
			'title'					=> 'Subject',
			'start.mday' => 'Start Date', 'start.month' => '+/', 'start.year' => '+/',
			'start.hour' => 'Start Time', 'start.min' => '+:', 'start.sec' => '+:',
			'end.mday' => 'End Date', 'end.month' => '+/', 'end.year' => '+/',
			'end.hour' => 'End Time', 'end.min' => '+:', 'end.sec' => '+:',
			'All day event'			=> 'All day event',
			'alarm'					=> 'Reminder on/off',
			'Reminder Date'			=> 'Reminder Date',
			'Reminder Time'			=> 'Reminder Time',
			'owner'					=> 'Meeting Organizer',
			'participants'			=> 'Required Attendees',
			'Optional Attendees'	=> 'Optional Attendees',
			'Meeting Resources'		=> 'Meeting Resources',
			'Billing Information'	=> 'Billing Information',
			'category'				=> 'Categories',
			'description'			=> 'Description',
			'location'				=> 'Location',
			'Mileage'				=> 'Mileage',
			'priority'				=> 'Priority',
			'private'				=> 'Private',
			'Sensitivity'			=> 'Sensitivity',
			'Show time as'			=> 'Show time as'
		);

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

		function export_start_file($buffer)
		{
			$this->id=-1;

			// $buffer is still empty
			return $buffer;
		}

		// Read each entry
		function export_start_record($buffer)
		{
			$this->id++;
			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])
			{
				$buffer[$this->id][$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)
		{
			return $buffer;
		}

		function export_end_file($buffer)
		{
			// Build the header for the file (field list)
			reset($this->export);
			while (list($name,$value)=each($this->export))
			{
				if (substr($value,0,1) != '+')
				{
					$entries .= '"';
					$entries .= $value . '",';
				}
			}
			$entries = substr($entries,0,-2);
			$entries .= "\r\n";

			// Now add all the data
			for ($i=0;$i<=$this->id;$i++)
			{
				reset($this->export);
				while (list($name,$value)=each($this->export))
				{
					$entries .= '"';
					if (substr($value,0,1) == '+')
					{
						$entries = substr($entries,0,-3);
						$entries .= substr($value,1);
					}
					$entries .= $buffer[$i][$name] . '",';
				}
				$entries = substr($entries,0,-1);
				$entries .= "\r\n";
			}
			$buffer = $entries;
			return $buffer;
		}
	}
?>
