<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class IndexAction extends MemberAction
{
    public function run()
    {
        $uid = isset($_GET['uid']) ? S::gp('uid','G',2) : $this->uid();
        $isSelf = $uid == $this->uid() ? true : false;
        $member = $this->getMember($uid);
        $tid = S::gp('tid','G',2);
        
        $sqlAdd = '';
        $tid && $sqlAdd = ' AND tid='.S::sqlEscape($tid);
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Diary::table().' WHERE uid='.S::sqlEscape($uid).$sqlAdd);
        $pageObj = new iPage();
        $pageObj->setPageSize(10);
        $pageObj->setTotalRecord($count);
        
        $sql = 'SELECT d.*,dt.name FROM '.Diary::table().' AS d LEFT JOIN '.DiaryType::table().' AS dt ON dt.id = d.tid WHERE d.uid='.S::sqlEscape($uid).$sqlAdd.' ORDER BY istoped DESC,createdate DESC '.S::sqlLimit($pageObj->getOffset(),$pageObj->getPageSize());
        $diaries = $this->db()->setQueryString($sql)->find();
        
        $typeList = $this->db()->select()->from(DiaryType::table())->where('uid='.S::sqlEscape($uid))->find();

        $this->getController()->render('index',array(
                'uid' => $uid,
                'member' => $member,
                'isSelf' => $isSelf,
                'diaries' => $diaries,
                'pageObj' => $pageObj,
                'typeList' => $typeList,
            )
        );
    }
}