Coupons.php 3.59 KB
<?php
use Action\WebAction;
use WebPlugin\Helpers;
use Home\UserModel;
use Home\CouponsModel;
use WebPlugin\Paging;

class CouponsController extends WebAction
{

    public function indexAction()
    {
        //判断是否登录
        $uid = $this->auditJumpLogin();

        //头部导航
        $channel = Helpers::getChannelNameByCookie();
        $this->setWebNavHeader($channel);
        //面包屑-左侧导航
        $path = UserModel::getCenterCrumb('我的优惠券');
        $leftNav = UserModel::getCenterLeftNav('我的优惠券', $uid);

        //分页
        $page = $this->get('page', 1);
        $limit = 10;
        $type = $this->get('type', CouponsModel::UNUSED);

        //优惠券信息
        $coupons = CouponsModel::getCouponsList($uid, $type, $page, $limit);

        $data = array (
            'path' => $path,
            'homeNav' => $leftNav,
            'userThumb' => UserModel::getUserHeadImg($uid),
        );

        $total = isset($coupons['pager']['total']) ? $coupons['pager']['total'] : 0;
        $pageTotal = isset($coupons['pager']['pageTotal']) ? $coupons['pager']['pageTotal'] : 0;
        $page = isset($coupons['pager']['page']) ? $coupons['pager']['page'] : 0;
        unset($coupons['pager']);

        $paging = new Paging('yoho');
        $paging->setTotal($total)->setSize($limit)->setQuery(array('type' => $type, 'page' => $page));

        $data['pager'] = array(
            'hasCheckAll' => false,
            'count' => $total,
            'curPage' => $page,
            'totalPages' => $pageTotal,
            'pagerHtml' => $paging->view(false)
        );

        if ($type == CouponsModel::UNUSED) {
            if (count($coupons) == 0) {
                $data['unUseCoupons'] = array('empty' => '您没有优惠券');
            } else {
                $data['unUseCoupons'] = $coupons;
            }
            $data['unUse'] = true;
        } else if ($type == CouponsModel::USED) {
            if (count($coupons) == 0) {
                $data['usedCoupons'] = array('empty' => '您没有优惠券');
            } else {
                $data['usedCoupons'] = $coupons;
            }
            $data['used'] = true;
        } else if ($type == CouponsModel::INVALID) {
            if (count($coupons) == 0) {
                $data['noValidCoupons'] = array('empty' => '您没有优惠券');
            } else {
                $data['noValidCoupons'] = $coupons;
            }
            $data['noValid'] = true;
        }

        $unusedUrl = Helpers::url('/home/coupons', array('type' => CouponsModel::UNUSED));
        $usedUrl = Helpers::url('/home/coupons', array('type' => CouponsModel::USED));
        $overTimeUrl = Helpers::url('/home/coupons', array('type' => CouponsModel::INVALID));

        $data['tabs'] = array(
            array(
                'active' => $type == CouponsModel::UNUSED ? true : false,
                'url' => $unusedUrl,
                'name' => '未使用优惠券'
            ),
            array(
                'active' => $type == CouponsModel::USED ? true : false,
                'url' => $usedUrl,
                'name' => '已使用优惠券'
            ),
            array(
                'active' => $type == CouponsModel::INVALID ? true : false,
                'url' => $overTimeUrl,
                'name' => '已失效优惠券'
            )
        );

        $this->_view->display('coupons', array(
            'meCoupons' => $data,
            'hasCoupons' => count($coupons) == 0 ? false : true,
        ));

    }

}