<?php
set_time_limit(0);
$filename = "insert_cars.sql";
$filehandle = fopen($filename, "a+b");
$vehicle_id = 0;
$query = "INSERT INTO `vroom360`.`vehicle`(`vehicle_id`, `vehicle_make`, `vehicle_model`, `vehicle_year`) VALUES ({vehicle_id}, {vehicle_make}, {vehicle_model}, {vehicle_year});";
fwrite($filehandle, date('d F Y h:i:s A', time()). chr(13).$rawRequest.chr(13).'-------------'.chr(13).chr(13));

 create a new cURL resource
$ch = curl_init();

 set URL and other appropriate options
$options = array(
                 CURLOPT_HEADER => false,
                 CURLOPT_RETURNTRANSFER  => 1,
                 CURLOPT_REFERER => '',
                );

curl_setopt_array($ch, $options);

year - 2011 - 1991
makes from year. collect makes and id

for($i = 2011; $i>1990; $i--)
{
    $url = "http://www.kbb.com/jsdata/2.0.0.149_38333/_makes?vehicleclass=UsedCar&yearid=$i&filterbycpo=false&filter=&priceMin=&priceMax=&categoryId=0";
    curl_setopt($ch, CURLOPT_URL, $url);
    $makes = json_decode(curl_exec($ch), true);
    for($j = 0; $j<count($makes); $j++)
    {
        $modelUrl = "http://www.kbb.com/jsdata/2.0.0.149_38333/_models?vehicleclass=UsedCar&yearid=$i&makeid=".$makes[$j]['Id']."&filterbycpo=false&filter=&priceMin=&priceMax=&categoryId=0";
        curl_setopt($ch, CURLOPT_URL, $modelUrl);
        $models = json_decode(curl_exec($ch), true);
        for($k = 0; $k<count($models); $k++)
        {
            $vehicle_id += 1;
            $values = array(
                '{vehicle_id}' => $vehicle_id,
                '{vehicle_make}' => "'".$makes[$j]['Name']."'",
                '{vehicle_model}' => "'".$models[$k]['Name']."'",
                '{vehicle_year}' => $i
            );
            $insertQuery = strtr($query, $values);
            echo "$insertQuery<br/>";
            fwrite($filehandle, $insertQuery.chr(13));
        }
    }
}
model from make and year
http://www.kbb.com/jsdata/2.0.0.149_38333/_models?vehicleclass=UsedCar&yearid=2009&makeid=57&filterbycpo=false&filter=&priceMin=&priceMax=&categoryId=0
 close cURL resource, and free up system resources
curl_close($ch);
fclose($filehandle);
echo 'Done with data mining';

//Code to dump Unavailable car data in iOS array format so that we can straight away copy it to Objective-C.
//Remove NOT IN in the query to get available car markers...
//dont forget to change the name of the file to write to! Just trying to help my team mates. :)
$dbConnection = new mysqli(
                                    'localhost',
                                    'webuser',
                                    '2b00a6ee707290a755f1a880783fcc40',
                                    'vroom360',
                                    3306
                                   );
if ($dbConnection == null || $dbConnection == false)
{
    echo "\nCouldn't get hold of the DB.";
    return;
}
$filename = "UnavailableMakers.txt";
$filehandle = fopen($filename, "wb");
$unavailableQuery = 'select distinct vehicle_make from vehicle
where vehicle_make NOT IN (SELECT vehicle_make from vehicle where vehicle_year=?) order by vehicle_make asc';
$availableQuery = 'select distinct vehicle_make from vehicle where vehicle_year=?  order by vehicle_make asc';
$query = $unavailableQuery;
$stmt = $dbConnection->prepare($query);
for($year = 2011; $year > 1990; $year--)
{
    fwrite($filehandle, $year.chr(13));
    $stmt->bind_param('i', $year);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($make);
    while($stmt->fetch())
    {
        fwrite($filehandle, "@\"$make\",");
    }

    fwrite($filehandle, ' nil'.chr(13).'-------'.chr(13));
}
fclose($filehandle);
echo "\nDone"
?>
