<?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
 ****************************************************************************************/

session_start();

require_once('../lib/gettext/gettext.inc');
include_once('../inc/config.inc.php');
include_once('../inc/fic_age.inc.php');
include_once('../inc/fic_cli.inc.php');
include_once('../inc/fic_do.inc.php');
include_once('../inc/fic_dos.inc.php');
include_once('../inc/fic_hum.inc.php');
include_once('../inc/fic_int.inc.php');
include_once('../inc/fic_st.inc.php');
include_once('../inc/fic_tec.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_log.inc.php');
include_once('../inc/func_time.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../classes/dirtool/class_dirtool.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');
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$affok = TRUE;

if (!isset($_GET['log'])) {
    $affok = FALSE;
}
if (!isset($_GET['pw'])) {
    $affok = FALSE;
}
$trss = '';
if (isset($_GET['trss'])) {
    $trss = $_GET['trss'];
} else {
    $affok = FALSE;
}

// Paramètres de période
if (isset($_GET['lst']) OR isset($_GET['week']) OR isset($_GET['nday'])) {
    // On ne oeut envoyer qu'un paramètre à la fois
    if (isset($_GET['lst']) AND isset($_GET['week']) AND isset($_GET['nday'])) {
        $affok = FALSE;
    }
    {
        // On vérifie si paramètre "x" derniers enregistrements
        $last_enr = 0;
        if (isset($_GET['lst'])) {
            $last = (int)$_GET['lst'];
            $last_enr = $last;
            if ($last <= 0) {
                $last_enr = $limit;
            }
        } else {
            // On vérifie si paramètre "date"
            $first_date = '';
            // Cette semaine ?
            if (isset($_GET['week'])) {
                $week = (int)$_GET['week'];
                if ($week == 1) {
                    $first_date = date_lundi();
                } else {
                    $affok = FALSE;
                }
            } else {
                // 15 ou 30 derniers jours ?
                $nday = 0;
                if (isset($_GET['nday'])) {
                    $nday = (int)$_GET['nday'];
                    if ($nday <= 0) {
                        $nday = 15;
                    }
                    $first_date = nouv_date_moins_jours($datea, $nday);
                } else {
                    $affok = FALSE;
                }
            }
        }
    }
} else {
    $affok = FALSE;
}

$loginok = FALSE;

if ($affok == TRUE) {
    $login = $_GET['log'];
    $pass = $_GET['pw'];

    $log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP

    // Contrôle du login et du mot de passe
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    $rech = 'SELECT * FROM `' . TBPW . "` WHERE `pw_code` = '" . $login . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);

        if ($login == $pw_code) // Le code entré doit correspondre exactement au code stocké (majuscules/minuscules)
        {
            $pwok = FALSE;

            // Le mot de passe doit correspondre exactement (en mode 'natif' ou MD5)
            if ($pw_pass == $pass) {
                $pwok = TRUE;
            } else if ($pw_pass == md5($pass)) {
                $pwok = TRUE;
            }

            if ($pwok == TRUE) {
                if ($pw_actif == 1) // Le compte doit être actif
                {
                    $loginok = TRUE;
                    $_SESSION['etat_log'] = 'connecte';
                    $_SESSION['code_log'] = $pw_code;
                    $_SESSION['niv_log'] = $pw_level;
                    // Codes session complémentaires
                    switch ($pw_level) {
                        // Code agence
                        case 4 :
                            $_SESSION['code_age'] = $pw_code;
                            break;
                        // Code technicien agence
                        case 3 :
                            $rech2 = 'SELECT tec_codeage FROM `' . TBTEC . "` WHERE `tec_code` = '" . $pw_code . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                extract($row2);
                                $_SESSION['code_age'] = $tec_codeage;
                            }
                            break;
                        // Code donneur d'ordres
                        case 6 :
                            $_SESSION['code_do'] = $pw_code;
                            break;
                        // Code technicien donneur d'ordres
                        case 7 :
                            $rech2 = 'SELECT tec_codedo FROM `' . TBTEC . "` WHERE `tec_code` = '" . $pw_code . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                extract($row2);
                                $_SESSION['code_do'] = $tec_codedo;
                            }
                            break;
                        // Code Sous-traitant
                        case 10 :
                            $_SESSION['code_st'] = $pw_code;
                            break;
                        // Code technicien sous-traitant
                        case 11 :
                            $rech2 = 'SELECT tec_codest FROM `' . TBTEC . "` WHERE `tec_code` = '" . $pw_code . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                extract($row2);
                                $_SESSION['code_st'] = $tec_codest;
                            }
                            break;
                    }
                    $rech2 = 'UPDATE `' . TBPW . "` SET `pw_lastsuccess`='" . $dateheurea . "', `pw_lastip`='" . $log_ip . "' WHERE `pw_id`='" . $pw_id . "'";
                    $db->query($rech2);
                }
            }
        }
    }
    close_database();
}

$affok = $loginok;

if ($affok == TRUE) {
    switch ($trss) {
        case 'int' :
            if (($_SESSION['niv_log'] <> 1) AND ($_SESSION['niv_log'] <> 3) AND ($_SESSION['niv_log'] <> 4) AND ($_SESSION['niv_log'] <> 6) AND ($_SESSION['niv_log'] <> 7) AND ($_SESSION['niv_log'] <> 5) AND ($_SESSION['niv_log'] <> 9)) {
                $affok = FALSE;
            }
            break;
        case 'ot' :
            if (($_SESSION['niv_log'] <> 3) AND ($_SESSION['niv_log'] <> 4) AND ($_SESSION['niv_log'] <> 10) AND ($_SESSION['niv_log'] <> 11) AND ($_SESSION['niv_log'] <> 5) AND ($_SESSION['niv_log'] <> 9)) {
                $affok = FALSE;
            }
            break;
        case 'cocl' :
            if (($_SESSION['niv_log'] <> 1) AND ($_SESSION['niv_log'] <> 3) AND ($_SESSION['niv_log'] <> 4) AND ($_SESSION['niv_log'] <> 5) AND ($_SESSION['niv_log'] <> 9)) {
                $affok = FALSE;
            }
            break;
        case 'coloc' :
            if (($_SESSION['niv_log'] <> 1) AND ($_SESSION['niv_log'] <> 3) AND ($_SESSION['niv_log'] <> 4) AND ($_SESSION['niv_log'] <> 10) AND ($_SESSION['niv_log'] <> 11) AND ($_SESSION['niv_log'] <> 5) AND ($_SESSION['niv_log'] <> 9)) {
                $affok = FALSE;
            }
            break;
        case 'tac' :
            if (($_SESSION['niv_log'] <> 3) AND ($_SESSION['niv_log'] <> 4) AND ($_SESSION['niv_log'] <> 10) AND ($_SESSION['niv_log'] <> 11) AND ($_SESSION['niv_log'] <> 5) AND ($_SESSION['niv_log'] <> 9)) {
                $affok = FALSE;
            }
            break;
        case 'dos' :
            if (($_SESSION['niv_log'] <> 3) AND ($_SESSION['niv_log'] <> 4) AND ($_SESSION['niv_log'] <> 5) AND ($_SESSION['niv_log'] <> 9)) {
                $affok = FALSE;
            }
            break;
        default :
            $affok = FALSE;
            break;
    }
}

