<?php


class Plugin_bbsviewer extends Plugin
{
	function do_block($pagename, $param1, $param2)
	{
		if(!mb_ereg('^\s*(.+?)\s*,\s*(\d+)\s*$', $param1, $m) || $m[2] <= 0){
			throw new PluginException('引数が正しくありません。', 'bbsviewer');
		}
		$home = $m[1];
		$num = $m[2];
		
		$get = KinoWiki::getinstance()->getGET();
		$p = isset($get['p']) ? max(0, $get['p']) : 0;
		$point = $num * $p;
		$db = DataBase::getinstance();
		$pattern = $db->escape('^' . mb_ereg_quote($home) . '/(\d+)/.+$');
		$query  = "SELECT pagename,source FROM page";
		$query .= " WHERE php('mb_ereg', '{$pattern}', pagename)";
		$query .= " ORDER BY timestamp DESC";
		$query .= " LIMIT $num OFFSET $point";
		$ret = array();
		$result = $db->query($query);
		while($row = $db->fetch($result)){
			$ret[] = $this->includepage($row['pagename'], $row['source'], $home);
		}
		
		$smarty = new PluginSmarty('bbsviewer');
		$smarty->assign('pagename', $pagename);
		if($p > 0){
			$smarty->assign('next', $p - 1);
		}
		$smarty->assign('prev', $p + 1);
		$smarty->assign('body', join("\n", $ret));
		return $smarty->fetch('bbsviewer.tpl.htm');
	}
	
	
	protected function includepage($pagename, $source, $home)
	{
		mb_ereg('^' . mb_ereg_quote($home) . '/\d+/(.+)$', $pagename, $m);
		$smarty = new PluginSmarty('bbsviewer');
		$smarty->assign('title', $pagename);
		$smarty->assign('alias', $m[1]);
		$smarty->assign('body', convert_block($source, $pagename));
		return $smarty->fetch('include.tpl.htm');
	}
}


?>