<?php
if(!defined('INDEX')) { header('Location: ../'); exit; }
$titre = 'Destinations rapides';

$a = gpc('a');
$amas = gpc('amas', 'post');
$ss = gpc('ss', 'post');
$pos = gpc('pos', 'post');
if (!empty($a)) {
	$fav = explode(';', $queryUser['destinationsFavoris']);
	$cntfav = count($fav);

	$d = gpc('d');
	if (!empty($d) && $a == md5(date('AG').'@'.$d)) {
		for($i = 0; $i < $cntfav; $i++){
			if ($fav[$i] == $d) unset($fav[$i]);
		}
		$queryUser['destinationsFavoris'] = implode(';', $fav);
		$bdd->connexion();
		$bdd->query("UPDATE $table_user SET destinationsFavoris = '".$queryUser['destinationsFavoris']."' WHERE id = $id_user;");
		$bdd->deconnexion();
	}
	header('Location: ./?p=destinationsrapides');
	exit;
}
elseif (!empty($amas) && !empty($ss) && !empty($pos)) {
	$bdd->connexion();
	$plan = $bdd->unique_query("SELECT id FROM $table_planete WHERE galaxie = $amas AND ss = $ss AND position = $pos;");
	if ($plan) {
		if (in_array($plan['id'], explode(';', $queryUser['destinationsFavoris']))) {
			$bdd->deconnexion();
			erreur('Cette planète est déjà dans vos destinations rapides.', "red", '?p=destinationsrapides');
		}
		$queryUser['destinationsFavoris'] .= ";".$plan['id'];
		$bdd->query("UPDATE $table_user SET destinationsFavoris = '".$queryUser['destinationsFavoris']."' WHERE id = $id_user;");
	}
	else {
		$bdd->deconnexion();
		erreur('Impossible d\'ajouter cette planète, elle n\'est pas habitée !', "red", '?p=destinationsrapides');
	}
	$bdd->deconnexion();

	header('Location: ./?p=destinationsrapides');
	exit;
}
else {
	$page = 'destinsrapid';

	$fav = explode(';', $queryUser['destinationsFavoris']);
	$cntfav = count($fav);
	$destins = array();
	$bdd->connexion();
	for($i = 0; $i < $cntfav; $i++){
		$res = $bdd->unique_query("SELECT nom_planete, galaxie, ss, position FROM $table_planete WHERE id = ".$fav[$i].";");
		if ($res != false) {
			$destins[] = array($fav[$i], $res['nom_planete'], '['.$res['galaxie'].':'.$res['ss'].':'.$res['position'].']', md5(date('AG').'@'.$fav[$i]));
		}
	}
	$bdd->deconnexion();
	$template->assign('destins', $destins);
}
?>