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

class GeolocatedUsers_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		// Set table name
		$this->table = 'agent_plus';
		// Set orderable column fields
		$this->column_order = array('code_parrain','lastname', 'adresse', 'code_membre', 'email' , 'phone',  'active', 'provider_class',null, null);
		// Set searchable column fields
		$this->column_search = array('code_parrain','lastname', 'adresse', 'code_membre', 'email' , 'phone','provider_class');
		// Set default order
		$this->order = array('created' => 'desc');
	}

	/*
	 * Fetch members data from the database
	 * @param $_POST filter data based on the posted parameters
	 */
	public function getRows($postData)
	{
		$this->_get_datatables_query($postData);
		if ($postData['length'] != -1) {
			$this->db->limit($postData['length'], $postData['start']);
		}
		$query = $this->db->get();
		return $query->result();
	}

	/*
	 * Count all records
	 */
	public function countAll($postData)
	{
		$this->db->from($this->table);

		if(!empty($postData['category'])){
			$this->db->where('category', $postData['category']);
		}
		if(!empty($postData['network_id'])){
			$this->db->where('network_id', $postData['network_id']);
		}
		if(!empty($postData['code_parrain'])){
			$this->db->where('code_parrain', $postData['code_parrain']);
		}
		if(!empty($postData['town_id'])){
			$this->db->where('town_id', $postData['town_id']);
		}
		return $this->db->count_all_results();
	}

	/*
	 * Count records based on the filter params
	 * @param $_POST filter data based on the posted parameters
	 */
	public function countFiltered($postData)
	{
		$this->_get_datatables_query($postData);
		$query = $this->db->get();
		return $query->num_rows();
	}

	/*
	 * Perform the SQL queries needed for an server-side processing requested
	 * @param $_POST filter data based on the posted parameters
	 */
	private function _get_datatables_query($postData)
	{

		$this->db->from($this->table);

		if(!empty($postData['category'])){
			$this->db->where('category', $postData['category']);
		}
		if(!empty($postData['network_id'])){
			$this->db->where('network_id', $postData['network_id']);
		}
		if(!empty($postData['code_parrain'])){
			$this->db->where('code_parrain', $postData['code_parrain']);
		}
		if(!empty($postData['town_id'])){
			$this->db->where('town_id', $postData['town_id']);
		}

		$i = 0;
		// loop searchable columns
		foreach ($this->column_search as $item) {
			// if datatable send POST for search
			if ($postData['search']['value']) {
				// first loop
				if ($i === 0) {
					// open bracket
					$this->db->group_start();
					$this->db->like($item, $postData['search']['value']);
				} else {
					$this->db->or_like($item, $postData['search']['value']);
				}

				// last loop
				if (count($this->column_search) - 1 == $i) {
					// close bracket
					$this->db->group_end();
				}
			}
			$i++;
		}

		if (!empty($postData['order'])) {
			$this->db->order_by($this->column_order[$postData['order']['0']['column']], $postData['order']['0']['dir']);
		} else if (!empty($this->order)) {
			$order = $this->order;
			$this->db->order_by(key($order), $order[key($order)]);
		}
	}
}
