<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], 'inc.php');
if ($pos !== FALSE) {
    header('Location: ../index.php');
}

include_once('../inc/func.inc.php');
include_once('../inc/func_geo.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../inc/fic_age.inc.php');
include_once('../inc/fic_dos.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_hum.inc.php');
include_once('../inc/fic_int.inc.php');
include_once('../inc/lst.inc.php');
include_once('../classes/ezSQL/shared/ez_sql_core.php');
include_once('../classes/ezSQL/mysqli/ez_sql_mysqli.php');
include_once('../inc/func_sql.inc.php');
Archivelog(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);
$_POST = clean_str($_POST);

$td = '';
$codeage = '';
$idcage = 0;
$pbm_age = FALSE;
$totalcage = 0;
$action_to_do = '';

if (isset($_POST['nbutton'])) {
    if (trim(valpost('nbutton')) == 'enr') // On a cliqué sur valider
    {
        $action_to_do = 'valid';
    }
    if (trim(valpost('nbutton')) == 'del') // On a cliqué sur supprimer
    {
        $action_to_do = 'delete';
    }
}

if (isset($_GET['td'])) {
    $td = valget('td');
} else {
    if ($action_to_do <> '') {
        $td = valpost('td');
    }
}

if (isset($_GET['ca'])) {
    $codeage = valget('ca');
} else {
    if ($action_to_do <> '') {
        $codeage = valpost('ca');
    }
}

if (isset($_GET['ica'])) {
    $idcage = (int)valget('ica');
} else {
    if ($action_to_do <> '') {
        $idcage = (int)valpost('ica');
    }
}

$tx = '';
if (isset($_GET['tx'])) {
    $tx = valget('tx');
} else {
    if (isset($_POST['tx'])) {
        $tx = valpost('tx');
    } else {
        $pbm_age = TRUE;
    }
}
if (($td <> 'add') AND (($tx == 'dosd_cage') OR ($tx == 'coloc'))) {
    $pbm_age = TRUE;
}

