<?php



class Admin_model extends CI_Model{	

	public function __construct(){
		//database loading
		$this->load->database();
	}

	public function signin($user_name,$password){
		$this->db->select('*')
				->from('login')
				->where('l_user',$user_name)
				->where('l_pwd',md5($password));
		$query=$this->db->get();
		
		// print_r($this->db);exit;
		// var_dump($query->num_rows());
		if($query->num_rows()==1){
			return $query->row_array();
		}
		else return false;
	}
	 
	public function check_role($i_user_id,$ul_type){
		$this->db->select('*')
				->from('assigned_user_roles_list')
				->where('aurl_ul_id',$i_user_id);
		$this->db->where('aurl_roles',$ul_type);
		$query=$this->db->get();
		
		// print_r($this->db);exit;
		if($query->num_rows()==1){
			return $query->row_array();
		}
		else return false;
	}
	public function insert_item($table,$data){	
		$this->db->insert($table,$data);		
		return $this->db->insert_id();
	}
	
	public function get_result_array($table,$field='',$id='',$order_field='',$order_type=''){
		$this->db->select('*')
				->from($table);
		
		if($id==''){
			if($order_field!='' && $order_type!=''){
				$this->db->order_by($order_field, $order_type);			
			}
			$query=$this->db->get();
			return $query->result_array();			
		}
		if($id!=''){
			$this->db->where($field,$id);
			$query=$this->db->get();
			return $query->row_array();			
		}
	}	
	
	public function update_data($table,$field,$id,$data){
		$this->db->where($field,$id)
				->update($table,$data);
		return $this->db->affected_rows();

	}
	
	public function get_data_with_one_join($table,$field='',$id='',$join_tb='',$join_tb_field='',$tb_filed=''){
		$this->db->select('*')
				->from($table)				
				->join($join_tb,''.$join_tb.'.'.$join_tb_field.'='.$table.'.'.$tb_filed.'');
		if($id==''){
			$query=$this->db->get();
			return $query->result_array();			
		}
		if($id!=''){
			$this->db->where($field,$id);
			$query=$this->db->get();
			return $query->row_array();			
		}
	}
	
	
	public function get_data_with_two_join($table,$field='',$id='',$join_tb='',$join_tb_field='',$tb_filed='',$join_tb1='',$join_tb_field1='',$tb_filed1=''){
		$this->db->select('*')
				->from($table)				
				->join($join_tb,''.$join_tb.'.'.$join_tb_field.'='.$table.'.'.$tb_filed.'')
				->join($join_tb1,''.$join_tb1.'.'.$join_tb_field1.'='.$table.'.'.$tb_filed1.'');
		if($id==''){
			$query=$this->db->get();
			return $query->result_array();			
		}
		if($id!=''){
			$this->db->where($field,$id);
			$query=$this->db->get();
			return $query->row_array();			
		}
	}
	
	public function get_result_array_with_one_where($table,$field='',$id=''){
		$this->db->select('*')
				->from($table);
			$this->db->where($field,$id);	
		$query=$this->db->get();
	//	print_r($this->db);
		return $query->result_array();	
	}	
	public function get_result_array_with_two_where($table,$field='',$id='',$field1='',$id1=''){
		$this->db->select('*')
				->from($table);
			$this->db->where($field,$id);	
			$this->db->where($field1,$id1);	
		$query=$this->db->get();
		// print_r($this->db);exit;
		return $query->result_array();	
	}	
	
	public function get_result_array_with_one_where_one_join($table,$field='',$id='',$join_field='',$j_table1='',$j_field1=''){
		$this->db->select('*')
				->from($table)
				->join($j_table1,''.$j_table1.'.'.$j_field1.'='.$table.'.'.$join_field.'');
		$this->db->where($field,$id);	
		$query=$this->db->get();
		return $query->result_array();	
	}

