<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class PropertyHandsel extends Model
{
    public static function single($className = __CLASS__)
	{
		return parent::single($className);
	}

	public static function table()
	{
		return 'ig_property_handsel';
	}

    public function create($data)
    {
        $table = $this->escape(self::table());
        $sql = "INSERT INTO $table SET ";
        $data['createdate'] = time();
        $data['status'] = 0;

        foreach ($data as $key => $value)
		{
			$insert[] = self::getEscapeString($key, $value);
		}
		$sql .= implode(',', $insert);
		$this->_db->query($sql);

		return $this->_db->getInsertId();
    }

    public function getOne($id)
    {
        return $this->_db->select()->from(self::table())->where('id='.$id)->find(true);
    }

    public function update($id, $data)
    {
        $table = self::escape(self::table());
        $data['updatedate'] = time();
		$sql = "UPDATE $table SET ";
        foreach ($data as $key => $value)
		{
			$update[] = self::getEscapeString($key, $value);
		}
		$sql .= implode(',', $update) . " WHERE `id` = $id";
		$this->_db->query($sql);
    }

    public function handselLink($content)
    {
        if (preg_match('/PropertyHandsel/is', $content))
        {
            $content = preg_replace('/\[PropertyHandsel=(\d+)\]接受\[\/PropertyHandsel\]/i', ilink('接受', url('system',array('s'=>'member','a'=>'handsel','do'=>'accept','id'=>'$1'))), $content);
            $content = preg_replace('/\[PropertyHandsel=(\d+)\]拒绝\[\/PropertyHandsel\]/i', ilink('拒绝', url('system',array('s'=>'member','a'=>'handsel','do'=>'denial','id'=>'$1'))), $content);
        }

        return $content;
    }
}