if ($codeage <> '') {
    $row = lit_enr_age($codeage, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        $totalcage = nb_cage_in_age($codeage);
        if ($totalcage == 0) {
            $td = 'add';
        }
        $retourliste = FALSE;
        if ($action_to_do <> '') {
            $titre = valpost('titre');
            $idcage = valpost('ica');
            $code = valpost('ca');
            $civil = str_majuscules(valpost('civil'));
            $prenom = ucname(valpost('prenom'));
            $nom = str_majuscules(valpost('nom'));
            $fonction = str_majuscules(valpost('fonction'));
            $adr1 = str_majuscules(valpost('adr1'));
            $adr2 = str_majuscules(valpost('adr2'));
            $pays = str_majuscules(valpost('pays'));
            $dep = str_majuscules(valpost('dep'));
            $ville = str_majuscules(valpost('ville'));
            $telf = valpost('telf');
            $fax = valpost('fax');
            $telp = valpost('telp');
            $email = strtolower(valpost('email'));
            $mess = strtolower(valpost('mess'));
            $nota = valpost('nota');

            $rech = '';
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');

            switch ($td) {
                case 'add' :
                    $rech = 'INSERT INTO `' . TBCAGE . '`';
                    $rech .= ' (cage_datecrea,cage_datemod,cage_codeage,cage_codefonc,cage_civilite,cage_prenom,cage_nom,';
                    $rech .= 'cage_adr1,cage_adr2,cage_dep,cage_ville,cage_codepays,';
                    $rech .= 'cage_telf,cage_fax,cage_telp,cage_email,cage_mess,cage_nota) VALUES';
                    $rech .= " ('$datea','$datea','" . $db->escape($code) . "','" . $db->escape($fonction) . "','" . $db->escape($civil) . "','" . $db->escape($prenom) . "','" . $db->escape($nom) . "',";
                    $rech .= " '" . $db->escape($adr1) . "','" . $db->escape($adr2) . "','$dep','$ville','$pays',";
                    $rech .= "'$telf','$fax','$telp','$email','$mess','" . $db->escape($nota) . "')";
                    break;
                case 'edit' :
                    if (valsession('niv_log') >= 5) {
                        $rech = 'UPDATE `' . TBCAGE . "` SET `cage_datemod`='" . $datea . "', `cage_codeage`='" . $db->escape($code) . "', `cage_codefonc`='" . $db->escape($fonction) . "',";
                        $rech .= " `cage_civilite`='" . $db->escape($civil) . "', `cage_prenom`='" . $db->escape($prenom) . "', `cage_nom`='" . $db->escape($nom) . "', `cage_telf`='" . $telf . "', ";
                        $rech .= " `cage_adr1`='" . $db->escape($adr1) . "', `cage_adr2`='" . $db->escape($adr2) . "', `cage_dep`='" . $dep . "', `cage_ville`='" . $ville . "', `cage_codepays`='" . $pays . "', ";
                        $rech .= " `cage_fax`='" . $fax . "', `cage_telp`='" . $telp . "', `cage_email`='" . $email . "', `cage_mess`='" . $mess . "', ";
                        $rech .= " `cage_nota`='" . $db->escape($nota) . "'";
                        $rech .= "  WHERE `cage_id` = '" . $idcage . "'";
                    } else {
                        $rech = 'UPDATE `' . TBCAGE . "` SET `cage_datemod`='" . $datea . "', `cage_codeage`='" . $db->escape($code) . "', `cage_codefonc`='" . $db->escape($fonction) . "',";
                        $rech .= " `cage_civilite`='" . $db->escape($civil) . "', `cage_prenom`='" . $db->escape($prenom) . "', `cage_nom`='" . $db->escape($nom) . "', `cage_telf`='" . $telf . "', ";
                        $rech .= " `cage_adr1`='" . $db->escape($adr1) . "', `cage_adr2`='" . $db->escape($adr2) . "', `cage_dep`='" . $dep . "', `cage_ville`='" . $ville . "', `cage_codepays`='" . $pays . "', ";
                        $rech .= " `cage_fax`='" . $fax . "', `cage_telp`='" . $telp . "', `cage_email`='" . $email . "', `cage_mess`='" . $db->escape($mess) . "' ";
                        $rech .= "  WHERE `cage_id` = '" . $idcage . "'";
                    }
                    break;
                case 'del' :
                    $rech = 'DELETE FROM `' . TBCAGE . "` WHERE `cage_id` = '$idcage'";
                    break;
                default :
                    break;
            }
            if ($rech <> '') {
                $db->query($rech);
                $retourliste = TRUE;
            }
            close_database();
        } else {
            if ($age_ouvert == FALSE) {
                $td = 'view';
            }
            switch ($td) {
                case 'add' :
                    $titre = _('CREATION CONTACT');
                    $cage_adr1 = $age_adr1;
                    $cage_adr2 = $age_adr2;
                    $cage_dep = $age_dep;
                    $cage_ville = $age_ville;
                    $cage_codepays = $age_codepays;
                    $cage_telf = $age_telf;
                    $cage_fax = $age_fax;
                    break;
                case 'edit' :
                case 'del' :
                case 'view' :
                    if ($td == 'edit') {
                        $titre = _('EDITION CONTACT');
                    }
                    if ($td == 'del') {
                        $titre = _('SUPPRESSION CONTACT');
                    }
                    if ($td == 'view') {
                        $titre = _('VISUALISATION CONTACT');
                    }
                    $row = lit_enr_cage_id_in_age($idcage, $codeage);
                    if ($row) {
                        $row = encode_str($row);
                        extract($row);
                    } else {
                        $pbm_age = TRUE;
                    }
                    break;
                case 'lst' :
                    $titre = _('LISTE CONTACTS');
                    break;
                default :
                    $pbm_age = TRUE;
                    break;
            }
        }
        // On recalcule le total contacts
        $totalcage = nb_cage_in_age($codeage);
        if ($totalcage == 0) {
            $td = 'add';
        }
    } else {
        $pbm_age = TRUE;
    }
} else {
    $pbm_age = TRUE;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI - <?php echo _('ESPACE PRIVE'); ?> : <?php echo _('Contact Agence'); ?></title>

    <?php include_once('../inc/header.inc.php'); ?>

    <script language="javascript">
        <!--
        var tab_champ = ["nom:civil ; format:liste ; req:Y ; txt:<?php echo _('Civilité'); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:prenom; format:txt ; req:Y ; txt:<?php echo _('Prénom'); ?>; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:nom; format:txt ; req:Y ; txt:<?php echo _('Nom'); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:fonction ; format:liste ; req:Y ; txt:<?php echo _('Fonction'); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:adr1 ; format:txt ; req:Y ; txt:<?php echo _('Adresse'); ?> ; lmn:1 ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:adr2 ; format:txt ; req:N ; txt:<?php echo _('Adresse (2)'); ?> ; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:ville ; format:liste ; req:Y ; txt:<?php echo _('Ville'); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:dep ; format:liste ; req:Y ; txt:<?php echo _('Département'); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:pays ; format:liste ; req:Y ; txt:<?php echo _('Pays'); ?> ; vmn:1; stok:frmok ; stbad:frmbad",
            "nom:telf; format:tel ; req:Y ; txt:<?php echo _('Téléphone Fixe'); ?>; stok:frmok ; stbad:frmbad",
            "nom:fax; format:tel ; req:N ; txt:<?php echo _('Télécopieur'); ?>; stok:frmok ; stbad:frmbad",
            "nom:telp; format:telp ; req:N ; txt:<?php echo _('Téléphone portable'); ?>; stok:frmok ; stbad:frmbad",
            "nom:email; format:mail ; req:N ; txt:<?php echo _('Email'); ?>; stok:frmok ; stbad:frmbad",
            "nom:mess; format:mail ; req:N ; txt:<?php echo _('Messagerie'); ?> ; stok:frmok ; stbad:frmbad",
            <?php if (valsession('niv_log') >= 5) { ?>
            "nom:nota; format:txt ; req:N ; txt:<?php echo _('Notes'); ?>; stok:frmok ; stbad:frmbad",
            <?php } ?>
            "nom:mess; format:mail ; req:N ; txt:<?php echo _('Messagerie'); ?> ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <?php if ($pbm_age == FALSE) { ?>
        <script type="text/javascript">
            <!--
            <?php
            if ($retourliste == TRUE)
            {
                switch ($tx) {
                    case 'coloc' :
                        case 'dosd_cage' :
                        cre_sel_ddeurs_age($codeage, 'window.opener.', 'ddeur');
                        break;
                    default :
                        break;
                }
            }
            ?>

            $(function () {
                $("#nom").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#adr1").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#adr2").Setcase({caseValue: 'upper', changeonFocusout: true});
                $("#email").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#mess").Setcase({caseValue: 'lower', changeonFocusout: true});
                $("#prenom").Setcase({caseValue: 'allfirst', changeonFocusout: true});
            });

            function init_all() {
                init_Body();
                init_autohref_classe('falselnkimg', '');
                <?php
                if ($retourliste == FALSE)
                {
                    switch ($td)
                    {
                        case 'add' :
                            echo "init_valid_form('enr','cage_maj','".valsession('date_fmtshort')."',tab_champ,'pays','');";
                            break;
                        case 'edit' :
                            echo "init_warn('".nb_cagepr($idcage,TRUE)."agespr','','','','');";
                            echo "init_valid_form('enr','cage_maj','".valsession('date_fmtshort')."',tab_champ,'pays','');";
                            break;
                        case 'del' :
                            echo "init_valid_form('del','cage_maj','".valsession('date_fmtshort')."',tab_champ,'pays','');";
                            echo "warn_del('ope');";
                            echo 'readonly_all();';
                            break;
                        case 'view' :
                            echo 'readonly_all();';
                            break;
                    }
                    echo 'init_ajax_ville();';
                    echo 'init_ajax_dep();';
                    echo "init_fensaisie('edit_pr', 780, 480);";
                    echo "chk_contacts('1', 'ctact', '".$totalcage."');";
                    echo "init_autohref('lst', 'click', 'cage_maj.php?ca=".$codeage. '&td=lst&tx=' .$tx."',false);";
                    echo "init_autohref('add', 'click', 'cage_maj.php?ca=".$codeage. '&td=add&tx=' .$tx."',false);";
                    echo "add_func_click('quit', 'sclose', '');";
                    echo 'Focus_first();';
                }
                else
                {
                    switch ($tx)
                    {
                        case 'age' :
                            echo "self.location.href=('cage_maj.php?cc=".$codeage. '&td=lst&tx=' .$tx."');";
                            break;
                        case 'coloc' :
                        case 'dosd_cage' :
                            echo 'update_sel_ddeurs();';
                            echo 'self.close();';
                            break;
                        default :
                            break;
                    }
                }
                ?>
            }

            $(document).ready(function () {
                init_all();
            });
            //-->
        </script>

    <?php } ?>
</head>
<body>


<?php
if ($pbm_age == FALSE) {
    cre_ent_form($titre, '', '', '', FALSE);
    ?>
    <p align="center"><span class="titre_gras"><?php echo _("Pour l'agence"); ?> : <?php echo $age_nom; ?> </span></p>
    <table width="890" border="0">
        <tr>
            <td width="33%">&nbsp;</td>
            <td width="33%" align="center" class="annot_petit_it">
                <div id="oblig">(<img src="../img/oblig.png" align="absmiddle"
                                      alt="<?php echo _('Champ obligatoire'); ?>">
                    : <?php echo _('Saisie obligatoire'); ?>)
                </div>
            </td>
            <td width="33%" align="right">
                <input name="quit" type="button" class="bton_std" id="quit" value="<?php echo _('Quitter'); ?>">
                <?php if (($totalcage > 0) AND ($td <> 'lst')) { ?>
                    <input name="lst" type="button" class="bton_std" id="lst"
                           value="<?php echo _('Revenir à la liste'); ?>">
                <?php } ?>
            </td>
        </tr>
    </table>
    <?php
    if ($retourliste == FALSE) {
        switch ($td) {
            case 'add' :
            case 'edit' :
            case 'del' :
                ?>
                <form name="cage_maj" id="cage_maj" method="post" action="cage_maj.php">
                    <input name="ica" type="hidden" id="ica" value="<?php echo $cage_id ?>">
                    <input name="ca" type="hidden" id="ca" value="<?php echo $codeage ?>">
                    <input name="td" type="hidden" id="td" value="<?php echo $td ?>">
                    <input name="tx" type="hidden" id="tx" value="<?php echo $tx ?>">
                    <input name="titre" type="hidden" id="titre" value="<?php echo $titre ?>">
                    <input name="nbutton" type="hidden" id="nbutton" value="">
                    <table width="890" border="0">
                        <tr>
                            <td width="160" class="rubfrm" id="f_civil" align="right"><?php echo _('Civilité'); ?></td>
                            <td width="84%">
                                <?php
                                // Création de la liste des civilités
                                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                                $rech = 'SELECT * FROM `' . TBCIV . '` ORDER BY civ_desc';
                                $rows = $db->get_results($rech, ARRAY_A);
                                close_database();
                                cre_select_tab('civil', 'civil', $rows, 'civ_code', 'civ_desc', '', $cage_civilite, '', _('-- Faites votre choix --'));

                                if (CheckNivLog('4-5-9') AND ($td == 'edit')) {
                                    $url_cagepr = 'cagepr_maj.php?td=add&ca=' . $codeage . '&ica=' . $cage_id;
                                    cre_falselnk('', 'edit_pr', 'span', '', _('Prix de revient'), $url_cagepr, '../img/pr.png', _('Prix de revient'));
                                }
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_prenom" align="right"><?php echo _('Prénom'); ?></td>
                            <td><input name="prenom" type="text" id="prenom" value="<?php echo $cage_prenom ?>"
                                       size="50"
                                       maxlength="50">
                                <img src="../img/oblig.png" align="absmiddle"
                                     alt="<?php echo _('Champ obligatoire'); ?>"></td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_nom" align="right"><?php echo _('Nom'); ?></td>
                            <td><input name="nom" type="text" id="nom" value="<?php echo $cage_nom ?>" size="50"
                                       maxlength="50">
                                <img src="../img/oblig.png" align="absmiddle"
                                     alt="<?php echo _('Champ obligatoire'); ?>"></td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_func" align="right"><?php echo _('Fonction'); ?></td>
                            <td width="84%">
                                <?php
                                // création de la liste des fonctions
                                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                                $rech = 'SELECT * FROM `' . TBFONC . '` ORDER BY fonc_desc';
                                $rows = $db->get_results($rech, ARRAY_A);
                                close_database();
                                cre_select_tab('fonction', 'fonction', $rows, 'fonc_code', 'fonc_desc', '', $cage_codefonc, '', _('-- Faites votre choix --'));
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_adr1" align="right"><?php echo _('Adresse'); ?></td>
                            <td>
                                <input name="adr1" type="text" id="adr1" value="<?php echo $cage_adr1; ?>" size="50">
                                <img src="../img/oblig.png" align="absmiddle"
                                     alt="<?php echo _('Champ obligatoire'); ?>">
                            </td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_adr2"
                                align="right"><?php echo _('Adresse (2)'); ?></td>
                            <td>
                                <input name="adr2" type="text" id="adr2" value="<?php echo $cage_adr2; ?>" size="50">
                            </td>
                        </tr>
                        <tr>
                            <td class="rubfrm" id="f_pays" align="right"><?php echo _('Pays'); ?></td>
                            <td>
                                <?php
                                // Création de la liste des pays
                                $rows = give_rows_pays();
                                cre_select_tab('pays', 'pays', $rows, 'pays_code', 'pays_nom', '', $cage_codepays, '', _('-- Faites votre choix --'), 1, FALSE, '-1', _('-- Inconnu --'));
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="rubfrm" id="f_dep" align="right"><?php echo _('Département'); ?></td>
                            <td>
                                <?php
                                // Création de la liste des départements
                                $rows = give_rows_dep($cage_codepays);
                                cre_select_tab('dep', 'dep', $rows, 'dep_code', 'dep_code', 'dep_nom', $cage_dep, '', _('-- Faites votre choix --'), 1, FALSE, '-1', _('-- Inconnu --'));
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td class="rubfrm" id="f_ville" align="right"><?php echo _('Ville'); ?></td>
                            <td>
                                <?php
                                // Création de la liste des villes
                                $rows = give_rows_villes($cage_dep, $cage_codepays);
                                cre_select_tab('ville', 'ville', $rows, 'villes_id', 'villes_nom', 'villes_cp', $cage_ville, '', _('-- Faites votre choix --'), 1, FALSE, '-1', _('-- Inconnu --'));
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_telf" align="right"><?php echo _('Tél. Fixe'); ?></td>
                            <td><input name="telf" type="text" id="telf" value="<?php echo $cage_telf ?>" size="20"
                                       maxlength="20">
                                <img src="../img/oblig.png" align="absmiddle"
                                     alt="<?php echo _('Champ obligatoire'); ?>"></td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_fax" align="right"><?php echo _('Télécopieur'); ?></td>
                            <td><input name="fax" type="text" id="fax" value="<?php echo $cage_fax ?>" size="20"
                                       maxlength="20"></td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_telp"
                                align="right"><?php echo _('Téléphone portable'); ?></td>
                            <td><input name="telp" type="text" id="telp" value="<?php echo $cage_telp ?>" size="20"
                                       maxlength="20">
                            </td>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_email" align="right"><?php echo _('Email'); ?></td>
                            <td><input name="email" type="text" id="email" value="<?php echo $cage_email ?>" size="50"
                                       maxlength="50">
                                <?php if ($td == 'edit') { ?>
                                <a href="mailto:<?php echo $cage_email ?>"><img src="../img/mail.png"
                                                                                title="<?php echo _('Envoyer e-mail'); ?>"
                                                                                border="0"
                                                                                align="absmiddle"></a></td>
                            <?php } ?>
                        </tr>
                        <tr>
                            <td width="160" class="rubfrm" id="f_mess" align="right"><?php echo _('Messagerie'); ?></td>
                            <td><input name="mess" type="text" id="mess" value="<?php echo $cage_mess ?>" size="50"
                                       maxlength="50"></td>
                        </tr>
                        <?php if (valsession('niv_log') >= 5) {
                            ?>
                            <tr>
                                <td width="160" class="rubfrm" id="f_nota" align="right"><?php echo _('Notes'); ?></td>
                                <td><textarea name="nota" id="nota"><?php echo $cage_nota ?></textarea></td>
                            </tr>
                        <?php } ?>
                    </table>
                    <p align="center">
                        <?php
                        switch ($td) {
                            case 'add' :
                            case 'edit' :
                                echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Valider') . '">';
                                break;
                            case 'del' :
                                echo '<input name="del" type="button" class="bton_std" id="del" value="' . _('Supprimer') . '">';
                                break;
                        }
                        echo ' <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _('Réinitialiser') . '">';

                        if ($td == 'edit') {
                            echo '<br />';
                            echo '<br />';
                            echo '<input name="add" type="button" class="bton_std" id="add" value="' . _('Saisir un nouveau contact') . '">';
                        }
                        ?>
                    </p>
                </form>
                <?php
                break;
            case 'lst' :
                // création de la liste des contacts agence
                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
                $rech = 'SELECT * FROM `' . TBCAGE . "` WHERE `cage_codeage` = '" . $db->escape($codeage) . "' ORDER BY 'cage_nom'";
                $rows = $db->get_results($rech, ARRAY_A);

                if ($rows AND (count($rows) <> 0)) {
                    echo '<table class="MtableD" border="0" width="890" cellpadding="0" cellspacing="0" align="center">';

                    // création de l'entête de la table contacts clients
                    echo '<th>';
                    echo _('Nom, Prénom');
                    echo '</th>';
                    echo '<th>';
                    echo _('Fonction');
                    echo '</th>';
                    echo '<th>';
                    echo _('Téléphone');
                    echo '</th>';
                    echo '<th>';
                    echo _('Email');
                    echo '</th>';
                    echo '<th>';
                    echo _('Choisir');
                    echo '</th>';

                    foreach ($rows as $row) {
                        $row = encode_str($row);
                        // création de l'entête de ligne (TR)
                        echo '<tr>';

                        echo '<td>';
                        echo $row['cage_nom'] . ' ' . $row['cage_prenom'];
                        // CREATION d'icône si manque prix de revient
                        if (nb_cagepr($row['cage_id'], FALSE) == 0) {
                            echo ' <img src="../img/warn.png" title ="' . _('Manque prix de revient') . '" border="0" align="absmiddle">';
                        }
                        echo '</td>';
                        echo '<td>';
                        $txtfonc = '&nbsp;';
                        $rech2 = 'SELECT fonc_desc FROM `' . TBFONC . "` WHERE `fonc_code` = '" . $row['cage_codefonc'] . "'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2) {
                            $row2 = encode_str($row2);
                            $txtfonc = $row2['fonc_desc'];
                        }
                        echo $txtfonc;
                        echo '</td>';
                        echo '<td align="center">';
                        echo $row['cage_telf'];
                        echo '</td>';
                        echo '<td align="center">';
                        echo ' <a href="mailto:' . $row['cage_email'] . '">' . $row['cage_email'] . '</a>';
                        echo '</td>';

                        echo '<td align="center">';
                        $lst_maj = TRUE;
                        cre_falselnk_img('', 'cage_edit_' . $row['cage_id'], 'span', 'center', _('Edition'), 'lnk', 'cage_maj.php?td=edit&ca=' . $row['cage_codeage'] . '&ica=' . $row['cage_id'] . '&tx=' . $tx, '../img/edit.png');
                        $lst_del = TRUE;
                        cre_falselnk_img('', 'cage_del_' . $row['cage_id'], 'span', 'center', _('Suppression'), 'lnk', 'cage_maj.php?td=edit&ca=' . $row['cage_codeage'] . '&ica=' . $row['cage_id'] . '&tx=' . $tx, '../img/del.png');
                        echo '</td>';
                        echo '</tr>';
                    }

                    echo '</table>';
                    // Fin affichage table liste contacts agences
                    $taille_piedla = '';
                    include_once('piedla.inc.php');
                    echo '<p align="center">';
                    echo '<input name="add" type="button" class="bton_std" id="add" value="' . _('Saisir un nouveau contact') . '">';
                    echo '</p>';
                }
                close_database();
                break;

            default :
                break;
        }
    }
} else {
    close_fen_auto('', '', '');
}
?>
</body>
</html>
