<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class Dynamic extends Model
{
    public static $_list = array(
        1 => '看[DYNAMIC]书会有书评',
        2 => '猛![DYNAMIC]书会有精评',
        3 => '强烈推荐[DYNAMIC]书会',
        4 => '勇将加盟[DYNAMIC]书会',
        5 => '',
        6 => '',
    );

    public static $forumDynamicKey = 'forumDynamic';
    
    public static $propWaDynamicKey = 'propsWaDynamic';

	public static function single($className = __CLASS__)
	{
		return parent::single($className);
	}

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

	public function createWaDynamic($message)
	{
	    $key = md5(self::$propWaDynamicKey);
	    $this->Cache()->set($key, $message, 300);
	}

	public function getWaDynamic()
	{
	    $string = $this->Cache()->get(md5(self::$propWaDynamicKey));
	    if (!preg_match('/{{(\d+)}}/i', $string, $matches) || empty($matches[1]))
	    {
	        return false;
	    }
	    $uid = $matches[1];
	    $userName = Members::single()->getNameByUid($uid);
	    if (empty($userName))
	    {
	        return false;
	    }
	    $string = preg_replace('/{{\d+}}/i', ilink(filter_content($userName),url('system',array('s'=>'member','uid'=>$uid))), $string);
	    
	    if (false !== strpos($string, '{{Tag}}'))
	    {
	        return str_replace('{{Tag}}', ilink('挖宝', url('system',array('s'=>'props','c'=>'wa'))), $string);
	    }
	    
	    return preg_replace('/{{(.*?)}}/i', ilink("$1", url('system',array('s'=>'props','c'=>'wa'))), $string);
	}

    public function createForumDynamic($id, $type = 1)
    {
        $message = self::$_list[$type];
        if (!empty($message))
        {
            $book = getBookInfo($id);
            $dynamic = '[forumdynamic=' . $id .']《' . $book['bookname'] . '》[/forumdynamic]';
            $message = str_replace('[DYNAMIC]', $dynamic, $message);
            $key = md5(self::$forumDynamicKey);
            $this->Cache()->set($key, $message, 24 * 3600);
        }
    }

    public function getForumDynamic()
    {
        $value = $this->Cache()->get(md5(self::$forumDynamicKey));
        if (false !== $value)
        {
            preg_match('/\[forumdynamic=(\d+)\](.*?)\[\/forumdynamic\]/is', $value, $x);
            $id = $x[1];
            $name = $x[2];
            $search = '[forumdynamic=' . $x[1] . ']' . $x[2] . '[/forumdynamic]';
            $value = str_replace($search, $name, $value);
            $value = ilink($value, url('system',array('s'=>'forum','id'=>$id)));
        }
        
        return $value;
    }
}