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


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


include_once("../inc/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(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);
$_GET = clean_str($_GET);

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

$action_to_do = '';
if (trim(@$_POST["nbutton"]) == 'enr') // On a cliqué sur valider
    $action_to_do = 'valid';
if (trim(@$_POST["nbutton"]) == 'del') // On a cliqué sur supprimer
    $action_to_do = 'delete';
if (trim(@$_POST["nbutton"]) == 'add') // On a cliqué sur ajouter
    $action_to_do = 'add';

if (isset($_GET['td']))
    $td = @$_GET['td'];
else {
        $td = @$_POST['td'];
}

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

if (isset($_GET['iwm']))
    $idwmque = intval(@$_GET['iwm']);
else {
    if ($action_to_do <> '')
        $idwmque = intval(@$_POST['iwm']);
}

$pbm_rlpma = FALSE;
$wmque_prop = "";
$wmquen_nota = "";
$codeuser = @$_SESSION["code_log"];
if (@$_SESSION["niv_log"] == 3)
    $codeuser = @$_SESSION["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 = intval(@$_POST["iwm"]);
            $prop = @$_POST["prop"];
            $code = @$_POST["cm"];
            $desc = @$_POST["desc"];
            $lien = @$_POST["lien"];
            $login = @$_POST["login"];
            $mdp = @$_POST["mdp"];
            $notaa = @$_POST["notaa"];
            $notat = @$_POST["notat"];
            $notap = @$_POST["notap"];

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

            // 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 (@$_SESSION["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;
            }
            $db->disconnect();
            $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 (is_array($row)) {
                $row = encode_str($row);
                extract($row);
                $row = lit_enr_WMqueNPriv($wmque_id, $codeuser);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);
                }
            } else
                $pbm_rlpma = TRUE;
            break;
        case "lst" :
            $titre = _("LISTE LIENS");
            break;
        default :
            $pbm_rlpma = TRUE;
            break;
    }
} else
    $pbm_rlpma = TRUE;

$affliste = TRUE;
if (((@$_SESSION["niv_log"] == 3) OR (@$_SESSION["niv_log"] == 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 (@$_SESSION["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','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                            break;
                        case "edit" :
                            echo "init_lien_ext('lnk_wmque','','lien');";
                            echo "init_valid_form('enr','wmque_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                            break;
                        case "view" :
                            echo 'readonly_all();';
                            break;
                        case "del" :
                            echo "init_valid_form('del','wmque_maj','".$_SESSION['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)
{
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>

<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 (@$_SESSION["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 (@$_SESSION["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);
        $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\">\n";

            // 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>\n";
            echo "<th>";
            echo _("Choisir");
            echo "</th>\n";

            $j = 0;

            foreach ($rows as $row) {
                $row = encode_str($row);
                // création de l'entête de ligne (TR)
                creetrlstdet($j);

                echo "<td>\n";
                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>\n";
                echo "<td align=\"center\">\n";
                $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>\n";
                echo "</tr>\n";

                $j++;
            }

            echo "</table>\n";
            // 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
        }
        $db->disconnect();
        break;

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