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

class BackgroundModel extends CI_Model { 

    public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	function get_rows($where)
    {
		$sql = "select background_tb.*, 
		               location_tb.title as location, 
		               site_tb.title as site, 
		               country_tb.title as country 
				from background_tb
				left join site_tb on background_tb.site_id = site_tb.id
				left join country_tb on background_tb.country_id = country_tb.id
				left join location_tb on background_tb.location_id = location_tb.id
		where background_tb.id > 0 and background_tb.isDeleted = 0 ".$where;
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function insert_row($insert_data)
	{
		$this->db->insert('background_tb', $insert_data);
	}

	function delete_row($edit_id)
    {		
		$this->db->delete('background_tb', array('id' => $edit_id));
	}
	
	function get_row($id)
    {
		$sql = "select * from background_tb where id = '".$id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	function get_row_data($where)
    {
		$sql = "select * from background_tb".$where;
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	public function update_row($update_data, $id)
	{
		$this->db->update('background_tb', $update_data, $id);
	}	
	
	function get_count($where)
    {
		$sql = "select count(background_tb.id) as cnt
				from background_tb
				left join location_tb on background_tb.location_id = location_tb.id".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();
		if(isset($row["cnt"]))
			return $row["cnt"];
		else
			return 0;
	}
	
	function get_video_rows($where)
    {
		$sql = "select background_tb.*, 
		               location_tb.type_id as type_id, 
		               site_tb.title as site_name
				from background_tb
				left join site_tb on background_tb.site_id = site_tb.id
				left join location_tb on background_tb.location_id = location_tb.id
				".$where;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
}