<?php

/**
 * Show All {{ name }} / Show Detail (if {{ name }} id is present)
 */
Slim::get('/{{ name }}(/:id)',function($id = 0) {
	if ($id == 0) {
		$current_page = (isset($_GET['p']) ? $_GET['p'] : 1);
		$count_per_page = 30;
		
		$data = {{ class }}::getAll($current_page,$count_per_page);
		$page = {{ class }}::generatePage($current_page,$count_per_page,Slim::urlfor("{{name}}"));
		
		Slim::render('{{ name }}/list.html',array(
			'page' => $page,
			'data' => $data
		));
	} else {
		$data = {{ class }}::get($id);
		Slim::render('{{ name }}/view.html',array(
			"data" => $data
		));
	}
})->name('{{ name }}');

/**
 * New {{ name }}
 */
Slim::get('/{{ name }}/new/',function() {
	$columns = {{ class }}::getColumns();
	Slim::render("{{ name }}/new.html",array("columns"=>$columns));
})->name('{{ name }}_new');

Slim::post('/{{ name }}/new/',function() {
	if ({{ class }}::create($_POST)) {
		redir_by_id("{{ name }}");
	} else {
		Slim::render('{{ name }}/new.html',array(
			"data" => $_POST
		));
	}
});

/**
 * Edit {{ name }}
 */
Slim::get('/{{ name }}/edit(/:id)',function($id = 0) {
	if ($id == 0) redir_by_id("{{ name }}");
	$data = {{ class }}::get($id);
	Slim::render('{{ name }}/edit.html',array(
		"data" => $data
	));
})->name('{{ name }}_edit'); 

Slim::post('/{{ name }}/edit(/:id)',function($id = 0) {
	if ($id == 0) redir_by_id('{{ name }}');
	
	if ({{ class }}::update($id,$_POST)) {
		redir_by_id('{{ name }}');
	} else {
		Slim::render('{{ name }}/edit.html',array(
			"data" => $_POST
		));
	}
});

/**
 * Delete {{ name }}
 */
Slim::get('/{{ name }}/del(/:id)',function($id = 0) {
	if ($id == 0) redir_by_id('{{ name }}');
	
	{{ class }}::remove($id);
	redir_by_id('{{ name }}');
})->name('{{ name }}_del');

?>
