App flow is more sane

This commit is contained in:
Timothy Warren 2012-09-05 15:46:51 +00:00
parent e91625300f
commit e316378dad
8 changed files with 95 additions and 81 deletions

View File

@ -29,20 +29,17 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
return array( return [
// Default Paths // Default Paths
'default_controller' => 'welcome', 'default_controller' => 'welcome',
'default_module' => 'meta', 'default_module' => 'meta',
'genre' => 'meta/genre/index', 'genre' => 'meta/genre/index',
'genre/add' => 'meta/genre/add', 'genre/add_category' => 'meta/genre/add_category',
'category' => 'meta/category/index', 'category' => 'meta/category/index',
'category/add' => 'meta/category/add', 'category/add_section' => 'meta/category/add_section',
'category/detail' => 'meta/category/detail',
'section' => 'meta/section/index', 'section' => 'meta/section/index',
'section/add' => 'meta/section/add', 'section/add_data' => 'meta/section/add_data',
'data/add' => 'meta/data/add',
'data/update' => 'meta/data/update',
'404_route' => '', '404_route' => '',
); ];
// End of routes.php // End of routes.php

View File

@ -28,43 +28,10 @@ class category extends meta\controller {
parent::__construct(); parent::__construct();
} }
/**
* Default controller method
*/
public function index()
{
$this->detail();
}
/**
* Adds a new category
*/
public function add()
{
// 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.
$res = $this->model->add_category($name, $id);
if ($res === TRUE)
{
$this->page->set_message('success', 'Added new category');
}
else
{
$this->page->set_message('error', 'Category already exists for this genre');
}
// Render the basic page
$this->detail($this->model->get_last_id('category'));
}
/** /**
* Returns the sections / editing options for a category * Returns the sections / editing options for a category
*/ */
public function detail($id = 0) public function index($id = 0)
{ {
if ($id === 0) if ($id === 0)
{ {
@ -85,6 +52,30 @@ class category extends meta\controller {
$this->load_view('category_detail', $data); $this->load_view('category_detail', $data);
} }
/**
* Adds a section to the current category
*/
public function add_section()
{
// Strip away tags for the sake of security
$name = strip_tags($_POST['section']);
$id = (int) $_POST['category_id'];
// Make sure the name doesn't already exist. If it does, show an error.
$res = $this->model->add_section($name, $id);
if ($res === TRUE)
{
$this->page->set_message('success', 'Added new section');
}
else
{
$this->page->set_message('error', 'Section already exists for this category');
}
$this->detail($id);
}
} }
// End of genre.php // End of genre.php

View File

@ -31,22 +31,27 @@ class genre extends meta\controller {
/** /**
* Default controller method * Default controller method
*/ */
public function index() public function index($id = 0)
{
if ($id === 0)
{ {
// Re-route to detail page if the last segment // Re-route to detail page if the last segment
// is a valid integer // is a valid integer
$id = (int) miniMVC\get_last_segment(); $id = (int) miniMVC\get_last_segment();
if ($id !== 0)
{
return $this->detail($id);
} }
if ($id === 0)
{
// Otherwise, display list of genres // Otherwise, display list of genres
$data = array(); $data = array();
$data['genres'] = $this->model->get_genres(); $data['genres'] = $this->model->get_genres();
$this->load_view('genres', $data); $this->load_view('genres', $data);
return;
}
return $this->detail($id);
} }
/** /**
@ -91,6 +96,27 @@ class genre extends meta\controller {
$this->load_view('genre_detail', $data); $this->load_view('genre_detail', $data);
} }
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.
$res = $this->model->add_category($name, $id);
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 // End of genre.php

View File

@ -54,28 +54,27 @@ class section extends meta\controller {
} }
/** /**
* Adds a new category * Adds a data item to the current section
*/ */
public function add() public function add_data()
{ {
// Strip away tags for the sake of security $section_id = (int) $_POST['section_id'];
$name = strip_tags($_POST['section']);
$id = (int) $_POST['category_id'];
// Make sure the name doesn't already exist. If it does, show an error.
$res = $this->model->add_section($name, $id);
if ($res === TRUE) $keys = filter_var_array($_POST['name'], FILTER_SANITIZE_STRING);
{ $vals = filter_var_array($_POST['val'], FILTER_SANITIZE_STRING);
$this->page->set_message('success', 'Added new section');
}
else
{
$this->page->set_message('error', 'Section already exists for this category');
}
// Render the basic page
$this->index($this->model->get_last_id('section')); $data = array_combine($keys, $vals);
$res = $this->model->add_data($section_id, $data);
($res)
? $this->page->set_message('success', 'Added data')
: $this->page->set_message('error', 'Data already exists');
$this->index($section_id);
} }
} }

