<?php

	include_once ("_dirinfo.php");
	include_once (PATH_TO_ROOT."init.php");
	include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");
	include_once(PATH_TO_ROOT."config.php");
	
	
	class sampleManipulator extends GridDataManipulator
	{
		public function selectAction()
		{		
			// pripravljeno, ce bo treba kdaj kaj manipulirati s podatki...

			$sql = "SELECT status, status_rec, opis FROM _TelephoneStatus ORDER BY orderBy";
			$res = $this->db->SQLexecute($sql);
			while ($row = $this->db->fetchAssoc($res))
			{
				$all_index_lab[$row['status']][$row['status_rec']] = $row['opis'];

			}
		
			$all_index_lab['-1']['0'] = "interviewer error or in dial process";
			$all_index_lab['0']['0'] = "free numbers";
			$all_index_lab['0']['1'] = "reactivated number";
			$all_index_lab['1']['1'] = "finished interview";
			$all_index_lab['1']['99'] = "browser shutdown or crash";
			$all_index_lab['3']['1'] = "person does not want to participate, after appointment";
			$all_index_lab['3']['2'] = "person does not want to participate, after appointment in the middle";
			$all_index_lab['89']['0'] = "canceled appointment";
			$all_index_lab['90']['0'] = "appointment in dial process";
			$all_index_lab['91']['1'] = "appointment on status screen";
			$all_index_lab['91']['2'] = "appointment in questionnaire";
			$all_index_lab['50']['0'] = "quota unsuccessful";
			$all_index_lab['51']['1'] = "quota full - start with new interview";
			$all_index_lab['51']['2'] = "quota full - exit interview";
			$all_index_lab['52']['0'] = "no more telephone numbers";
			$all_index_lab['53']['1'] = "question is final - start with new interview";
			$all_index_lab['53']['2'] = "question is final - exit interview";
			$all_index_lab['54']['1'] = "break the interview - start with new interview";
			$all_index_lab['54']['2'] = "break the interview - exit interview";
			
			
			$sql = "SELECT count(*) as cnt,status, status_rec FROM ".$_POST['RMtable']." GROUP BY status, status_rec";
			$res = $this->db->SQLexecute($sql);
			while ($row = $this->db->fetchAssoc($res))
			{
				$all_index_pct[$row['status']][$row['status_rec']] = $row['cnt'];
				$totalCount += $row['cnt']; 
				
				$data_arr['status'] = $row['status'];
				$data_arr['status_rec'] = $row['status_rec'];
				$data_arr['status_name'] = $all_index_lab[$row['status']][$row['status_rec']];
				$data_arr['total_count'] = $all_index_pct[$row['status']][$row['status_rec']];
			
				$this->arr[] = $data_arr;

			}
			
			$data_arr['status'] = "";
			$data_arr['status_rec'] = "";
			$data_arr['status_name'] = "<b>Total</b>";
			$data_arr['total_count'] = "<b>".$totalCount."</b>";
			$this->arr[] = $data_arr;
			
			return parent::selectAction();
		}
	}

	

	$db->ChangeDatabase($_POST['database']);
	

	$manipulator = new sampleManipulator($db,$_POST['RMtable']);

	$comm = $_REQUEST['action'].'Action';
	if(method_exists($manipulator,$comm))
		echo $manipulator->$comm();
	else 
	{
		echo '{success:false,error:"Unknown action!"}';	
	}


?>
