<?php

class subscribersModel extends baseModel
{
    public $tableName = "subscribers";
	
	public function subscribe($subscriberdata) {
		if (!$subscriberdata['subemail']) {
			header("Location: /");
			exit;
		}

		$this->db->where("email=?", $subscriberdata['subemail']);
		$u = $this->db->select("subscribers");
		if (count($u) > 0) {
			header("Location: /?msg=User+already+exists");
			exit;
		}

		$subscriberdata['code'] = rand(1,999);	
		
		$this->db->insert('subscribers', $subscriberdata);
	}	
	
	function getList() {
		// email, code, date, redeemed, interestedin
		//$this->db->order_by('sort ASC');
		//this->db->where('active=?', 1 );
		$subscribers = $this->db->select('subscribers');
		return $subscribers;
	}	
	
	function get($id = 0) {
		if (!is_numeric($id)) {
			return -1;
		}
		$this->db->where('id = ?', $id);
		$subscriber = $this->db->select('subscribers')[0];

		if ($subscriber) {
			$this->db->where('id=?', $id);
		}

		return $subscriber;
	}

	function getItem($id) {
		$this->db->where('id=?', $id);
		return $this->db->select('subscribers')[0];
	}
	
	function insert($data){
		$randomNo = rand(1,999);	
		$data['code'] = str_pad($randomNo, 4, '0', STR_PAD_LEFT);
		$this->db->insert('subscribers', $data);
	}
	
	function update($id, $data) {
		$this->db->where('id=?', $id);	
		$this->db->update('subscribers', $data);
	}	
	
	function delete($id) {
		$this->db->where('id=?', $id);
		$this->db->delete('subscribers');
	}
	
	///////////////////////////////////////////////
	// TOTAL SUBSCRIBERS FUNCTIONS
	///////////////////////////////////////////////
	
	// NOTE: You can set different values for the 'interestedin' column to track 
	// the various interests of subscribers. Of course, you have to then use this 
	// as a condition when checking if a subscriber's email already exist - thus
	// allowing you to have repeat emails (for different interests)
	
	function getTotalCount() {
		return $this->db->select('subscribers', 'COUNT(*) AS c')[0]['c'];
	}
	function getTotalPartnersCount()
	{
		$this->db->where("interestedin = ?", 'partners');
		return $this->db->select('subscribers', 'COUNT(*) AS c')[0]['c'];
	}	
	function getTotalPromosCount()
	{
		$this->db->where("interestedin = ?", 'reserve');
		return $this->db->select('subscribers', 'COUNT(*) AS c')[0]['c'];
	}	
	
	function getTotalBETACount()
	{
		$this->db->where("payment_status = ?", 'Completed');
		return $this->db->select('payments', 'COUNT(*) AS c')[0]['c'];
	}	

}