<?php
class Campaign_model
  extends CI_Model {
	
	public function __construct()
	{
		$this->load->database();	
		// mysql_set_charset('utf8');
	}

	public function create_campaign($campaign)
	{
		$query = $this->db->insert('campaign', $campaign);
		$query = $this->db->get_where('campaign', array('id' => $this->db->insert_id()));
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->row();
		}
	}

	public function update_campaign($c_slug, $data)
	{
		$this->db->where('c_slug', $c_slug);
		$this->db->update('campaign', $data);		
	}	

	public function update_coupons_one($slug_coupons,$c_coupons, $data)
	{
		$this->db->where('slug_coupons', $slug_coupons);
		$this->db->where('c_coupons', $c_coupons);
		$this->db->update('coupons', $data);		
	}

	public function delete_coupon($slug)
	{
		$this->db->delete('coupons', array('slug_coupons' => $slug, 'used' => '' )); 
	}	
	public function delete_campaign($slug)
	{
		$this->db->delete('campaign', array('c_slug' => $slug )); 
		$this->db->delete('coupons', array('slug_coupons' => $slug )); 
	}
	public function create_coupon($coupon)
	{
		$query = $this->db->insert('coupons', $coupon);
		$query = $this->db->get_where('coupons', array('id' => $this->db->insert_id()));
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->row();
		}
	}

	public function get_user($log)
	{
		$query = $this->db->get_where('users', array('login' => $log));
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->row();		
		}
	}

	public function save_password($pwd, $id)
	{
		$update['password'] = md5($pwd);
		$this->db->update('users', $update, 'id = ' . $id);
	}
	public function get_campaign()
	{
		$query = $this->db->get('campaign');
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->result();
		}
	}
	public function get_campaign_one($c_slug)
	{
		$query = $this->db->get('campaign');
		$query = $this->db->get_where('campaign',array('c_slug'=>$c_slug) );
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->row();
		}
	}	
	public function get_coupons_one($c_slug)
	{
		$query = $this->db->get('coupons');
		$query = $this->db->get_where('coupons',array('slug_coupons'=>$c_slug, 'used' => '') );
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->row();
		}
	}

	public function get_coupons_unused($c_slug)
	{
		$query = $this->db->get('coupons');
		$query = $this->db->get_where('coupons',array('slug_coupons'=>$c_slug, 'used' => '') );
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->result();
		}
	}
	public function get_coupons_used($c_slug)
	{
		$query = $this->db->get('coupons');
		$query = $this->db->get_where('coupons',array('slug_coupons'=>$c_slug, 'used' => 'yes') );
		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->result();
		}
	}
	public function get_coupons_used_ip($c_slug, $ip)
	{
		$query = $this->db->get('coupons');
		$query = $this->db->order_by('id',"desc");
		$query = $this->db->limit(1);
		$query = $this->db->get_where('coupons',array('slug_coupons'=>$c_slug, 'ip_use' => $ip ,'used' => 'yes') );

		if ($query->num_rows() == 0) {
			return false;
		} else {
			return $query->result();
		}
	}

}
?>