Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Category management now in place.

  • Loading branch information...
commit 8056e54b6fd703ab6b23ca55b2659e12e285134b 1 parent 09c6228
@daveWid authored
View
3  classes/controller/admin/soapbox/category.php
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller_Admin_Soapbox_Category extends Soapbox_Controller_Admin_Category{}
View
137 classes/soapbox/controller/admin/category.php
@@ -0,0 +1,137 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Soapbox category administration.
+ *
+ * @package Soapbox
+ * @author Dave Widmer <dave@davewidmer.net>
+ * @copyright 2011 © Dave Widmer
+ */
+class Soapbox_Controller_Admin_Category extends Controller_Template
+{
+ /**
+ * @var array An array of configuration values
+ */
+ protected $_config;
+
+ /**
+ * Get the configuration
+ */
+ public function before()
+ {
+ // Do a quick check for a user
+ if ( ! Auth::instance()->get_user())
+ {
+ $this->request->redirect(Route::get('soapbox/login')->uri(array('action' => "login")));
+ }
+
+ $this->_config = Kohana::$config->load('soapbox');
+ return parent::before();
+ }
+
+ /**
+ * View list of categories for administration.
+ */
+ public function action_index()
+ {
+ $this->template->title = $this->_config['title']." :: Administration :: Categories";
+ $this->template->content = View::factory('soapbox/admin/category/list')->set(array(
+ 'categories' => Model::factory('category')->fetch(),
+ 'message' => Session::instance()->get_once('soapbox-message', null)
+ ));
+ }
+
+ /**
+ * Create a new post
+ */
+ public function action_add()
+ {
+ if ($this->request->method() === "POST")
+ {
+ $this->do_add();
+ }
+
+ $this->template->title = "Administration :: Add Category";
+ $this->template->content = View::factory('soapbox/admin/category/form')->set(array(
+ 'post' => $this->request->post(),
+ 'action' => Route::url('soapbox/admin/category', array('action' => "add")),
+ 'error' => Session::instance()->get_once('soapbox-message', null),
+ ));
+ }
+
+ /**
+ * Processes the add post form.
+ */
+ public function do_add()
+ {
+ $model = new Model_Category;
+ $post = $this->request->post();
+
+ if (Valid::not_empty($post['display']))
+ {
+ list($id, $num) = $model->create($post); // Save the category
+ $this->request->redirect(Route::get('soapbox/admin/category')->uri(array('index' => false)));
+ }
+ else
+ {
+ Session::instance()->set('soapbox-message', Kohana::message('soapbox', 'category.error'));
+ }
+ }
+
+ /**
+ * Edit a post
+ */
+ public function action_edit()
+ {
+ $id = $this->request->param('id');
+
+ if ($this->request->method() === "POST")
+ {
+ $this->do_edit($id);
+ }
+ else
+ {
+ // Get the post data
+ $this->request->post((array) Model::factory('category')->read($id));
+ }
+
+ $this->template->title = "Administration :: Edit Category";
+ $this->template->content = View::factory('soapbox/admin/category/form')->set(array(
+ 'post' => $this->request->post(),
+ 'action' => Route::url('soapbox/admin/category', array('action' => "edit", 'id' => $id)),
+ 'error' => Session::instance()->get_once('soapbox-message', null),
+ ));
+ }
+
+ /**
+ * Does the editing of a post
+ *
+ * @param int The post id to edit
+ */
+ protected function do_edit($id)
+ {
+ $model = new Model_Category;
+ $post = $this->request->post();
+
+ if (Valid::not_empty($post['display']))
+ {
+ $model->update($id, $post); // Save the category
+ $this->request->redirect(Route::get('soapbox/admin/category')->uri(array('index' => false)));
+ }
+ else
+ {
+ Session::instance()->set('soapbox-message', Kohana::message('soapbox', 'category.error'));
+ }
+ }
+
+ /**
+ * Delete a post
+ */
+ public function action_delete()
+ {
+ $id = $this->request->param('id');
+ Model::factory('category')->delete($id);
+
+ $this->request->redirect(Route::get('soapbox/admin/category')->uri(array('action' => false)));
+ }
+
+}
View
37 classes/soapbox/model/category.php
@@ -24,6 +24,43 @@ class Soapbox_Model_Category extends Soapbox_Model
protected $fields = array('category_id','slug','display');
/**
+ * Creates a new category.
+ *
+ * @param array $data The posted data
+ * @return array array [0] insert id [1] number of rows
+ */
+ public function create(array $data)
+ {
+ $data['slug'] = str_replace(" ", "-", strtolower(trim($data['display'])));
+ return parent::create($data);
+ }
+
+ /**
+ * Creates a new category.
+ *
+ * @param int $key The category id
+ * @param array $data The posted data
+ * @return array array [0] insert id [1] number of rows
+ */
+ public function update($key, array $data)
+ {
+ $data['slug'] = str_replace(" ", "-", strtolower(trim($data['display'])));
+ return parent::update($key, $data);
+ }
+
+ /**
+ * Removes the category id with the given category id
+ *
+ * @param int $key The post id
+ * @return boolean the number of affected rows
+ */
+ public function delete($key)
+ {
+ Model_Post_Category::remove_category($key);
+ parent::delete($key);
+ }
+
+ /**
* Sets the validation for a category
*
* @param Validation $valid The current validation object
View
13 classes/soapbox/model/post/category.php
@@ -128,6 +128,19 @@ public static function link(array $category, $delimiter = ", ")
}
/**
+ * Removes all of the rows with the given category id.
+ *
+ * @param int $id The category_id to remove
+ * @return int The number of affected rows
+ */
+ public static function remove_category($id)
+ {
+ return DB::delete(static::$table)
+ ->where(Model_Category::$primary, '=', $id)
+ ->execute();
+ }
+
+ /**
* Gets the validation rules for a post category
*
* @param Validation $valid The current validation object
View
12 init.php
@@ -16,6 +16,18 @@
'id' => null
));
+// Admin categories
+Route::set('soapbox/admin/category', "{$section}/{$admin}/category(/<action>(/<id>))", array(
+ 'action' => "(|add|edit|delete)",
+ 'id' => "\d+"
+ ))
+ ->defaults(array(
+ 'directory' => "admin",
+ 'controller' => "soapbox_category",
+ 'action' => "index",
+ 'id' => null
+ ));
+
// Login
Route::set('soapbox/login', "{$section}/<action>", array('action' => "(login|logout)"))
->defaults(array(
View
4 messages/soapbox.php
@@ -16,4 +16,8 @@
'edit' => array(
'success' => "Your post has been saved!"
),
+
+ 'category' => array(
+ 'display_empty' => "Please enter the category name"
+ ),
);
View
14 views/soapbox/admin/category/form.php
@@ -0,0 +1,14 @@
+<?php if (isset($error)): ?>
+<div id="message" class="error">
+ <?php echo $error; ?>
+</div>
+<?php endif; ?>
+
+<form action="<?php echo $action; ?>" method="post" id="category-form">
+ <div>
+ <label for="display">Category</label>
+ <input type="text" name="display" value="<?php echo Arr::get($post, 'display', ""); ?>" id="display" />
+ </div>
+
+ <input type="submit" value="Submit" />
+</form>
View
37 views/soapbox/admin/category/list.php
@@ -0,0 +1,37 @@
+<?php $route = Request::current()->route(); ?>
+<h1>Administration :: Categories</h1>
+
+<?php if (isset($message)): ?>
+<div id="message" class="success">
+ <?php echo $message; ?>
+</div>
+<?php endif; ?>
+
+<div id="logout">
+ <a href="<?php echo Route::url('soapbox/login', array('action' => "logout")); ?>">Logout</a>
+</div>
+
+<div id="create">
+ <?php echo HTML::anchor($route->uri(array('action' => "add")), "Create New Category"); ?>
+</div>
+
+<table id="category-admin">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+<?php foreach ($categories as $category): ?>
+ <tr>
+ <td><?php echo $category->display; ?></td>
+ <td>
+ <?php
+ echo HTML::anchor($route->uri(array('action' => "edit", 'id' => $category->category_id)), "Edit")." | ".
+ HTML::anchor($route->uri(array('action' => "delete", 'id' => $category->category_id)), "Delete");
+ ?></td>
+ </tr>
+<?php endforeach; ?>
+ </tbody>
+</table>
View
4 views/soapbox/admin/list.php
@@ -36,4 +36,6 @@
</tr>
<?php endforeach; ?>
</tbody>
-</table>
+</table>
+
+<p><?php echo HTML::anchor(Route::get('soapbox/admin/category')->uri(array('index' => false)), "Category Management →"); ?></p>
Please sign in to comment.
Something went wrong with that request. Please try again.