<?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');
$ok_inc = FALSE;
if ($pos === FALSE) {
    $ok_inc = TRUE;
}
if ($ok_inc == FALSE) {
    header('Location: ../index.php');
}


include_once('../inc/config.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_mque.inc.php');
include_once('../inc/func.inc.php');
include_once('../inc/func_log.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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);
$_GET = clean_str($_GET);

$coderlpma = '';
$idwmque = 0;
$totalwmque = 0;
$td = '';

$action_to_do = '';
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 (trim(valpost('nbutton')) == 'add') // On a cliqué sur ajouter
{
    $action_to_do = 'add';
}

$td = valgetpost('td');

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

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

$pbm_rlpma = FALSE;
$wmque_prop = '';
$wmquen_nota = '';
$codeuser = valsession('code_log');
if (CheckNivLog('3')) {
    $codeuser = valsession('code_age');
}

if ($coderlpma <> '') {
    $row = lit_enr_mque($coderlpma);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        $totalwmque = nb_wmque_in_mque($coderlpma);
        if ($totalwmque == 0) {
            $td = 'add';
        }
        $retourliste = FALSE;

        if ($action_to_do <> '') {
            $idwmque = (int)valpost('iwm');
            $prop = valpost('prop');
            $code = valpost('cm');
            $desc = valpost('desc');
            $lien = valpost('lien');
            $login = valpost('login');
            $mdp = valpost('mdp');
            $notaa = valpost('notaa');
            $notat = valpost('notat');
            $notap = valpost('notap');

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

            // Fiche lien Marque
            switch ($td) {
                case 'add' :
                    $rech = 'INSERT INTO `' . TBWMQUE . '`';
                    $rech .= ' (wmque_codemque,wmque_desc,wmque_lien,wmque_login,wmque_mdp,wmque_notaa,wmque_notat,wmque_prop) VALUES';
                    $rech .= " ('" . $db->escape($code) . "','" . $db->escape($desc) . "','" . $db->escape($lien) . "','" . $db->escape($login) . "', ";
                    $rech .= " '" . $db->escape($mdp) . "','" . $db->escape($notaa) . "','" . $db->escape($notat) . "','$prop')";
                    break;
                case 'edit' :
                    if (valsession('niv_log') >= 5) {
                        $rech = 'UPDATE `' . TBWMQUE . "` SET `wmque_codemque`='" . $db->escape($code) . "', `wmque_desc`='" . $db->escape($desc) . "', `wmque_lien`='" . $db->escape($lien) . "',";
                        $rech .= " `wmque_login`='" . $db->escape($login) . "', `wmque_mdp`='" . $db->escape($mdp) . "',";
                        $rech .= " `wmque_notaa`='" . $db->escape($notaa) . "', `wmque_notat`='" . $db->escape($notat) . "', `wmque_prop`='" . $prop . "'";
                        $rech .= "  WHERE `wmque_id` = '" . $idwmque . "'";
                    } else {
                        $rech = 'UPDATE `' . TBWMQUE . "` SET `wmque_codemque`='" . $code . "', `wmque_desc`='" . $db->escape($desc) . "', `wmque_lien`='" . $db->escape($lien) . "',";
                        $rech .= " `wmque_login`='" . $db->escape($login) . "', `wmque_mdp`='" . $db->escape($mdp) . "',";
                        $rech .= " `wmque_notat`='" . $db->escape($notat) . "', `wmque_prop`='" . $prop . "'";
                        $rech .= "  WHERE `wmque_id` = '" . $idwmque . "'";
                    }
                    break;
                case 'del' :
                    $rech = 'DELETE FROM `' . TBWMQUE . "` WHERE `wmque_id` = '" . $idwmque . "'";
                    break;
                default :
                    break;
            }

            if ($rech <> '') {
                $db->query($rech);

                $rech = '';
                // Notes privées
                switch ($td) {
                    case 'add' :
                        $id = $db->insert_id;
                        $rech = 'INSERT INTO `' . TBWMQUEN . '` (wmquen_idwmque,wmquen_codeu,wmquen_nota)';
                        $rech .= " VALUES ('$id','$codeuser','$notap')";
                        break;
                    case 'edit' :
                        $rech = 'SELECT COUNT(*) FROM `' . TBWMQUEN . "` WHERE `wmquen_idwmque` = '$idwmque' AND `wmquen_codeu` = '$codeuser'";
                        $nbres = $db->get_var($rech, ARRAY_A);
                        if ($nbres > 0) {
                            $rech = 'UPDATE `' . TBWMQUEN . '`';
                            $rech .= " SET `wmquen_nota`='" . $notap . "'";
                            $rech .= " WHERE (`wmquen_idwmque` = '" . $idwmque . "' AND `wmquen_codeu` = '" . $codeuser . "')";
                        } else {
                            $rech = 'INSERT INTO `' . TBWMQUEN . '` (wmquen_idwmque,wmquen_codeu,wmquen_nota)';
                            $rech .= " VALUES ('$idwmque','$codeuser','$notap')";
                        }
                        break;
                    case 'del' :
                        $rech = 'DELETE FROM `' . TBWMQUEN . "` WHERE `wmquen_idwmque` = '$idwmque'";
                        break;
                    default :
                        break;
                }
                if ($rech <> '') {
                    $db->query($rech);
                }
                $retourliste = TRUE;
            }
            close_database();
            $td = 'lst'; // On revient à la liste
            // On recalcule le total contacts
            $totalwmque = nb_wmque_in_mque($coderlpma);
        }
    } else {
        $pbm_rlpma = TRUE;
    }

    switch ($td) {
        case 'add' :
            $titre = _('CREATION LIEN');
            break;
        case 'edit' :
        case 'del' :
            if ($td == 'edit') {
                $titre = _('EDITION LIEN');
            } else {
                $titre = _('SUPPRESSION LIEN');
            }
            $row = lit_enr_wmque_id_in_mque($idwmque, $coderlpma);
            if ($row) {
                $row = encode_str($row);
                extract($row);
                $row2 = lit_enr_WMqueNPriv($wmque_id, $codeuser);
                if ($row2) {
                    $row2 = encode_str($row2);
                    extract($row2);
                }
            } else {
                $pbm_rlpma = TRUE;
            }
            break;
        case 'lst' :
            $titre = _('LISTE LIENS');
            break;
        default :
            $pbm_rlpma = TRUE;
            break;
    }
} else {
    $pbm_rlpma = TRUE;
}