	public function get_result_array_with_two_where_one_join($table,$field='',$value='',$field1='',$value1='',$join_field='',$j_table1='',$j_field1=''){
		$this->db->select('*')
				->from($table)
				->join($j_table1,''.$j_table1.'.'.$j_field1.'='.$table.'.'.$join_field.'');
		$this->db->where($field,$value);	
		$this->db->where($field1,$value1);	
		$query=$this->db->get();
		return $query->result_array();	
	}
	
	public function get_counts($table){
		$this->db->select('*')
				->from($table);
		$query=$this->db->get();
		return $query->num_rows();			
		
	}	

	public function get_row_array_with_one_where($table,$field='',$id=''){
		$this->db->select('*')
				->from($table);
			$this->db->where($field,$id);	
		$query=$this->db->get();
		return $query->row_array();	
	}	
	

	public function get_row_array_with_two_where($table,$field='',$id='',$field1='',$id1=''){
		$this->db->select('*')
				->from($table);
			$this->db->where($field,$id);	
			$this->db->where($field1,$id1);	
		$query=$this->db->get();
		return $query->row_array();	
	}	
	

	public function get_meterial_list($ml_id='',$com_id=''){
		$this->db->select('*')
				->from('meterial_list')
				->join('commodity_list','commodity_list.com_id=meterial_list.ml_com_id')
				->join('units_list','units_list.unl_id=meterial_list.ml_unl_id');
		if($com_id!=''){
			$this->db->where('com_id',$com_id);
		}
		if($ml_id!=''){
			$this->db->where('ml_id',$ml_id);
			$query=$this->db->get();
			return $query->row_array();
		}				
		else{		
			$query=$this->db->get();
			return $query->result_array();
		}		
	}
	
	
	
	public function update_pwd($login_user,$old_pwd,$confirm_pwd){
		$this->db->select('l_pwd')
				->from('login')
				->where('l_pwd',$old_pwd)->where('l_id',$login_user);
		$query = $this->db->get();
		if($query->num_rows()==1){
			$this->db->where('l_pwd',$old_pwd)
					->where('l_id',$login_user)
					->update('login',array('l_pwd'=>$confirm_pwd));
			return $this->db->affected_rows();
		}
		else return '0';
	}


	public function user_list(){
		$this->db->select('*')
				->from('users_list')				
				->join('login','login.i_user_id=users_list.ul_id');
		$query=$this->db->get();
		return $query->result_array();			
		
	}
	








	function send_mail($from,$from_name,$to,$subject,$msg,$attachment='') {
		$config1 = array();
		$config1['protocol']    = 'smtp';
		$config1['smtp_host']    = 'ssl://smtp.gmail.com';
		$config1['smtp_port']    = '465'; 
		$config1['smtp_timeout'] = '7';
		$config1['smtp_user']    = 'jancycozak@gmail.com';
		$config1['smtp_pass']    = '7896@jancy';
		$config1['charset']    = 'utf-8';
		$config1['newline']    = "\r\n";
		$config1['mailtype'] = 'html'; // or html
		$config1['validation'] = TRUE; // bool whether to validate email or not      
		
		$this->email->initialize($config1);		
		
		$this->email->from($from, $from_name);
		$this->email->to($to);
		//
		$this->email->subject($subject);
		$this->email->message($msg);
		$this->email->attach(''.$attachment.'');
		$this->email->send();
		return 'ok';
	}
	
	public function send_sms($s_msg,$s_number){
		// $request='https://api-alerts.solutionsinfini.com/v4/?api_key= A6acbc3bdd7fcc142d6bd84c3007d7fd2 &method=sms&message='.$s_msg.'&to='.$s_number.'&sender=HGENGG&unicode=auto';
		//$request = str_replace(" ", '%20', $request);
		echo $request;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $request);
		$resuponce = curl_exec($ch);
		var_dump($resuponce);
		curl_close($ch); 
		return true;
		 
	}
	
	








	
	
	





	

	


	





}	

?>