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

	class import_conv
	{
		var $currentrecord = array(); //used for buffering to allow uid lines to go first
		var $id;
		var $type = 'csv';

		var $import = array(
			'Subject'				=> 'title',
			'Start Date'			=> 'Start Date',
			'Start Time'			=> 'Start Time',
			'End Date'				=> 'End Date',
			'End Time'				=> 'End Time',
			'Reminder on/off'		=> 'alarm',
			'Meeting Organizer'		=> 'owner',
			'Required Attendees'	=> 'participants',
			'Categories'			=> 'category',
			'Description'			=> 'description',
			'Location'				=> 'location',
			'Priority'				=> 'priority',
			'Private'				=> 'private'
		);

		function import_start_file($buffer)
		{
			return $buffer;
		}

		function import_start_record($buffer)
		{
			$top=array();
			++$this->id;
			$this->currentrecord = $top;
			return $buffer;
		}

		function import_new_attrib($buffer,$name,$value)
		{
			$value = trim($value);
			$value = str_replace('\n','<BR>',$value);
			$value = str_replace('\r','',$value);
			switch ($name)
			{
			case 'Start Date':
				$this->currentrecord += array('start.mday' => substr($value, 0, 2));
				$this->currentrecord += array('start.month' => substr($value, 3, 2));
				$this->currentrecord += array('start.year' => substr($value, 6, 4));
				break;
			case 'Start Time':
				$this->currentrecord += array('start.hour' => substr($value, 0, 2));
				$this->currentrecord += array('start.min' => substr($value, 3, 2));
				$this->currentrecord += array('start.sec' => substr($value, 6, 2));
				break;
			case 'End Date':
				$this->currentrecord += array('end.mday' => substr($value, 0, 2));
				$this->currentrecord += array('end.month' => substr($value, 3, 2));
				$this->currentrecord += array('end.year' => substr($value, 6, 4));
				break;
			case 'End Time':
				$this->currentrecord += array('end.hour' => substr($value, 0, 2));
				$this->currentrecord += array('end.min' => substr($value, 3, 2));
				$this->currentrecord += array('end.sec' => substr($value, 6, 2));
				break;
			default:
				$this->currentrecord += array($name => $value);
				break;
			}
			return $buffer;
		}

		function import_end_record($buffer)
		{
			$buffer[$this->id]='';
			while ( list($name, $value) = each($this->currentrecord))
			{
				$buffer[$this->id][$name] = $value;
				//echo '<br>'.$name.' => '.$value;
			}
			return $buffer;
		}

		function get_existing_event($date, $title)
		{	
			$events = $this->bo->store_to_cache($date);
			if (is_array($events))
			{
				foreach($events as $day=>$eventsperday)
				{
					if(is_array($eventsperday))
					{
						foreach ($eventsperday as $eventtest)
						{
							if ($eventtest['title'] == $title)
							{
								$uid = $eventtest['uid'];
								$found = $this->bo->so->get_event_ids(TRUE, " AND (phpgw_cal.cal_id = '".$eventtest['cal_id']."')"); 
								if(is_array($found))
								{
//									echo $title;
									return $found[0];
								}
							}
						}
					}
				}
			}
			return false;
		}

		function import_end_file($buffer,$access='private',$cat_id=0)
		{
			$user = $phpgw_info['user']['account_id'];
			$this->bo = CreateObject('calendar.bocalendar',1);

			if(!is_object($GLOBALS['uicalendar']))
			{
				$so_event = createobject('calendar.socalendar',
					Array(
						'owner'	=> 0,
						'filter'	=> '',
						'category'	=> ''
					)
				);
			}
			else
			{
				$so_event = $GLOBALS['uicalendar']->bo->so;
			}


			for ($i=1;$i<=count($buffer);$i++)
			{
				$so_event->event_init();
				$so_event->add_attribute('id',0);
				$so_event->add_attribute('reference',0);
				$so_event->set_class(1);
				$so_event->add_attribute('priority',2);
				$so_event->set_category(0);
				$so_event->set_recur_none();
				while ( list($name,$value) = @each($buffer[$i]) )
				{
					//echo '<br>'.$i.': '.$name.' => '.$value;
					switch ($name)
					{
					case 'title':
						$so_event->set_title($value);
						break;
					case 'description':
						$so_event->set_description($value);
						break;
					case 'priority':
						$so_event->add_attribute('priority',$value);
						break;
					case 'start.mday':
					case 'start.month':
					case 'start.year':
					case 'start.hour':
					case 'start.min':
					case 'start.sec':
					case 'end.mday':
					case 'end.month':
					case 'end.year':
					case 'end.hour':
					case 'end.min':
					case 'end.sec':
						$dates[$name] = $value;
						break;
					case 'alarm':
					case 'priority':
					case 'private':
					//case 'owner':
						$so_event->add_attribute($name,$value);
						break;
					default:	
						break;
					}
				}
				$so_event->set_start(
					$dates['start.year'],
					$dates['start.month'],
					$dates['start.mday'],
					$dates['start.hour'],
					$dates['start.min'],
					$dates['start.sec']);
				$so_event->set_end(
					$dates['end.year'],
					$dates['end.month'],
					$dates['end.mday'],
					$dates['end.hour'],
					$dates['end.min'],
					$dates['end.sec']);
				$so_event->add_attribute('owner',$GLOBALS['phpgw_info']['user']['account_id']);
				$so_event->add_attribute('participants','A',intval($GLOBALS['phpgw_info']['user']['account_id']));

				$event = $so_event->get_cached_event();
				$date = Array(
					'syear'		=> $dates['start.year'],
					'smonth'	=> $dates['start.month'],
					'sday'		=> $dates['start.mday'],
					'eyear'		=> $dates['start.year'],
					'emonth'	=> $dates['start.month'],
					'eday'		=> $dates['start.mday']);
				
				$uid = -1;
				
				$foundevent = $this->get_existing_event($date, $event['title']);
				if ($foundevent === false)
				{
					 $so_event->add_entry($event);
				}
				else
				{
//					$so_event->read_entry($foundevent);
//					$changedevent = $so_event->get_cached_event();
	// todo: update it here
//					$so_event->add_entry($changedevent);
				}
			}
			$num = $i - 1;
			return lang('Successfully imported %1 records into your calendar.',$num);
		}
	}
?>
