<?php 
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.
*/
# ***************************************
# 
#			RSS Widget
# 
# ***************************************

$refreshDelay = 10;
$proxy = '';

require_once('../../../refreshfeed.inc.php');//check if feed has been refreshed lately
require_once('../includes/date.inc.php');


$pfid = (isset($_REQUEST['pfid']))         ?       $_REQUEST['pfid']       :       parser($variables,'pfid=');
$page = (isset($_REQUEST['page']))         ?       $_REQUEST['page']       :       0;
$tab = (isset($_REQUEST['tab']))           ?       $_REQUEST['tab']        :       $tab;
$mod_id = (isset($_REQUEST['mod_id']))     ?       $_REQUEST['mod_id']     :       $mod_id;
$nb_articles = (isset($_REQUEST['max']))   ?       $_REQUEST['max']        :       $nb_articles;
$act = (isset($_REQUEST['act']))           ?       $_REQUEST['act']        :       '';
$cpt = (isset($_REQUEST['cpt']))           ?       $_REQUEST['cpt']        :       5;

$oldcpt = $cpt;
if ($nb_articles>35)   $nb_articles = 35;
if ($act == 'prev')    $cpt = ($cpt - 5);
if ($act == 'more')    $cpt = ($cpt + 5);

$nb_display = ($cpt>$nb_articles)        ?       ($nb_articles-$oldcpt)      :        5;
$previousStr = ''; 
$nextStr = '';
$feeds = array();
$inc = 0;
$k = 0;
$limit = ($cpt > $nb_articles && $cpt > 5)      ?       ($cpt - $nb_articles) + 1         :         $nb_articles;

if ($cpt <= 35) {
    $DB->getResults($xmlfeeds_getItemsWithStatus,$DB->escape($_SESSION['user_id']),$DB->escape($pfid),$DB->escape($cpt-5),$DB->escape($limit));
    $nbRes = $DB->nbResults();
    if ($nbRes>0) {
        while (($row = $DB->fetch(0)) && $k<$nb_display) {
            $k++;
            $feeds[$inc]=array(
                "id"=>$row['id'],
                "title"=>$row['title'],
                "link"=>$row['link'],
                "date"=>$row['pubdate'],
                "status"=>$row['status']
            );
            $inc++;
        }
        $DB->freeResults();
    }

    foreach ($feeds AS $feed) {
        $convertedDate = '';
        if ($feed["date"] != '') {
            $convertedDate = rssToDate($feed["date"]);
        }
        echo '<div class="item">
                <a class="lien_'.($feed["status"]==1?'read':'unread').'" href="rss_reader.php?pfid='.$pfid
                                                                                             .'&id='.$feed["id"]
                                                                                            .'&tab='.$selectedTab
                                                                                           .'&pdif='.$pfid
                                                                                           .'&max='.$nb_articles
                                                                                           .'&cpt='.$oldcpt
                                                                                           .'&act='.$act.'#article "><span>'.$feed["title"].'</span></a>&nbsp;<span class="time">'.$convertedDate.'</span>
                <div class="options">
                </div>
             </div>';
    }
}

if (($cpt-5)>=5) {
	$previousStr = '<a href="display_widget.php?mod_id='.$mod_id
                                                .'&pfid='.$pfid
                                                .'&id='.$feeds[$inc-1]['id']
                                                .'&max='.$nb_articles
                                                .'&cpt='.$cpt
                                                .'&act=prev&tab='.$tab.'">'.lg("page_back").'</a>';
}
if ($nbRes > $nb_display && $cpt < $limit) {
	$nextStr = '<a href="display_widget.php?mod_id='.$mod_id
                                            .'&pfid='.$pfid
                                            .'&id='.$feeds[$inc-1]['id']
                                            .'&max='.$nb_articles
                                            .'&cpt='.$cpt
                                            .'&act=more&tab='.$tab.'">'.lg("page_next").'</a>';
}

echo '<center><div>'.$previousStr.' &nbsp;&nbsp; '.$nextStr.'</div></center>';  
?>