<?php
 function strExists($haystack, $needle){
    return !(strpos($haystack, $needle) === FALSE);
}
class IndexAction extends BaseAction{
    private $tpl;
    private $info;
    private $wecha_id;
    private $copyright;
    public $company;
    public $token;
    public $weixinUser;
    public $homeInfo;
    public function _initialize(){
        parent :: _initialize();
        $agent = $_SERVER['HTTP_USER_AGENT'];
        if(!strpos($agent, "icroMessenger") && !isset($_GET['show'])){
        }
        $this -> token = $this -> _get('token', 'trim');
        $where['token'] = $this -> token;
        $tpl = D('Wxuser') -> where($where) -> find();
        $this -> weixinUser = $tpl;
        if (isset($_GET['wecha_id']) && $_GET['wecha_id']){
            $_SESSION['wecha_id'] = $_GET['wecha_id'];
            $this -> wecha_id = $this -> _get('wecha_id');
            $this -> assign('wecha_id', $this -> wecha_id);
        }
        if (isset($_SESSION['wecha_id'])){
            $this -> wecha_id = $_SESSION['wecha_id'];
        }
        $info = M('Classify') -> where(array('token' => $this -> _get('token'), 'status' => 1, 'fid' => 0)) -> order('sorts desc') -> select();
        $info = $this -> convertLinks($info);
        $info2 = M('Classify') -> where(array('token' => $this -> _get('token'), 'status' => 1, 'fid' => array('gt', 0))) -> order('sorts desc') -> select();
        $info2 = $this -> convertLinks($info2);
        $gid = D('Users') -> field('gid') -> find($tpl['uid']);
        $this -> userGroup = M('User_group') -> where(array('id' => $gid['gid'])) -> find();
        $this -> copyright = $this -> userGroup['iscopyright'];
        $this -> info = $info;
        $this -> info2 = $info2;
        $tpl['color_id'] = intval($tpl['color_id']);
        $this -> tpl = $tpl;
        $company_db = M('company');
        $this -> company = $company_db -> where(array('token' => $this -> token, 'isbranch' => 0)) -> find();
        $this -> assign('company', $this -> company);
        $homeInfo = M('home') -> where(array('token' => $this -> token)) -> find();
        $catemenu_db = M('catemenu');
        $catemenu = $catemenu_db -> where(array('token' => $this -> token, 'status' => 1)) -> order('orderss desc') -> select();
        $menures = array();
        if ($catemenu){
            $res = array();
            $rescount = 0;
            foreach ($catemenu as $val){
                $val['url'] = $this -> getLink($val['url']);
                $res[$val['id']] = $val;
                if ($val['fid'] == 0){
                    $val['vo'] = array();
                    $menures[$val['id']] = $val;
                    $menures[$val['id']]['k'] = $rescount;
                    $rescount++;
                }
            }
            foreach ($catemenu as $val){
                $val['url'] = $this -> getLink($val['url']);
                if ($val['fid'] > 0){
                    array_push($menures[$val['fid']]['vo'], $val);
                }
            }
        }
        $catemenu = $menures;
        $this -> assign('catemenu', $catemenu);
        $styleset_db = M('home');
        $radiogroup = $homeInfo['radiogroup'];
        if($radiogroup == false){
            $radiogroup = 0;
        }
        $cateMenuFileName = 'tpl/Wap/default/Index_menuStyle' . $radiogroup . '.html';
        $this -> assign('cateMenuFileName', $cateMenuFileName);
        $this -> assign('radiogroup', $radiogroup);
        $gid = D('Users') -> field('gid') -> find($tpl['uid']);
        $this -> userGroup = M('User_group') -> where(array('id' => $gid['gid'])) -> find();
        $this -> copyright = $this -> userGroup['iscopyright'];
        $this -> info = $info;
        $tpl['color_id'] = intval($tpl['color_id']);
        $this -> tpl = $tpl;
        $company_db = M('company');
        $this -> company = $company_db -> where(array('token' => $this -> token, 'isbranch' => 0)) -> find();
        $this -> assign('company', $this -> company);
        $homeInfo = M('home') -> where(array('token' => $this -> token)) -> find();
        $this -> homeInfo = $homeInfo;
        $this -> assign('iscopyright', $this -> copyright);
        $this -> assign('siteCopyright', C('copyright'));
        $this -> assign('homeInfo', $homeInfo);
        $this -> assign('token', $this -> token);
        $this -> assign('copyright', $this -> copyright);
        $plugMenus = $this -> _getPlugMenu();
        $this -> assign('plugmenus', $plugMenus);
        $this -> assign('showPlugMenu', count($plugMenus));
    }
    public function classify(){
        $this -> assign('info', $this -> info);
        $this -> display($this -> tpl['tpltypename']);
    }
    public function index(){
        if ($this -> homeInfo['advancetpl']){
            echo '<script>window.location.href="/cms/index.php?token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '";</script>';
            exit();
        }
        $gwmac = $_GET['gwmac'];
        $mac = $_GET['mac'];
        if($gwmac && $mac){
            session('mac', $mac);
            session('gwaddr', $_GET['gwaddr']);
            session('token', $this -> token);
            $apModel = M ('ApNodes');
            $nodewhere ['mac'] = $gwmac;
            $node = $apModel -> where ($nodewhere) -> field ('uid,id') -> find ();
            $user_info ['apnode_id'] = $node ['id'];
            $user_info ['uid'] = $node ['uid'];
            session('apnode_id', $node ['id']);
            $user_info['token'] = $this -> token;
            $user_info['mac'] = $mac;
            $user_info['ip'] = $_GET['ip'];
            $apUserModel = M ('ApUsers');
            $userinfo = $apUserModel -> where("mac='$mac' and uid=" . $node ['uid']) -> find();
            if(!$userinfo){
                $apUserModel -> where ($userwhere) -> add ($user_info);
            }
        }
        $where['token'] = $this -> _get('token');
        $allflash = M('Flash') -> where($where) -> select();
        $allflash = $this -> convertLinks($allflash);
        $flash = array();
        $flashbg = array();
        foreach ($allflash as $af){
            if ($af['tip'] == 1){
                array_push($flash, $af);
            }elseif ($af['tip'] == 2){
                array_push($flashbg, $af);
            }
        }
        $this -> assign('flashbg', $flashbg);
        if(!$flashbg && $this -> homeInfo['homeurl']){
            $flash_db = M('Flash');
            $arr = array();
            $arr['token'] = $this -> token;
            $arr['img'] = $this -> homeInfo['homeurl'];
            $arr['url'] = '';
            $arr['info'] = '';
            $arr['tip'] = 2;
            if ($arr['img']){
                $flash_db -> add($arr);
            }
        }
        $count = count($flash);
        $this -> assign('flash', $flash);
        $this -> assign('info', $this -> info);
        $this -> assign('info2', $this -> info2);
        $this -> assign('num', $count);
        $this -> assign('flashbgcount', count($flashbg));
        $this -> assign('info', $this -> info);
        $this -> assign('tpl', $this -> tpl);
        $this -> display($this -> tpl['tpltypename']);
    }
    public function lists(){
        $where['token'] = $this -> _get('token', 'trim');
        $db = D('Img');
        if($_GET['p'] == false){
            $page = 1;
        }else{
            $page = $_GET['p'];
        }
        $where['classid'] = $this -> _get('classid', 'intval');
        $count = $db -> where($where) -> count();
        $pageSize = 8;
        $pagecount = ceil($count / $pageSize);
        if($page > $count){
            $page = $pagecount;
        }
        if($page >= 1){
            $p = ($page-1) * $pageSize;
        }
        if($p == false){
            $p = 0;
        }
        $res = $db -> where($where) -> order('createtime DESC') -> limit("{$p}," . $pageSize) -> select();
        $res = $this -> convertLinks($res);
        $this -> assign('page', $pagecount);
        $this -> assign('p', $page);
        $this -> assign('info', $this -> info);
        $this -> assign('tpl', $this -> tpl);
        $this -> assign('res', $res);
        $this -> assign('copyright', $this -> copyright);
        if ($count == 1){
            $this -> content($res[0]['id']);
            exit();
        }
        $this -> display($this -> tpl['tpllistname']);
    }
    public function content($contentid = 0){
        $db = M('Img');
        $where['token'] = $this -> _get('token', 'trim');
        if (!$contentid){
            $contentid = intval($_GET['id']);
        }
        $where['id'] = array('neq', $contentid);
        $lists = $db -> where($where) -> limit(5) -> order('uptatetime') -> select();
        $where['id'] = $contentid;
        $res = $db -> where($where) -> find();
        $this -> assign('info', $this -> info);
        $this -> assign('lists', $lists);
        $this -> assign('res', $res);
        $this -> assign('tpl', $this -> tpl);
        $this -> assign('copyright', $this -> copyright);
        $this -> display($this -> tpl['tplcontentname']);
    }
    public function flash(){
        $where['token'] = $this -> _get('token', 'trim');
        $flash = M('Flash') -> where($where) -> select();
        $count = count($flash);
        $this -> assign('flash', $flash);
        $this -> assign('info', $this -> info);
        $this -> assign('num', $count);
        $this -> display('ty_index');
    }
    public function getLink($url){
        $url = $url?$url:'javascript:void(0)';
        $urlArr = explode(' ', $url);
        $urlInfoCount = count($urlArr);
        if ($urlInfoCount > 1){
            $itemid = intval($urlArr[1]);
        }
        if (strExists($url, '刮刮卡')){
            $link = '/index.php?g=Wap&m=Guajiang&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link .= '&id=' . $itemid;
            }
        }elseif (strExists($url, '大转盘')){
            $link = '/index.php?g=Wap&m=Lottery&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link .= '&id=' . $itemid;
            }
        }elseif (strExists($url, '优惠券')){
            $link = '/index.php?g=Wap&m=Coupon&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link .= '&id=' . $itemid;
            }
        }elseif (strExists($url, '刮刮卡')){
            $link = '/index.php?g=Wap&m=Guajiang&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link .= '&id=' . $itemid;
            }
        }elseif (strExists($url, '商家订单')){
            if ($itemid){
                $link = $link = '/index.php?g=Wap&m=Host&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&hid=' . $itemid;
            }else{
                $link = '/index.php?g=Wap&m=Host&a=Detail&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            }
        }elseif (strExists($url, '万能表单')){
            if ($itemid){
                $link = $link = '/index.php?g=Wap&m=Selfform&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&id=' . $itemid;
            }
        }elseif (strExists($url, '相册')){
            $link = '/index.php?g=Wap&m=Photo&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link = '/index.php?g=Wap&m=Photo&a=plist&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&id=' . $itemid;
            }
        }elseif (strExists($url, '全景')){
            $link = '/index.php?g=Wap&m=Panorama&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link = '/index.php?g=Wap&m=Panorama&a=item&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&id=' . $itemid;
            }
        }elseif (strExists($url, '会员卡')){
            $link = '/index.php?g=Wap&m=Card&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
        }elseif (strExists($url, '商城')){
            $link = '/index.php?g=Wap&m=Product&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
        }elseif (strExists($url, '订餐')){
            $link = '/index.php?g=Wap&m=Product&a=dining&dining=1&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
        }elseif (strExists($url, '团购')){
            $link = '/index.php?g=Wap&m=Groupon&a=grouponIndex&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
        }elseif (strExists($url, '首页')){
            $link = '/index.php?g=Wap&m=Index&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
        }elseif (strExists($url, '网站分类')){
            $link = '/index.php?g=Wap&m=Index&a=lists&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link = '/index.php?g=Wap&m=Index&a=lists&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&classid=' . $itemid;
            }
        }elseif (strExists($url, '图文回复')){
            if ($itemid){
                $link = '/index.php?g=Wap&m=Index&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&id=' . $itemid;
            }
        }elseif (strExists($url, 'LBS信息')){
            $link = '/index.php?g=Wap&m=Company&a=map&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
            if ($itemid){
                $link = '/index.php?g=Wap&m=Company&a=map&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&companyid=' . $itemid;
            }
        }elseif (strExists($url, 'DIY宣传页')){
            $link = '/index.php/show/' . $this -> token;
        }elseif (strExists($url, '婚庆喜帖')){
            if ($itemid){
                $link = '/index.php?g=Wap&m=Wedding&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&id=' . $itemid;
            }
        }elseif (strExists($url, '投票')){
            if ($itemid){
                $link = '/index.php?g=Wap&m=Vote&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id . '&id=' . $itemid;
            }
        }else{
            $link = str_replace(array('{wechat_id}', '{siteUrl}', '&amp;'), array($this -> wecha_id, C('site_url'), '&'), $url);
            if (!!(strpos($url, 'tel') === false) && $url != 'javascript:void(0)' && !strpos($url, 'wecha_id=')){
                if (strpos($url, '?')){
                    $link = $link . '&wecha_id=' . $this -> wecha_id;
                }else{
                    $link = $link . '?wecha_id=' . $this -> wecha_id;
                }
            }
        }
        return $link;
    }
    public function convertLinks($arr){
        $i = 0;
        foreach ($arr as $a){
            if ($a['url']){
                $arr[$i]['url'] = $this -> getLink($a['url']);
            }
            $i++;
        }
        return $arr;
    }
    public function _getPlugMenu(){
        $company_db = M('company');
        $this -> company = $company_db -> where(array('token' => $this -> token, 'isbranch' => 0)) -> find();
        $plugmenu_db = M('site_plugmenu');
        $plugmenus = $plugmenu_db -> where(array('token' => $this -> token, 'display' => 1)) -> order('taxis ASC') -> limit('0,4') -> select();
        if ($plugmenus){
            $i = 0;
            foreach ($plugmenus as $pm){
                switch ($pm['name']){
                case 'tel': if (!$pm['url']){
                        $pm['url'] = 'tel:/' . $this -> company['tel'];
                    }else{
                        $pm['url'] = 'tel:/' . $pm['url'];
                    }
                    break;
                case 'memberinfo': if (!$pm['url']){
                        $pm['url'] = '/index.php?g=Wap&m=Userinfo&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
                    }
                    break;
                case 'nav': if (!$pm['url']){
                        $pm['url'] = '/index.php?g=Wap&m=Company&a=map&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
                    }
                    break;
                case 'message': break;
                case 'share': break;
                case 'home': if (!$pm['url']){
                        $pm['url'] = '/index.php?g=Wap&m=Index&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
                    }
                    break;
                case 'album': if (!$pm['url']){
                        $pm['url'] = '/index.php?g=Wap&m=Photo&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
                    }
                    break;
                case 'email': $pm['url'] = 'mailto:' . $pm['url'];
                    break;
                case 'shopping': if (!$pm['url']){
                        $pm['url'] = '/index.php?g=Wap&m=Product&a=index&token=' . $this -> token . '&wecha_id=' . $this -> wecha_id;
                    }
                    break;
                case 'membercard': $card = M('member_card_create') -> where(array('token' => $this -> token, 'wecha_id' => $this -> wecha_id)) -> find();
                    if (!$pm['url']){
                        if($card == false){
                            $pm['url'] = rtrim(C('site_url'), '/') . U('Wap/Card/index', array('token' => $this -> token, 'wecha_id' => $this -> wecha_id));
                        }else{
                            $pm['url'] = rtrim(C('site_url'), '/') . U('Wap/Card/index', array('token' => $this -> token, 'wecha_id' => $this -> wecha_id));
                        }
                    }
                    break;
                case 'activity': $pm['url'] = $this -> getLink($pm['url']);
                    break;
                case 'weibo': break;
                case 'tencentweibo': break;
                case 'qqzone': break;
                case 'wechat': $pm['url'] = 'weixin://addfriend/' . $this -> weixinUser['wxid'];
                    break;
                case 'music': break;
                case 'video': break;
                case 'recommend': $pm['url'] = $this -> getLink($pm['url']);
                    break;
                case 'other': $pm['url'] = $this -> getLink($pm['url']);
                    break;
                }
                $plugmenus[$i] = $pm;
                $i++;
            }
        }else{
            $plugmenus = array();
        }
        return $plugmenus;
    }
}
?>