<?php
	class import_conversion
	{
		protected $db;
		public $messages = array();
		public $warnings = array();
		public $errors = array();
		public $debug = true;

		public function __construct()
		{
			set_time_limit(10000); //Set the time limit for this request
			$this->account		= (int)$GLOBALS['phpgw_info']['user']['account_id'];
			$this->db           = & $GLOBALS['phpgw']->db;
		}

		public function add($data)
		{
			$error = false;
			$table = 'fm_entity_4_1';
			$byggid = $data[0];
			if(!$byggid)
			{
				$this->errors[] = "Mangler objekt, hopper over: {$data[1]}";
				return true;
			}
			if(substr($byggid,0,1) == 'B')
			{
				$this->warnings[] = "BBB-objekt, hopper over: {$byggid}";
				return true;
			}
			$location_code = substr($byggid,0,4) . '-0' . substr($byggid,-1);
//_debug_array($location_code);
			$location_data = execMethod('property.solocation.read_single', $location_code );
_debug_array($data);
//_debug_array($location_data);die();
			if(!$location_data)
			{
				$this->errors[] = "Error importing location: {$location_code}";
				$error = true;
				return false;
			}

			if($location_data['street_name'])
			{
				$address[]= $location_data['street_name'];
				$address[]= $location_data['street_number'];
				$address	= $this->db->db_addslashes(implode(" ", $address));
			}

			if(!$address)
			{
				$address = $this->db->db_addslashes($location_data['loc1_name']);
			}

			$id = (int)$data[2];
			

			$this->messages[] = "Dagens dato er lagt inn som registreringsdato for {$id}  (mangler info)";
			$entry_date = time();

//_debug_array($entry_date_info);
//_debug_array($entry_date);
			$value_set = array();


			$value_set['num'] 					= sprintf('%04s',$id);
    		$value_set['loc1']					= $location_data['loc1'];
    		$value_set['loc2']					= $location_data['loc2'];
			$value_set['user_id']				= $this->account;
			$value_set['location_code']			= $location_code;
			$value_set['address']				= $address;
			$value_set['entry_date']			= $entry_date;
			$value_set['museumsnr']				= $this->db->db_addslashes($data[1]);
			$value_set['betegnelse']			= $this->db->db_addslashes($data[3]);
			$value_set['beskrivelse']			= $this->db->db_addslashes($data[4]);
			$value_set['tilstand']				= $this->db->db_addslashes($data[5]);

			$this->db->query("SELECT id FROM {$table} WHERE id = {$id}",__LINE__,__FILE__);
			if($this->db->next_record())
			{
				$this->warnings[] = "ID finnes fra før: {$id}, oppdaterer";
				$value_set	= $this->db->validate_update($value_set);
				$sql = "UPDATE {$table} SET {$value_set} WHERE id = {$id}";
			}
			else
			{
				$value_set['id'] = $id;
				$cols = implode(',', array_keys($value_set));
				$values	= $this->db->validate_insert(array_values($value_set));

				$sql = "INSERT INTO {$table} ({$cols}) VALUES ({$values})";
			}

			if($this->debug)
			{
				_debug_array($sql);
			}
			else
			{
				$request_ok = $this->db->query($sql,__LINE__,__FILE__);
			}

			if(!$error)
			{
				$this->messages[] = "Successfully imported location: Title ({$data[1]})";
				$ok = true;
			}
			else
			{
				$this->errors[] = "Error importing location: Title ({$data[1]})";
				$ok = false;
			}
			return $ok;
		}
	}
