diff --git a/app/classes/controller.php b/app/classes/controller.php
index 3de2d2f..6972490 100644
--- a/app/classes/controller.php
+++ b/app/classes/controller.php
@@ -30,14 +30,35 @@ abstract class Controller extends \miniMVC\Controller {
parent::__construct();
$this->load_model('meta\model');
$this->load_model('meta\user_model');
+
+ $this->session =& \miniMVC\Session::get_instance();
+
+ // Check if user is logged in
+ $this->check_login_status();
+
$this->page->build_header();
}
+ /**
+ * Require user login for access
+ */
+ private function check_login_status()
+ {
+ if ( ! isset($this->session->uid))
+ {
+ // Redirect to login
+ }
+
+ return;
+
+ }
+
/**
* Destruct controller and build page footer
*/
public function __destruct()
{
+ $this->page->set_foot_js_group('js');
$this->page->build_footer();
}
diff --git a/app/config/config.php b/app/config/config.php
index fc291f4..425346a 100644
--- a/app/config/config.php
+++ b/app/config/config.php
@@ -134,4 +134,14 @@ define('DEFAULT_JS_GROUP', "js");
*/
define('SHOW_DEBUG_BACKTRACE', TRUE);
+/*
+|--------------------------------------------------------------------------
+| Gzip compress
+|--------------------------------------------------------------------------
+|
+| Whether or not use gzip compression on page output
+|
+*/
+define('GZ_COMPRESS', FALSE);
+
// End of config.php
\ No newline at end of file
diff --git a/app/config/routes.php b/app/config/routes.php
index 3c052e5..1f6bdd8 100644
--- a/app/config/routes.php
+++ b/app/config/routes.php
@@ -31,17 +31,18 @@
return array(
// Default Paths
- 'default_controller' => 'welcome',
- 'default_module' => 'meta',
- 'genre' => 'meta/genre/index',
- 'genre/add' => 'meta/genre/add',
- 'category' => 'meta/category/index',
- 'category/add' => 'meta/category/add',
- 'category/detail' => 'meta/category/detail',
- 'section' => 'meta/section/index',
- 'section/add' => 'meta/section/add',
- 'section/detail' => 'meta/section/detail',
- '404_route' => '',
+ 'default_controller' => 'welcome',
+ 'default_module' => 'meta',
+ 'genre' => 'meta/genre/index',
+ 'genre/add' => 'meta/genre/add',
+ 'category' => 'meta/category/index',
+ 'category/add' => 'meta/category/add',
+ 'category/detail' => 'meta/category/detail',
+ 'section' => 'meta/section/index',
+ 'section/add' => 'meta/section/add',
+ 'data/add' => 'meta/data/add',
+ 'data/update' => 'meta/data/update',
+ '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 92ef9a2..76df7d5 100644
--- a/app/modules/meta/controllers/category.php
+++ b/app/modules/meta/controllers/category.php
@@ -58,7 +58,7 @@ class category extends meta\controller {
}
// Render the basic page
- $this->detail(-1);
+ $this->detail($this->model->get_last_id('category'));
}
/**
@@ -78,7 +78,8 @@ class category extends meta\controller {
$data = array(
'category' => $this->model->get_category_by_id($id),
- 'sections' => $this->model->get_sections($id),
+ 'sections' => $this->model->get_category_outline_data($id),
+ 'genre' => $this->model->get_genre_by_category($id),
'category_id' => $id
);
diff --git a/app/modules/meta/controllers/data.php b/app/modules/meta/controllers/data.php
new file mode 100644
index 0000000..b27c98b
--- /dev/null
+++ b/app/modules/meta/controllers/data.php
@@ -0,0 +1,66 @@
+model->get_data($section_id);
+
+
+ $keys = filter_var_array($_POST['name'], FILTER_SANITIZE_STRING);
+ $vals = filter_var_array($_POST['val'], FILTER_SANITIZE_STRING);
+
+ //echo miniMVC\to_string($_POST);
+
+ $data = array_combine($keys, $vals);
+
+ $res = /*(empty($old_data))
+ ?*/ $this->model->add_data($section_id, $data);
+ //: FALSE;
+
+ ($res)
+ ? $this->page->set_message('success', 'Added data')
+ : $this->page->set_message('error', 'Data already exists');
+
+ }
+}
+
+// End of data.php
\ No newline at end of file
diff --git a/app/modules/meta/controllers/section.php b/app/modules/meta/controllers/section.php
index e2a1daf..f711ac9 100644
--- a/app/modules/meta/controllers/section.php
+++ b/app/modules/meta/controllers/section.php
@@ -31,9 +31,26 @@ class section extends meta\controller {
/**
* Default controller method
*/
- public function index()
+ public function index($id=0)
{
- $this->detail();
+ if ($id === 0)
+ {
+ $id = (int) miniMVC\get_last_segment();
+ }
+
+ if ($id === 0)
+ {
+ miniMVC\show_404();
+ }
+
+ $data = array(
+ 'section' => $this->model->get_section_by_id($id),
+ 'sdata' => $this->model->get_data($id),
+ 'p' => $this->model->get_path_by_section($id),
+ 'section_id' => $id
+ );
+
+ $this->load_view('section_detail', $data);
}
/**
@@ -58,33 +75,8 @@ class section extends meta\controller {
}
// Render the basic page
- $this->detail(-1);
+ $this->index($this->model->get_last_id('section'));
}
-
- /**
- * Returns the sections / editing options for a category
- */
- public function detail($id = 0)
- {
- if ($id === 0)
- {
- $id = (int) miniMVC\get_last_segment();
- }
-
- if ($id === 0)
- {
- miniMVC\show_404();
- }
-
- $data = array(
- 'section' => $this->model->get_section_by_id($id),
- 'data' => $this->model->get_data($id),
- 'section_id' => $id
- );
-
- $this->load_view('section_detail', $data);
- }
-
}
// End of section.php
\ No newline at end of file
diff --git a/app/modules/meta/controllers/welcome.php b/app/modules/meta/controllers/welcome.php
index 07f0460..6370827 100644
--- a/app/modules/meta/controllers/welcome.php
+++ b/app/modules/meta/controllers/welcome.php
@@ -54,9 +54,19 @@ class welcome extends \meta\controller {
{
$this->page->render('login');
}
-
+
// --------------------------------------------------------------------------
-
+
+ /**
+ * Logout
+ */
+ public function logout()
+ {
+
+ }
+
+ // --------------------------------------------------------------------------
+
/**
* Display an outline of the data for a table of contents
*/
diff --git a/app/modules/meta/models/model.php b/app/modules/meta/models/model.php
index 16ac208..d21303f 100644
--- a/app/modules/meta/models/model.php
+++ b/app/modules/meta/models/model.php
@@ -47,6 +47,8 @@ class model extends \miniMVC\Model {
$this->db =& \miniMVC\db::get_instance();
}
+ // --------------------------------------------------------------------------
+ // ! Data Manipulation
// --------------------------------------------------------------------------
/**
@@ -104,7 +106,7 @@ class model extends \miniMVC\Model {
// for databases that do not support
// grabbing result counts (SQLite / Firebird)
$array = $query->fetchAll();
- if (count($array) === 0)
+ if (count($array)< 1)
{
$this->db->set('genre', $genre)
->insert('genre');
@@ -118,21 +120,6 @@ class model extends \miniMVC\Model {
// --------------------------------------------------------------------------
- /**
- * Rename a genre
- *
- * @param int
- * @param string
- */
- public function update_genre($genre_id, $genre)
- {
- $this->db->set('genre', $genre)
- ->where('id', $genre_id)
- ->update('genre');
- }
-
- // --------------------------------------------------------------------------
-
/**
* Add category to genre
*
@@ -146,13 +133,14 @@ class model extends \miniMVC\Model {
$query = $this->db->from('category')
->where('genre_id', $genre_id)
->where('category', $cat)
+ ->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) === 0)
+ if (count($array)< 1)
{
$this->db->set('category', $cat)
->set('genre_id', $genre_id)
@@ -166,21 +154,6 @@ class model extends \miniMVC\Model {
// --------------------------------------------------------------------------
- /**
- * Rename a category
- *
- * @param int
- * @param string
- */
- public function update_category($cat_id, $category)
- {
- $this->db->set('category', $category)
- ->where('id', (int) $cat_id)
- ->update('category');
- }
-
- // --------------------------------------------------------------------------
-
/**
* Add a section to a category
*
@@ -189,24 +162,27 @@ class model extends \miniMVC\Model {
*/
public function add_section($section, $category_id)
{
- $this->db->set('section', $section)
- ->set('category_id', (int) $category_id)
- ->insert('section');
- }
+ // Check if the section exists
+ $q = $this->db->from('section')
+ ->where('category_id', $category_id)
+ ->where('section', $section)
+ ->limit(1)
+ ->get();
- // --------------------------------------------------------------------------
+ // Fetch the data as a workaround
+ // for databases that do not support
+ // grabbing result counts (SQLite / Firebird)
+ $array = $q->fetchAll();
+ if (count($array) < 1)
+ {
+ $this->db->set('section', $section)
+ ->set('category_id', (int) $category_id)
+ ->insert('section');
- /**
- * Rename a section
- *
- * @param int
- * @param string
- */
- public function update_section($section_id, $section)
- {
- $this->db->set('section', $section)
- ->where('id', (int) $section_id)
- ->update('section');
+ return TRUE;
+ }
+
+ return FALSE;
}
// --------------------------------------------------------------------------
@@ -219,13 +195,40 @@ class model extends \miniMVC\Model {
*/
public function add_data($section_id, $data)
{
- // Convert the data to json for storage
- $data_str = json_encode($data);
+ foreach($data as $key => $val)
+ {
+ // See if the data exists
+ $q = $this->db->from('data')
+ ->where('section_id', $section_id)
+ ->where('key', $key)
+ ->get();
- // Save the data
- $this->db->set('data', $data_str)
- ->set('section_id', (int) $section_id)
- ->insert('data');
+ if ($this->db->num_rows() > 0) return FALSE;
+
+ // Save the data
+ $this->db->set('key', $key)
+ ->set('value', $val)
+ ->set('section_id', (int) $section_id)
+ ->insert('data');
+ }
+
+ return TRUE;
+ }
+
+ // --------------------------------------------------------------------------
+
+ /**
+ * Rename a genre/category/section
+ *
+ * @param string
+ * @param int
+ * @param string
+ */
+ public function update($type, $id, $name)
+ {
+ $this->db->set($type, $name)
+ ->where('id', (int) $id)
+ ->update('genre');
}
// --------------------------------------------------------------------------
@@ -248,6 +251,49 @@ class model extends \miniMVC\Model {
}
+ // --------------------------------------------------------------------------
+ // ! Data Retrieval
+ // --------------------------------------------------------------------------
+
+ /**
+ * Get the id of the last item of the type
+ *
+ * @param string $type
+ * @return int
+ */
+ public function get_last_id($type)
+ {
+ $query = $this->db->select('id')
+ ->from($type)
+ ->order_by('id', 'DESC')
+ ->limit(1)
+ ->get();
+
+ $r = $query->fetch(\PDO::FETCH_ASSOC);
+
+ return $r['id'];
+ }
+
+ // --------------------------------------------------------------------------
+
+ /**
+ * Get breadcrumb data for section
+ *
+ * @param section_id
+ * @return array
+ */
+ public function get_path_by_section($section_id)
+ {
+ $query = $this->db->select('genre, genre_id, category, category_id')
+ ->from('section s')
+ ->join('category c', 'c.id=s.category_id')
+ ->join('genre g', 'g.id=c.genre_id')
+ ->where('s.id', $section_id)
+ ->get();
+
+ return $query->fetch(\PDO::FETCH_ASSOC);
+ }
+
// --------------------------------------------------------------------------
/**
@@ -313,6 +359,27 @@ class model extends \miniMVC\Model {
// --------------------------------------------------------------------------
+ /**
+ * Get the genre name by category id
+ *
+ * @param int
+ * @return array
+ */
+ public function get_genre_by_category($cat_id)
+ {
+ $query = $this->db->select('g.id, genre')
+ ->from('genre g')
+ ->join('category c', 'c.genre_id=g.id', 'inner')
+ ->where('c.id', (int)$cat_id)
+ ->get();
+
+ $row = $query->fetch(\PDO::FETCH_ASSOC);
+
+ return $row;
+ }
+
+ // --------------------------------------------------------------------------
+
/**
* Gets the name of the section from its id
*
@@ -393,21 +460,71 @@ class model extends \miniMVC\Model {
{
$data = array();
- $query = $this->db->select('id, data')
+ $query = $this->db->select('id, key, value')
->from('data')
->where('section_id', (int) $section_id)
->get();
while($row = $query->fetch(\PDO::FETCH_ASSOC))
{
- $data[$row['id']] = json_decode($row['data'], TRUE);
+ $data[$row['id']] = array($row['key'] => str_replace("\n", "
", $row['value']));
}
return $data;
}
-
+
// --------------------------------------------------------------------------
-
+
+ /**
+ * Get sections and data for a general data outline
+ *
+ * @param int $category_id
+ * @return array
+ */
+ public function get_category_outline_data($category_id)
+ {
+ // Get the sections
+ $s_query = $this->db->from('section')
+ ->where('category_id', (int) $category_id)
+ ->get();
+
+ $sections = array();
+
+ while($row = $s_query->fetch(\PDO::FETCH_ASSOC))
+ {
+ $sections[$row['id']] = $row['section'];
+ }
+
+ // Get the data for the sections
+ $d_array = array();
+
+ if ( ! empty($sections))
+ {
+ $d_query = $this->db->from('data')
+ ->where_in('section_id', array_keys($sections))
+ ->get();
+
+ while($row = $d_query->fetch(\PDO::FETCH_ASSOC))
+ {
+ $d_array[$row['section_id']][$row['key']] = str_replace("\n", "
", $row['value']);
+ }
+ }
+
+ // Reorganize the data
+ $data = array();
+
+ foreach($sections as $section_id => $section)
+ {
+ $data[$section_id] = (isset($d_array[$section_id]))
+ ? array($section, $d_array[$section_id])
+ : $section;
+ }
+
+ return $data;
+ }
+
+ // --------------------------------------------------------------------------
+
/**
* Get data for a full outline
*
@@ -418,53 +535,53 @@ class model extends \miniMVC\Model {
// Get the genres
$g_query = $this->db->from('genre')
->get();
-
+
$genres = array();
-
+
while ($row = $g_query->fetch(\PDO::FETCH_ASSOC))
{
$genres[$row['id']] = $row['genre'];
}
-
+
// Get the categories
$c_query = $this->db->from('category')
->get();
-
+
$categories = array();
-
+
while($row = $c_query->fetch(\PDO::FETCH_ASSOC))
{
$categories[$row['genre_id']][$row['id']] = $row['category'];
}
-
+
// Get the sections
$s_query = $this->db->from('section')
->get();
-
+
$sections = array();
-
+
while($row = $s_query->fetch(\PDO::FETCH_ASSOC))
{
$sections[$row['category_id']][$row['id']] = $row['section'];
}
-
-
- // Organize into a nested array
+
+
+ // Organize into a nested array
foreach($genres as $genre_id => $genre)
{
$return[$genre_id][$genre] = array();
$g =& $return[$genre_id][$genre];
-
+
// Categories for this genre
if (isset($categories[$genre_id]))
{
$g = $categories[$genre_id];
-
+
foreach($categories[$genre_id] as $category_id => $category)
{
$g[$category_id] = array($category => array());
$c =& $g[$category_id][$category];
-
+
// Sections for this category
if (isset($sections[$category_id]))
{
@@ -473,7 +590,7 @@ class model extends \miniMVC\Model {
}
}
}
-
+
return $return;
}
diff --git a/app/modules/meta/models/user_model.php b/app/modules/meta/models/user_model.php
index b24c71f..883b40d 100644
--- a/app/modules/meta/models/user_model.php
+++ b/app/modules/meta/models/user_model.php
@@ -35,7 +35,7 @@ class user_model extends \miniMVC\Model {
* @var Bcrypt
*/
protected $bcrypt;
-
+
/**
* Reference to session
*
@@ -56,7 +56,7 @@ class user_model extends \miniMVC\Model {
}
// --------------------------------------------------------------------------
-
+
/**
* Add a user for access
*
@@ -66,9 +66,29 @@ class user_model extends \miniMVC\Model {
*/
public function add_user($username, $pass1, $pass2)
{
-
+ // Check for the existing username
+ $query = $this->db->select('username')
+ ->from('user')
+ ->where('username', $username)
+ ->get();
+
+ $res = $query->fetch(\PDO::FETCH_ASSOC);
+
+ if (empty($res)) return FALSE;
+
+ // Verify that passwords match
+ if ($pass1 !== $pass2) return FALSE;
+
+ // Add user
+ $hashed = $this->bcrypt->hash($pass1);
+
+ $this->db->set('username', $username)
+ ->set('hash', $hashed)
+ ->insert('user');
+
+ return TRUE;
}
-
+
// --------------------------------------------------------------------------
/**
diff --git a/app/modules/meta/views/category_detail.php b/app/modules/meta/views/category_detail.php
index 0ecdb9a..5ca2608 100644
--- a/app/modules/meta/views/category_detail.php
+++ b/app/modules/meta/views/category_detail.php
@@ -1,15 +1,12 @@
-