<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Testing_dev extends CI_Model {
	function parseOrderby($col){
		if($col==1){
			return "error_type"; 
		}else if($col==2){
			return "date"; 
		}else if($col==3){
			return "error_msg"; 
		}else if($col==4){
			return "respond_msg"; 
		}else if($col==5){
			return "try_by"; 
		}else{
			return "status";
		}
	}

	function get_bugs_list($col=31,$asc_desc='ASC',$begin=0,$key=''){
		$orderby = $this->parseOrderby($col);
		$where = '';
		if(!empty($key)){
			$where .= ' WHERE (error_msg LIKE "%'.$key.'%" OR respond_msg LIKE "%'.$key.'%" OR try_by LIKE "%'.$key.'%" OR respond_msg LIKE "%'.$key.'%" OR handling_by LIKE "%'.$key.'%") ';
			if (strpos('reported', strtolower($key)) !== false) {
    			$where .= ' OR status = 1';
			}
			if (strpos('readed', strtolower($key)) !== false) {
    			$where .= ' OR status = 2';
			}
			if (strpos('on progress', strtolower($key)) !== false) {
    			$where .= ' OR status = 3';
			}
			if (strpos('done', strtolower($key)) !== false) {
    			$where .= ' OR status = 4';
			}
			if (strpos('rejected', strtolower($key)) !== false) {
    			$where .= ' OR status = 5';
			}
		}
		try {
			return $this->db->query("SELECT SQL_CALC_FOUND_ROWS id,error_type, 
				date,error_msg,respond_msg,try_by,handling_by,status 
				FROM testing_dev ".$where." ORDER BY ".$orderby." ".$asc_desc." LIMIT 0,1500;");
		} catch (Exception $e) {
			
		}
	}

	function fecth_data_num(){
		$query = $this->db->query("SELECT FOUND_ROWS() as cnt;");
		if($query->num_rows>0){
			foreach ($query->result() as $row) {
				return $row->cnt;
			}
    		return $query->num_rows;
    	}else{
    		return 0;
    	}
	}

	function getData($id){
		try {
			$this->db->where('id',$id);
			return $this->db->get('testing_dev');
		} catch (Exception $e) {
			
		}
	}

	function addData($data){
		try {
			$this->db->insert('testing_dev',$data);
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function updateData($data,$id){
		try {
			$this->db->where('id',$id);
			$this->db->update('testing_dev',$data);
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}
}
?>