<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Jure
 * Date: 26.3.14
 * Time: 11:28
 * To change this template use File | Settings | File Templates.
 */
include_once ("_dirinfo.php");
include_once ("../../../../init.php");
include_once ("../../../../_ajaxManipulators/_class/class.gridData.php");
include_once ("../../../../config.php");

class ProjectSamplePoints extends GridDataManipulator
{

    function selectAction()
    {
        $project_sample_table = $_POST['projectSamplePointTableName'];
        $projectId = $_POST['project_id'];

        //Get capiIdTable from project
        $sql = "SELECT p.id_capiTable as id  FROM _Projects as p WHERE p.id = $projectId ";
        $result = $this->db->SQLexecute($sql);

        if (!$result) {
            return "{success:false,error:'Get Capi tables id'}";
        }
        $capiTableId = $this->db->fetchAssoc($result);
        $sql = "SELECT c.name  FROM _CapiTables as c WHERE c.id = ".$capiTableId['id'];
        $result = $this->db->SQLexecute($sql);
        if (!$result) {
            return "{success:false,error:'Get Capi tables id'}";
        }
        $capiTableName = $this->db->fetchAssoc($result);
        $capiName = 'capi___'.$capiTableName['name'];

        $sql = "SELECT s.id_sample,s.user_id,s.id_sampleTable,c.id, c.street, c.address, c.town, c.zip, s.planned_visit_time, s.num_interviews
                FROM ".$capiName." as c
                LEFT JOIN  " . $project_sample_table . " as s
                ON c.id = s.id_sample";

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

        while ($row = $this->db->fetchAssoc($res)) {
            $this->arr[] = $row;
        }
        $allData = array(
            'success' => 'true',
            'results' => sizeof($this->arr),
            'rows' => $this->arr
        );
        return json_encode($allData);
        // return parent::selectAction();
    }

    function exportAction()
    {
        if ($_REQUEST['group_id'] != 0)
            $this->addWhere("id_group = " . $_POST['group_id']);
        if (isset($_POST['template_id'])) {
            //we call the function which prepares the $_POST paramaters to create the appropriate SQL
            $this->createTemplateSQL();
        }
        return parent::exportAction();
    }


    function insertAction()
    {
        $res = parent::insertAction();
        $resEnc = json_decode($res, true);

        if ($resEnc['success']) {
            $sql = "INSERT INTO _group_link(id_group,id) VALUE(" . $_POST['group_id'] . ',' . $resEnc['insert_id'] . ")";
            if (!$this->db->SQLexecute($sql)) {
                return '{"success":false,"error":"' . $this->db->GetError() . '", STATEMENT: "' . $sql . '"}';
            }
        }

        return $res;
    }


}

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


$manipulator = new ProjectSamplePoints($db, $_REQUEST['RMtable']);

$comm = $_REQUEST['action'] . 'Action';
if (method_exists($manipulator, $comm))
    echo $manipulator->$comm();
else {

    Echo '{success:false,error:"Unknown action!"}';

}


?>
