From e316378dad3852eaea40eab9a3638fcbb5532c81 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 5 Sep 2012 15:46:51 +0000 Subject: [PATCH] App flow is more sane --- app/config/routes.php | 13 ++--- app/modules/meta/controllers/category.php | 59 +++++++++------------- app/modules/meta/controllers/genre.php | 48 ++++++++++++++---- app/modules/meta/controllers/section.php | 33 ++++++------ app/modules/meta/models/model.php | 15 +++--- app/modules/meta/views/category_detail.php | 2 +- app/modules/meta/views/genre_detail.php | 2 +- app/modules/meta/views/section_detail.php | 4 +- 8 files changed, 95 insertions(+), 81 deletions(-) diff --git a/app/config/routes.php b/app/config/routes.php index 1f6bdd8..f8a56c7 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -29,20 +29,17 @@ // -------------------------------------------------------------------------- -return array( +return [ // Default Paths 'default_controller' => 'welcome', 'default_module' => 'meta', 'genre' => 'meta/genre/index', - 'genre/add' => 'meta/genre/add', + 'genre/add_category' => 'meta/genre/add_category', 'category' => 'meta/category/index', - 'category/add' => 'meta/category/add', - 'category/detail' => 'meta/category/detail', + 'category/add_section' => 'meta/category/add_section', 'section' => 'meta/section/index', - 'section/add' => 'meta/section/add', - 'data/add' => 'meta/data/add', - 'data/update' => 'meta/data/update', + 'section/add_data' => 'meta/section/add_data', '404_route' => '', -); +]; // End of routes.php \ No newline at end of file diff --git a/app/modules/meta/controllers/category.php b/app/modules/meta/controllers/category.php index 76df7d5..32266ea 100644 --- a/app/modules/meta/controllers/category.php +++ b/app/modules/meta/controllers/category.php @@ -28,43 +28,10 @@ class category extends meta\controller { 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 */ - public function detail($id = 0) + public function index($id = 0) { if ($id === 0) { @@ -85,6 +52,30 @@ class category extends meta\controller { $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 \ No newline at end of file diff --git a/app/modules/meta/controllers/genre.php b/app/modules/meta/controllers/genre.php index 3163137..19f3133 100644 --- a/app/modules/meta/controllers/genre.php +++ b/app/modules/meta/controllers/genre.php @@ -31,22 +31,27 @@ class genre extends meta\controller { /** * Default controller method */ - public function index() + public function index($id = 0) { - // Re-route to detail page if the last segment - // is a valid integer - $id = (int) miniMVC\get_last_segment(); - - if ($id !== 0) + if ($id === 0) { - return $this->detail($id); + // Re-route to detail page if the last segment + // is a valid integer + $id = (int) miniMVC\get_last_segment(); } - // Otherwise, display list of genres - $data = array(); - $data['genres'] = $this->model->get_genres(); + if ($id === 0) + { + // Otherwise, display list of genres + $data = array(); + $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); } + + 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 \ No newline at end of file diff --git a/app/modules/meta/controllers/section.php b/app/modules/meta/controllers/section.php index f711ac9..04d572b 100644 --- a/app/modules/meta/controllers/section.php +++ b/app/modules/meta/controllers/section.php @@ -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 - $name = strip_tags($_POST['section']); - $id = (int) $_POST['category_id']; + $section_id = (int) $_POST['section_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'); - } + $keys = filter_var_array($_POST['name'], FILTER_SANITIZE_STRING); + $vals = filter_var_array($_POST['val'], FILTER_SANITIZE_STRING); - // 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); } } diff --git a/app/modules/meta/models/model.php b/app/modules/meta/models/model.php index d21303f..c5a31ff 100644 --- a/app/modules/meta/models/model.php +++ b/app/modules/meta/models/model.php @@ -100,13 +100,14 @@ class model extends \miniMVC\Model { // Check for duplicates $query = $this->db->from('genre') ->where('genre', $genre) + ->limit(1) ->get(); // Fetch the data as a workaround // for databases that do not support // grabbing result counts (SQLite / Firebird) $array = $query->fetchAll(); - if (count($array)< 1) + if (empty($array)) { $this->db->set('genre', $genre) ->insert('genre'); @@ -228,7 +229,7 @@ class model extends \miniMVC\Model { { $this->db->set($type, $name) ->where('id', (int) $id) - ->update('genre'); + ->update($type); } // -------------------------------------------------------------------------- @@ -237,15 +238,15 @@ class model extends \miniMVC\Model { * Update the data * * @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 - $this->db->set('data', $data_str) + $this->db->set('key', $key) + ->set('value', $val) ->where('id', (int) $data_id) ->update('data'); diff --git a/app/modules/meta/views/category_detail.php b/app/modules/meta/views/category_detail.php index 5ca2608..8e7e0be 100644 --- a/app/modules/meta/views/category_detail.php +++ b/app/modules/meta/views/category_detail.php @@ -4,7 +4,7 @@ Genres > >

-
" method="post"> +" method="post">
Add Section
diff --git a/app/modules/meta/views/genre_detail.php b/app/modules/meta/views/genre_detail.php index 1077671..caab403 100644 --- a/app/modules/meta/views/genre_detail.php +++ b/app/modules/meta/views/genre_detail.php @@ -4,7 +4,7 @@ Genres >

-" method="post"> +" method="post">
Add Category
diff --git a/app/modules/meta/views/section_detail.php b/app/modules/meta/views/section_detail.php index d7206b1..cc00275 100644 --- a/app/modules/meta/views/section_detail.php +++ b/app/modules/meta/views/section_detail.php @@ -2,12 +2,12 @@ -" method="post"> +" method="post">
Add Data