View File

@ -100,13 +100,14 @@ class model extends \miniMVC\Model {
// Check for duplicates // Check for duplicates
$query = $this->db->from('genre') $query = $this->db->from('genre')
->where('genre', $genre) ->where('genre', $genre)
->limit(1)
->get(); ->get();
// Fetch the data as a workaround // Fetch the data as a workaround
// for databases that do not support // for databases that do not support
// grabbing result counts (SQLite / Firebird) // grabbing result counts (SQLite / Firebird)
$array = $query->fetchAll(); $array = $query->fetchAll();
if (count($array)< 1) if (empty($array))
{ {
$this->db->set('genre', $genre) $this->db->set('genre', $genre)
->insert('genre'); ->insert('genre');
@ -228,7 +229,7 @@ class model extends \miniMVC\Model {
{ {
$this->db->set($type, $name) $this->db->set($type, $name)
->where('id', (int) $id) ->where('id', (int) $id)
->update('genre'); ->update($type);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -237,15 +238,15 @@ class model extends \miniMVC\Model {
* Update the data * Update the data
* *
* @param int * @param int
* @param mixed * @param string
* @param string
*/ */
public function update_data($data_id, $data) public function update_data($data_id, $key, $val)
{ {
// Convert the data to json for storage
$data_str = json_encode('data', $data_str);
// Save the data // Save the data
$this->db->set('data', $data_str) $this->db->set('key', $key)
->set('value', $val)
->where('id', (int) $data_id) ->where('id', (int) $data_id)
->update('data'); ->update('data');

View File

@ -4,7 +4,7 @@
<a href="<?= miniMVC\site_url('') ?>">Genres</a> > <a href="<?= miniMVC\site_url('genres/detail/'.$genre['id']) ?>"><?= $genre['genre'] ?></a> > <?= $category ?> <a href="<?= miniMVC\site_url('') ?>">Genres</a> > <a href="<?= miniMVC\site_url('genres/detail/'.$genre['id']) ?>"><?= $genre['genre'] ?></a> > <?= $category ?>
</p> </p>
<form action="<?= miniMVC\site_url("section/add") ?>" method="post"> <form action="<?= miniMVC\site_url("category/add_section") ?>" method="post">
<fieldset> <fieldset>
<legend>Add Section</legend> <legend>Add Section</legend>
<dl> <dl>

View File

@ -4,7 +4,7 @@
<a href="<?= miniMVC\site_url('') ?>">Genres</a> > <?= $genre ?> <a href="<?= miniMVC\site_url('') ?>">Genres</a> > <?= $genre ?>
</p> </p>
<form action="<?= miniMVC\site_url("category/add") ?>" method="post"> <form action="<?= miniMVC\site_url("genre/add_category") ?>" method="post">
<fieldset> <fieldset>
<legend>Add Category</legend> <legend>Add Category</legend>
<dl> <dl>

View File

@ -7,7 +7,7 @@
<?= $section ?> <?= $section ?>
</p> </p>
<form action="<?= miniMVC\site_url("data/add") ?>" method="post"> <form action="<?= miniMVC\site_url("section/add_data") ?>" method="post">
<fieldset> <fieldset>
<legend>Add Data</legend> <legend>Add Data</legend>
<dl> <dl>