<?php
if (!defined("_ECRIRE_INC_VERSION")) return;

function calcule_repertoire_max() {
    $i=0;
    $j=0;
    $cheminCG_rel=_DIR_PLUGIN_COMPTEURGRAPHIQUE."img_pack/";
    while ($j==0) {
        $i++;
        if (file_exists($cheminCG_rel.$i.'/0.gif') || file_exists($cheminCG_rel.$i.'/0.png')) {}
        else {$j=1;}
    }
    $i--;
    return $i;        
}

function exec_compteur_graphique(){

    include_spip("inc/presentation");
    $CG_nom_table = "ext_compteurgraphique";

    if (isset($_POST['tous_habillage']) && is_numeric($_POST['tous_habillage']) && is_numeric($_POST['tous_chiffres'])) {
        $requete_nouveau_tous = "INSERT ".$CG_nom_table." VALUE (NULL,NULL,NULL,NULL,6,".$_POST['tous_chiffres'].",".$_POST['tous_habillage'].")";
        $resultat_nouveau_tous = spip_query($requete_nouveau_tous);
    }
    
    if (isset($_POST['changement_habillage_tous']) && is_numeric($_POST['changement_habillage_tous'])) {
        $requete_changement_habillage_tous = "UPDATE ".$CG_nom_table." SET habillage = ".$_POST['changement_habillage_tous']." WHERE statut = 6";
        $resultat_changement_habillage_tous = spip_query($requete_changement_habillage_tous);
    }
    
    if (isset($_POST['changement_chiffres_tous']) && is_numeric($_POST['changement_chiffres_tous'])) {
        $requete_changement_chiffres_tous = "UPDATE ".$CG_nom_table." SET longueur = ".$_POST['changement_chiffres_tous']." WHERE statut = 6";
        $resultat_changement_chiffres_tous = spip_query($requete_changement_chiffres_tous);
    }
    
    if (is_numeric($_POST['suppression_tous']) && ($_POST['suppression_tous']==1)) {
        $requete_suppr_tous = "DELETE FROM ".$CG_nom_table." WHERE statut = 6";
        $resultat_suppr_tous = spip_query($requete_suppr_tous);
    }


    $icone = "../"._DIR_PLUGIN_COMPTEURGRAPHIQUE."/img_pack/CompteurGraphique.gif";
    $cheminCG_rel=_DIR_PLUGIN_COMPTEURGRAPHIQUE."img_pack/";

	debut_page(_T('compteurgraphique:CG_nom'));
    gros_titre(_T('compteurgraphique:CG_nom'));
   
    debut_gauche();
    debut_boite_info();
    echo _T('compteurgraphique:CG_exec_info_page');
    fin_boite_info();

    if (!isset($compteur_block)) {$compteur_block=0;}
    $compteur_block++;

    debut_droite();
    debut_cadre_trait_couleur($icone, false, '', _T('compteurgraphique:CG_exec_titre'));
    include_spip('inc/CompteurGraphique_VerifBase');
    echo "<br />";
    
        debut_cadre_sous_rub('', false, '',_T('compteurgraphique:CG_exec_tous_articles'));
        $requete1="SELECT longueur,habillage FROM ".$CG_nom_table." WHERE statut = 6";
        $resultat1= spip_query($requete1);
        $resultat1_tableau = spip_fetch_array($resultat1);       
        $CG_tous_longueur = $resultat1_tableau['longueur'];
        $CG_tous_habillage = $resultat1_tableau['habillage'];
        if (isset($CG_tous_habillage)) {
            echo _T('compteurgraphique:CG_exec_modele_tous');
            if ($CG_tous_longueur==0) {echo _T('compteurgraphique:CG_gestion_chiffres_automatique');}
            else {
                echo $CG_tous_longueur." ";
                if ($CG_tous_longueur==1) {echo _T('compteurgraphique:CG_affiche_chiffre');}
                else {echo _T('compteurgraphique:CG_affiche_chiffres');}
                echo _T('compteurgraphique:CG_exec_modele_compteur');
            }
            echo _T('compteurgraphique:CG_habillage_choisi');
            for ($m=1;$m<=5;$m++){
                echo '<img src="'.$cheminCG_rel.$CG_tous_habillage.'/'.$m.'.gif">';
            }
            echo "</div><hr />";
            if (!isset($compteur_block)) {$compteur_block=0;}
            $compteur_block++;
            echo bouton_block_invisible($compteur_block);
            echo _T('compteurgraphique:CG_modif_habillage');
            echo debut_block_invisible($compteur_block);
            echo '<form method="POST" action="'.generer_url_ecrire("compteur_graphique").'">';
            $limiteCG = calcule_repertoire_max();
            $nbre_cellules=10;
            echo '<table border cellpadding="4" align="center">';
            for ($k=1;$k<=$limiteCG;$k++) {
                if (($k%$nbre_cellules)==1){echo '<tr>';}
                echo '<td align="center">';
                if (file_exists($cheminCG_rel.$k.'/8.gif')) {
                    echo '<div style="text-align:center;"><img src="'.$cheminCG_rel.$k.'/8.gif"></div><br /><input type="radio" name="changement_habillage_tous" value='.$k;
                    if ($k==1) {echo ' checked';}
                echo ' >';
                }
                echo '</td>';
                if (($k%$nbre_cellules)==0){echo '</tr>';}
            }
            if (($limiteCG%$nbre_cellules)!=0) {echo '</tr>';}
            echo '</table><br /><div style="text-align:center;"><input type="submit" value="'._T('compteurgraphique:CG_modif_habillage').'"></div></form>';
            echo fin_block();
            echo "<br />&nbsp;<br />";
            
            $compteur_block++;
            echo bouton_block_invisible($compteur_block);
            echo _T('compteurgraphique:CG_modif_nombre_chiffres');
            echo debut_block_invisible($compteur_block);?>            
            <div style="text-align:center;"><form method="POST" action="<?php echo generer_url_ecrire("compteur_graphique");?>">
            <select name="changement_chiffres_tous">
                <option value="0" selected><?php echo _T('compteurgraphique:CG_chiffre_auto');?></option>
                <?php for ($n=1;$n<=10;$n++) {
                    echo '<option value="'.$n.'">'.$n._T('compteurgraphique:CG_chiffre');
                    if ($n!=1){echo _T('compteurgraphique:CG_pluriel');}
                    echo '</option>';
                } ?>
            </select><br /><input type="submit" value="<?php echo _T('compteurgraphique:CG_modif'); ?>"></form></div> 
            <?php echo fin_block();
            echo "<br />";
            
            $compteur_block++;
            echo bouton_block_invisible($compteur_block);
            echo _T('compteurgraphique:CG_suppr_modele_compteur');
            echo debut_block_invisible($compteur_block);
            echo '<div style="text-align:center;"><form method="POST" action="'.generer_url_ecrire("compteur_graphique").'">';
            echo '<input type="hidden" name="suppression_tous" value="1"><input type="submit" value="'._T('compteurgraphique:CG_suppr').'"></form></div>';
            echo fin_block();
        }
        else {
            echo _T('compteurgraphique:CG_exec_aucun_modele_compteur');
            echo bouton_block_invisible($compteur_block);
            echo _T('compteurgraphique:CG_exec_creer_modele_compteur');
            echo debut_block_invisible($compteur_block);?>
            <form method="POST" action="<?php echo generer_url_ecrire("compteur_graphique");?>">
            <?php    $limiteCG = calcule_repertoire_max();
            $nbre_cellules=10;
            echo _T('compteurgraphique:CG_choix_habillage');
            echo '<table border cellpadding="4" align="center">';
            for ($k=1;$k<=$limiteCG;$k++) {
                if (($k%$nbre_cellules)==1){echo '<tr>';}
                echo '<td align="center">';
                if (file_exists($cheminCG_rel.$k.'/8.gif')) {
                    echo '<div style="text-align:center;"><img src="'.$cheminCG_rel.$k.'/8.gif"></div><br /><input type="radio" name="tous_habillage" value='.$k;
                    if ($k==1) {echo ' checked';}
                    echo ' >';
                }
                echo '</td>';
                if (($k%$nbre_cellules)==0){echo '</tr>';}
            }
            if (($limiteCG%$nbre_cellules)!=0) {echo '</tr>';}
            echo '</table><hr />&nbsp;<br />';
            echo _T('compteurgraphique:CG_choix_nombre_chiffres');?>
            <select name="tous_chiffres">
                <option value="0" selected><?php echo _T('compteurgraphique:CG_chiffre_auto');?></option>
                <?php for ($n=1;$n<=10;$n++) {
                    echo '<option value="'.$n.'">'.$n._T('compteurgraphique:CG_chiffre');
                    if ($n!=1){echo _T('compteurgraphique:CG_pluriel');}
                    echo '</option>';
                } ?>
            </select><br />&nbsp;<br /><input type="submit" value="<?php echo _T('compteurgraphique:CG_creer');?>"></div></form>
        
            <?php echo fin_block();
        }
        
        fin_cadre_sous_rub();
        echo "<br />";

        
        debut_cadre_enfonce('', false, '', _T('compteurgraphique:CG_exec_titre'));
        echo _T('compteurgraphique:CG_exec_a_venir');
        fin_cadre_enfonce();
        
    fin_cadre_trait_couleur();
    
    echo fin_gauche(), fin_page();
}
?>