$affliste = TRUE;
if (CheckNivLog('3-4') AND ($cmque_prop == '*')) {
    $affliste = FALSE;
}
?>
<!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 _('Liens Web Marque'); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = ["nom:prop ; format:liste ; req:Y ; txt:<?php echo _('Type de contact'); ?> ; vmn:1 ; stok:frmok ; stbad:frmbad",
            "nom:desc; format:txt ; req:Y ; txt:<?php echo _('Libellé'); ?>; lmx:50; stok:frmok ; stbad:frmbad",
            "nom:lien; format:url_pref ; req:Y ; txt:<?php echo _('Lien'); ?>; lmx:200; stok:frmok ; stbad:frmbad",
            "nom:login ; format:txt ; req:N ; txt:<?php echo _('Login'); ?> ; lmx:40 ; stok:frmok ; stbad:frmbad",
            "nom:mdp ; format:txt ; req:N ; txt:<?php echo _('Mot de Passe'); ?> ; lmx:40 ; stok:frmok ; stbad:frmbad",
            <?php
            if (valsession('niv_log') >= 5)
                {echo '"nom:notaa; format:txt ; req:N ; txt:'. _('Notes Administration').' ; stok:frmok ; stbad:frmbad",';}
            ?>
            "nom:notat; format:txt ; req:N ; txt:<?php echo _('Notes Agence'); ?> ; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

    <?php if ($pbm_rlpma == FALSE) { ?>
        <script type="text/javascript">
            <!--
            $(function () {
                $("#desc").Setcase({caseValue: 'upper', changeonFocusout: true});
            });

            function init_all() {
                init_Body();
                init_autohref_classe('falselnkimg', '');
                <?php
                if ($retourliste == FALSE)
                {
                    switch ($td)
                    {
                        case 'add' :
                            echo "init_lien_ext('lnk_wmque','','lien');";
                            echo "init_valid_form('enr','wmque_maj','".valsession('date_fmtshort')."',tab_champ,'','');";
                            break;
                        case 'edit' :
                            echo "init_lien_ext('lnk_wmque','','lien');";
                            echo "init_valid_form('enr','wmque_maj','".valsession('date_fmtshort')."',tab_champ,'','');";
                            break;
                        case 'view' :
                            echo 'readonly_all();';
                            break;
                        case 'del' :
                            echo "init_valid_form('del','wmque_maj','".valsession('date_fmtshort')."',tab_champ,'','');";
                            echo 'readonly_all();';
                            echo 'warn_del();';
                            break;
                    }
                    echo "chk_wmque('1', 'wmque', '".$totalwmque."');";
                    echo "add_func_click('quit', 'sclose','');";
                    echo "init_autohref('lst', 'click', 'wmque_maj.php?cm=".$coderlpma."&td=lst',false);";
                    echo "init_autohref('add', 'click', 'wmque_maj.php?cm=".$coderlpma."&td=add',false);";
                    if ($affliste == FALSE)
                        {echo "readonly_id('prop',false);";}
                    echo 'Focus_first();';
                }
                else
                    {echo "self.location.href = ('wmque_maj.php?cm=".$coderlpma."&td=lst');";}
                ?>
            }

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

<body>
<?php
if ($pbm_rlpma == FALSE) {
    cre_ent_form($titre, '', '', '', FALSE);
    ?>
    <p align="center"><span class="titre_gras"><?php echo _('Pour la marque'); ?> : <?php echo $mque_ste; ?> </span></p>
    <table width="100%" 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'); ?>"
                       style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;">
                <?php if (($totalwmque > 0) AND ($td <> 'lst')) { ?>
                    <input name="lst" type="button" class="bton_std" id="lst"
                           value="<?php echo _('Revenir à la liste'); ?>"
                           style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;">
                <?php } ?>
            </td>
        </tr>
    </table>
    <?php
    switch ($td) {
        case 'add' :
        case 'edit' :
        case 'del' :
            ?>
            <form name="wmque_maj" id="wmque_maj" method="post" action="wmque_maj.php">
                <input name="iwm" type="hidden" id="iwm" value="<?php echo $wmque_id ?>">
                <input name="cm" type="hidden" id="cm" value="<?php echo $coderlpma ?>">
                <input name="td" type="hidden" id="td" value="<?php echo $td ?>">
                <input name="nbutton" type="hidden" id="nbutton" value="">
                <table width="100%" border="0">
                    <tr>
                        <td width="160" class="rubfrm" id="f_prop" align="right"><?php echo _('Type de lien'); ?></td>
                        <td>
                            <?php
                            $tab_typectact = [
                                '*' => _('Public'),
                                $codeuser => _('Privé')];

                            $val_seltype = '*';
                            if ($cmque_prop <> '*') {
                                if (($cmque_prop == $codeuser) OR (valsession('niv_log') >= 5)) {
                                    $val_seltype = $codeuser;
                                }
                            }

                            cre_select('prop', 'prop', $tab_typectact, $val_seltype, '', _('-- Faites votre choix --'));

                            echo '<br />';
                            echo '<span class="annot_petit_it">';
                            echo _("'Privé' signifie que vous seul pourrez voir cette fiche") . '<br />' . _("Si vous passez ce lien en 'public', il le restera (sauf modification de l'administrateur)");
                            echo '</span>';
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_desc" align="right"><?php echo _('Libellé'); ?></td>
                        <td><input name="desc" type="text" id="desc" value="<?php echo $wmque_desc ?>" 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_lien" align="right"><?php echo _('Lien web'); ?></td>
                        <td><input name="lien" type="text" id="lien" value="<?php echo $wmque_lien ?>" size="80"
                                   maxlength="200">
                            <img src="../img/oblig.png" align="absmiddle" alt="<?php echo _('Champ obligatoire'); ?>">
                            <?php
                            echo ' <img src="../img/web_go.png" id ="lnk_wmque" border="0" align="absmiddle" alt="' . _('Se connecter au site du transporteur') . '">';
                            ?>
                            <div class="annot_petit_it">
                                (<?php echo _('Vous pouvez saisir sous la forme http://xxx.xxx.xxx ou  ftp://login:mdp@xxx.xxx.xxx'); ?>
                                )
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_login" align="right"><?php echo _('Login'); ?></td>
                        <td><input name="login" type="text" id="login" value="<?php echo $wmque_login ?>" size="40"
                                   maxlength="40"></td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_mdp" align="right"><?php echo _('Mot de passe'); ?></td>
                        <td><input name="mdp" type="text" id="mdp" value="<?php echo $wmque_mdp ?>" size="40"
                                   maxlength="40">
                        </td>
                    </tr>
                    <?php if (valsession('niv_log') >= 5) {
                        ?>
                        <tr>
                            <td width="160" class="rubfrm" id="f_notaa"
                                align="right"><?php echo _('Notes Administration'); ?></td>
                            <td><textarea name="notaa" id="notaa"><?php echo $wmque_notaa ?></textarea></td>
                        </tr>
                    <?php } ?>
                    <tr>
                        <td width="160" class="rubfrm" id="f_notat"
                            align="right"><?php echo _('Notes publiques Agences'); ?>
                            <br/><span class="annot_petit_it">(<?php echo _("Saisies par l'administrateur ou"); ?>
                                <br/><?php echo _('le centre de gestion'); ?>)</span></td>
                        <td><textarea name="notat" id="notat"><?php echo $wmque_notat ?></textarea></td>
                    </tr>
                    <tr>
                        <td width="160" class="rubfrm" id="f_notap">
                            <div align="right"><?php echo _('Notes Personnelles'); ?><br/><span
                                    class="annot_petit_it">(<?php echo _('Pour les techniciens ce sont'); ?>
                                    <br/><?php echo _('celles de leur agence'); ?>)</span></div>
                        </td>
                        <td>
                            <textarea name="notap" rows="5" id="notap"><?php echo $wmquen_nota ?></textarea>
                            <span class="annot_petit_it"><?php echo _('Vous seul pourrez voir ces notes'); ?></span>
                        </td>
                    </tr>
                </table>
                <p align="center">
                    <?php
                    switch ($td) {
                        case 'add' :
                        case 'edit' :
                            echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _('Valider') . '">';
                            if ($td == 'edit') {
                                echo '<input name="add" type="button" class="bton_std" id="add" value="' . _('Saisir un nouveau lien') . '">';
                            }
                            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') . '">';
                    ?>
                </p>
            </form>
            <?php
            break;
        case 'lst' :
            // Création de la liste des liens marques
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech = 'SELECT * FROM `' . TBWMQUE . "` WHERE `wmque_codemque` = '" . $coderlpma . "' ORDER BY 'wmque_nom'";

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

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

                // Création de l'entête de la table liens marques
                echo '<th width="90%">';
                echo _('Descriptif');
                echo ' <span class="annot_petit_it">' . _('Cliquer pour ouvrir le lien') . '</span>';
                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 ' <a href="' . $row['wmque_lien'] . '" target="_blank">' . $row['wmque_desc'] . '</a>';
                    echo '<div class="annot_petit_it" align="right">';
                    if ($row['wmque_prop'] == '*') {
                        echo _('Lien public');
                    } else {
                        echo _('Lien privé');
                        if ($row['wmque_prop'] <> $codeuser) {
                            echo ' (' . $row['wmque_prop'] . ')';
                        }
                    }
                    echo '</div>';
                    echo '</td>';
                    echo '<td align="center">';
                    $lst_maj = TRUE;
                    cre_falselnk_img('', 'wmque_edit_' . $row['wmque_id'], 'span', 'center', _('Edition'), 'lnk', 'wmque_maj.php?td=edit&cm=' . $row['wmque_codemque'] . '&iwm=' . $row['wmque_id'], '../img/edit.png');
                    $lst_del = TRUE;
                    cre_falselnk_img('', 'wmque_del_' . $row['wmque_id'], 'span', 'center', _('Suppression'), 'lnk', 'wmque_maj.php?td=del&cm=' . $row['wmque_codemque'] . '&iwm=' . $row['wmque_id'], '../img/del.png');
                    echo '</td>';
                    echo '</tr>';
                }

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

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