<?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");

// Récupération des variables "tri" et "ord" pour la création de la chaîne de recherche
// Si $inc_limit est vraie, Ajout de "LIMIT" à la requête MySQL

/**
 * Rajout du type de trie à une requête
 *
 * @param $tableau
 * @param $clefdefaut
 * @param $inc_limit
 * @param $val_limit
 *
 * @return string
 */
function crestr_triord($tableau, $clefdefaut, $inc_limit, $val_limit)
{
    if (isset($_GET['tri'])) {
        if (@$_GET['tri'] <> "")
            $complement_rech = " ORDER BY " . $tableau[$_GET['tri']];
        else
            $complement_rech = " ORDER BY " . $clefdefaut;
    } else
        $complement_rech = " ORDER BY " . $clefdefaut;

    if (isset($_GET['ord'])) {
        if (@$_GET["ord"] == "DES")
            $complement_rech .= " DESC";
    }

    if ($inc_limit == TRUE) {
        $npage = 0;
        if (isset($_GET['page']))
            $npage = intval($_GET['page']) - 1;
        $complement_rech .= " LIMIT " . $npage . ", $val_limit"; // Ajout de LIMIT
    }

    return $complement_rech;
}

/**
 * Met à jour une chaine de recherche existante, avec récupération des variables "tri" et "ord"
 * pour la création de la chaîne de recherche
 * Si $inc_limit est vraie, Ajout de "LIMIT" à la requête MySQL
 *
 * @param $str_rech
 * @param $tableau
 * @param $clefdefaut
 * @param $inc_limit
 * @param $val_limit
 *
 * @return string
 */
function modstr_triord($str_rech, $tableau, $clefdefaut, $inc_limit, $val_limit)
{
    $new_rech = $str_rech;

    if (trim($str_rech) <> "") {

        $pos = strripos($str_rech, 'ORDER BY');

        if ($pos === false)
            $a_changer = FALSE;
        else
            $a_changer = TRUE;

        if ($a_changer == TRUE) {
            $new_rech = substr($str_rech, 0, $pos);

            if (isset($_GET['tri'])) {
                if (@$_GET['tri'] <> "")
                    $new_rech .= " ORDER BY " . $tableau[$_GET['tri']];
                else
                    $new_rech .= " ORDER BY " . $clefdefaut;
            } else
                $new_rech .= " ORDER BY " . $clefdefaut;

            if (isset($_GET['ord'])) {
                if (@$_GET["ord"] == "DES")
                    $new_rech .= " DESC";
            }

            if ($inc_limit == TRUE) {
                $npage = 0;
                if (isset($_GET['page']))
                {
                    $npage = intval($_GET['page']);
                    if ($npage >= 1)
                        $npage = intval($_GET['page']) - 1;
                }
                $new_rech .= " LIMIT " . $npage . ", $val_limit"; // Ajout de LIMIT
            }
        }
    }

    return $new_rech;
}

/**
 * Retourne un tableau (2 dimensions) avec les opérandes d'entêtes sur listes
 *
 * @param $tab_prm
 * @param $tab_prm_rech
 *
 * @return array
 */
function give_tab_tri_col($tab_prm, $tab_prm_rech)
{
    $tab_tri = [];

    if (is_array($tab_prm)) {
        foreach ($tab_prm as $key => $value) {
            if ($tab_prm_rech[$key] == 1) {
                $tab_tmp = [
                    $key,
                    "null",
                    ''];
                $tab_tri[$value] = $tab_tmp;
            }
        }
    }

    return $tab_tri;
}

/**
 * Retourne une chaine représentant la recherche à effectuer sur un affichage de liste
 * sur la base des filtres complémentaires par colonne
 *
 * @param $rech
 *
 * @return string
 */
function cree_strrechlst_filtre_col($rech)
{
    global
    $_SESSION,
    $tab_comp_symbole;

    $rech_tmp = '';
    $nom_table = '';

    if (count($_SESSION["tri_col"]) > 0) {
        switch ($_SESSION["typ_filtres"]) {
            case "int" :
                $nom_table = TBINT;
                break;
            case "cli" :
                $nom_table = TBCLI;
                break;
            default :
                break;
        }

        if (trim($nom_table) <> "") {
            foreach ($_SESSION["tri_col"] as $key => $value) {
                $tab_tmp = $value;
                if ((trim($tab_tmp[2]) <> '') AND (trim($tab_tmp[1]) <> 'null')) {
                    $rech_tmp .= " " . $nom_table . "." . $key;
                    switch ($tab_comp_symbole[trim($tab_tmp[1])]) {
                        case ".." :
                            $rech_tmp .= " LIKE";
                            $rech_tmp .= " '%" . trim($tab_tmp[2]) . "%' AND ";
                            break;
                        case "##" :
                            $rech_tmp .= " NOT LIKE";
                            $rech_tmp .= " '%" . trim($tab_tmp[2]) . "%' AND ";
                            break;
                        case "A." :
                            $rech_tmp .= " LIKE";
                            $rech_tmp .= " '" . trim($tab_tmp[2]) . "%' AND ";
                            break;
                        case ".A" :
                            $rech_tmp .= " LIKE";
                            $rech_tmp .= " '%" . trim($tab_tmp[2]) . "' AND ";
                            break;
                        default :
                            $rech_tmp .= " " . $tab_comp_symbole[trim($tab_tmp[1])];
                            $rech_tmp .= " '" . trim($tab_tmp[2]) . "' AND ";
                            break;
                    }
                }
            }
        }
    }

    if (trim($rech_tmp) <> "") {
        // Suppression du dernier 'OR'
        $rech_tmp = substr($rech_tmp, 0, strlen($rech_tmp) - 4);
        if (!stristr($rech, "WHERE "))
            $rech_tmp = " WHERE (" . $rech_tmp . ")";
        else
            $rech_tmp = " AND (" . $rech_tmp . ")";
    }

    return $rech . $rech_tmp;
}
?> 
