<?php
/***************************************************************************
 *                            class.user.php
 *                           ----------------
 *   begin                : Dimanche 7 septembre 2008
 *   update               : Dimanche 7 septembre 2008
 *   email                : nemunaire@gmail.com
 *
 *
 ***************************************************************************/
class User{
	var $id_user,
		$pseudo,
		$auth_level,
		$race,
		$id_alliance,
		$id_grade_alliance,
		$mail,
		$envoyerMail,
		$technologies = array(),
		$destinationsFavoris,
		$amis,
		$combatAT_tactique,
		$combatDE_tactique,
		$modifUser = array();

	/**
	 * Constructeur
	 * @param    int	$id	id de la planète à importer
	 *
	 * @return   void
	 * @access   public
	 */
	function User($id = 0){
		if (!empty($id)) {
			global $var___db, $config, $table_user;
			global $technoloVAR;
			$bdd = new bdd();
			$bdd->connexion();
			$bdd->escape($id);
			$user = $bdd->unique_query("SELECT * FROM $table_user WHERE id = $id;");
			$bdd->deconnexion();
			if (!empty($user)) {
				$this->id_user = $user["id"];
				$this->pseudo = $user["pseudo"];
				$this->auth_level = $user["auth_level"];
				$this->race = $user["race"];
				$this->id_alliance = $user["id_alliance"];
				$this->id_grade_alliance = $user["id_grade_alliance"];
				$this->mail = $user["mail"];
				$this->envoyerMail = $user["envoyerMail"];
				$this->destinationsFavoris = $user["destinationsFavoris"];
				$this->combatAT_tactique = $user["combatAT_tactique"];
				$this->combatDE_tactique = $user["combatDE_tactique"];

				foreach($technoloVAR as $tech){
					$this->technologies[] = $user[$tech];
				}
			}
		}
	}

	/**
	 * Destructeur
	 *
	 * @return   void
	 * @access   public
	 */
	function __destruct(){
		global $var___db, $config, $table_user;
		$nb = count($this->modifUser);
		$out = array();
		$bdd = new bdd();
		$bdd->connexion();
		for($i = 0; $i < $nb; $i++){
			if (!is_array($this->{$this->modifUser[$i]})) {
				$bdd->escape($this->{$this->modifUser[$i]});
				if (is_int($this->{$this->modifUser[$i]}) || is_float($this->{$this->modifUser[$i]})) $out[] .= $this->modifUser[$i]." = ".$this->{$this->modifUser[$i]};
				else $out[] .= $this->modifUser[$i]." = '".$this->{$this->modifUser[$i]}."'";
			}
			else {
				if (ereg('file', $this->modifUser[$i])) {
					$prep = implode(';', $this->{$this->modifUser[$i]});
					$bdd->escape($prep);
					$out[] .= $this->modifUser[$i]." = '$prep'";
				}
				else {
					if ($this->modifUser[$i] == "batiments") $calc = "batiment";
					elseif ($this->modifUser[$i] == "technologies") $calc = "technolo";
					elseif ($this->modifUser[$i] == "casernes")$calc = "casernen";
					elseif ($this->modifUser[$i] == "terrestres") $calc = "nomterrn";
					elseif ($this->modifUser[$i] == "vaisseaux") $calc = "nomvaisn";

					if (!isset(${$calc.'VAR'})) global ${$calc.'VAR'};

					$nombr = count(${$calc.'VAR'});
					for($j = 0; $j < $nombr; $j++){
						$bdd->escape($this->{$this->modifUser[$i]}[$j]);
						$out[] .= ${$calc.'VAR'}[$j]." = ".$this->{$this->modifUser[$i]}[$j];
					}
				}
			}
		}
		if (!empty($out)) $plan = $bdd->unique_query("UPDATE $table_user SET ".implode(', ', $out)." WHERE id = ".$this->id_user.";");
		$bdd->deconnexion();
	}
}
?>