<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class SearchAction extends SoftAction
{
    public function run()
    {
        $key = S::gp('key', 'G');
        $class = S::gp('class', 'G', 2);
        $class = !in_array($class, array(1, 2)) ? 1 : $class;
        $field = (1 == $class) ? 'name' : 'info';

        if (empty($key))
        {
            throw new iSystemException('请输入关键字');
        }
        $key = preg_replace('/(select|union|%|\*)/i', '', $key);

        $count = $this->db()->getValue('SELECT COUNT(*) FROM '.SoftContents::table().' WHERE '.$field.' LIKE "%'.$key.'%"');
        $pageObj = new iPage();
        $pageObj->setPageSize(10);
        $pageObj->setTotalRecord($count);

        $sql = 'SELECT id,cid,name,date FROM '.SoftContents::table().' WHERE '.$field.' LIKE "%'.$key.'%" ORDER BY id DESC '.S::sqlLimit($pageObj->getOffset(), $pageObj->getPageSize());
        $softs = $this->db()->setQueryString($sql)->find();
        $floor = $pageObj->getFloor();

        $this->getController()->render('search', array(
                'softs' => $softs,
                'pageObj' => $pageObj,
                'floor' => $floor,
                'key' => $key,
            )
        );
    }
}