<?php // vim: ts=4 sw=4 sts=4 fdm=marker ai noet:
// $Id: bpopular.inc.php,v 1.3 2004/12/08 23:37:30 ishii Exp $

function plugin_bpopular_convert()
{
	global $_popular_plugin_frame, $_popular_plugin_today_frame;
	global $whatsnew,$non_list;
	global $xoopsDB;

	$table_name = $xoopsDB->prefix('bwiki_bcounter');

	$max = 10;
	$except = '';

	$array = func_get_args();
	$today = FALSE;

	switch (func_num_args()) {
	case 3:
		if ($array[2])
			$today = get_date('Y-m-d');
	case 2:
		$except = $array[1];
	case 1:
		$max = $array[0];
	}

	if($today)
	{
		$sql = "select * from {$table_name} where date = '{$today}' order by today desc";
	}
	else
	{
		$sql = "select * from {$table_name} order by total desc";
	}

	$items = '';
	$result = $xoopsDB->query($sql, $max);
	$isExist = (int)$xoopsDB->getRowsNum($result);

	if($isExist)
	{
		$items = '<ul class="popular_list">';
	}

	while($row = $xoopsDB->fetchArray($result))
	{
		// TODO ここで表示しないページを削ってしまうと
		// $maxよりも少ない表示になってしまう、どうしようかな…
		if ($except != '' and ereg($except,$row['page_name']))
		{
			continue;
		}

		// TODO ここも同様に$maxよりも少なくなる。
		if ($row['page_name'] == $whatsnew
			or preg_match("/$non_list/",$row['page_name'])
			or !is_page($row['page_name']))
		{
			continue;
		}

		if ($today)
		{
			$count = $row['today'];
		}
		else
		{
			$count = $row['total'];
		}

		$s_page = htmlspecialchars($row['page_name']);
		$items .= " <li>".make_pagelink($row['page_name'],"$s_page<span class=\"counter\">($count)</span>")."</li>\n";
	}

	if($isExist)
	{
		$items .= '</ul>';
	}

	return sprintf($today ? $_popular_plugin_today_frame : $_popular_plugin_frame,$isExist,$items);
}

?>
