<?php 
	require "../commonincludefiles.php";

	
	
	$start   = isset($_REQUEST['start'])  ? $_REQUEST['start']  :  0;
	$limit   = isset($_REQUEST['limit'])  ? $_REQUEST['limit']  : 30;
	$sort    = isset($_REQUEST['sort'])   ? $_REQUEST['sort']   : '';
	$dir     = isset($_REQUEST['dir'])    ? $_REQUEST['dir']    : 'ASC';
	$filters = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : null;
	
	// GridFilters sends filters as an Array if not json encoded
	if (is_array($filters)) {
    	$encoded = false;
	} else {
    	$encoded = true;
    	$filters = json_decode($filters);
	}
	
	// initialize variables*/
	
	$where = "";
	
	$order_field = "";
	$order_type = "";
	$qs = '';

	if (is_array($filters)) {
    	for ($i=0;$i<count($filters);$i++){
        	$filter = $filters[$i];
			// assign filter data (location depends if encoded or not)
        	if ($encoded) {
            	$field = $filter->field;
            	$value = $filter->value;
            	$compare = isset($filter->comparison) ? $filter->comparison : null;
            	$filterType = $filter->type;
        	} else {
            	$field = $filter['field'];
            	$value = $filter['data']['value'];
           	 	$compare = isset($filter['data']['comparison']) ? $filter['data']['comparison'] : null;
            	$filterType = $filter['data']['type'];
        	}
			switch($filterType){
            case 'string' : $qs .= " AND ".$field." LIKE '%".$value."%'"; break;
            case 'list' :
               if (strstr($value,',')){
                    $fi = explode(',',$value);
                    for ($q=0;$q<count($fi);$q++){
                        $fi[$q] = "'".$fi[$q]."'";
                    }
                    $value = implode(',',$fi);
                    $qs .= " AND ".$field." IN (".$value.")";
                }else{
                    $qs .= " AND ".$field." = '".$value."'";
                }	
				
            break;
            case 'boolean' : $qs .= " AND ".$field." = ".($value); break;
            case 'numeric' :
                switch ($compare) {
                    case 'eq' : $qs .= " AND ".$field." = ".$value; break;
                    case 'lt' : $qs .= " AND ".$field." < ".$value; break;
                    case 'gt' : $qs .= " AND ".$field." > ".$value; break;
                }
            break;
            case 'date' :
                switch ($compare) {
                    case 'eq' : $qs .= " AND ".$field." = '".date('Y-m-d',strtotime($value))."'"; break;
                    case 'lt' : $qs .= " AND ".$field." < '".date('Y-m-d',strtotime($value))."'"; break;
                    case 'gt' : $qs .= " AND ".$field." > '".date('Y-m-d',strtotime($value))."'"; break;
                }
            break;
        }
    }
    $where .= $qs;
}
	if($sort=="")
{
 	 $order_field = "member_id";
	 $order_type  = "ASC";
}
else
{
     $order_field = $sort;
	 $order_type  = $dir;
}

	if($_SESSION["aCollegeId"]!="" && isset($_SESSION["aCollegeId"])){
	    
		
		$objGroup = new MemberMgmt();
		
		/*$limit
		$start
		$where
		$order_field
		$order_type*/
		
		$rs = $objGroup->getAllRecords($limit,$start,$where,$order_field,$order_type);
		
		$total = dbRows($rs);
		//$total = $objGroup->getCountRecords(0,0,$where,"","");
		
		$i = 0;   
			   
		$MemberData = array("count" => $total, "data" => array());
	
		while($row = dbObject($rs)){
			$MemberData["data"][$i] = $row;   
			$i++;
		}
	
		echo json_encode($MemberData);
	}
?>