<?php

class patient
{
    var $id_pat;
    var $id_zone;
    var $pat_sexe;
    var $pat_nom;
    var $pat_prenom;
    var $pat_datenaiss;
    var $pat_nom_ep;
    var $pat_adresse;
    var $pat_code;
    var $pat_ville;
    var $pat_tel;
    var $pat_mail;
    var $pat_envoi;
    var $pat_qui;
    var $pat_hab;

    function patient($id_pat = "", $id_zone = "", $pat_sexe = "", $pat_nom = "", $pat_prenom = "", $pat_datenaiss = "", $pat_nom_ep = "", $pat_adresse = "", $pat_code = "", $pat_ville = "", $pat_tel = "", $pat_mail = "", $pat_envoi = "", $pat_qui = "", $pat_hab = "")
    {
        $this->id_pat = $id_pat;
        $this->id_zone = $id_zone;
        $this->pat_sexe = $pat_sexe;
        $this->pat_nom = $pat_nom;
        $this->pat_prenom = $pat_prenom;
        $this->pat_datenaiss = $pat_datenaiss;
        $this->pat_nom_ep = $pat_nom_ep;
        $this->pat_adresse = $pat_adresse;
        $this->pat_code = $pat_code;
        $this->pat_ville = $pat_ville;
        $this->pat_tel = $pat_tel;
        $this->pat_mail = $pat_mail;
        $this->pat_envoi = $pat_envoi;
        $this->pat_qui = $pat_qui;
        $this->pat_hab = $pat_hab;
    }

    function get()
    {
        global $error;

        $q = "SELECT * FROM patient WHERE id_pat = '" . $this->id_pat . "'";
        $r = mysql_query($q);

        if ($r)
        {
            if ($t = mysql_num_rows($r))
            {
                $row = mysql_fetch_assoc($r);

                foreach ($row AS $key => $value)
                    $this->{$key} = $value;

                return true;
            }
        }
        else
            die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

        return false;
    }

    function check()
    {
        global $error;

        $q = "SELECT * FROM patient WHERE id_pat = '" . $this->id_pat . "'";
        $r = mysql_query($q);

        if ($r)
        {
            if ($t = mysql_num_rows($r))
                return true;
        }
        else
            die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

        return false;
    }

    function edit()
    {
        global $error;

        if (!$this->id_pat)
        {
            $error = 2;
            return false;
        }

        if ($this->id_pat && $this->check())
            $q = "UPDATE patient SET id_zone = '" . $this->id_zone . "', pat_sexe = '" . $this->pat_sexe . "', pat_nom = '" . sqldata($this->pat_nom) . "', pat_prenom = '" . sqldata($this->pat_prenom) . "', pat_datenaiss = '" . sql_date($this->pat_datenaiss) . "', pat_nom_ep = '" . sqldata($this->pat_nom_ep) . "', pat_adresse = '" . sqldata($this->pat_adresse) . "', pat_code = '" . sqldata($this->pat_code) . "', pat_ville = '" . sqldata($this->pat_ville) . "', pat_tel = '" . sqldata($this->pat_tel) . "', pat_mail = '" . sqldata($this->pat_mail) . "', pat_envoi = '" . $this->pat_envoi . "', pat_qui = '" . sqldata($this->pat_qui) . "', pat_hab = '" . $this->pat_hab . "' WHERE id_pat = '" . $this->id_pat . "'";
        else
            $q = "INSERT INTO patient (id_zone, pat_sexe, pat_nom, pat_prenom, pat_datenaiss, pat_nom_ep, pat_adresse, pat_code, pat_ville, pat_tel, pat_mail, pat_envoi, pat_qui, pat_hab) VALUES ('" . $this->id_zone . "', '" . $this->pat_sexe . "', '" . sqldata($this->pat_nom) . "', '" . sqldata($this->pat_prenom) . "', '" . sql_date($this->pat_datenaiss) . "', '" . sqldata($this->pat_nom_ep) . "', '" . sqldata($this->pat_adresse) . "', '" . sqldata($this->pat_code) . "', '" . sqldata($this->pat_ville) . "', '" . sqldata($this->pat_tel) . "', '" . sqldata($this->pat_mail) . "', '" . $this->pat_envoi . "', '" . sqldata($this->pat_qui) . "', '" . $this->pat_hab . "')";

        $r = mysql_query($q);

        if ($r)
            return true;
        else
            die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

        return false;
    }

    function delete()
    {
        global $error;

        $q = "DELETE FROM patient WHERE id_pat = '" . $this->id_pat . "'";
        $r = mysql_query($q);

        if ($r)
            return true;
        else
            die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

        return false;
    }

    function form()
    {
        echo "<div id=\"form-patient\">\n";
        echo "<form action=\"" . basename($_SERVER["PHP_SELF"]) . "\" method=\"post\" name=\"patient\">\n";

        echo "<table>\n";

        echo "<tr>\n";
        echo "<td>Zone géographique <span class=\"form-need\">*</span></td>\n";

        $q = "SELECT id_zone AS row_key, zone_nom AS row_value FROM zone_geo";
        $r = mysql_query($q);

        echo "<td>";
        echo "<select name=\"id_zone\" size=\"1\">";

        while ($tmp = mysql_fetch_assoc($r))
            echo "<option value=\"" . $tmp["row_key"] . "\"" . ($this->id_zone == $tmp["row_key"] ? " selected=\"selected\"" : "") . ">" . $tmp["row_value"] . "</option>";
        
        echo "</select>";
        echo "</td>\n";
        echo "</tr>\n";

        echo "<tr>\n";
        echo "<td>Sexe <span class=\"form-need\">*</span></td>\n";
        echo "<td><input type=\"text\" name=\"pat_sexe\" value=\"" . $this->pat_sexe . "\" /></td>\n";
        echo "</tr>\n";

        echo "<tr>\n";
        echo "<td>Nom</td>\n";
        echo "<td><textarea name=\"pat_nom\">" . $this->pat_nom . "</textarea></td>\n";
        echo "</tr>\n";

        echo "<tr>\n";
        echo "<td>Prénom</td>\n";
        echo "<td><input type=\"text\" name=\"pat_prenom\" value=\"" . $this->pat_prenom . "\" /></td>\n";
        echo "</tr>\n";

        echo "<tr>\n";
        echo "<td>Adresse</td>\n";
        echo "<td><input type=\"text\" name=\"pat_adresse\" value=\"" . $this->pat_adresse . "\" /></td>\n";
        echo "</tr>\n";

        echo "</table>\n";

        echo "<input type=\"hidden\" name=\"id_pat\" value=\"" . $this->id_pat . "\" />\n";
        echo "<input type=\"submit\" name=\"edit_submit\" value=\"Enregistrer\" />\n";
        echo "</form>\n";
        echo "</div>\n";
    }
}

include ("dbconfig.php");
include ("functions.php");
include ("header.php");

if (!mysql_connect($dbconfig["server"], $dbconfig["login"], $dbconfig["pass"]) || !mysql_select_db($dbconfig["database"]))
  die ("Erreur m&eacute;canique " . mysql_errno() . " : " . mysql_error());

$pat = new patient(4);
$pat->get();
$pat->form();

include ("footer.php");

?> 