<?php 
class ModelSocnetAuthSocnetAuth extends Model {

	public function setRecord($state, $redirect)
	{
		$this->db->query("DELETE FROM `" . DB_PREFIX . "socnetauth_records` 
			WHERE DATE_ADD(cdate, INTERVAL 15 MINUTE)<NOW()");
			
		$this->db->query("INSERT INTO `" . DB_PREFIX . "socnetauth_records` 
			SET 
				`state` = '".$this->db->escape($state)."',
				`redirect` = '".$this->db->escape($redirect)."',
				`cdate` = NOW()");
	}

	public function getRecord($state)
	{
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "socnetauth_records` 
			WHERE state='".$this->db->escape($state)."'");
		
		return $query->row;
	}
	
  	public function facebook_check($id) 
	{
		$query_check = $this->db->query("SELECT * 
			FROM `" . DB_PREFIX . "customer` 
			WHERE socnetauth_facebook_id='".$this->db->escape($id)."'");			
			
		return $query_check->row;
	}
	
	public function twitter_check($id) 
	{
		$query_check = $this->db->query("SELECT * 
			FROM `" . DB_PREFIX . "customer` 
			WHERE socnetauth_twitter_id='".$this->db->escape($id)."'");
			
		return $query_check->row;
	}
	
	public function vkontakte_check($id) 
	{
		$query_check = $this->db->query("SELECT * 
			FROM `" . DB_PREFIX . "customer` 
			WHERE socnetauth_vkontakte_id='".$this->db->escape($id)."'");
			
		return $query_check->row;
	}
	
	public function odnoklassniki_check($id) 
	{
		$query_check = $this->db->query("SELECT * 
			FROM `" . DB_PREFIX . "customer` 
			WHERE socnetauth_odnoklassniki_id='".$this->db->escape($id)."'");
			
		return $query_check->row;
	}
	
	public function facebook_preregistration($data)
	{
		/*				
			id=>100000402380563
			name=>Petrov Konstantin
			first_name=>Petrov
			last_name=>Konstantin
			link=>http://www.facebook.com/petrov.konstantin
			username=>petrov.konstantin
			email=>kin208@gmail.com
			timezone=>6
			locale=>en_US
			verified=>1
			updated_time=>2012-02-11T12:39:00+0000
		*/
		
		$this->db->query("DELETE FROM `" . DB_PREFIX . "socnetauth_preregistration` 
			WHERE DATE_ADD(cdate, INTERVAL 1 HOUR)<NOW()");
			
		$precode = md5(rand());
		
		$query_check = $this->db->query("INSERT INTO `" . DB_PREFIX . "socnetauth_preregistration` 
			SET 
				`soc_id`='".(int)$data['id']."',
				`type`='facebook',
				`firstname`='".$this->db->escape($data['first_name'])."',
				`lastname`='".$this->db->escape($data['last_name'])."',
				`email`='".$this->db->escape($data['email'])."',
				`precode`='".$precode."', 
				cdate=NOW()");
		
		return $precode;
	}
	
	public function get_predata($precode)
	{
		$query = $this->db->query("
			SELECT * FROM `" . DB_PREFIX . "socnetauth_preregistration` 
			WHERE `precode`='".$this->db->escape($precode)."'");
		
		return $query->row;
	}
	
	public function fixCustomer($data) 
	{
	
		$this->db->query("UPDATE " . DB_PREFIX . "customer 
							  SET
								socnetauth_facebook_id = '" . $this->db->escape($data['socnetauth_facebook_id']) . "', 
								socnetauth_twitter_id = '" . $this->db->escape($data['socnetauth_twitter_id']) . "', 
								socnetauth_vkontakte_id = '" . $this->db->escape($data['socnetauth_vkontakte_id']) . "', 
								socnetauth_odnoklassniki_id = '" . $this->db->escape($data['socnetauth_odnoklassniki_id']) . "',
								
								socnetauth_facebook_profile = '" . $this->db->escape($data['socnetauth_facebook_profile']) . "', 
								socnetauth_twitter_profile = '" . $this->db->escape($data['socnetauth_twitter_profile']) . "', 
								socnetauth_vkontakte_profile = '" . $this->db->escape($data['socnetauth_vkontakte_profile']) . "', 
								socnetauth_odnoklassniki_profile = '" . $this->db->escape($data['socnetauth_odnoklassniki_profile']) . "'
								
							  WHERE email = '" . $this->db->escape($data['email']) . "'");
	}
	
}
?>