meta/app/modules/meta/controllers/genre.php

125 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/**
* meta
*
* Hierarchial data tool
*
* @package meta
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/meta
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Genre controller
*
* @package meta
*/
2012-07-12 10:31:08 -04:00
class genre extends meta\controller {
/**
* Initialize the Controller
*/
public function __construct()
{
parent::__construct();
}
/**
* Default controller method
*/
2012-09-05 11:46:51 -04:00
public function index($id = 0)
{
2012-09-05 11:46:51 -04:00
if ($id === 0)
{
2012-09-05 11:46:51 -04:00
// Re-route to detail page if the last segment
// is a valid integer
$id = (int) miniMVC\get_last_segment();
}
2012-09-05 11:46:51 -04:00
if ($id === 0)
{
// Otherwise, display list of genres
$data = array();
2012-09-12 10:36:43 -04:00
$data['genres'] = $this->data_model->get_genres();
2012-09-05 11:46:51 -04:00
$this->load_view('genres', $data);
return;
}
2012-09-05 11:46:51 -04:00
return $this->detail($id);
}
/**
* Adds a new genre
*/
public function add()
{
// Strip away tags for the sake of security
$name = strip_tags($_POST['genre']);
// Make sure the name doesn't already exist. If it does, show an error.
2012-09-12 10:36:43 -04:00
$res = $this->data_model->add_genre($name);
if ($res === TRUE)
{
$this->page->set_message('success', 'Added new genre');
}
else
{
$this->page->set_message('error', 'Genre already exists');
}
// Render the basic page
$this->index();
}
/**
* Returns the categories / editing options for a genre
*
* @param int
*/
public function detail($id)
{
2012-09-12 10:36:43 -04:00
$genre = $this->data_model->get_genre_by_id($id);
$categories = $this->data_model->get_categories($id);
$data = array(
'genre' => $genre,
'categories' => $categories,
'genre_id' => $id
);
$this->load_view('genre_detail', $data);
}
2012-09-05 11:46:51 -04:00
/**
* Adds a category to the current genre
*/
2012-09-05 11:46:51 -04:00
public function add_category()
{
// Strip away tags for the sake of security
$name = strip_tags($_POST['category']);
$id = (int) $_POST['genre_id'];
// Make sure the name doesn't already exist. If it does, show an error.
2012-09-12 10:36:43 -04:00
$res = $this->data_model->add_category($name, $id);
2012-09-05 11:46:51 -04:00
if ($res === TRUE)
{
$this->page->set_message('success', 'Added new category');
}
else
{
$this->page->set_message('error', 'Category already exists for this genre');
}
$this->detail($id);
}
}
// End of genre.php