<?php
require_once($_SERVER['USER_DIR'] . '/lib/simple/AbstractController.php');
require_once($_SERVER['USER_DIR'] . '/lib/simple/Database.php');
require_once($_SERVER['USER_DIR'] . '/lib/simple/Paginate.php');

class Controller extends AbstractController {
	var $amount = 10;
	var $pageAmount = 10;

	function Controller() {
		$this->AbstractController();
	}
	
	function init() {
		AbstractController::init();

		$this->__default_action = 'diary';
		$this->__default_view = 'diary';
		
		$this->__auto_session = false;
		$this->__auto_redirect_default_action = false;
	}
	
	function diary() {
		if (!$this->page) {
			$this->page = 1;
		}
		if (!isset($this->amount)) {
			$this->amount = 20;
		}

		$db = new Database('mysql://g106355:Fjt2pyZC@localhost/g106355'); 
		$db->connect();
		$db->assign('limit', $this->amount);
		$db->assign('offset', ($this->page - 1) * $this->amount);
		
		//diary
		$result = $db->statement('sql/diary_list_total.sql');
		$row = $db->wrapper->fetch_assoc($result);
		$total = $row['total'];
		$paginate = new Paginate();
		$this->pageInfo = $paginate->getPageInfo($this->page, $total, $this->amount, $this->pageAmount);
		
		$result = $db->statement('sql/diary_list.sql');
		$tree = $db->buildTree($result, 'diary_no');
		$this->diaryList = $tree;
		
		return $this->__default_view;
	}
}

$controller = new Controller();
$controller->run();
?>