<?php
class Events_m extends CI_Model {


    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
	function get_used_code_listing_counter($code)
	{
		$code = trim($code);
		
		$code = str_replace('"','',$code);
		
		$sql = 'SELECT COUNT(id) as total from registration_participants WHERE code = "'.$code.'" AND confirmed = 1 AND deleted = 0';
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
	function get_imas_company()
	{		
		$sql = 'SELECT * from imas_membership2 ORDER BY company ASC';
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	function get_imas_company_info($id)
	{		
		$sql = 'SELECT * from imas_membership2 WHERE id ='.$id;
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
	function list_all_events()
	{		
		$sql = 'SELECT * from events WHERE deleted = 0 ORDER BY is_current DESC, event_date DESC';
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	
	
	
	function get_attendees_all($id,$re,$reg_type)
	{		
		$sql = 'SELECT * from registration_participants WHERE registration_id = '.$id;
		
		if($reg_type != '')
			$sql .= ' AND reg_type = '.$reg_type;
		
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
		
		
	function get_attendees($id)
	{		
		$sql = 'SELECT count(id) as total from registration_participants WHERE registration_id = '.$id;
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
	
		
	function info($id)
	{		
		$sql = 'SELECT * from events WHERE id = '.$id;
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
		
	function get_contact_person($id)
	{		
		$sql = 'SELECT * from registration WHERE deleted = 0 AND registration_id = '.$id.' ORDER BY transaction_num ASC';
	
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	function get_contact_person_info($id)
	{		
		$sql = 'SELECT * from registration WHERE id = '.$id;
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
	
	
	function get_registration_participants_by_event($event_id,$reg_type)
	{		
		if($reg_type != '')
		{
			$sql = 'SELECT * from registration_participants WHERE reg_type = '.$reg_type.' AND confirmed = 1 AND deleted = 0 AND event_id = '.$event_id.' ORDER BY qr_number ASC';
		}
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	
	function get_registration_participants_by_event_in_person($event_id,$reg_type)
	{		
		if($reg_type != '')
		{
			$sql = 'SELECT * from registration_participants WHERE in_person = 1 AND reg_type = '.$reg_type.' AND confirmed = 1 AND deleted = 0 AND event_id = '.$event_id.' ORDER BY qr_number ASC';
		}
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	function get_registration_participants($id,$reg_type)
	{		
		if($reg_type != '')
		{
			$sql = 'SELECT * from registration_participants WHERE reg_type = '.$reg_type.' AND deleted = 0 AND registration_id = '.$id.' ORDER BY qr_number ASC';
		}
		
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	function get_registration_participants2($id)
	{	
		
		$sql = 'SELECT * from registration_participants WHERE deleted = 0 AND event_id = '.$id.' ORDER BY qr_number ASC';
		
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
		
	function is_current_event()
	{		
		
		$sql = 'SELECT * from events WHERE is_current = 1';
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
	function get_events_promo_info($id)
	{		
		$sql = 'SELECT * from events_promo WHERE id = '.$id;
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
		
	function get_events_promo($event_id)
	{		
		$sql = 'SELECT * from events_promo WHERE status = 1 AND event_id = '.$event_id;
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	
	function get_class_capacity()
	{		
		$sql = 'SELECT * from class_capacity ORDER BY id ASC';
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->result_array();   	
	    }
		
		return $result;
	}
	
	
	function check_if_exist_promo($code_title)
	{		
		$sql = 'SELECT * from events_promo WHERE code_title = "'.$code_title.'" AND status = 1';
		
		$query = $this->db->query($sql);
	
        if($query->num_rows() == 0)
        {
            $result = array(); 
        }        
        else
        {
            $result = $query->row_array();   	
	    }
		
		return $result;
	}
	
	
	function delete_event($id)
	{		
		if($id == '')
			return false;
			
		$sql = 'UPDATE events set deleted = 1, datetime_deleted = "'.date('Y-m-d H:i:s').'" WHERE id = '.$id;
		
		$query = $this->db->query($sql);

	}
	
	function delete_promo($id)
	{		
		if($id == '')
			return false;
			
		$sql = 'UPDATE events_promo set status = 2, datetime_deleted = "'.date('Y-m-d H:i:s').'" WHERE id = '.$id;
		
		$query = $this->db->query($sql);

	}
	
	function update_paid($registration_id)
	{		
		if($registration_id == '')
			return false;
			
		$sql = 'UPDATE registration_participants set confirmed = 1 WHERE registration_id = '.$registration_id;
		
		$query = $this->db->query($sql);

	}
	
	
	function update_capacity($id,$amount)
	{		
		if($id == '')
			return false;
			
		$sql = 'UPDATE class_capacity set amount = '.$amount.' WHERE id = '.$id;
		
		$query = $this->db->query($sql);

	}
	
	
	function update_nopaid($registration_id)
	{		
		if($registration_id == '')
			return false;
			
		$sql = 'UPDATE registration_participants set confirmed = 0 WHERE registration_id = '.$registration_id;
		
		$query = $this->db->query($sql);

	}
	
	function delete_participants($registration_id)
	{		
		if($registration_id == '')
			return false;
			
		$sql = 'UPDATE registration_participants set deleted = 1 WHERE registration_id = '.$registration_id;
		
		$query = $this->db->query($sql);

	}
	
	
	function delete_participants2($registration_id)
	{		
		if($registration_id == '')
			return false;
			
		$sql = 'UPDATE registration_participants set deleted = 1 WHERE id = '.$registration_id;
		
		$query = $this->db->query($sql);

	}
	
	function reset_current()
	{		
			
		$sql = 'UPDATE events set is_current = 0';
		
		$query = $this->db->query($sql);

	}
	
			
	function update($id = '', $data = '') {

		$this->db->where('id', $id);

		$status = $this->db->update('events', $data);

		if ($status) {
			return true;
		} else {
			return false;
		}
	}
		
	function update_registration($id = '', $data = '') {

		$this->db->where('id', $id);
	
		$status = $this->db->update('registration', $data);

		if ($status) {
			return true;
		} else {
			return false;
		}
	}
	
	function update_attendee($id = '',$registration_id, $data = '') {

		$this->db->where('registration_id', $registration_id);
		$this->db->where('id', $id);

		$status = $this->db->update('registration_participants', $data);

		if ($status) {
			return true;
		} else {
			return false;
		}
	}
	
	
		
	function add($data = '') {

		$status = $this->db->insert('events', $data);

		if ($status) {
			return $this->db->insert_id();
		} else {
			return false;
		}
	}	
	
	function add_promo($data = '') {

		$status = $this->db->insert('events_promo', $data);

		if ($status) {
			return $this->db->insert_id();
		} else {
			return false;
		}
	}	
	  
}
?>