<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class FindAction extends MemberAction
{
    public function run()
    {
        $t = S::gp('t','G');
        $mfields = $this->db()->select(array('birth','city','book'))->from(Memberfields::table())->where('uid='.S::sqlEscape($this->uid()))->find(true);
        
        if (!empty($t) && in_array($t, array('age','book','city')))
        {
            $sql = 'SELECT m.uid,m.username,m.gender,m.oltime,mf.birth,mf.city,mf.book FROM '.Memberfields::table().' AS mf INNER JOIN '.Members::table().' AS m ON m.uid=mf.uid WHERE mf.issearch = 1 AND m.uid <>'.$this->uid();
            switch ($t)
            {
                case 'age':
                    $label = '同龄书友';
                    $condition = " AND birth LIKE '".date('Y', strtotime($mfields['birth']))."%'";
                    break;
                case 'book':
                    $label = '同爱好书友';
                    $condition = " AND book LIKE '%".$mfields['book']."%'";
                    break;
                case 'city':
                    $label = '同城书友';
                    $condition = " AND city LIKE '%".$mfields['city']."%'";
                    break;
            }
            $count = $this->db()->getValue('SELECT COUNT(*) FROM '.Memberfields::table().' WHERE issearch = 1 AND uid <> '.$this->uid().$condition);
            $pageObj = new iPage();
            $pageObj->setPageSize(10);
            $pageObj->setTotalRecord($count);
            
            $sql = $sql . $condition . S::sqlLimit($pageObj->getOffset(), $pageObj->getPageSize());
            $result = $this->db()->setQueryString($sql)->find();
            
            $this->getController()->render('find_result', array(
                    'label' => $label,
                    'result' => $result,
                    'pageObj' => $pageObj,
                )
            );
            exit(0);
        }
        
        $this->getController()->render('find', array(
        		'mfields' => $mfields,
            )
        );
    }
}