<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MSubscribers class
 *
 * @package GetID CMS/E-shop
 * @author George Georgaras
 **/
class MSubscribers extends Model {


	function MSubscribers()
	{
		parent::Model();
		log_message('debug', 'MSubscribers Model Initialized');		
	}
	function checkSubscriber($email){
		#initiate
		$check='not_done';
		$now = date("Y-m-d");
		$ch_email=$email;
		$this->db->where('email', $email);
		$this->db->where("status != 'subscribed'");
		$Q2 = $this->db->get('initial_list');
     	if ($Q2->num_rows() > 0){
	  		$row = $Q2->row_array();
			$data = array(
				'status' => 'subscribed',
				'sb_date'=>$now
			);
			$data2 = array(
				'name'=>$row['name'],
				'occupation'=>$row['occupation'],
				'email' => db_clean($_POST['email']),
				'company'=>$row['company'],
				'status' => 'active',
				'sb_date'=>$now
			);
			$this->db->insert('subscribers', $data2);
			$this->db->where('email',$email);
			$this->db->update('initial_list', $data);
			$check='done';
			
	 	}else{
			$check='not_done';
		}
		if($check=='not_done'){
			$this->db->where('email',$ch_email);
	     	$Q = $this->db->get('subscribers');
	     	if ($Q->num_rows() > 0){
		  		$check='exist';
		 	}
		}
	 	return $check;
	}
	function checkIfExistSub($email){
		#initiate
		$check_sub=false;
		$this->db->where('email', $email);
		$Q2 = $this->db->get('subscribers');
		if ($Q2->num_rows() > 0){
	  	$check_sub=true;
		}
		else {
		$check_sub=false;
		}
	 	return $check_sub;
	}

	function checkIfExistInit($email){
		#initiate
		$check_init=false;
		$this->db->where('email', $email);
		$Q3 = $this->db->get('initial_list');
		if ($Q3->num_rows() > 0){
	  	$check_init=true;
		}
		else {
		$check_init=false;
		}
	 	return $check_init;
	}

	function createSubscriber(){
		$now = date("Y-m-d");
		$data = array(
			//'name' => db_clean($_POST['name']),
			'email' => db_clean($_POST['email']),
			//'occupation' => db_clean($_POST['occupation']),
			//'company' => db_clean($_POST['company'])
			'sb_date'=>$now
			);
		$this->db->insert('subscribers', $data);
	}
	
	function removeSubscriber($email){

		$this->db->where('email', $email);
		$this->db->delete('subscribers');
	}
		function UnsubscribeInitial($email){
		$data = array(
			'status' => 'unsubscribed'
			);
		//$status='unsubscribed';
		$this->db->where('email', $email);
		$this->db->update('initial_list', $data);
	}
	
	function removeSubscriberID($id){
		$data = array(
			'status' => 'not-active'
		);
		$this->db->where('id', id_clean($id));
		$this->db->update('clients', $data);
	}
	function removeNewsSubscriberID($id){
		$this->db->where('id', id_clean($id));
		$this->db->delete('subscribers');
	}
	
	function updateSubscriber(){
		$data = array(
			'name' => db_clean($_POST['name']) ,
			'email' => db_clean($_POST['email'])
		);
		$this->db->where('id', id_clean($_POST['id']) );
		$this->db->update('subscribers', $data);
	}
	
	function getAllSubscribers(){
		$data = array();
		$this->db->where('status', 'active');
		$Q = $this->db->get('clients');
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getAllAllSubscribers(){
		$data = array();
		$this->db->where('status', 'not-active');
		$Q = $this->db->get('clients');
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getAllNewsSubscribers(){
		$data = array();
		$this->db->orderby("sb_date",'desc');
		$Q = $this->db->get('subscribers');
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getInitSubscribers() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('email');
		$this->db->from('clients');
		$this->db->where('status','active');
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getNewsSubscribers() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('email');
		$this->db->from('subscribers');
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getAllSubscribersforNewsletter(){
		$data = array();
		$Q = $this->db->query('select email from clients where status="active" UNION DISTINCT select email from subscribers');
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getSubscriber($id){
	    $data = array();
		$this->db->select('*');
		$this->db->from('clients');
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }

}//end class 
?>