<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class IndexAction extends MemberAction
{
    public function run()
    {
        $fTable = Friends::table();
        $mTable = Members::table();

        $type = $_GET['t'] ? trim(S::gp('t', 'G')) : '';
        
        $field = 'm.username AS fusername,f.fuid,m.lastactivity';
        $sql = "SELECT {{field}} 
                FROM {$fTable} AS f 
                INNER JOIN {$mTable} AS m ON m.uid = f.fuid
                WHERE f.uid = {$this->uid()}";
        //echo $sql;die;
        switch($type)
        {
            case 'on':
                $sql .= " AND s.lastolupdate IS NOT NULL";
                break;
            case 'off':
                $sql .= " AND s.lastolupdate IS NULL";
                break;
            default:
                $type = 'all';
        }
        $count = $this->db()->getValue(preg_replace('/{{field}}/s', 'COUNT(*)', $sql));
        $pageObj = new iPage();
        $pageObj->setPageSize(10);
        $pageObj->setTotalRecord($count);
        
        $sql = preg_replace('/{{field}}/i', $field, $sql) . ' LIMIT '.$pageObj->getOffset().','.$pageObj->getPageSize();
        $friends = $this->db()->setQueryString($sql)->find();
        
        $tags = array('all'=>'所有');
        foreach ($tags as $key => $tag)
        {
            if ($key == $type)
            {
                $nav[] = $tag;
            }
            else
            {
                $nav[] = ilink($tag, url('system',array('s'=>'member','c'=>'friend','t'=>$key)));
            }
        }
        $navString = implode('.',$nav);
        
        $this->getController()->render('index',array(
                'type' => $type,
                'pageObj' => $pageObj,
        		'friends' => $friends,
                'navString' => $navString,
            )
        );
    }
}