<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_fici.inc.php");
include_once("../inc/fic_pw.inc.php");
include_once("../inc/fic_zc.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.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(@$_SESSION["code_log"]);
check_modules();

$_SESSION["typ_filtres"] = "age";
$_SESSION['tab_tec_sel'] = [];
$_SESSION['tab_sites_sel'] = [];

if (isset($_SESSION['tab_tec_sel']))
    unset($_SESSION['tab_tec_sel']);

?>
<!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 _("Liste agences"); ?></title>

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

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            init_autohref_classe('falselnkimg','');
            Focus_first();
        }

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

</head>

<body>

<?php
include_once("../inc/entete.inc.php");
?>
<p align="center"><span class="titrepage"><?PHP echo _("AGENCES"); ?></span></p>
<?PHP
if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
    $fic_imp = "age_lsti.php";
    $url_add = "age_maj.php?td=add&code=";
} else {
    echo "<p align=\"center\"><span class=\"annot_gras_it\">";
    echo _("Pour location");
    echo "</span></p>";
    $fic_imp = "";
    $url_add = "";
}

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

$rech = cree_strrechlstage();
if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE " . TBAGE . ".age_code<>'" . @$_SESSION["code_age"] . "'";
    else
        $rech .= " AND " . TBAGE . ".age_code<>'" . @$_SESSION["code_age"] . "'";
}
$rech .= crestr_triord($age_titre_col, "age_code", FALSE, $limit);

$rows = $db->get_results($rech, ARRAY_A);
$db->disconnect();

