<?php
/**
 * @file
 * Affiche le flux rss badiste.
 *
 * @author Stéphane Levant <sun@tuxfamily.org>
 */

/**
 * Ajoutez ces deux lignes dans le "head" du code HTML :

   <link rel="stylesheet" type="text/css"
         href="http://badiste.fr/theme/defaut/rss.css" />

 *
 * Ensuite dans le code php, là où vous voulez le tableau badiste :

   @include_once('badiste-rss.php');
   badiste_affiche_tableau("http://badiste.fr/rss.php", true);

 * l'url peut être modifiée comme indiqué ici :
 * http://badiste.fr/rss/avance.html
 */

/**
 * Affiche le flux rss badiste.
 * 
 * @param $url l'url du flux badiste. ex: http://badiste.fr/rss.php?dep=33
 * @param $grand affiche un grand tableau si <i>true</i>, un petit sinon
 */
function badiste_affiche_tableau($url$grand false) {
    
$MOIS = array("janvier"   => "jan.",
                  
"février"   => "fév.",
                  
"mars"      => "mars",
                  
"avril"     => "avr.",
                  
"mai"       => "mai",
                  
"juin"      => "juin",
                  
"juillet"   => "juil.",
                  
"août"      => "août",
                  
"septembre" => "sept.",
                  
"octobre"   => "oct.",
                  
"novembre"  => "nov.",
                  
"décembre"  => "déc.");

    
$rss badiste_lit_rss($url,
                           array(
"title","link","description","pubDate"));
    if (
$grand) {
        echo 
'<div id="badisteban">'"\n";
    }
    echo 
'<div id="badiste">'"\n";
    if (
$grand) {
        echo 
'<table cellpadding="0" cellspacing="0">',
            
'<tr><th colspan="5">',"\n";
    }
    echo 
'<a id="badistetitle" href="http://badiste.fr">Badiste</a>'"\n";
    if (
$grand) {
        echo 
'</th></tr>'"\n";
    }
    
$fill 0;
    foreach(
$rss as $item) {
        
preg_match('/Le (\d+) (.+) \d\d\d\d .+club : (.+)[.]'
                   
' catégorie : (.*). tableaux : (.*)[.]'
                   
' date limite d\'inscription/',
                   
$item[2], $t);
        if (
$grand) {
            echo 
'<tr', ($fill ' class="badistetux"'''),
                
'><td align="right">'$t[1], ' '$MOIS[$t[2]], '</td>',
                
'<td align="left">'"\n";
            
$fill $fill;
        }
        if (
$grand) {
            
$url_title 'Voir ce tournoi';
        } else {
            
$url_title $t[1] . ' ' $MOIS[$t[2]]
                . 
", $t[3], $t[4], $t[5]";
        }
        echo 
'<a class="badistetournoi" title="'$url_title,
            
'" href="'$item[1].'">',
            
$item[0], '</a>'"\n";
        if (
$grand) {
            echo 
'</td>',
                
'<td align="center">'$t[3], '</td>',
                
'<td align="center">'$t[4], '</td>',
                
'<td align="center">'$t[5], '</td></tr>'"\n";
        }
    }
    if (
$grand) {
        echo 
'</table></div>'"\n";
    }
    echo 
'</div>'"\n";
}

/**
 * Lit et découpe un flux rss en item.
 * Tiré de http://ghostdogpr.developpez.com/articles/rss/
 * 
 * @param $fichier l'url du flux rss ou le fichier xml
 * @param $objets un tableau des nom des paramètres à récupérer
 * @return un tableau avec pour chaque item un tableau des paramètres
 */
function badiste_lit_rss($fichier,$objets) {
    if(
$chaine = @implode("",@file($fichier))) {
        
$tmp preg_split("/<\/?"."item".">/",$chaine);
        for(
$i=1;$i<sizeof($tmp)-1;$i+=2)
            foreach(
$objets as $objet) {
                
$tmp2 preg_split("/<\/?".$objet.">/",$tmp[$i]);
                
$resultat[$i-1][] = htmlspecialchars(@$tmp2[1], ENT_NOQUOTES, 'iso-8859-1');
            }
        return 
$resultat;
    }
}

?>