<?php
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."init.php");


$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

$sentData = $_POST['dataProject'];
$searchString = $_POST['search']['value'];

$tmp_array = explode("-", $sentData);
$tmp_array = explode("&", end($tmp_array),2);
$id_session = $tmp_array[0];

if ($id_session) {
	$search = new respondentSearch($db, $id_session, $searchString);
	$search_result = $search->getRespondentData();
	$arr = array();

	$returnData['draw'] = $_POST['draw'];
	$returnData['recordsTotal'] = $search->allRows;
	$returnData['recordsFiltered'] = $search->allRows;
	$returnData['data'] = $search_result?$search_result:array();
	print json_encode($returnData);


}




class respondentSearch {

	protected $db;
	protected $id_session;
	protected $id_project;
	protected $searchString;

	protected $telname;
	protected $projname;

	public $allRows;


	function __construct($db, $id_session, $searchString)
	{
		$this->db = $db;
		$this->id_session = $id_session;
		$this->searchString = $searchString;
		//echo "id session:".$this->id_session."<br>";
		//echo "search string:".$this->searchString."<br>";
	}

	protected function getProjectName() {
		$sql = "SELECT _Projects.id, _Projects.proj_name, _Projects.id_telname, _TelephoneTables.name, loginSID
				FROM _www_ids
				LEFT JOIN _Projects
				ON _www_ids.id_project = _Projects.id
				LEFT JOIN _TelephoneTables
				ON _Projects.id_telname = _TelephoneTables.id
				WHERE _www_ids.id=".$this->id_session;

		$res = $this->db->SQLexecute($sql);
		$row = $this->db->fetchAssoc($res);


		$this->telname = $row['name'];
		$this->projname = $row['proj_name'];
		$this->id_project = $row['id'];
	}


	protected function getWhere() {
		if ($this->searchString) {
			if (strpos($this->searchString,"=") !== false) {
				$searchString = "AND (a.id".$this->searchString.")";
			} else {
				$addSearchFields = $this->additionalSearchFieds();
				$searchString = "AND (a.address LIKE '%" . $this->searchString ."%' OR a.name LIKE '%".$this->searchString."%' OR a.telephone LIKE '%".$this->searchString."%' $addSearchFields)";
			}
			return $searchString;
		} else {
			return "";
		}
	}


	protected function additionalSearchFieds() {
		$sql = "SELECT respondent_searched_fields FROM _Projects WHERE id=".$this->id_project;
		//echo "$sql<br>";
		$res = $this->db->SQLexecute($sql);
		$row = $this->db->fetchAssoc($res);

		if ($row['respondent_searched_fields']) {
			$allFields_arr = explode(";",$row['respondent_searched_fields']);
			foreach ($allFields_arr AS $item) {
				$searhFields[] = " OR a.".$item." LIKE '%".$this->searchString."%'";
			}
			return implode(" ",$searhFields);
		} else {
			return null;
		}
	}

	protected function getOrder()
	{
		$column = "a.id";
		$columnKey = $_POST['order'][0]['column'];
		$column = $_POST['columns'][$columnKey]['data'];
		$column = "a." . $column;
		return " ORDER BY " . $column . " " . $_POST['order'][0]['dir'];
	}


	public function getRespondentData()
	{
		$this->getProjectName();
		$where_cond = $this->getWhere();

		$sql = "SELECT a.id, a.oms, a.telephone, a.name, a.address
				FROM ".$this->projname."___telIndex AS b
				LEFT JOIN tel___".$this->telname." AS a
				ON b.id_tel=a.id";

		try {
			$res = $this->db->SQLexecute($sql);
			$this->allRows = $this->db->NumRows($res);
		} catch (Exception $e) {
			return null;
		}



		$sql.=" WHERE 1=1 ".$where_cond;
		$sql .= $this->getOrder();
		$sql .= " LIMIT " . intval($_POST['length']) . " OFFSET " . intval($_POST['start']);
		//echo "$sql";
		try {
			$res = $this->db->SQLexecute($sql);
			unset($arr);
			while ($row  = $this->db->fetchAssoc($res))
			{
			//	$arr[$row['id']] = $row['id']." - ".$row['oms']."/".$row['telephone']." - ".$row['name'].", ".$row['address'];
				$arr[] = array(
					'id' => $row['id'],
					'oms' => $row['oms'],
					'telephone' =>	$row['telephone'],
					'name' => $row['name'],
					'address' => $row['address']
				);
			}
			return $arr;
		} catch(Exception $e) {
			return null;
		}
	}


}

?>
