Location: PHPKode > projects > Apartment Classfieds > apartment_classifieds_script/class_apartment.php
<?php
require_once('globals.php');

class Apartment
{
	private $id;
	private $category_id;
	private $location;
	private $title;
	private $bathrooms;
	private $rent;
	private $lease_term;
	private $required_credit_check;
	private $damage_deposit;
	private $required_reference_check;
	private $description;
	private $email;
	private $phone;
	private $allowed_pets;
	private $timestamp;
	private $ip;

	function __construct()
	{
		$this->id = null;
		$this->category_id = null;
		$this->location = null;
		$this->title = null;
		$this->bathrooms = null;
		$this->rent = null;
		$this->lease_term = null;
		$this->required_credit_check = null;
		$this->required_reference_check = null;
		$this->damage_deposit = null;
		$this->description = null;
		$this->email = null;
		$this->phone = null;
		$this->allowed_pets = null;
		$this->timestamp = null;
		$this->ip = null;
	}
	
	//===================================
	// ACCESSORS
	//===================================
	
    public function get_id()
    {
    	return htmlentities($this->id, ENT_QUOTES);
    }
    
	public function get_category_id()
	{
		return htmlentities($this->category_id, ENT_QUOTES);
	}
	
	public function get_title()
	{
		$ret = null;
		
		$ret = wordwrap($this->title, 40, ' ', true);
		$ret = htmlentities($ret, ENT_QUOTES);
		$ret = nl2br($ret);
	
		return $ret;
	}
	
	public function get_location()
	{
		$ret = null;
		
		$ret = wordwrap($this->location, 40, ' ', true);
		$ret = htmlentities($ret, ENT_QUOTES);
		$ret = nl2br($ret);
	
		return $ret;
	}
	
	public function get_rooms()
	{
		return htmlentities($this->rooms, ENT_QUOTES);
	}
	
	public function get_bathrooms()
	{
		return htmlentities($this->bathrooms, ENT_QUOTES);
	}
	
	public function get_rent()
	{
		return htmlentities($this->rent, ENT_QUOTES);
	}
	
	public function get_lease_term()
	{
		return htmlentities($this->lease_term, ENT_QUOTES);
	}
	
	public function get_damage_deposit()
	{
		return htmlentities($this->damage_deposit, ENT_QUOTES);
	}
	
	public function get_email()
	{
		$ret = null;
		
		$ret = wordwrap($this->email, 40, ' ', true);
		$ret = htmlentities($ret, ENT_QUOTES);
		$ret = nl2br($ret);
	
		return $ret;
	}
	
	public function get_phone()
	{
		return htmlentities($this->phone, ENT_QUOTES);
	}
	
	public function get_timestamp()
	{
		return htmlentities(date('r', $this->timestamp), ENT_QUOTES);
	}
	
	public function get_ip()
	{
		return htmlentities($this->ip, ENT_QUOTES);
	}
	
	public function get_required_credit_check()
	{
		$ret = null;
		
		if ($this->required_credit_check == 0)
		{
			$ret = 'No';
		}
		else if ($this->required_credit_check == 1)
		{
			$ret = 'Yes';
		}
		
		return $ret;
	}

	public function get_required_reference_check()
	{
		$ret = null;
		
		if ($this->required_reference_check == 0)
		{
			$ret = 'No';
		}
		else if ($this->required_reference_check == 1)
		{
			$ret = 'Yes';
		}
		
		return $ret;
	}
	
	public function get_allowed_pets()
	{
		$ret = null;
		
		if ($this->allowed_pets == 0)
		{
			$ret = 'No';
		}
		else if ($this->allowed_pets == 1)
		{
			$ret = 'Yes';
		}
		
		return $ret;
	}
	
	public function get_description()
	{
		$ret = null;
		
		$ret = wordwrap($this->description, 40, ' ', true);
		$ret = htmlentities($ret, ENT_QUOTES);
		$ret = nl2br($ret);
	
		return $ret;
	}
	
	//===================================
	// MUTATORS
	//===================================
	
