<?php

class Plugin_counter extends Plugin
{
	protected static $flag = false;
	
	
	function do_inline($pagename, $param1, $param2)
	{
		$db = DataBase::getinstance();
		$db->begin();
		
		$_pagename = KinoWiki::getinstance()->getpagename();
		$query  = "SELECT total,today,yesterday,date FROM counter";
		$query .= " WHERE pagename = '$_pagename'";
		$count = $db->fetch($db->query($query));
		if(Plugin_counter::$flag == false){
			$date = date('Y-m-d', TIME);
			if($count == null || $date != $count['date']){
				$count['total'] = isset($count['total']) ? $count['total'] + 1 : 1;
				$count['yesterday'] = isset($count['today']) ? $count['today'] : 0;
				$count['today'] = 1;
				$query  = "INSERT OR REPLACE INTO counter";
				$query .= " (pagename, total, today, yesterday, date)";
				$query .= " VALUES('$_pagename', {$count['total']}, {$count['today']}, {$count['yesterday']}, '$date')";
			}
			else{
				$count['total']++;
				$count['today']++;
				$query  = "UPDATE counter";
				$query .= " SET total = total + 1, today = today + 1";
				$query .= " WHERE pagename = '$_pagename'";
			}
			$db->query($query);
			Plugin_counter::$flag = true;
		}
		$db->commit();
		
		switch(trim($param1)){
			case 'today':
				return $count['today'];
			case 'yesterday':
				return $count['yesterday'];
			default:
				return $count['total'];
		}
	}
}

?>