<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
define('IN_IORANGE', true);
include('include/init.php');

$type = S::gp('type','G');
empty($type) && $type = 'all';
$uid = S::gp('uid', 'G', 2);

$iFeed = iFeed::single();
$condition = $merge = array();
$navArray = array('all' => '所有', 'book'=>'小说','forum'=>'书会','bbs'=>'社区','member'=>'书友');

if ('all' != $type && key_exists($type, $navArray))
{
    $condition[] = array('type', $type);
}


if ($uid)
{
    $merge = array('uid'=>$uid);
    $sql = 'SELECT fuid FROM '.Friends::table().' WHERE uid='.S::sqlEscape($uid);
    $result = $db->setQueryString($sql)->find();
    if (!empty($result))
    {
        $fuids = array();
        foreach ($result as $item)
        {
            $fuids[] = (int)$item['fuid'];
        }
        $condition[] = array('uid', $fuids, 'in');
    }
}

if (getrobot() && $_GET['page'] > 10)
{
    $_GET['page'] = 1;
}
$count = $iFeed->condition($condition)->count();
$pageObj = new iPage();
$pageObj->setPageSize(20);
$pageObj->setTotalRecord($count);

$result = $iFeed->findAll(array('feed'), array('createdate'=>-1), $pageObj->getOffset(), $pageObj->getPageSize());
$feeds = array();

if (!empty($result))
{
    foreach ($result as $row)
    {
        $feeds[] = $iFeed->analyzeFeed($row['feed']);
    }
}

$array = array();
foreach ($navArray as $key => $nav)
{
    if ($key == $type)
    {
        $array[] = $nav;
    }
    else
    {
        $array[] = ilink($nav, url('feed',array_merge(array('type'=>$key), $merge)));
    }
}
$navString = implode('.', $array);

include_once tpl('feed');