<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class control extends wapbase {
    private $type = null; 
    private $keyword = null; 
    private $_tplname = NULL;
    private $fieldname = null; 

    private function _getSearchItems() {
        $this->type = XRequest::getArgs('type');
        $this->keyword = XRequest::getArgs('keyword');
        if (!in_array($this->type, array('article', 'product', 'photo', 'hr', 'download'))) {
            XHandle::halt("请选择要搜索的类型", "", 1);
        }
        if (empty($this->keyword)) {
            XHandle::halt("请输入要搜索的关键字", "", 1);
        }
        if ($this->type == 'product') {
            $this->fieldname = 'productname';
        }
        else {
            $this->fieldname = 'title';
        }
        $this->_tplname = $this->type.'_search';
    }

    
    public function control_run() {
        $this->_getSearchItems();
        $searchsql = "";
        $orderby = "";
        $searchsql .= " AND v.{$this->fieldname} LIKE '%{$this->keyword}%'";

        if ($this->type == 'article') {
            $pagesize = intval(parent::$cfg['wap_articlepagesize']);
        }
        elseif ($this->type == 'product') {
            $pagesize = intval(parent::$cfg['wap_productpagesize']);
        }
        elseif ($this->type == 'photo') {
            $pagesize = intval(parent::$cfg['wap_photopagesize']);
        }
        elseif ($this->type == 'hr') {
            $pagesize = intval(parent::$cfg['wap_hrpagesize']);
        }
        elseif ($this->type == 'download') {
            $pagesize = intval(parent::$cfg['wap_downpagesize']);
        }
        if ($pagesize <= 0) {
            $pagesize = 10;
        }
        $model = M($this->type, "im");
        list($total, $data) = $model->getList(
            array(
                'page'=>$this->page,
                'pagesize'=>$pagesize,
                'searchsql'=>$searchsql,
                'orderby'=>$orderby,
            )
        );
        unset($model);
        
        $url = $this->wapfile."?c=search&type=".$this->type."&keyword=".urlencode($this->keyword);
        $showpage = XPage::wapindex($total, $pagesize, $this->page, $url, 10);
        
        $this->getMeta('ch_'.$this->type.'_search');
        $page_title = str_ireplace('{page}', $this->page, $this->metawrap['title']);
        $page_description = str_ireplace('{page}', $this->page, $this->metawrap['description']);
        $page_keyword = str_ireplace('{page}', $this->page, $this->metawrap['keyword']);
        $prepage = $nextpage = $pagecount = 0;
        if (!empty($data)) {
            $pagecount = ceil($total/$this->pagesize);
            if ($pagecount > $this->page) {
                $nextpage = $this->page+1;
            }
            if ($this->page > 1) {
                $prepage = ($this->page-1);
            }
        }
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$nextpage,
            'prepage'=>$prepage,
            'pagecount'=>$pagecount,
            'pagesize'=>$pagesize,
            'total'=>$total,
            'showpage'=>$showpage,
            'search'=>$data,
            'page_title'=>$page_title,
            'page_keyword'=>$page_keyword,
            'page_description'=>$page_description,
            'type'=>$this->type,
            'keyword'=>$this->keyword,
        );
        $this->view($var_array, $this->_tplname);
    }
}
?>
