<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 1.0.2

Copyright (C) 2006-2015  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_droits.inc.php");
include_once("../inc/func_log.inc.php");
if (CheckURLLog(FALSE) == FALSE) header("Location: ../index.php");
if (CheckNivLog("5-9") == FALSE) header("Location: ../index.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.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);

$td = $_GET['td'];
$coderech = $_GET['code'];

$pbm_clcg = FALSE;

if ($coderech <> "") {
    $row = lit_enr_clcg($coderech);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    } else
        $pbm_clcg = TRUE;
} else {
    if ($td <> "add") $pbm_clcg = TRUE;
}

if ($pbm_clcg == FALSE) {
    switch ($td) {
        case "add" :
            $titre = _("CREATION GROUPEMENT CHAMPS CHECK-LIST");
            break;
        case "edit" :
            $titre = _("MISE A JOUR GROUPEMENT CHAMPS CHECK-LIST");
            break;
        case "del" :
            $titre = _("SUPPRESSION GROUPEMENT CHAMPS CHECK-LIST");
            break;
        default :
            $pbm_clcg = TRUE;
            break;
    }
}
?>
<!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 _("Fiche groupement champs check-list"); ?></title>

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

    <script language="javascript">
        <!--
        var tab_champ = ["nom:code ; format:alphanum ; req:Y ; txt:<?PHP echo _("Code"); ?> ; lmn:1 ; lmx:5; stok:frmok ; stbad:frmbad",
            "nom:desc ; format:txt ; req:Y ; txt:<?PHP echo _("Libellé"); ?>; lmn:1 ; lmx:50; stok:frmok ; stbad:frmbad"];
        //-->
    </script>

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

            function init_all() {
                init_Body();
                <?php
                 switch ($td)
                 {
                    case "add" :
                        echo "init_valid_clcg('enr','clcg_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                        break;
                    case "edit" :
                        echo "init_valid_clcg('enr','clcg_maj','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                        break;
                    case "view" :
                        echo "readonly_all();";
                        break;
                    case "del" :
                        echo "readonly_all();";
                        echo "warn_del('');";
                        break;
                 }
                ?>
                init_move_sel('addclc', 'clc', 'clcclcg');
                init_move_sel('delclc', 'clcclcg', 'clc');
                init_sel_all('enr', 'clcclcg','<?PHP echo _("Aucun"); ?>',0,'<?PHP echo _("Tous"); ?>',1,'');
                init_autosubmit('enr', 'click', 'clcg_maj');
                Focus_first();
            }

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

    <?PHP } ?>
</head>

<body>
<?PHP
include_once("../inc/entete.inc.php");
if ($pbm_clcg == FALSE) {
    cre_ent_form($titre, 'clcg_lst.php', $_SERVER['HTTP_REFERER'], '');
    ?>
    <form name="clcg_maj" id="clcg_maj" method="post" action="clcg_enr.php">
        <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
        <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">
        <input name="nbutton" type="hidden" id="nbutton" value="">
        <div align="center">
            <table width="80%" border="0">
                <tr>
                    <td width="10%" class="rubfrm" id="f_code"><?PHP echo _("Code"); ?></td>
                    <td width="47%">
                        <?PHP if ($td == "add") { ?>
                            <input name="code" type="text" id="code" value="<?PHP echo $clcg_code ?>" size="5">
                            <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>">
                        <?PHP } else { ?>
                        <input name="code" type="text" id="code" value="<?PHP echo $clcg_code ?>" size="5" readonly="1">
                    </td>
                    <?PHP } ?>
                </tr>
                <tr>
                    <td class="rubfrm" id="f_desc"><?PHP echo _("Libellé"); ?></td>
                    <td><input name="desc" type="text" id="desc" value="<?PHP echo $clcg_desc; ?>" size="50">
                        <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo _("Champ obligatoire"); ?>"></td>
                </tr>
            </table>

            <?PHP if ($td == "edit") { ?>
                <table width="100%" border="0">
                    <tr>
                        <td class="rubfrm" colspan="3" align="center">
                            <?PHP echo _("Choix des champs pour ce groupement de champs Check-list"); ?>
                            <div class="annot_petit_it">
                                (<?PHP echo _("Si vous ne voulez pas définir de check-list, ne saisissez rien"); ?>))
                            </div>

                        </td>
                    </tr>
                    <tr>
                        <td align="center">
                            <?PHP
                            // On crée le tableau des champs check list utilisés
                            $tab_clc = give_tab_clc_clcg($clcg_code, TRUE);

                            echo '<div class="annot_gras_it">';
                            echo _("Champs à choisir");
                            echo '</div>';
                            echo '<br />';

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

                            $rech = "SELECT * FROM `" . TBCLC . "`";
                            if (count($tab_clc) > 0) {
                                $rech .= " WHERE (";
                                foreach ($tab_clc as $key => $clc) {
                                    if (trim($clc) <> "")
                                        $rech .= " clc_code <> '" . $db->escape($clc) . "' AND";
                                }
                                // Suppression du dernier 'AND'
                                $rech = substr($rech, 0, strlen($rech) - 4);
                                $rech .= ")";
                            }
                            $rech .= " ORDER BY clc_desc";
                            $rows = $db->get_results($rech, ARRAY_A);
                            $db->disconnect();
                            cre_select_tab('clc', 'clc', $rows, 'clc_code', 'clc_desc', '', '', '', _("-- Faites votre choix --"), 10, TRUE);
                            ?>
                        </td>
                        <td align="center">
                            <input type="button" id="addclc" value="<?PHP echo _("Ajouter"); ?> >>>">
                            <br />
                            <br />
                            <input type="button" id="delclc" value="&lt;&lt;&lt; <?PHP echo _("Supprimer"); ?>">
                        </td>
                        <td align="center">
                            <?PHP
                            echo '<div class="annot_gras_it">';
                            echo _("Champs sélectionnés");
                            echo '</div>';
                            echo '<br />';

                            if (count($tab_clc) > 0) {
                                $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

                                $rech = "SELECT * FROM `" . TBCLC . "`";
                                if (count($tab_clc) > 0) {
                                    $rech .= " WHERE (";
                                    foreach ($tab_clc as $key => $clc) {
                                        if (trim($clc) <> "")
                                            $rech .= " clc_code = '" . $db->escape($clc) . "' OR";
                                    }
                                    // Suppression du dernier 'OR'
                                    $rech = substr($rech, 0, strlen($rech) - 3);
                                    $rech .= ")";
                                }
                                $rech .= " ORDER BY clc_desc";
                                $rows = $db->get_results($rech, ARRAY_A);
                                $db->disconnect();
                            } else
                                $rows = [];

                            cre_select_tab('clcclcg[]', 'clcclcg', $rows, 'clc_code', 'clc_desc', '', '', '', _("-- Faites votre choix --"), 10, TRUE);
                            ?>
                        </td>
                    </tr>
                </table>
            <?PHP } ?>
        </div>
        <p align="center">
            <?PHP
            switch ($td) {
                case "add" :
                case "edit" :
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="' . _("Réinitialiser") . '">';
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="' . _("Enregistrer") . '">';
                    break;
                case "del" :
                    echo '<input name="del" type="submit" class="bton_std" id="del" value="' . _("Supprimer") . '">';
                    break;
            }
            ?>
        </p>
    </form>
<?PHP
} else
    pop_ret_auto(_("Problème de paramètres"), 'warn', "clcg_lst.php");
include_once("pied.php");
?>
</body>

</html>