<?php
/**
 * Pet class
 */
class Pet {
	var $pid;
	
	var $tsn;
	var $tsn_completename;
	var $familiar_name;
	
	var $oid;
	var $name;
	var $gender;
	var $description;
	var $color;
	
	var $date_of_birth_timestamp;
	var $date_of_birth_date;
	var $age_years;
	var $age_months;
	var $date_of_green_meadows_timestamp;
	var $date_of_green_meadows_date;
	
	var $microchip;
	var $microchip_timestamp;
	var $microchip_location;
	var $tatuatge;
	var $tatuatge_timestamp;
	var $created;
	var $created_date;
	var $modified;
	var $modified_date;
	
	public $html;
	public $array;

    function __construct( $pid = null ){
		if ( is_null( $pid ) == false ) {
			try {
				$this->load ( $pid );
			} catch ( Exception $ex ) {
				// log exception
			}
		}
	}
	
	/**
	 * load a Pet into a Object
	 * 
	 * @param int $pid
	 * @return $this
	 */
    public function load($pid) {
		$con = new Connection ();
		$sql = "SELECT * FROM pets p, species s " . 
					"WHERE p.tsn = s.tsn " . 
					"AND p.pid = " . $pid;
		
		// execute query...
		$rs = $con->selectOneRow ( $sql );
		
		// if pet exists
		if( is_array($rs) == true && empty( $rs ) == false ){
			$this->pid								= $rs ['pid'];
			$this->tsn								= $rs ['tsn'];
			$this->tsn_completename					= $rs ['complete_name'];
			$this->familiar_name					= $rs ['familiar_name'];
			$this->oid						 		= $rs ['oid'];
			$this->name						 		= $rs ['name'];
			$this->gender							= $rs ['gender'];
			$this->description						= $rs ['description'];
			$this->color							= $rs ['color'];
			
			$this->date_of_birth_timestamp			= $rs ['date_of_birth'];
			$this->date_of_green_meadows_timestamp	= $rs ['date_of_green_meadows'];
			
			$this->microchip						= $rs ['microchip'];
			$this->microchip_timestamp				= $rs ['microchip_timestamp'];
			$this->microchip_location				= $rs ['microchip_location'];
			$this->tatuatge					 		= $rs ['tatuatge'];
			$this->tatuatge_timestamp				= $rs ['tatuatge_timestamp'];
			
			$this->created							= $rs ['created'];
			$this->modified							= $rs ['modified'];
			
			$this->created_date						= Data::UnixTimestampToDate( $rs['created'] );
			$this->modified_date					= Data::UnixTimestampToDate( $rs['modified'] );
			$this->microchip_date					= Data::UnixTimestampToDate( $rs['microchip_timestamp'] );
			$this->tatuatge_date					= Data::UnixTimestampToDate( $rs['tatuatge_timestamp'] );
			$this->date_of_birth_date				= Data::UnixTimestampToDate( $rs['date_of_birth'] );
			$this->date_of_green_meadows_date		= Data::UnixTimestampToDate( $rs['date_of_green_meadows'] );

			
			// calculating age
			$date_of_birth = new DateTime();
			$date_of_birth->setTimestamp( $rs['date_of_birth'] );


			$now = new DateTime();
			if($rs ['date_of_green_meadows'] > 0){
				$now->setTimestamp($rs['date_of_green_meadows']);
			}

			$interval = $now->diff($date_of_birth);
			$this->age_years	 = $interval->format('%y');
			$this->age_months	 = $interval->format('%m');
			
			$this->description		 	= $rs['description'];
			$this->color				= $rs['color'];
			
			$this->array = (array)$this;
			
			$html = '<div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-id">' . _('ID') . ': ' . $this->pid . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-breed">' . _('breed') . ': ' . $this->tsn_completename . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-name">' . _('name') . ': ' . $this->name . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-gender">' . _('gender') . ': ' . $this->gender . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-description">' . _('description') . ': ' . $this->description . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-date-of-birth">' . _('date of birth') . ': ' . $this->date_of_birth_date . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-age">' . _('age') .  ': ' . $this->age_years . ' ' . _( 'years' ) . ' / ' . $this->age_months . ' ' . _( 'months' ) . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-microchip">' . _('microchip') . ': ' . $this->microchip . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-microchip-date">' . _('microchip date') . ': ' . $this->microchip_date . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-microchip-location">' . _('microchip location') . ': ' . $this->microchip_location . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-tatuatge">' . _('tatuatge') . ': ' . $this->tatuatge . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-tatuatge-date">' . _('tatuatge date') . ': ' . $this->tatuatge_date . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-created">' . _('record created') . ': ' . $this->created_date . '</div>';
				$html .= '<div class="id="pet-full-detail" id="pet-full-detail-created">' . _('record modified') . ': ' . $this->modified_date . '</div>';
			$html .= '</div>';
				
			$this->html = $html;
		}
			
		$con->close ();
		return $this;
	}
	
