<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class FindAction extends MemberAction
{
    public function run()
    {
        $key = S::gp('key', 'G');
        $key = preg_replace('/(select|union|%|\*)/i', '', $key);
        if (empty($key))
        {
            throw new iSystemException('请输入搜索关键字');
        }
        if (2 > mb_strlen($key, 'utf-8'))
        {
            throw new iSystemException('关键字最少要2个');
        }

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

        $sql = 'SELECT uid,username,lastactivity FROM '.Members::table().$condition.' ORDER BY uid DESC '.S::sqlLimit($pageObj->getOffset(),$pageObj->getPageSize());
        $members = $this->db()->setQueryString($sql)->find();

        $this->getController()->render('find', array(
                'members' => $members,
                'pageObj' => $pageObj,
                'key' => $key,
            )
        );
    }
}