<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'ship_name.php');
/**
 * ship_names class 
 *
 */
class ship_names extends databasetable
{
	/**
	 * initiates the ship_names class
	 *
	 */
	function ship_names(){
		$this->set_tablename(TABLE_SHIP_NAMES);
		$this->set_index('ship_names_id');
		$this->related_tables = array();
	}
	
	/**
	 * return a ship_name object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_ship_name($id){
		$values = $this->get_row_by_index($id);
		$ship_name_instance = new ship_name( (object) current($values) );
		return $ship_name_instance;
	}

	function update_default_name($ships_id){
		$update = false;
		$default_query = $this->query(sprintf("SELECT count(%s) as anz FROM %s WHERE ships_id='%s' and is_default=1",$this->get_index(),$this->get_tablename(),$ships_id ));
		if($default_query){
			$default = current($default_query);
			if($default['anz']<1)
				$update = true;
		}
		if($update){
			$this->query(sprintf("UPDATE %s SET is_default=1 WHERE ships_id='%s' ORDER BY date_added LIMIT 1",$this->get_tablename(),$ships_id ));
		}
	}

	function set_default_name($id,$ships_id){
		$this->query(sprintf("UPDATE %s SET is_default=0 WHERE ships_id='%s' and ship_names_id != '%s'",$this->get_tablename(),$ships_id,$id));
	}

	function update_baptismal_name($ships_id){
		$update = false;
		$default_query = $this->query(sprintf("SELECT count(%s) as anz FROM %s WHERE ships_id='%s' and is_baptismal_name=1",$this->get_index(),$this->get_tablename(),$ships_id ));
		if($default_query){
			$default = current($default_query);
			if($default['anz']<1)
				$update = true;
		}
		if($update){
			$this->query(sprintf("UPDATE %s SET is_baptismal_name=1 WHERE ships_id='%s' ORDER BY date_added LIMIT 1",$this->get_tablename(),$ships_id ));
		}
	}

	function set_baptismal_name($id,$ships_id){
		$this->query(sprintf("UPDATE %s SET is_baptismal_name=0 WHERE ships_id='%s' and ship_names_id != '%s'",$this->get_tablename(),$ships_id,$id));
	}
}