	/**
	 * object wrapper to save pet object to database
	 * 
	 * @return boolean
	 */
	public function save() {
		if (empty ( $this->pid ) == true ) {
			return $this->insert();
		} else {
			return $this->update();
		}
	}
		
		public function insert() {
			$con = new Connection ();
		
			$sql = "INSERT into pets 
	    			(
	    			pid,
	    			tsn,
	    			oid,
	    			name,
	    			gender,
	    			description,
	    			color,
	    			date_of_birth,
	    			date_of_green_meadows,
	    			microchip,
	    			microchip_timestamp,
	    			microchip_location,
	    			tatuatge,
	    			tatuatge_timestamp,
	    			created,
	    			modified
	    			)values(" 
						. "NULL, " 
						. "'" . $this->tsn . "', " 
						.		$this->oid . ", " 
						. "'" . $this->name . "', " 
						. "'" . $this->gender . "', " 
						. "'" . $this->description . "', " 
						. "'" . $this->color . "', " 
						.		$this->date_of_birth_timestamp . ", " 
						.		$this->date_of_green_meadows_timestamp . ", " 
						. "'" . $this->microchip . "', " 
						.		$this->microchip_timestamp . ", " 
						. "'" . $this->microchip_location . "', " 
						. "'" . $this->tatuatge . "', " 
						.		$this->tatuatge_timestamp . ", " 
						. "UNIX_TIMESTAMP()," 
						. "UNIX_TIMESTAMP()" 
						. ")";
		
		$rv = $con->execute ( $sql );
	
		if ($rv === true) {
			$this->oid = $con->insertID ();
			$con->commit();
		}else{
			Watchdog::write( Watchdog::TYPE_DATABASE, $con->sqlstate.': '.$con->sql, 0, Watchdog::WATCHDOG_WARNING );
			$con->rollback();
		}

		$con->close();
		return $rv;
		
	}
	
	
	public function update() {
		$con = new Connection ();

		$sql = "UPDATE pets set " 
						. "tsn = '" . $this->tsn . "', " 
						. "oid = '" . $this->oid . "', " 
						. "name = '" . $this->name . "', " 
						. "gender = '" . $this->gender . "', " 
						. "date_of_birth = " . $this->date_of_birth_timestamp . ", " 
						. "date_of_green_meadows = " . $this->date_of_green_meadows_timestamp . ", " 
						. "description = '" . $this->description . "', " 
						. "color = '" . $this->color . "', " 
						. "microchip = '" . $this->microchip . "', " 
						. "microchip_timestamp = " . $this->microchip_timestamp . ", " 
						. "microchip_location = '" . $this->microchip_location . "', " 
						. "tatuatge = '" . $this->tatuatge . "', " 
						. "tatuatge_timestamp = '" . $this->tatuatge_timestamp . "', " 
						. "modified = UNIX_TIMESTAMP() " 
						. "WHERE pid = " . $this->pid;

		$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;
	}
	
	static function exists( $pid ){
		$pet = new Pet( $pid );
		if( empty( $pet->pid ) == true ){
			return false;
		}else{
			return true;
		}
	}
	
	public function updateLastOwner( $oid ){
		$con = new Connection();
		
		$sql = "UPDATE pets SET oid = " . $oid . " WHERE pid = " . $this->pid;
		
		$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;
		
	}
	
}
?>
