<?php

if(!defined('BASEPATH')) exit('Hacking Attempt : Get Out of the system ..!');

class Templates_model extends CI_Model
{
	public function __construct(){
		parent::__construct();
	}
	

/*##########################################################*/
/*################ Merchants ####################*/
/*##########################################################*/

	public function get_templates($status=NULL){
		$this->db->select('templates.*');
		if($status>=0){
			$where = array('templates.status='=>$status);
			$this->db->where($where);
		}
		$this->db->order_by('templates.submited_on','ASC');
		$query = $this->db->get('templates');
		
		return $query->result();
	}
	
	public function get_single_templates($id){
		$this->db->select('*');
		$where = array('templateId='=>$id);
		$this->db->where($where);
		$query = $this->db->get('templates');		
		return $query->result();
	}
	
	public function get_single_templates_merchants($id){
		$this->db->select('*');
		$where = array('templateId='=>$id);
		$this->db->where($where);
		$query = $this->db->get('template_merchant');		
		return $query->result_array();
	}
	
	public function delete_templates($id){
		$this->db->where('templateId', $id );
		$this->db->delete('templates');
		
		$this->db->where('templateId', $id );
		$this->db->delete('template_merchant');
	}
	
	public function set_templates($data,$id){
		
		if($id>0)
		{
			$this->db->where('templateId', $id);
			$this->db->update('templates', $data['templates']);	
		}else{			
			$this->db->insert('templates', $data['templates']);			
			$id = $this->db->insert_id();
								
		}
		
		$this->updatetemplatemerchant($data['templates_merchant'],$id);		

	}
	
	public function updatetemplatemerchant($merchants,$id){
		$this->db->where('templateId', $id );
		$this->db->delete('template_merchant');
		
		foreach($merchants as $mch){
			$this->db->insert('template_merchant', array('templateId'=>$id,'merchantId'=>$mch,'updatedon'=>date('Y-m-d H:i:s')));			
			$insert_id = $this->db->insert_id();
		}
		
		
	}
		
/*##########################################################*/	
}

?>