<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class Polls extends Model
{
	public static function single($className = __CLASS__)
	{
		if (is_null(self::$_self[$className]))
		{
			self::$_self[$className] = new $className;
		}
		
		return self::$_self[$className];
	}
	
    public static function table()
    {
        return 'ig_polls';
    }
    
    public function create($tid, $data)
    {
        $this->Db()->insert(self::table())->value(array('tid'=>$tid,'options'=>$data['options'],'multiple'=>$data['multiple'],'days'=>$data['days'],'max'=>$data['max'],'min'=>$data['min']))->q();
    }
    
    public function getOne($tid, $uid)
    {
        $polls = $this->db()->setQueryString('SELECT p.*,v.tid AS havevote FROM '.self::table().' AS p LEFT JOIN '.Voter::table().' v ON p.tid=v.tid AND v.uid= '.$uid.' WHERE p.tid= '.$tid.' GROUP BY p.tid')->find(true);
        if (empty($polls))
        {
            throw new PollsNotExistsException('不存在的投票');
        }
        return $polls;
    }

    public function createForm()
    {
        
    }
}

class PollsNotExistsException extends Exception {}