if ($rows) {
    include_once("../inc/formr.inc.php");

    $numrows = count($rows);
    $rech = modstr_triord($rech, $age_titre_col, "age_code", TRUE, $limit);

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

    $rows = $db->get_results($rech, ARRAY_A);

    // *** Pagination de pied
    pagine_lst($numrows, $limit);

    // *** Affichage de la table

    echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";
    // création entête
    if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9))
        titlst($age_titre_col);
    else
        titlst($age_titre_col2);
    echo "<th>" . _("Cliquer") . "</th>\n";

    $i = 0;
    foreach ($rows as $row) {
        $row = encode_str($row);
        $affok = TRUE;
        $verrou = FALSE;
        if (account_locked($row["age_code"], FALSE) == TRUE) {
            $verrou = TRUE;
            if (@$_SESSION["niv_log"] == 4)
                $affok = FALSE;
        } else {
            if ((@$_SESSION["niv_log"] == 4) AND ($row["age_ouvert"] == FALSE))
                $affok = FALSE;
        }

        if ($affok == TRUE) {
            // création de l'entête de ligne (TR)
            creetrlst($i);

            echo "<td>" . $row["age_code"];

            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                $rech2 = "SELECT COUNT(*) FROM `" . TBST . "` WHERE st_codeage = '" . $row["age_code"] . "'";
                $nbres2 = $db->get_var($rech2);
                if ($nbres2 > 0)
                    echo ' <a href="#" class="popa"><img src="../img/otlink.png" alt="'._("Egalement sous-traitant").'" border="0" align="absmiddle"><span>'._("Egalement sous-traitant").'</span></a>';

                // icône du planning
                // On vérifie si le module planning est bien en place
                if ($mod_planning == TRUE) {
                    $url_planning = 'plan_maj.php?typd=2&coded=' . $row["age_code"] . '&typr=-1&tx=agel&from=agel';
                    cre_falselnk_img('', 'pla_edit_'.$row["age_id"], 'span', 'center', _("Planning/Agenda"), 'lnkpop', $url_planning , '../img/planning.png',1020,760);
                }

                // CREATION d'icône si manque prix de revient ou logo
                $txtalert = "";
                if (exist_logo($row["age_code"]) == FALSE)
                    $txtalert = _("Manque un logo");
                if (nb_agepr($row["age_code"], FALSE) == 0) {
                    if (trim($txtalert) <> "")
                        $txtalert .= "<br />";
                    $txtalert .= _("Manque prix de revient");
                }
                if (trim($txtalert) <> "")
                    echo ' <a href="#" class="popw"><img src="../img/warn.png" alt="'._("pbm").'" border="0" align="absmiddle"><span>' . formatetxtbr($txtalert) . '</span></a>';


                // CREATION d'icône si l'agence est fermée
                if ($row["age_ouvert"] == FALSE) {
                    $txtalert = _("Est fermée depuis le ") . datetolocal($row["age_datec"]);
                    echo ' <a href="#" class="popw"><img src="../img/out.png" alt="'.("fermé").'" border="0" align="absmiddle"><span>' . formatetxtbr($txtalert) . '</span></a>';
                    $lst_ferm = TRUE;
                }

                // Verrouillé ?
                if ($verrou == TRUE) {
                    $txtalert = _("Compte désactivé");
                    echo ' <a href="#" class="popw"><img src="../img/locked.png" alt="' . _("Compte désactivé") . '" border="0" align="absmiddle"><span>' . formatetxtbr($txtalert) . '</span></a>';
                    $locked = TRUE;
                }
            }
            echo "</td>\n";
            echo "<td>";
            echo $row["age_nom"];

            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                // Type agence
                echo "<br />";
                echo "<span class=\"annot_petit_it\">";
                $txt = formatetxt($row["acat_desc"]);
                echo "(" . $txt . ")";
                echo "</span>";
            }

            echo "</td>\n";
            echo "<td>" . $row["age_telf"] . "</td>\n";
            echo "<td>" . Retournecp_Id($row["age_ville"], FALSE) . "</td>\n";
            echo "<td>" . Retournepays($row["age_codepays"], FALSE) . "</td>\n";
            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                echo "<td>";
                $detzone1 = "";
                $detzone2 = "";
                $tab_zone = tab_zcc_age_open_full($row["age_code"], FALSE);
                $idx = 0;
                foreach ($tab_zone as $key => $value) {
                    if ($idx < 5) {
                        if ($idx > 0) {
                            $detzone1 .= "<br />";
                            $detzone2 .= "<br />";
                        }
                        $detzone1 .= $value;
                        $detzone2 .= $value;
                    } else {
                        $detzone2 .= "<br />";
                        $detzone2 .= $value;
                    }
                    $idx++;
                }
                if ($idx >= 5) {
                    $detzone1 .= '<div align="right"><a href="#" class="popa"><img src="../img/expand.png" alt="'._("Détail").'" border="0" align="absmiddle" onclick="show_hide(\''.$row["age_code"] . '_z2\',\''.$row["age_code"] . '_z1\')"><span>' . _("Détail") . '</span></a></div>';
                    $detzone2 .= '<div align="right"><a href="#" class="popa"><img src="../img/reduce.png" alt="'._("Réduire").'" border="0" align="absmiddle" onclick="show_hide(\''.$row["age_code"] . '_z1\',\''.$row["age_code"] . '_z2\')"><span>' . _("Réduire") . '</span></a></div>';
                    echo '<div id="' . $row["age_code"] . '_z1" style="display:block;">';
                    echo $detzone1;
                    echo "</div>";
                    echo '<div id="' . $row["age_code"] . '_z2" style="display:none;">';
                    echo $detzone2;
                    echo "</div>";
                } else
                    echo $detzone1;
                echo "</td>\n";

                // CREATION lien dynamique sur email
                echo "<td>";
                echo "<a href=\"mailto:" . $row["age_email"] . "\">" . $row["age_email"] . "</a>";
                echo "</td>\n";
            }

            // CREATION lien dynamique sur icône (édition, suppression, etc...)
            echo "<td align=\"center\">";
            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                if ($row["age_ouvert"] == TRUE) {
                    $lst_maj = TRUE;
                    cre_falselnk_img('', 'age_edit_'.$row["age_id"], 'span', 'center', _("Edition"), 'lnk', 'age_maj.php?td=edit&code=' . $row["age_code"] . '&dep=' . $row["age_dep"] , '../img/edit.png');
                } else {
                    $lst_voir = TRUE;
                    cre_falselnk_img('', 'age_view_'.$row["age_id"], 'span', 'center', _("Visualisation"), 'lnk', 'age_maj.php?td=view&code=' . $row["age_code"] . '&dep=' . $row["age_dep"] , '../img/eye.png');
                }
            }
            $lst_coloc = TRUE;
            cre_falselnk_img('', 'coloc_add_'.$row["age_id"], 'span', 'center', _("Ajout contrat location"), 'lnk', 'coloc_maj.php?td=add&tc=1&co=&cc=' . $row["age_code"] . '&ca=' , '../img/coloc.png');
            if ((@$_SESSION["niv_log"] == 5) OR (@$_SESSION["niv_log"] == 9)) {
                $urlimp = "age_maji.php?ca=" . $row["age_code"];
                $lst_imp = TRUE;
                cre_falselnk_img('', 'age_prn_'.$row["age_id"], 'span', 'center', _("Impression"), 'lnkpop', $urlimp , '../img/print.png');
            }
            if ($row["age_ouvert"] == TRUE) {
                $lst_del = TRUE;
                cre_falselnk_img('', 'age_del_'.$row["age_id"], 'span', 'center', _("Suppression"), 'lnk', 'age_maj.php?td=del&code=' . $row["age_code"] . '&dep=' . $row["age_dep"] , '../img/del.png');
            }
            echo "</td>\n";

            echo "</tr>\n";

            $i++;
        }
    }
    echo "</table>\n";
    // *** Fin affichage de la table

    $db->disconnect();

    if ($locked == TRUE) {
        echo ' <img src="../img/locked.png" title="' . _("Compte désactivé") . '" alt="' . _("Compte désactivé") . '" border="0" align="absmiddle">';
        echo " : " . _("Compte désactivé");
    }

    // *** Pagination de pied
    pagine_lst($numrows, $limit);

    $taille_piedla = "";
    include_once("../inc/piedla.inc.php");
} else
    retpp();

include_once("pied.php")
?>