	public function set_id($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->id = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set id to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_category_id($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->category_id = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not category id to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_location($value)
	{
		$ret = null;
		
		if (strlen($value) <= MAX_APT_LOCATION_LENGTH)
		{
			$this->location = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set location id to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_title($value)
	{
		$ret = null;
		
		if (strlen($value) <= MAX_APT_TITLE_LENGTH)
		{
			$this->title = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set title to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_bathrooms($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->bathrooms = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set bathrooms to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_timestamp($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->timestamp = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set timestamp to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_rent($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->rent = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set rent to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_lease_term($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->lease_term = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set lease term to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_required_credit_check($value)
	{		
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->required_credit_check = $value;
			$ret = true;
		}
		else
		{
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_required_reference_check($value)
	{		
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->required_reference_check = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set required reference check to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_allowed_pets($value)
	{		
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->allowed_pets = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set required reference check to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_damage_deposit($value)
	{
		$ret = null;
		
		if (is_numeric($value))
		{
			$this->damage_deposit = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set damage deposit to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_description($value)
	{
		$ret = null;
		
		if (strlen($value) < MAX_APT_DESCRIPTION_LENGTH)
		{
			$this->description = $value;
			$ret = true;
		}
		else
		{
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_email($value)
	{
		$ret = null;
		
		if (strlen($value) <= MAX_APT_EMAIL_LENGTH)
		{
			$this->email = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set email to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_phone($value)
	{
		$ret = null;
		
		if (strlen($value) <= MAX_APT_PHONE_LENGTH)
		{
			$this->phone = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set phone to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	public function set_ip($value)
	{
		$ret = null;
		
		if (strlen($value) < 100)
		{
			$this->ip = $value;
			$ret = true;
		}
		else
		{
			if (DEBUG)
			{
				die('debug error: could not set phone to: ' . $value);
			}
			$ret = false;
		}
		
		return $ret;
	}
	
	//===================================
	// DATABASE
	//===================================
	
	public function save($db)
	{
		$sql = null;
		$statement = null;
		$result = null;
		$escaped_array = null;
	
		try
		{
		// make sure this ip didn't write to db within the minimum 
		// threshhold time
		$sql = 'SELECT apartment_timestamp
				FROM '.DB_TABLE_PREFIX.'apartments
				WHERE apartment_ip = :ip
				ORDER BY apartment_id DESC';


		$statement = $db->prepare($sql);
		$statement->execute(array(':ip' => $_SERVER['REMOTE_ADDR']));

		$result = $statement->fetchAll();

		if (isset($result[0]) &&
			($result[0]['apartment_timestamp'] + INSERT_THRESHOLD) > time())
		{
			if (DEBUG)
			{
				die('insert threshold error');
			}
			return false;
		}

		// new ip, let them post
		$sql = 'INSERT INTO '.DB_TABLE_PREFIX.'apartments (apartment_timestamp, category_id, apartment_ip, 
					apartment_location, apartment_title, apartment_bathrooms, 
					apartment_rent, apartment_lease_term, 
					apartment_required_credit_check, 
					apartment_required_reference_check, 
					apartment_damage_deposit, apartment_description, 
					apartment_allowed_pets, apartment_email, apartment_phone)
				VALUES (:timestamp, :category_id, :ip, :location, :title, :bathrooms, :rent, 
						:lease_term, :required_credit_check, :required_reference_check, 
						:damage_deposit, :description, :allowed_pets, :email, :phone)';
		
		$statement = $db->prepare($sql);

		// execute statement
		$this->set_timestamp(time());
		$this->set_ip($_SERVER['REMOTE_ADDR']);
		$escaped_array = array(':timestamp' => $this->timestamp,
								':category_id' => $this->category_id,
								':ip' => $this->ip,
								':location' => $this->location,
								':title' => $this->title,
								':bathrooms' => $this->bathrooms,
								':rent' => $this->rent,
								':lease_term' => $this->lease_term,
								':required_credit_check' => $this->required_credit_check,
								':required_reference_check' => $this->required_reference_check,
								':damage_deposit' => $this->damage_deposit,
								':allowed_pets' => $this->allowed_pets,
								':email' => $this->email,
								':phone' => $this->phone,
								':description' => $this->description);
		$statement->execute($escaped_array);
						
		// check to make sure statement inserted
		if ($statement->rowCount() == 0)
		{
			if (DEBUG)
			{
				die('insert error, no rows inserted: ' . print_r($escaped_array));
			}
			return false;
		}
		
		}
		catch (Exception $exc)
		{
			if (DEBUG)
			{
				die('Database exception while saving: ' . $exc->getMessage());
			}
			return false;
		}

		return true;
	}
	
	public function retrieve($db)
	{
		$results = null;
		$sql = null;
		$statement = null;
	
		if ($this->id == null)
		{
			if (DEBUG)
			{
				die('invalid apartment id');
			}
			return false;
		}
		
		$results = array();
		$sql = 'SELECT *
				FROM '.DB_TABLE_PREFIX.'apartments
				WHERE apartment_id = :id
				LIMIT 1';

		try
		{
			$statement = $db->prepare($sql);
			$statement->execute(array(':id' => $this->id));
		}
		catch (Exception $exc)
		{	
			if (DEBUG)
			{
				die('Error retrieving: ' . $exc->getMessage());
			}
			return false;
		}

		$results = $statement->fetchAll(PDO::FETCH_ASSOC);

		if (count($results) > 0)
		{		
			Apartment::assign($this, $results[0]);
		}
		
		return true;
	}
	
	public function delete($db)
	{
		$results = null;
		$sql = null;
		$statement = null;
	
		if ($this->id == null)
		{
			if (DEBUG)
			{
				die('invalid apartment id');
			}
			return false;
		}
		
		$results = array();
		$sql = 'DELETE
				FROM '.DB_TABLE_PREFIX.'apartments
				WHERE apartment_id = :id
				LIMIT 1';

		try
		{
			$statement = $db->prepare($sql);
			$statement->execute(array(':id' => $this->id));
		}
		catch (Exception $exc)
		{	
			if (DEBUG)
			{
				die('Error deleting: ' . $exc->getMessage());
			}
			return false;
		}

		$results = $statement->fetchAll(PDO::FETCH_ASSOC);

		if (count($results) == 0)
		{		
			return false;
		}
		
		return true;
	}
	
	//===================================
	// DATABASE (STATIC FUNCS)
	//===================================
	
	public static function search_category(&$apartments, $category_id, $offset, $db)
	{
		$categories = null;
		$results = null;
		$sql = null;
		$statement = null;
		$apartment = null;
	
		if (!is_numeric($category_id) || !is_numeric($offset))
		{
			if (DEBUG)
			{
				die('invalid category or offset input');
			}
			return false;
		}
		
		$categories = array();
		$results = array();
		$sql = 'SELECT *
				FROM '.DB_TABLE_PREFIX.'apartments
				WHERE category_id = '.$category_id.'
				ORDER BY apartment_id DESC
				LIMIT '.$offset.', '.RESULTS_PER_PAGE;

		try
		{
			$statement = $db->prepare($sql);
			$statement->execute();
		}
		catch (Exception $exc)
		{	
			if (DEBUG)
			{
				die('Error searching by category: ' . $exc->getMessage());
			}
			return false;
		}

		$results = $statement->fetchAll(PDO::FETCH_ASSOC);

		if (count($results) > 0)
		{		
			for($i = 0; $i < count($results); $i++)
			{
				$apartment = new Apartment();
				Apartment::assign($apartment, $results[$i]);
				array_push($apartments, $apartment);
			}
		}
		
		return true;
	}
	
	public static function search_category_total_rows($category_id, $db)
	{
		$results = null;
		$sql = null;
		$statement = null;
	
		if (!is_numeric($category_id))
		{
			if (DEBUG)
			{
				die('invalid category or offset input');
			}
			return false;
		}
		
		$results = array();
		$sql = 'SELECT count(*) AS total_count
				FROM '.DB_TABLE_PREFIX.'apartments
				WHERE category_id = :category_id';

		try
		{
			$statement = $db->prepare($sql);
			$statement->execute(array(':category_id' => $category_id));
		}
		catch (Exception $exc)
		{	
			if (DEBUG)
			{
				die('Error searching by category: ' . $exc->getMessage());
			}
			return false;
		}

		$results = $statement->fetchAll(PDO::FETCH_ASSOC);

		return $results[0]['total_count'];
	}
	
	private static function assign(&$apartment, $apartment_db)
	{
		$debug_errors = null;
		
		if (!$apartment->set_id($apartment_db['apartment_id']))
		{
			$debug_errors .= 'Unable to set timestamp';
		}
		
		if (!$apartment->set_timestamp($apartment_db['apartment_timestamp']))
		{
			$debug_errors .= 'Unable to set timestamp';
		}
		
		if (!$apartment->set_category_id($apartment_db['category_id']))
		{
			$debug_errors .= 'Unable to set category id';
		}
		
		if (!$apartment->set_ip($apartment_db['apartment_ip']))
		{
			$debug_errors .= 'Unable to set ip';
		}
		
		if (!$apartment->set_location($apartment_db['apartment_location']))
		{
			$debug_errors .= 'Unable to set location';
		}
		
		if (!$apartment->set_title($apartment_db['apartment_title']))
		{
			$debug_errors .= 'Unable to set title';
		}
		
		if (!$apartment->set_bathrooms($apartment_db['apartment_bathrooms']))
		{
			$debug_errors .= 'Unable to set bathrooms';
		}
		
		if (!$apartment->set_rent($apartment_db['apartment_rent']))
		{
			$debug_errors .= 'Unable to set rent';
		}
		
		if (!$apartment->set_lease_term($apartment_db['apartment_lease_term']))
		{
			$debug_errors .= 'Unable to set lease_term';
		}
		
		if (!$apartment->set_required_credit_check($apartment_db['apartment_required_credit_check']))
		{
			$debug_errors .= 'Unable to set required credit check';
		}
		
		if (!$apartment->set_required_reference_check($apartment_db['apartment_required_reference_check']))
		{
			$debug_errors .= 'Unable to set required reference check';
		}
		
		if (!$apartment->set_damage_deposit($apartment_db['apartment_damage_deposit']))
		{
			$debug_errors .= 'Unable to set damage_deposit';
		}
		
		if (!$apartment->set_allowed_pets($apartment_db['apartment_allowed_pets']))
		{
			$debug_errors .= 'Unable to set allowed_pets';
		}
		
		if (!$apartment->set_email($apartment_db['apartment_email']))
		{
			$debug_errors .= 'Unable to set email';
		}
		
		if (!$apartment->set_phone($apartment_db['apartment_phone']))
		{
			$debug_errors .= 'Unable to set phone';
		}
		
		if (!$apartment->set_description($apartment_db['apartment_description']))
		{
			$debug_errors .= 'Unable to set description';
		}
		
		if ($debug_errors != null)
		{
			if (DEBUG)
			{
				die('debug error setting apartment row to object: ' . $debug_errors);
			}
			
			return false;
		}
		
		return true;
	}
}
?>
Return current item: Apartment Classfieds