<?php
	class Owner{
		var $oid;
		var $country_id;
		var $firstname;
		var $lastname;
		var $address;
		var $zip;
		var $city;
		var $ssn;
		var $phone;
		var $mobile;
		var $email;
		var $created;
		var $created_by;
	
		function __construct( $oid = null ){
			if ( is_null ( $oid ) == false && is_numeric ( $oid ) == true ) {
				try {
					$this->load ( $oid );
				} catch ( Exception $ex ) {
					// log exception
				}
			}
		}
		
		
		public function load($oid) {
			$con = new Connection ();
			$sql = "SELECT * FROM owners  " . "WHERE oid = " . $oid;
				
			// execute query...
			$rs = $con->selectOneRow ( $sql );
				
				// if user exists
			if (is_array ( $rs ) && ! empty ( $rs )) 
			{
				$this->oid 			= $rs ['oid'];
				$this->country_id 	= $rs ['country_id'];
				$this->firstname 	= $rs ['firstname'];
				$this->lastname 	= $rs ['lastname'];
				$this->address	 	= $rs ['address'];
				$this->zip		 	= $rs ['zip'];
				$this->city		 	= $rs ['city'];
				$this->ssn	 		= $rs ['ssn'];
				$this->phone 		= $rs ['phone'];
				$this->mobile 		= $rs ['mobile'];
				$this->email 		= $rs ['email'];
				$this->created 		= $rs ['created'];
			}
				
			$con->close ();
			return $this;
		}
		
		
		public function save() {
			if (empty ( $this->oid )) {
				return $this->insert();
			} else {
				return $this->update();
			}
		}
		
		public function insert() {
			$con = new Connection ();
		
			$sql = "INSERT into owners 
	    			(
	    			oid,
	    			country_id,
	    			firstname,
	    			lastname,
	    			address,
	    			zip,
	    			city,
	    			ssn,
	    			phone,
	    			mobile,
	    			email,
	    			created
	    			)values(" 
						. "NULL, " 
						. "'" . $this->country_id . "', " 
						. "'" . $this->firstname . "', " 
						. "'" . $this->lastname . "', " 
						. "'" . $this->address . "', " 
						. "'" . $this->zip . "', " 
						. "'" . $this->city . "', " 
						. "'" . $this->ssn . "', " 
						. "'" . $this->phone . "', " 
						. "'" . $this->mobile . "', " 
						. "'" . $this->email . "', " 
						. "UNIX_TIMESTAMP()" 
						. ")";
		
		
		$rv = $con->execute( $sql );
		
		if ($rv === true) {
			$this->oid = $con->insertID ();
			$con->commit();
			return $this->oid;
		}else{
			Watchdog::write( Watchdog::TYPE_DATABASE, $con->sqlstate.': '.$con->sql, 0, Watchdog::WATCHDOG_WARNING );
			$con->rollback();
			return false;
		}

		$con->close();
		
	}
	
	
		public function update() {
			$con = new Connection ();
			
			$sql = "UPDATE owners set " 
							. "country_id = '" . $this->country_id . "', " 
							. "firstname = '" . $this->firstname . "', " 
							. "lastname = '" . $this->lastname . "', " 
							. "address = '" . $this->address . "', " 
							. "zip = '" . $this->zip . "', " 
							. "city = '" . $this->city . "', " 
							. "ssn = '" . $this->ssn . "', " 
							. "phone = '" . $this->phone . "', " 
							. "mobile = '" . $this->mobile . "', " 
							. "email = '" . $this->email . "' " 
							. "WHERE oid = " . $this->oid;
			
			$rv = $con->execute( $sql );
			
			if ($rv === true) {
				$con->commit();
			}else{
				Watchdog::write( Watchdog::TYPE_DATABASE, $con->sqlstate.': '.$con->sql, 0, Watchdog::WATCHDOG_WARNING );
				$con->rollback();
			}
			
			$con->close();
			return $rv;
		}
		
		public function delete() {
			$con = new Connection ();
			
			$sql = "DELETE FROM owners WHERE oid = " . $this->oid;
			
		
			$rv = $con->execute( $sql );
			
			if ($rv === true) {
				$con->commit();
			}else{
				Watchdog::write( Watchdog::TYPE_DATABASE, $con->sqlstate.': '.$con->sql, 0, Watchdog::WATCHDOG_WARNING );
				$con->rollback();
			}
			
			$con->close();
			return $rv;
		}
	}
