<?php

class City
{
	protected static $table_name="city";
	protected static $db_fields = array('id', 'areacode', 'name', 'pagename', 'suburbs', 'content', 'longitude', 'latitude', 'nearbyMarkers');
	public $id;
	public $areacode;
	public $name;
	public $pagename;
	public $suburbs;
	public $content;
	public $longitude;
	public $latitude;
	public $nearbyMarkers;
	
	// Common Database Methods	
	public static function find_all()
	{
		return self::find_by_sql("SELECT * FROM ".self::$table_name);
	}
	
	public static function find_by_id($id="")
	{
		global $database;
		$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id=".$database->escape_value($id)." LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
	}
	
	public static function find_by_name($name="")
	{
		global $database;
		$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE name='".$database->escape_value($name)."' LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
	}
	
	public static function find_by_pagename($pagename="")
	{
		global $database;
		$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE pagename='".$database->escape_value($pagename)."' LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
	}
	
	public static function find_by_name_area($name="", $areacode = "")
	{
		global $database;
		$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE name='".$database->escape_value($name)."' AND areacode='".$database->escape_value($areacode)."' LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
	}
	
	public static function find_by_sql($sql="")
	{
		global $database;
		$result_set = $database->query($sql);
		$object_array = array();
		while ($row = $database->fetch_array($result_set))
		{
			$object_array[] = self::instantiate($row);
		}
		return $object_array;
	}
	
	private static function instantiate($record)
	{
		// could check that $record exists and is an array
		$object = new self;

		foreach($record as $attribute=>$value)
		{
			if($object->has_attribute($attribute))
			{
				$object->$attribute = $value;
			}
		}
		return $object;		
	}
	
	private function has_attribute($attribute)
	{
		//get_object_vars returns an associative array with all attributes 
		//(incl. private ones!!) as the keys and their current values as the value
		$object_vars = $this->attributes();
		// We don't care about the value, we just want to know if the key exists
		// Will return true or false
		return array_key_exists($attribute, $object_vars);
	}

	protected function attributes()
	{  	// return an array of attribute keys and their values
		$attributes = array();
		foreach(self::$db_fields as $field)
		{
			if(property_exists($this, $field))
			{
				$attributes[$field] = $this->$field; // use dynamic $field
			}
		}
		return $attributes; // for more complex tables can use SQL show_fields_from (list) can be parsed then build array dynamically
	}
	
	protected function sanitized_attributes()
	{
		global $database;
		$clean_attributes = array();
		foreach($this->attributes() as $key => $value)
		{
			$clean_attributes[$key] = $database->escape_value($value);
		}
		return $clean_attributes;
	}
	
	public function update()
	{
		global $database;

		$attributes = $this->sanitized_attributes();
		$attribute_pairs = array();
		foreach ($attributes as $key => $value)
		{
			$attribute_pairs[] = "{$key}='{$value}'";
		}
		
		$sql  = "UPDATE ".self::$table_name." SET ";
		$sql .= join(", ", $attribute_pairs);
		$sql .= " WHERE id=".$database->escape_value($this->id); //dont have to escape id, but no harm
		$database->query($sql);
		return ($database->affected_rows() > 0) ? true: false;
	}
	
	public function create()
	{
		global $database;
		
		$attributes = $this->sanitized_attributes();
		
		$sql  = "INSERT INTO ".self::$table_name." (";
		$sql .= join(", ", array_keys($attributes));
		$sql .= ") VALUES ('";
		$sql .= join("', '", array_values($attributes));
		$sql .= "')";
		
		if($database->query($sql))
		{
			$this->id = $database->insert_id();
			return true;
		} else
		{
			return false;
		}
	}
	
	public function delete()
	{
		global $database;
		
		$sql  = "DELETE FROM ".self::$table_name;
		$sql .= " WHERE id=".$database->escape_value($this->id);
		$sql .= " LIMIT 1";
		$database->query($sql);
		return ($database->affected_rows() == 1) ? true : false;
	}
}

?>