<?php
/**
 * Created by PhpStorm.
 * User: Jure
 * Date: 8.5.2014
 * Time: 15:18
 */
header('Content-type: text/plain; charset=utf-8');
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT . "init.php");
define("absolute_path", "/var/www/html/warpitDevelopment/");

class DownloadParser
{
    private $db;
    public static $id_structure = "0,8";

    function __construct($db)
    {
        $this->db = $db;
    }

    function returnParseSintax()
    {
        $sintaksa['1000000010'] = "28,8"; // interview_date
        $sintaksa['1000000020'] = "122,4"; //tstart
        $sintaksa['1000000030'] = "589,4"; //tstop
        $sintaksa['1000000040'] = "2103,1"; // D10
        $sintaksa['1000000050'] = "2104,2"; // D11
        $sintaksa['1000000060'] = "2106,2"; //D15a
        $sintaksa['1000000070'] = "2111,2"; // D40a
        $sintaksa['1000000080'] = "2113,2"; // D40b
        $sintaksa['1000000090'] = "2115,2"; //D40c

        return $sintaksa;
    }

    function getDataFromServer($pathfileName)
    {
        $allTextData = "";
        $handle = fopen($pathfileName, "r");
        if ($handle) {
            while (($line = fgets($handle)) !== false) {
                //$line = utf8_encode(fgets($handle));
                $allTextData .= $line;
            }
        } else {
            print 'Error opening file';
        }
        fclose($handle);
        return $allTextData;
    }

    function parseVisitorData($data)
    {

        $l = explode("\n", $data);
        foreach ($l as $line) {
            $t = explode("\t", $line);
            $t = preg_replace('/[^(\x20-\x7F)]*/', '', $t);
            $id = $t[0];
            if ($t[1] == "Address") {

                $data = json_decode($t[2], true);
                $sql = "INSERT IGNORE INTO nfield.project_data_raw
						SET
						id_structure 	= 	" . $id . ",
						data_type		= 1,
						data_value		= '" . mysql_real_escape_string($data['details']) . "'";
                $this->db->SQLexecute($sql);
            } elseif ($t[1] == "InterviewerId") {
                echo "Interviewer ID: " . $t[2] . "<br>";
                $sql = "INSERT IGNORE INTO nfield.project_data_raw
						SET
						id_structure 	= 	" . $id . ",
						data_type		= 2,
						data_value		= '" . str_replace("\"", "", $t[2]) . "'";
                $this->db->SQLexecute($sql);
            } elseif ($t[1] == "SamplingPointId") {
                echo "Sample ID: " . $t[2] . "<br>";
            } elseif ($t[1] == "SamplingPointGroupId") {
                echo "Sample Group ID: " . $t[2] . "<br>";
            }
        }
    }

    function parseOpenQuestionsData($data)
    {
        $all = explode("\n", $data);
        foreach ($all as $line) {
            $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
            $p = explode(" ", $line, 2);
            $sql = "INSERT IGNORE INTO nfield.project_data_check SET
						id_structure 	= 	" . substr($p[0], 0, 8);

            $this->db->SQLexecute($sql);
            $sql = "INSERT IGNORE INTO nfield.project_data_raw SET
						id_structure 	= 	" . substr($p[0], 0, 8) . ",
						data_type 		= 	" . substr($p[0], 8, 10) . ",
						data_value 		= 	'" . mysql_real_escape_string($p[1]) . "'";
            $this->db->SQLexecute($sql);
        }
    }

    function parseDat($sintaksa, $data)
    {

        $l = explode("\n", $data);

        foreach ($l as $line) {

            $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);

            $t = explode(",", DownloadParser::$id_structure);

            $id = substr($line, $t[0], $t[1]);

            if (!$id) continue;
            unset($data);
            foreach ($sintaksa as $k => $v) {

                $t = explode(",", $v);
                $data = substr($line, $t[0], $t[1]);

                $sql = "INSERT INTO nfield.project_data_raw SET id_structure = " . $id . ", data_type = " . $k . ", data_value='" . mysql_real_escape_string($data) . "'";

                $this->db->SQLexecute($sql);
                echo $sql . "<br>";
            }
        }
    }
}

$a = new DownloadParser(null);

$file = "http://sorspbx.rmplus.si/warpitDevelopment/nfield/docs/EB81.3-si.csv";
$data = $a->getDataFromServer($file);
$a->parseVisitorData($data);
$a->parseOpenQuestionsData($data);
exit;
$a->parseDat($a->returnParseSintax(), $data);
echo $data;