// édition du début du fichier XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<?xml-stylesheet type="text/css" href="../css/smi.css"?>';
echo '<rss version="2.0">';
echo '<channel>';

if ($affok == TRUE) {
    $link_racine = $rep_appli;
    $tab_cli = [];
    switch ($_SESSION['niv_log']) {
        case '1' :
            $tab_cli = give_tab_rcli($_SESSION['code_log'], TRUE);
            $link_racine .= '/cli/';
            break;
        case '2' :
            $link_racine .= '/ope/';
            break;
        case '3' :
        case '4' :
            $link_racine .= '/age/';
            break;
        case '6' :
        case '7' :
            $link_racine .= '/do/';
            break;
        case '5' :
        case '9' :
            $link_racine .= '/admin/';
            break;
        case '10' :
        case '11' :
            $link_racine .= '/st/';
            break;
    }
    $link_site = $link_racine;
    switch ($trss) {
        case 'int' :
            $titre = 'SMI - Interventions';
            $link_site .= 'int_lst.php?page=1&ord=DES&tri=Code';
            break;
        case 'ot' :
            $titre = 'SMI - Bons de travail';
            $link_site .= 'ot_lst.php?tx=m&&tl=1&ord=DES&tri=Code';
            break;
            break;
        case 'cocl' :
            $titre = 'SMI - Contrats de maintenance';
            $link_site .= 'cocl_lst.php?ord=DES&tri=Code';
            break;
        case 'coloc' :
            $titre = 'SMI - Contrats de location';
            $link_site .= 'coloc_lst.php?ord=DES&tri=Code';
            break;
        case 'tac' :
            $titre = 'SMI - Tâches à effectuer';
            $link_site .= 'tac_lst.php?ord=DES&tri=Code';
            break;
        case 'dos' :
            $titre = 'SMI - Dossiers de suivi';
            $link_site .= 'dos_lst.php?ord=ASC&tri=Fin';
            break;
    }
    $link_site .= '&log=' . $pw_code . '&pw=' . $pw_pass;

    // Rajout du filtrage et des limites
    if ($last_enr <> 0) {
        $titre .= ' (' . $last_enr;
        switch ($trss) {
            case 'int' :
            case 'tac' :
                $titre .= ' dernières créées)';
                break;
            case 'ot' :
            case 'cocl' :
            case 'coloc' :
            case 'dos' :
                $titre .= ' derniers créés)';
                break;
        }
    }
    if ($week == 1) {
        switch ($trss) {
            case 'int' :
            case 'tac' :
                $titre .= ' (créées cette semaine)';
                break;
            case 'ot' :
            case 'cocl' :
            case 'coloc' :
            case 'dos' :
                $titre .= ' (créés cette semaine)';
                break;
        }
    }
    if ($nday <> 0) {
        switch ($trss) {
            case 'int' :
            case 'tac' :
                $titre .= ' (créées ces ' . $nday;
                break;
            case 'ot' :
            case 'cocl' :
            case 'coloc' :
            case 'dos' :
                $titre .= ' (créés ces ' . $nday;
                break;
        }
        $titre .= ' derniers jours)';
    }
    $titre = htmlspecialchars($titre);
    $titre = utf8_encode($titre);
    echo '<title>' . $titre . '</title>';
    $link_site = htmlspecialchars($link_site);
    $link_site = utf8_encode($link_site);
    echo '<link>' . $link_site . '</link>';
    echo '<description>' . $titre . '</description>';
    echo '<language>fr</language>';
    echo '<generator>SMI' . $smiver . '</generator>';

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

    switch ($trss) {
        // Interventions
        case 'int' :
            $rech = 'SELECT int_code, int_datecrea, int_codeage, int_codecli, int_ddeur, int_idsite, int_mat, int_pbm, int_daterdv, int_hrdv, int_codeman, int_codeext';
            $rech .= ' FROM `' . TBINT . '`';
            switch ($_SESSION['niv_log']) {
                // Client
                case 1 :
                    if (count($tab_cli) == 1) {
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        $rech .= " int_codecli='" . $_SESSION['code_log'] . "'";
                    } else {
                        // On rajoute les comptes complémentaires (cas des regroupements de clients)
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        if (count($tab_cli) > 1) {
                            $rech .= '(';
                        }
                        foreach ($tab_cli as $key => $value) {
                            $rech .= " int_codecli='" . $value . "' OR";
                        }
                        // Suppression du dernier 'OR'
                        $rech = substr($rech, 0, -3);

                        if (count($tab_cli) > 1) {
                            $rech .= ')';
                        }
                    }
                    break;
                // Techniciens
                case 3 :
                    // Création du tableau des interventions affichables
                    $tab_int = give_tab_int_ok_tec(valsession('code_log'), valsession('code_age'), FALSE);

                    if (count($tab_int) > 0) {
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        if (count($tab_int) > 1) {
                            $rech .= '(';
                        }

                        foreach ($tab_int as $value) {
                            $rech .= ' ' . TBINT . ".int_code='" . $value . "' OR";
                        }

                        // Suppression du dernier 'OR'
                        $rech = substr($rech, 0, -3);

                        if (count($tab_int) > 1) {
                            $rech .= ')';
                        }
                    }
                    break;
                // Agences
                case 4 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE (int_codeage='" . valsession('code_age') . "' OR int_codeages='" . valsession('code_age') . "'";
                    } else {
                        $rech .= " AND (int_codeage='" . valsession('code_age') . "' OR int_codeages='" . valsession('code_age') . "'";
                    }
                    // On rajoute les interventions non affectées à une agence
                    $rech .= ' OR ' . TBINT . ".int_codeage='')";
                    break;
                // Donneurs d'Ordres et leurs Techniciens
                case 6 :
                case 7 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE int_codedo='" . valsession('code_do') . "'";
                    } else {
                        $rech .= " AND int_codedo='" . valsession('code_do') . "'";
                    }
                    break;
            }
            // Rajout du filtrage et des limites
            if ($first_date <> '') {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= " (int_datecrea>='" . $first_date . "' AND int_datecrea<='" . $datea . "')";
            }
            $rech .= ' ORDER BY int_code DESC';
            if ($last_enr <> 0) {
                $rech .= ' LIMIT 0, ' . $last_enr;
            }
            break;
        // Ordres de travail
        case 'ot' :
            switch ($_SESSION['niv_log']) {
                // Agences et leurs techniciens
                case 3 :
                case 4 :
                    $rech = 'SELECT ' . TBOT . '.ot_code, ' . TBOT . '.ot_datecrea, ' . TBOT . '.ot_codeint, ' . TBOT . '.ot_codecli, ';
                    $rech .= TBOT . '.ot_ccli, ' . TBOT . '.ot_idsite, ' . TBOT . '.ot_mat, ' . TBOT . '.ot_pbm, ';
                    $rech .= TBCLI . '.cli_prop ';
                    $rech .= 'FROM `' . TBOT . '`, `' . TBCLI . '`';
                    $rech .= ' WHERE ' . TBOT . '.ot_codecli=' . TBCLI . '.cli_code';
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE cli_prop='" . valsession('code_age') . "'";
                    } else {
                        $rech .= " AND cli_prop='" . valsession('code_age') . "'";
                    }
                    break;
                // Sous-traitants et leurs techniciens
                case 10 :
                case 11 :
                    $rech = 'SELECT ot_code, ot_datecrea, ot_codeint, ot_codecli, ot_ccli, ot_idsite, ot_mat, ot_pbm';
                    $rech .= ' FROM `' . TBOT . '`';
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE ot_codest='" . valsession('code_st') . "'";
                    } else {
                        $rech .= " AND ot_codest='" . valsession('code_st') . "'";
                    }
                    break;
                default :
                    $rech = 'SELECT ot_code, ot_datecrea, ot_codeint, ot_codecli, ot_ccli, ot_idsite, ot_mat, ot_pbm';
                    $rech .= ' FROM `' . TBOT . '`';
                    break;
            }
            // Rajout du filtrage et des limites
            if ($first_date <> '') {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= " (ot_datecrea>='" . $first_date . "' AND ot_datecrea<='" . $datea . "')";
            }
            $rech .= ' ORDER BY ot_code DESC';
            if ($last_enr <> 0) {
                $rech .= ' LIMIT 0, ' . $last_enr;
            }
            break;
        // Contrats de maintenance clients
        case 'cocl' :
            $rech = 'SELECT cocl_code, cocl_datecrea, cocl_codeage, cocl_codecli, cocl_matos';
            $rech .= ' FROM `' . TBCOCL . '`';
            switch ($_SESSION['niv_log']) {
                // Client
                case 1 :
                    if (count($tab_cli) == 1) {
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        $rech .= " cocl_codecli='" . $_SESSION['code_log'] . "'";
                    } else {
                        // On rajoute les comptes complémentaires (cas des regroupements de clients)
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        if (count($tab_cli) > 1) {
                            $rech .= '(';
                        }
                        foreach ($tab_cli as $key => $value) {
                            $rech .= " cocl_codecli='" . $value . "' OR";
                        }
                        // Suppression du dernier 'OR'
                        $rech = substr($rech, 0, -3);

                        if (count($tab_cli) > 1) {
                            $rech .= ')';
                        }
                    }
                    break;
                // Agences et leurs techniciens
                case 3 :
                case 4 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE cocl_codeage='" . valsession('code_age') . "'";
                    } else {
                        $rech .= " AND cocl_codeage='" . valsession('code_age') . "'";
                    }
                    break;
            }
            // Rajout du filtrage et des limites
            if ($first_date <> '') {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= " (cocl_datecrea>='" . $first_date . "' AND cocl_datecrea<='" . $datea . "')";
            }
            $rech .= ' ORDER BY cocl_code DESC';
            if ($last_enr <> 0) {
                $rech .= ' LIMIT 0, ' . $last_enr;
            }
            break;
        // Contrats de location
        case 'coloc' :
            $rech = 'SELECT coloc_code, coloc_datecrea, coloc_codeage, coloc_codecli, coloc_typcli, coloc_datedeb, coloc_datefin';
            $rech .= ' FROM `' . TBCOLOC . '`';
            switch ($_SESSION['niv_log']) {
                // Client
                case 1 :
                    if (count($tab_cli) == 1) {
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        $rech .= " coloc_codecli='" . $_SESSION['code_log'] . "'";
                    } else {
                        // On rajoute les comptes complémentaires (cas des regroupements de clients)
                        if (!stristr($rech, 'WHERE ')) {
                            $rech .= ' WHERE ';
                        } else {
                            $rech .= ' AND ';
                        }
                        if (count($tab_cli) > 1) {
                            $rech .= '(';
                        }
                        foreach ($tab_cli as $key => $value) {
                            $rech .= " coloc_codecli='" . $value . "' OR";
                        }
                        // Suppression du dernier 'OR'
                        $rech = substr($rech, 0, -3);

                        if (count($tab_cli) > 1) {
                            $rech .= ')';
                        }
                    }
                    break;
                // Techniciens agences
                // Donneurs d'ordre et leurs techniciens
                case 3 :
                case 10 :
                case 11 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE ';
                    } else {
                        $rech .= ' AND ';
                    }
                    $rech .= " coloc_codecli='" . $_SESSION['code_log'] . "'";
                    break;
                // Agences (contrats qu'elles ont créé)
                case 4 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE coloc_codeage='" . valsession('code_age') . "'";
                    } else {
                        $rech .= " AND coloc_codeage='" . valsession('code_age') . "'";
                    }
                    break;
            }
            // Rajout du filtrage et des limites
            if ($first_date <> '') {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= " (coloc_datecrea>='" . $first_date . "' AND coloc_datecrea<='" . $datea . "')";
            }
            $rech .= ' ORDER BY coloc_code DESC';
            if ($last_enr <> 0) {
                $rech .= ' LIMIT 0, ' . $last_enr;
            }
            break;
        // Tâches
        case 'tac' :
            $rech = 'SELECT tac_code, tac_datecrea, tac_codecrea, tac_codedest, tac_typdest, tac_datefinp, tac_titre, tac_afaire';
            $rech .= ' FROM `' . TBTAC . '`';
            switch ($_SESSION['niv_log']) {
                // Techniciens agences
                // Techniciens sous-traitants
                case 3 :
                case 11 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE ';
                    } else {
                        $rech .= ' AND ';
                    }
                    $rech .= " tac_codedest='" . $_SESSION['code_log'] . "'";
                    break;
                // Agences
                case 4 :
                    // On construit la liste des techniciens actifs de l'agence
                    $tab_lst_tec = [];
                    $rech2 = 'SELECT tec_code FROM `' . TBTEC . "` WHERE tec_codeage='" . valsession('code_log') . "' AND tec_out=0";
                    $rows2 = $db->get_results($rech2, ARRAY_A);
                    if ($rows2) {
                        foreach ($rows2 as $row2) {
                            extract($row2);
                            $tab_lst_tec[] = $tec_code;
                        }
                    }
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE ((' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
                    } else {
                        $rech .= ' AND ((' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
                    }
                    // Une agence peut voir les tâches de ses techniciens
                    foreach ($tab_lst_tec as $key => $value) {
                        $rech .= ' OR ' . TBTAC . ".tac_codedest='" . $value . "'";
                    }
                    $rech .= ')';
                    // On autorise aussi les auteurs à voir les tâches qu'ils ont créé
                    $rech .= ' OR (' . TBTAC . ".tac_codecrea='" . valsession('code_log') . "'";
                    // Une agence peut voir les tâches créées par ses techniciens
                    foreach ($tab_lst_tec as $key => $value) {
                        $rech .= ' OR ' . TBTAC . ".tac_codedest='" . $value . "'";
                    }
                    $rech .= '))';
                    break;
                case 10 :
                    // On construit la liste des techniciens actifs du sous-traitant
                    $tab_lst_tec = [];
                    $rech2 = 'SELECT tec_code FROM `' . TBTEC . "` WHERE tec_codest='" . valsession('code_log') . "' AND tec_out=0";
                    $rows2 = $db->get_results($rech2, ARRAY_A);
                    if ($rows2) {
                        foreach ($rows2 as $row2) {
                            extract($row2);
                            $tab_lst_tec[] = $tec_code;
                        }
                    }
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= ' WHERE ((' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
                    } else {
                        $rech .= ' AND ((' . TBTAC . ".tac_codedest='" . valsession('code_log') . "'";
                    }
                    // Une agence peut voir les tâches de ses techniciens
                    foreach ($tab_lst_tec as $key => $value) {
                        $rech .= ' OR ' . TBTAC . ".tac_codedest='" . $value . "'";
                    }
                    $rech .= ')';
                    // On autorise aussi les auteurs à voir les tâches qu'ils ont créé
                    $rech .= ' OR (' . TBTAC . ".tac_codecrea='" . valsession('code_log') . "'";
                    // Une agence peut voir les tâches créées par ses techniciens
                    foreach ($tab_lst_tec as $key => $value) {
                        $rech .= ' OR ' . TBTAC . ".tac_codedest='" . $value . "'";
                    }
                    $rech .= '))';
                    break;
            }
            // Rajout du filtrage et des limites
            if ($first_date <> '') {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= " (tac_datecrea>='" . $first_date . "' AND tac_datecrea<='" . $datea . "')";
            }
            $rech .= ' ORDER BY tac_code DESC';
            if ($last_enr <> 0) {
                $rech .= ' LIMIT 0, ' . $last_enr;
            }
            break;
        // Dossiers de suivi
        case 'dos' :
            $rech = 'SELECT dos_code, dos_datecrea, dos_codecli, dos_codeage, dos_titre, dos_res, dos_codedosc, dos_urg, dos_proba, dos_datefinp';
            $rech .= ' FROM `' . TBDOS . '`';
            switch ($_SESSION['niv_log']) {
                // Agences et leurs techniciens
                case 3 :
                case 4 :
                    if (!stristr($rech, 'WHERE ')) {
                        $rech .= " WHERE dos_codeage='" . valsession('code_log') . "'";
                    } else {
                        $rech .= " AND dos_codeage='" . valsession('code_log') . "'";
                    }
                    break;
            }
            // Rajout du filtrage et des limites
            if ($first_date <> '') {
                if (!stristr($rech, 'WHERE ')) {
                    $rech .= ' WHERE ';
                } else {
                    $rech .= ' AND ';
                }
                $rech .= " (dos_datecrea>='" . $first_date . "' AND dos_datecrea<='" . $datea . "')";
            }
            $rech .= ' ORDER BY dos_code DESC';
            if ($last_enr <> 0) {
                $rech .= ' LIMIT 0, ' . $last_enr;
            }
            break;
    }
    $rows = $db->get_results($rech, ARRAY_A);

    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);

            $add_ligne = TRUE;

            if ($trss == 'int') {
                // Au cas où l'intervention n'est pas encore affectée et que l'on est en mode technicien agence ou agence,
                // il faut vérifier si le client fait partie de la zone de chalandise de l'agence
                if (CheckNivLog('3-4') AND (trim($int_codeage) == '')) {
                    $row2 = lit_enr_cli($int_codecli, FALSE);
                    if (is_array($row2)) {
                        $row2 = encode_str($row2);
                        extract($row2);
                        if (trim($cli_prop) == '') {
                            $rech2 = 'SELECT COUNT(*) FROM `' . TBZCC . "` WHERE `zcc_code` = '" . $_SESSION['code_age'] . "' AND `zcc_codepays` = '" . $cli_codepays . "' AND `zcc_codedep` = '" . $cli_dep . "'";
                            $nbres2 = $db->get_var($rech2);
                            if ($nbres2 == 0) {
                                $add_ligne = FALSE;
                            }
                        } else {
                            if (trim($cli_prop) <> $_SESSION['code_age']) {
                                $add_ligne = FALSE;
                            }
                        }
                    }
                }
            }

            if ($add_ligne == TRUE) {
                $link_item = $link_racine;
                switch ($trss) {
                    case 'int' :
                        $titre_item = $int_code;
                        $link_item .= 'int_maj.php?td=edit&ci=' . $int_code . '&cc=' . $int_codecli . '&ca=' . $int_codeage;
                        break;
                    case 'ot' :
                        $titre_item = $ot_code;
                        $codetec = '';
                        $codeint = '';
                        if ((trim($ot_codeint) <> '') AND (trim($ot_codeint) <> '--AR--')) {
                            $rech2 = 'SELECT int_codetec FROM `' . TBINT . "` WHERE `int_code` = '" . $ot_codeint . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                extract($row2);
                                $codetec = $int_codetec;
                                $codeint = $ot_codeint;
                            }
                        }
                        $link_item .= 'ot_maj.php?tx=m&td=edit&ca=&ci=' . $codeint . '&cc=' . $ot_codecli . '&co=' . $ot_code . '&si=' . $ot_idsite;
                        $link_item .= '&tr=' . $codetec;
                        break;
                    case 'cocl' :
                        $titre_item = $cocl_code;
                        $link_item .= 'cocl_maj.php?td=edit&co=' . $cocl_code . '&cc=' . $cocl_codecli;
                        break;
                    case 'coloc' :
                        $titre_item = $coloc_code;
                        $link_item .= 'coloc_maj.php?td=edit&tc=' . $coloc_typcli . '&co=' . $coloc_code . '&cc=' . $coloc_codecli . '&ca=' . $coloc_codage;
                        break;
                    case 'tac' :
                        $titre_item = $tac_code;
                        $link_item .= 'tac_maj.php?td=edit&ct=' . $tac_code;
                        break;
                    case 'dos' :
                        $titre_item = $dos_code;
                        $link_item .= 'dos_maj.php?td=edit&cd=' . $dos_code . '&cc=' . $dos_codecli . '&ca=' . $dos_codage;
                        break;
                }
                $link_item .= '&log=' . $pw_code . '&pw=' . $pw_pass;

                $txt_desc = '';
                switch ($trss) {
                    // Interventions
                    case 'int' :
                        $titre_item .= ' (';
                        $date2 = date('r', strtotime($int_datecrea));
                        $txt_desc .= '<table width="100%"  border="0">';
                        $txt_desc .= '<tr align="center" style="text-decoration:underline;">';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Compte client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Site client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '<td width="50%">';
                        $row2 = lit_enr_cli($int_codecli, FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $txt_desc .= '<div style="margin-left:7px;">';
                            $txt_desc .= '<span style="font-weight: bold;">';
                            if (trim($cli_ste) <> '') {
                                $txt_desc .= $cli_ste;
                                $titre_item .= $cli_ste;
                            } else {
                                $txt_desc .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                                $titre_item .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                            }
                            $txt_desc .= '</span>';
                            $txt_desc .= '<br />';
                            $txt_desc .= $cli_adr1;
                            $txt_desc .= '<br />';
                            if ($cli_adr2 <> '') {
                                $txt_desc .= $cli_adr2;
                                $txt_desc .= '<br />';
                            }
                            $txt_desc .= Retournecp_Id($cli_ville, FALSE) . ' ' . Retourneville_Id($cli_ville, FALSE);
                            $txt_desc .= '<br />';
                            $txt_desc .= 'Tel : ' . $cli_telf;
                            $txt_desc .= '</div>';
                        }
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $titre_item .= ' - ';
                        switch ((int)$int_idsite) {
                            case 0 :
                                $txt_desc .= '<span style="font-weight: bold;">';
                                $txt_desc .= 'Site principal';
                                $titre_item .= 'Site principal';
                                $txt_desc .= '</span>';
                                break;
                            default :
                                $row2 = lit_enr_scli_id($int_idsite, FALSE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_desc .= '<span style="font-weight: bold;">';
                                    $txt_desc .= $scli_ste;
                                    $titre_item .= $scli_ste;
                                    $txt_desc .= '</span>';
                                    $txt_desc .= '<br />';
                                    $txt_desc .= $scli_adr1;
                                    $txt_desc .= '<br />';
                                    if ($scli_adr2 <> '') {
                                        $txt_desc .= $scli_adr2;
                                        $txt_desc .= '<br />';
                                    }
                                    $txt_desc .= Retournecp_Id($scli_ville, FALSE) . ' ' . Retourneville_Id($scli_ville, FALSE);
                                    $txt_desc .= '<br />';
                                    $txt_desc .= 'Tel : ' . $scli_telf;
                                    $txt_desc .= '<br />';
                                }
                                break;
                        }
                        $txt_desc .= '</div>';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '</table>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Contact client :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        switch ((int)$int_ddeur) {
                            case -1 :
                                $txt_desc .= $cli_prenom . ' ' . $cli_nom . ' (Contact principal';
                                $txt_desc .= ' - ' . $cli_telf . ')';
                                $txt_desc .= '<br />';
                                break;
                            case 0 :
                                $txt_desc .= $scli_rprenom . ' ' . $scli_rnom . ' (Contact site';
                                $txt_desc .= ' - ' . $scli_telf . ')';
                                $txt_desc .= '<br />';
                                break;
                            default :
                                $rech2 = 'SELECT * FROM `' . TBCCLI . "` WHERE `ccli_id` = '" . $int_ddeur . "' AND `ccli_codecli` = '" . $int_codecli . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2) {
                                    extract($row2);

                                    $txt_desc .= $ccli_prenom . ' ' . $ccli_nom;
                                    $row3 = lit_enr_fonc($ccli_codefonc, FALSE);
                                    if (is_array($row3)) {
                                        $row3 = encode_str($row3);
                                        extract($row3);
                                        $txt_fonc = $fonc_desc;
                                    } else {
                                        $txt_fonc = $ccli_codefonc;
                                    }
                                    $txt_desc .= ' (' . $txt_fonc;
                                    $txt_desc .= ' - ' . $ccli_telf . ')';
                                }
                                break;
                        }
                        $txt_desc .= '</div>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Programmée le  : ' . datetolocal($int_daterdv) . ' à ' . heure_to_hhmm($int_hrdv);
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Matériel :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $txt_desc .= $int_mat;
                        $txt_desc .= '</div>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Travail à effectuer :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $txt_desc .= $int_pbm;
                        $txt_desc .= '</div>';
                        if (($_SESSION['niv_log'] <> 6) AND ($_SESSION['niv_log'] <> 7)) {
                            if (trim($int_codeman) <> '') {
                                $titre_item .= ' - ' . $int_codeman;
                            }
                        } else {
                            if (trim($int_codeext) <> '') {
                                $titre_item .= ' - ' . $int_codeext;
                            }
                        }
                        $titre_item .= ')';
                        break;

                    // Bons de travail
                    case 'ot' :
                        $titre_item .= ' (';
                        $date2 = date('r', strtotime($ot_datecrea));
                        $txt_desc .= '<table width="100%"  border="0">';
                        $txt_desc .= '<tr align="center" style="text-decoration:underline;">';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Compte client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Site client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '<td width="50%">';
                        $row2 = lit_enr_cli($ot_codecli, FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $txt_desc .= '<div style="margin-left:7px;">';
                            $txt_desc .= '<span style="font-weight: bold;">';
                            if (trim($cli_ste) <> '') {
                                $txt_desc .= $cli_ste;
                                $titre_item .= $cli_ste;
                            } else {
                                $txt_desc .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                                $titre_item .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                            }
                            $txt_desc .= '</span>';
                            $txt_desc .= '<br />';
                            $txt_desc .= $cli_adr1;
                            $txt_desc .= '<br />';
                            if ($cli_adr2 <> '') {
                                $txt_desc .= $cli_adr2;
                                $txt_desc .= '<br />';
                            }
                            $txt_desc .= Retournecp_Id($cli_ville, FALSE) . ' ' . Retourneville_Id($cli_ville, FALSE);
                            $txt_desc .= '<br />';
                            $txt_desc .= 'Tel : ' . $cli_telf;
                            $txt_desc .= '</div>';
                        }
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $titre_item .= ' - ';
                        switch ((int)$ot_idsite) {
                            case 0 :
                                $txt_desc .= '<span style="font-weight: bold;">';
                                $txt_desc .= 'Site principal';
                                $titre_item .= 'Site principal';
                                $txt_desc .= '</span>';
                                break;
                            default :
                                $row2 = lit_enr_scli_id($ot_idsite, FALSE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_desc .= '<span style="font-weight: bold;">';
                                    $txt_desc .= $scli_ste;
                                    $titre_item .= $scli_ste;
                                    $txt_desc .= '</span>';
                                    $txt_desc .= '<br />';
                                    $txt_desc .= $scli_adr1;
                                    $txt_desc .= '<br />';
                                    if ($scli_adr2 <> '') {
                                        $txt_desc .= $scli_adr2;
                                        $txt_desc .= '<br />';
                                    }
                                    $txt_desc .= Retournecp_Id($scli_ville, FALSE) . ' ' . Retourneville_Id($scli_ville, FALSE);
                                    $txt_desc .= '<br />';
                                    $txt_desc .= 'Tel : ' . $scli_telf;
                                    $txt_desc .= '<br />';
                                }
                                break;
                        }
                        $txt_desc .= '</div>';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '</table>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Contact client :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        switch ((int)$ot_ccli) {
                            case -1 :
                                $txt_desc .= $cli_prenom . ' ' . $cli_nom . ' (Contact principal';
                                $txt_desc .= ' - ' . $cli_telf . ')';
                                $txt_desc .= '<br />';
                                break;
                            case 0 :
                                $txt_desc .= $scli_rprenom . ' ' . $scli_rnom . ' (Contact site';
                                $txt_desc .= ' - ' . $scli_telf . ')';
                                $txt_desc .= '<br />';
                                break;
                            default :
                                $rech2 = 'SELECT * FROM `' . TBCCLI . "` WHERE `ccli_id` = '" . $ot_ccli . "' AND `ccli_codecli` = '" . $ot_codecli . "'";
                                $row2 = $db->get_row($rech2, ARRAY_A);
                                if ($row2) {
                                    extract($row2);

                                    $txt_desc .= $ccli_prenom . ' ' . $ccli_nom;
                                    $row3 = lit_enr_fonc($ccli_codefonc, FALSE);
                                    if (is_array($row3)) {
                                        $row3 = encode_str($row3);
                                        extract($row3);
                                        $txt_fonc = $fonc_desc;
                                    } else {
                                        $txt_fonc = $ccli_codefonc;
                                    }
                                    $txt_desc .= ' (' . $txt_fonc;
                                    $txt_desc .= ' - ' . $ccli_telf . ')';
                                }
                                break;
                        }
                        $txt_desc .= '</div>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Matériel :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $txt_desc .= $ot_mat;
                        $txt_desc .= '</div>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Travail à effectuer :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $txt_desc .= $ot_pbm;
                        $txt_desc .= '</div>';
                        $titre_item .= ')';
                        break;
                    // Contrats de maintenance clients
                    case 'cocl' :
                        $titre_item .= ' (';
                        $date2 = date('r', strtotime($cocl_datecrea));
                        $txt_desc .= '<table width="100%"  border="0">';
                        $txt_desc .= '<tr align="center" style="text-decoration:underline;">';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Compte client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= '&nbsp;';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '<td width="50%">';
                        $row2 = lit_enr_cli($cocl_codecli, FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $txt_desc .= '<div style="margin-left:7px;">';
                            $txt_desc .= '<span style="font-weight: bold;">';
                            if (trim($cli_ste) <> '') {
                                $txt_desc .= $cli_ste;
                                $titre_item .= $cli_ste;
                            } else {
                                $txt_desc .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                                $titre_item .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                            }
                            $txt_desc .= '</span>';
                            $txt_desc .= '<br />';
                            $txt_desc .= $cli_adr1;
                            $txt_desc .= '<br />';
                            if ($cli_adr2 <> '') {
                                $txt_desc .= $cli_adr2;
                                $txt_desc .= '<br />';
                            }
                            $txt_desc .= Retournecp_Id($cli_ville, FALSE) . ' ' . Retourneville_Id($cli_ville, FALSE);
                            $txt_desc .= '<br />';
                            $txt_desc .= 'Tel : ' . $cli_telf;
                            $txt_desc .= '</div>';
                        }
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= '&nbsp;';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '</table>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Matériel :';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $txt_desc .= $cocl_matos;
                        $txt_desc .= '</div>';
                        $titre_item .= ')';
                        break;
                    // Contrats de location
                    case 'coloc' :
                        $titre_item .= ' (';
                        $date2 = date('r', strtotime($coloc_datecrea));
                        $txt_desc .= '<table width="100%"  border="0">';
                        $txt_desc .= '<tr align="center" style="text-decoration:underline;">';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Compte client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= '&nbsp;';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '<td width="50%">';
                        switch ($coloc_typcli) {
                            // Clients
                            case 0 :
                                $row2 = lit_enr_cli($coloc_codecli, FALSE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_desc .= '<div style="margin-left:7px;">';
                                    $txt_desc .= '<span style="font-weight: bold;">';
                                    if (trim($cli_ste) <> '') {
                                        $txt_desc .= $cli_ste;
                                        $titre_item .= $cli_ste;
                                    } else {
                                        $txt_desc .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                                        $titre_item .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                                    }
                                    $txt_desc .= '</span>';
                                    $txt_desc .= '<br />';
                                    $txt_desc .= $cli_adr1;
                                    $txt_desc .= '<br />';
                                    if ($cli_adr2 <> '') {
                                        $txt_desc .= $cli_adr2;
                                        $txt_desc .= '<br />';
                                    }
                                    $txt_desc .= Retournecp_Id($cli_ville, FALSE) . ' ' . Retourneville_Id($cli_ville, FALSE);
                                    $txt_desc .= '<br />';
                                    $txt_desc .= 'Tel : ' . $cli_telf;
                                    $txt_desc .= '</div>';
                                }
                                break;
                            // Techniciens agences & sous-traitant
                            case 2 :
                            case 4 :
                                $row2 = lit_enr_tec($coloc_codecli, FALSE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_desc .= '<div style="margin-left:7px;">';
                                    $txt_desc .= '<span style="font-weight: bold;">';
                                    $txt_desc .= $tec_civilite . ' ' . $tec_prenom . ' ' . $tec_nom;
                                    $titre_item .= $tec_civilite . ' ' . $tec_prenom . ' ' . $tec_nom;
                                    $txt_desc .= '</span>';
                                    $txt_desc .= '<br />';
                                    $txt_desc .= $tec_adr1;
                                    $txt_desc .= '<br />';
                                    if ($tec_adr2 <> '') {
                                        $txt_desc .= $tec_adr2;
                                        $txt_desc .= '<br />';
                                    }
                                    $txt_desc .= Retournecp_Id($tec_ville, FALSE) . ' ' . Retourneville_Id($tec_ville, FALSE);
                                    $txt_desc .= '<br />';
                                    $txt_desc .= 'Tel : ' . $tec_telf;
                                    $txt_desc .= '</div>';
                                }
                                break;
                            // Sous-traitant
                            case 3 :
                                $row2 = lit_enr_st($coloc_codecli, FALSE);
                                if (is_array($row2)) {
                                    $row2 = encode_str($row2);
                                    extract($row2);
                                    $txt_desc .= '<div style="margin-left:7px;">';
                                    $txt_desc .= '<span style="font-weight: bold;">';
                                    $txt_desc .= $st_nom;
                                    $titre_item .= $st_nom;
                                    $txt_desc .= '</span>';
                                    $txt_desc .= '<br />';
                                    $txt_desc .= $st_adr1;
                                    $txt_desc .= '<br />';
                                    if ($st_adr2 <> '') {
                                        $txt_desc .= $st_adr2;
                                        $txt_desc .= '<br />';
                                    }
                                    $txt_desc .= Retournecp_Id($st_ville, FALSE) . ' ' . Retourneville_Id($st_ville, FALSE);
                                    $txt_desc .= '<br />';
                                    $txt_desc .= 'Tel : ' . $st_telf;
                                    $txt_desc .= '</div>';
                                }
                                break;
                        }
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= '&nbsp;';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '</table>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Début : ' . datetolocal($coloc_datedeb) . ' - Fin : ' . datetolocal($coloc_datefin);
                        $titre_item .= ')';
                        break;
                    // Tâches
                    case 'tac' :
                        $date2 = date('r', strtotime($tac_datecrea));
                        if (CheckNivLog('10-4-5-9')) {
                            $titre_item .= ' (';
                            $txt_desc .= '<hr noshade>';
                            $txt_desc .= 'Auteur : ';
                            if (trim($tac_codecrea) <> valsession('code_log')) {
                                $txt_desc .= $tac_codecrea;
                            } else {
                                $txt_desc .= 'vous-même';
                            }
                            $txt_desc .= '<br />';
                            $txt_desc .= '<hr noshade>';
                            $txt_desc .= '<table width="100%"  border="0">';
                            $txt_desc .= '<tr align="center" style="text-decoration:underline;">';
                            $txt_desc .= '<td width="50%">';
                            $txt_desc .= 'Destinataire';
                            $txt_desc .= '</td>';
                            $txt_desc .= '<td width="50%">';
                            $txt_desc .= '&nbsp;';
                            $txt_desc .= '</td>';
                            $txt_desc .= '</tr>';
                            $txt_desc .= '<td width="50%">';
                            switch ($tac_typdest) {
                                // Techniciens agences
                                case 1 :
                                case 3 :
                                    $row2 = lit_enr_tec($tac_codedest, FALSE);
                                    if (is_array($row2)) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                        $txt_desc .= '<div style="margin-left:7px;">';
                                        $txt_desc .= '<span style="font-weight: bold;">';
                                        $txt_desc .= $tec_civilite . ' ' . $tec_prenom . ' ' . $tec_nom;
                                        $titre_item .= $tec_civilite . ' ' . $tec_prenom . ' ' . $tec_nom;
                                        if ($tac_typdest == 1) {
                                            $txt_desc .= ' (technicien agence)';
                                        } else {
                                            $txt_desc .= ' (technicien sous-traitant)';
                                        }
                                        $txt_desc .= '</span>';
                                        $txt_desc .= '<br />';
                                        $txt_desc .= $tec_adr1;
                                        $txt_desc .= '<br />';
                                        if ($tec_adr2 <> '') {
                                            $txt_desc .= $tec_adr2;
                                            $txt_desc .= '<br />';
                                        }
                                        $txt_desc .= Retournecp_Id($tec_ville, FALSE) . ' ' . Retourneville_Id($tec_ville, FALSE);
                                        $txt_desc .= '<br />';
                                        $txt_desc .= 'Tel : ' . $tec_telf;
                                        $txt_desc .= '</div>';
                                    }
                                    break;
                                // Agences
                                case 2 :
                                    $row2 = lit_enr_age($tac_codedest, FALSE);
                                    if (is_array($row2)) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                        $txt_desc .= '<div style="margin-left:7px;">';
                                        $txt_desc .= '<span style="font-weight: bold;">';
                                        $txt_desc .= $age_nom;
                                        $titre_item .= $age_nom;
                                        $txt_desc .= '</span>';
                                        $txt_desc .= '<br />';
                                        $txt_desc .= $age_adr1;
                                        $txt_desc .= '<br />';
                                        if ($age_adr2 <> '') {
                                            $txt_desc .= $age_adr2;
                                            $txt_desc .= '<br />';
                                        }
                                        $txt_desc .= Retournecp_Id($age_ville, FALSE) . ' ' . Retourneville_Id($age_ville, FALSE);
                                        $txt_desc .= '<br />';
                                        $txt_desc .= 'Tel : ' . $age_telf;
                                        $txt_desc .= '</div>';
                                    }
                                    break;
                                // Sous-traitant
                                case 4 :
                                    $row2 = lit_enr_st($tac_codedest, FALSE);
                                    if (is_array($row2)) {
                                        $row2 = encode_str($row2);
                                        extract($row2);
                                        $txt_desc .= '<div style="margin-left:7px;">';
                                        $txt_desc .= '<span style="font-weight: bold;">';
                                        $txt_desc .= $st_nom;
                                        $titre_item .= $st_nom;
                                        $txt_desc .= '</span>';
                                        $txt_desc .= '<br />';
                                        $txt_desc .= $st_adr1;
                                        $txt_desc .= '<br />';
                                        if ($st_adr2 <> '') {
                                            $txt_desc .= $st_adr2;
                                            $txt_desc .= '<br />';
                                        }
                                        $txt_desc .= Retournecp_Id($st_ville, FALSE) . ' ' . Retourneville_Id($st_ville, FALSE);
                                        $txt_desc .= '<br />';
                                        $txt_desc .= 'Tel : ' . $st_telf;
                                        $txt_desc .= '</div>';
                                    }
                                    break;
                            }
                            $titre_item .= ')';
                            $txt_desc .= '</td>';
                            $txt_desc .= '<td width="50%">';
                            $txt_desc .= '&nbsp;';
                            $txt_desc .= '</td>';
                            $txt_desc .= '</tr>';
                            $txt_desc .= '</table>';
                        }
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'A faire : ' . $tac_afaire . '<br />';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= 'Date butoir : ' . datetolocal($tac_datefinp);
                        break;
                    // Dossiers de suivi
                    case 'dos' :
                        $titre_item .= ' (';
                        $date2 = date('r', strtotime($dos_datecrea));
                        $txt_desc .= '<table width="100%"  border="0">';
                        $txt_desc .= '<tr align="center" style="text-decoration:underline;">';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Compte client';
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Détail';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '<td width="50%">';
                        $row2 = lit_enr_cli($dos_codecli, FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $txt_desc .= '<div style="margin-left:7px;">';
                            $txt_desc .= '<span style="font-weight: bold;">';
                            if (trim($cli_ste) <> '') {
                                $txt_desc .= $cli_ste;
                                $titre_item .= $cli_ste;
                            } else {
                                $txt_desc .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                                $titre_item .= $cli_civilite . ' ' . $cli_prenom . ' ' . $cli_nom;
                            }
                            $txt_desc .= '</span>';
                            $txt_desc .= '<br />';
                            $txt_desc .= $cli_adr1;
                            $txt_desc .= '<br />';
                            if ($cli_adr2 <> '') {
                                $txt_desc .= $cli_adr2;
                                $txt_desc .= '<br />';
                            }
                            $txt_desc .= Retournecp_Id($cli_ville, FALSE) . ' ' . Retourneville_Id($cli_ville, FALSE);
                            $txt_desc .= '<br />';
                            $txt_desc .= 'Tel : ' . $cli_telf;
                            $txt_desc .= '</div>';
                        }
                        $txt_desc .= '</td>';
                        $txt_desc .= '<td width="50%">';
                        $txt_desc .= 'Catégorie : ';
                        $row2 = lit_enr_dosc($dos_codedosc, FALSE);
                        if (is_array($row2)) {
                            $row2 = encode_str($row2);
                            extract($row2);
                            $txt_desc .= $dosc_desc;
                        }
                        $txt_desc .= '<br />';
                        $txt_desc .= 'Probabilité : ';
                        $txt_desc .= $dos_proba . '%';
                        $txt_desc .= '<br />';
                        $txt_desc .= 'Fin prévue le : ';
                        $txt_desc .= datetolocal($dos_datefinp);
                        $txt_desc .= '<br />';
                        $txt_desc .= '</td>';
                        $txt_desc .= '</tr>';
                        $txt_desc .= '</table>';
                        $txt_desc .= '<hr noshade>';
                        $txt_desc .= '<span style="font-weight: bold;">';
                        $txt_desc .= $dos_titre;
                        $txt_desc .= '</span>';
                        $txt_desc .= '<div style="margin-left:7px;">';
                        $txt_desc .= $dos_res;
                        $txt_desc .= '</div>';
                        $titre_item .= ')';
                        break;
                }

                echo '<item>';
                $titre_item = htmlspecialchars($titre_item);
                $titre_item = utf8_encode($titre_item);
                echo '<title>' . $titre_item . '</title>';
                $link_item = htmlspecialchars($link_item);
                $link_item = utf8_encode($link_item);
                echo '<link>' . $link_item . '</link>';
                echo '<pubDate>' . $date2 . ' GMT</pubDate>';
                $txt_desc = htmlspecialchars($txt_desc);
                $txt_desc = utf8_encode($txt_desc);
                echo '<description>' . $txt_desc . '</description>';
                echo '</item>';
            }
        }
    }
    close_database();
} else {
    $titre = _('Flux RSS généré par SMI');
    $titre = htmlspecialchars($titre);
    $titre = utf8_encode($titre);
    echo '<title>' . $titre . '</title>';
    echo '<link></link>';
    echo '<description>' . _('Description du channel') . '</description>';
    echo '<item>';
    echo '<title>' . _('Erreur !') . '</title>';
    echo '<link></link>';
    $txt_desc = _("Vous n'avez pas entré les bons paramètres");
    $txt_desc = htmlspecialchars($txt_desc);
    $txt_desc = utf8_encode($txt_desc);
    echo '<description>' . $txt_desc . '</description>';
    echo '</item>';
}

// édition de la fin du fichier XML
echo '</channel>';
echo '</rss>';
