<?php

require 'Slim/Slim.php';

$app = new Slim();

// MAILOVI
$app->post('/mail/:mail', 'DodajMail');

// ANKETE
$app->post('/anketa/', 'DodajAnketu');

$app->get('/prvi-dio/', 'getPrviDio');
$app->get('/misljenje/', 'getMisljenje');

// KORINICI
$app->get('/korisnik-single/:id', 'getKorisnikSingle');
$app->put('/korisnik-update/:id', 'updateKorisnik');
$app->delete('/korisnici-delete/:id', 'deleteKorisnici');





$app->run();

// dodaj sjednicu
function DodajMail($mail) {
	$request = Slim::getInstance()->request();
	$user = json_decode($request->getBody());
	$sql = "INSERT INTO mailovi (mail) VALUES ('$mail')";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql); 		
		$stmt->execute();				
		
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

// dodaj anketu
function DodajAnketu() {
	$request = Slim::getInstance()->request();
	$user = json_decode($request->getBody());
	$sql = "INSERT INTO ankete (pol, god, regija, stan, pare, rok, kvadrat, misljenje) VALUES (:pol, :god, :regija, :stan, :pare, :rok, :kvadrat, :misljenje)";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql); 	
		$stmt->bindParam("pol", $user->pol);
		$stmt->bindParam("god", $user->god);
		$stmt->bindParam("regija", $user->regija);
		$stmt->bindParam("stan", $user->stan);
		$stmt->bindParam("pare", $user->pare);
		$stmt->bindParam("rok", $user->rok);
		$stmt->bindParam("kvadrat", $user->kvadrat);
		$stmt->bindParam("misljenje", $user->misljenje);	
		$stmt->execute();				
		
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}


function getPrviDio() {
	$sql = "SELECT AVG(pare) as pare, AVG(kvadrat) as kvadrat, COUNT(pol) as broj, pol, stan
			FROM ankete 
			GROUP BY stan, pol";
	try {
		$db = getConnection();
		$stmt = $db->query($sql);  
		$rezultat = $stmt->fetchAll(PDO::FETCH_OBJ);
		$db = null;
		echo json_encode($rezultat);
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function getMisljenje() {
	$sql = "SELECT COUNT(misljenje) as misljenje
			FROM ankete 
			GROUP BY misljenje";
	try {
		$db = getConnection();
		$stmt = $db->query($sql);  
		$rezultat = $stmt->fetchAll(PDO::FETCH_OBJ);
		$db = null;
		echo json_encode($rezultat);
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function getKorisnikSingle($id) {
	$sql = "SELECT * FROM users WHERE uid = ".$id."";
	try {
		$db = getConnection();
		$stmt = $db->query($sql);  
		$rezultat = $stmt->fetchAll(PDO::FETCH_OBJ);
		$db = null;
		echo json_encode($rezultat);
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function updateKorisnik($id) {
	$request = Slim::getInstance()->request();
	$user = json_decode($request->getBody());
	$sql = "UPDATE users SET ime=:ime, prezime=:prezime, orgjedinica=:orgjedinica WHERE uid=:id";
	try {
		$db = getConnection();
		$stmt = $db->prepare($sql);  
		$stmt->bindParam("id", $id);
		$stmt->bindParam("ime", $user->ime);
		$stmt->bindParam("prezime", $user->prezime);
		$stmt->bindParam("orgjedinica", $user->orgjedinica);
		$stmt->execute();
		$db = null;
		echo json_encode($user); 
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}

function deleteKorisnici($id) {
	$sql = "DELETE FROM users WHERE uid=".$id;
	try {
		$db = getConnection();
		$stmt = $db->query($sql);  
		$rezultat = $stmt->fetchAll(PDO::FETCH_OBJ);
		$db = null;
		echo json_encode($rezultat);
	} catch(PDOException $e) {
		echo '{"error":{"text":'. $e->getMessage() .'}}'; 
	}
}




function getConnection() {
	$dbhost="72.167.233.101";
	$dbuser="kremici";
	$dbpass="Eur89linO!";
	$dbname="kremici";
	$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);	
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	return $dbh;
}
?>