<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class TypeAction extends BcartAction
{
    public function run()
    {
        $sortArray = array(
            1 => '最新',
            2 => '收藏',
            3 => '评分',
            4 => '评论',
        );
        $mapArray = array(
            1 => 'id',
            2 => 'favorite',
            3 => 'score',
            4 => 'comment',
        );
        $tid = S::gp('tid', 'G', 2);
        $pageSize = 10;

        $sort = isset($_GET['sort']) && key_exists($_GET['sort'], $sortArray) ? S::gp('sort', 'G', 2) : 1;
        $orderBy = $mapArray[$sort];
        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Bcarts::table().' WHERE tid='.S::sqlEscape($tid).' AND display = 1 AND isdelete = 0');
        $pageObj = new iPage();
        $pageObj->setPageSize($pageSize);
        $pageObj->setTotalRecord($count);

        $sql = 'SELECT * FROM '.Bcarts::table().' WHERE tid='.S::sqlEscape($tid).' AND display = 1 AND isdelete = 0 ORDER BY '.$orderBy.' DESC '.S::sqlLimit($pageObj->getOffset(), $pageObj->getPageSize());
        $bcarts = $this->db()->setQueryString($sql)->find();
        
        $floor = $pageObj->getFloor();

        $navArray = array();
        foreach ($sortArray as $k => $v)
        {
            if ($k == $sort)
            {
                $navArray[] = $v;
            }
            else
            {
                $navArray[] = ilink($v, url('system', array('s'=>'bcart','a'=>'type','tid'=>$tid,'sort'=>$k)));
            }
        }
        $navString = implode('.', $navArray);

        $this->getController()->render('type', array(
                'bcarts' => $bcarts,
                'pageObj' => $pageObj,
                'floor' => $floor,
                'navString' => $navString,
                'tid' => $tid,
            )
        );
    }
}
