<?php

class searchAction extends baseAction {

    public function index() {
        $keywords = isset($_REQUEST['keywords']) && trim($_REQUEST['keywords']) ? trim($_REQUEST['keywords']) : $this->setting['default_kw'];
        $sortby = isset($_REQUEST['sortby']) && trim($_REQUEST['sortby']) ? trim($_REQUEST['sortby']) : 'likes';
        $items_mod = D('items');
        import("ORG.Util.Page");
        $sql_where = "title LIKE '%" . $keywords . "%'";

        switch ($sortby) {
            case 'likes' :
                $sql_order = "likes DESC";
                break;
            case 'time' :
                $sql_order = "add_time DESC";
                break;
            default :
                $sql_order = "add_time DESC";
                break;
        }

        $p = !empty($_GET['p']) ? intval($_GET['p']) : 1;
        $sp = !empty($_GET['sp']) ? intval($_GET['sp']) : 1;
        $sp > 5 && exit;

        //先计算大的分页
        $list_rows = 200;
        $s_list_rows = 40;
        $show_sp = 0;
        $count = $items_mod->where($sql_where)->count();
        $count > $s_list_rows && $show_sp = 1;
        $pager = new Page($count, $list_rows);
        $page = $pager->show_1();
        $first_row = $pager->firstRow + $s_list_rows * ($sp - 1);
        $items_list = $items_mod->relation('items_site')->where($sql_where)->limit($first_row . ',' . $s_list_rows)->order($sql_order)->select();
        $this->assign('page', $page);
        $this->assign('p', $p);
        $this->assign('show_sp', $show_sp);
        $this->assign('sp', $sp);
        $this->assign('keywords', $keywords);
        $this->assign('sortby', $sortby);
        $this->assign('items_list', $items_list);
        $this->assign('items_total', $count);

        $this->display();
    }

}