Skip to content

Commit

Permalink
Implemented categories
Browse files Browse the repository at this point in the history
* New function: blog_getPostsByCategory()
* New function: blog_getCategoryId()
* New function: blog_getCategories()
* New function: ORM::getLastInsertId()
* Put the categories template var in a "global scope"
  • Loading branch information
Mayhem93 committed Feb 29, 2012
1 parent 08f29e0 commit 9cdfab6
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 23 deletions.
59 changes: 59 additions & 0 deletions internal/Posts.php
Expand Up @@ -44,6 +44,14 @@ function blog_addPost($title, $content, $category, $pinned = false) {
"date_posted" => date("d F Y, g:i:s a")
);
$database->insertRow("post", $row);
$post_id = $database->getLastInsertID();

$category_row = array(
"post_id" => $post_id,
"category_id" => blog_getCategoryId($category)
);

$database->insertRow("category_map", $category_row);
}
else {
if(!is_writable(POSTS_DIR))
Expand Down Expand Up @@ -180,6 +188,24 @@ function blog_getPosts($page=1) {
}
}

/**
* Get's the first posts specified by category
* @param string $category Category name.
* @param int $page A segment of posts.
*/
function blog_getPostsByCategory($category, $page=1) {
$database = SBFactory::Database();
$nr_posts = SBFactory::Settings()->getSetting("no_posts_per_page");

if (SBFactory::Settings()->getSetting("database_support")) {
$query = "SELECT * FROM `post` WHERE `category` LIKE '".$category."%' ORDER BY `id` DESC LIMIT ".(($page-1)*$nr_posts).", ".$nr_posts;

return $database->query($query);
} else {
//TODO
}
}

/**
* Gets a single post
* @param int $id The post ID. For no-mysql support this is the filename of the post (which is a unix timestamp).
Expand Down Expand Up @@ -244,6 +270,39 @@ function blog_postIsPinned($id) {
else
return file_exists(POSTS_DIR."/pinned/{$id}.json");
}

/**
* Returns category ID.
* @param string $category_name Category name.
* @return mixed The resulted id, or false if it doesn't exist.
*/
function blog_getCategoryId($category_name) {
$database = SBFactory::Database();

if(SBFactory::Settings()->getSetting("database_support")) {
$filter = array("name" => $category_name);

$result = $database->selectRows("category", "*", $filter);
return count($result) ? $result[0]['id'] : false;
}
else {
//TODO
}
}

function blog_getCategories() {
$database = SBFactory::Database();

if(SBFactory::Settings()->getSetting("database_support")) {
$result = $database->selectRows("category", "*", $filter);

return count($result) ? $result : false;
}
else {
//TODO
}
}

/**
* Gets all the comments from a post.
* @param int $postid The post ID. For no-mysql support this is the filename of the post (which is a unix timestamp).
Expand Down
2 changes: 1 addition & 1 deletion internal/SBDatabase.class.php
Expand Up @@ -143,7 +143,7 @@ public function updateRows($table, array $where, array $update_set) {
* @return Associative array with the results.
*/
public function query($query) {
$query = $this->_escapeSQL($query);
//$query = $this->_escapeSQL($query);

$results = parent::for_table("")->raw_query($query, array())->find_many();

Expand Down
7 changes: 6 additions & 1 deletion libs/idiorm.php
Expand Up @@ -140,6 +140,7 @@ class ORM {
// Name of the column to use as the primary key for
// this instance only. Overrides the config settings.
protected $_instance_id_column = null;
protected static $_last_insert_id = null;

// ---------------------- //
// --- STATIC METHODS --- //
Expand Down Expand Up @@ -291,6 +292,10 @@ public static function get_last_query() {
public static function get_query_log() {
return self::$_query_log;
}

public static function getLastInsertID() {
return self::$_last_insert_id;
}

// ------------------------ //
// --- INSTANCE METHODS --- //
Expand Down Expand Up @@ -1066,7 +1071,7 @@ public function save() {
if ($this->_is_new) {
$this->_is_new = false;
if (is_null($this->id())) {
$this->_data[$this->_get_id_column_name()] = self::$_db->lastInsertId();
self::$_last_insert_id = $this->_data[$this->_get_id_column_name()] = self::$_db->lastInsertId();
}
}

Expand Down
19 changes: 18 additions & 1 deletion public_html/index.php
Expand Up @@ -42,6 +42,7 @@
session_start();
$js_files = array();
$plugin_manager = SBFactory::PluginManager();
SBFactory::Template()->assign("categories", blog_getCategories());

switch ($_GET['action']) {
case 'show':
Expand All @@ -65,11 +66,26 @@

SBFactory::Template()->assign("post", $post);
SBFactory::Template()->assign("comments", $comments);
SBFactory::Template()->assign("page_title", $post['title']." - ".SBFactory::Settings()->getSetting("blog_title"));
SBFactory::Template()->assign("page_title", $post['title']." - ".
SBFactory::Settings()->getSetting("blog_title"));
SBFactory::Template()->assign("page_template", "post_page.tpl");

break;

case 'category':
$category = urldecode($_GET['name']);
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$post = blog_getPostsByCategory($category, $page);
//var_dump($post); exit();

SBFactory::Template()->assign("blog_posts", $post);
SBFactory::Template()->assign("page", $page);
SBFactory::Template()->assign("page_title", $category. " Category - ".
SBFactory::Settings()->getSetting("blog_title"));
SBFactory::Template()->assign("page_template", "main.tpl");

break;

case 'addpost':
if (!smarty_isAdminSession())
setHTTP(HTTP_UNAUTHORIZED);
Expand All @@ -85,6 +101,7 @@
$pinned = isset($_POST['pinned']);
blog_addPost($_POST['post_title'], $_POST['post_content'], $_POST['category'], $pinned);


redirectMainPage();
}
break;
Expand Down
17 changes: 17 additions & 0 deletions public_html/install/database.sql
Expand Up @@ -26,4 +26,21 @@ CREATE TABLE IF NOT EXISTS `comment` (
COLLATE='utf8_general_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `category` (
`id` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` TINYTEXT NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `category_map` (
`post_id` INT(10) UNSIGNED NOT NULL,
`category_id` SMALLINT(5) UNSIGNED NOT NULL
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=1;
12 changes: 11 additions & 1 deletion public_html/templates/addPost.tpl
Expand Up @@ -3,7 +3,17 @@
<form name="addPost" action="?action=addpost" method="post">
<ul class="forms">
<li><label for="post_title">Post title: </label><input name="post_title" type="text" /></li>
<li><label for="category">Category: </label><input name="category" type="text" /></li>
<li><label for="category">Category: </label>
<select name="category">
{if $categories}
{foreach $categories as $cat}
<option value="{$cat.name}">{$cat.name}</option>
{/foreach}
{else}
<option style="color: gray">no categories available</option>
{/if}
</select>
</li>
<li><label for="pinned">Pinned: </label><input type="checkbox" name="pinned" /></li>
<li><textarea name="post_content"></textarea></li>
<li><input name="submit_post" type="submit" value="Post" /><li>
Expand Down
2 changes: 1 addition & 1 deletion public_html/templates/bits/post.tpl
Expand Up @@ -10,7 +10,7 @@
</div>
{/if}
<div class="entry-meta">
<span class="author vcard">Category: {$post.category} | </span> <span
<span class="author vcard">Category: <a href="?action=category&name={urlencode($post.category)}">{$post.category}</a> | </span> <span
class="onDate">{$post.date_posted} | </span> <span class="onDate">{blog_getCommentsNumber({$post.id})}
Comment(s)</span>
</div>
Expand Down
31 changes: 13 additions & 18 deletions public_html/templates/index.tpl
Expand Up @@ -40,12 +40,9 @@
<div class="widget PageList" id="PageList1">
<div>
<ul>
<li class="page_item"><a
href="/">Home</a></li>
<li class="page_item"><a
href="">About</a></li>
<li class="page_item"><a
href="">Contact</a></li>
<li class="page_item"><a href="/">Home</a></li>
<li class="page_item"><a href="">About</a></li>
<li class="page_item"><a href="">Contact</a></li>
</ul>
<div class="clear"></div>

Expand All @@ -69,16 +66,13 @@
<div id="socials"></div>
<div style="clear: both;"></div>
<div id="container">
<div id="content">
{include file=$page_template}
</div>
<div id="content">{include file=$page_template}</div>
<!-- #content -->
</div>
<!-- #container -->
<div class="widget-area" id="primary">
<div class="widget-container widget_search" id="search">
<form action=""
id="searchform" method="get">
<form action="" id="searchform" method="get">
<input id="s" name="q" type="text" value="Search" /> <input
id="searchsubmit" type="submit" value="OK" />
</form>
Expand All @@ -97,9 +91,7 @@
<ul>
<li>
<div class="item-content">
<div class="item-thumbnail">
empty-thumbnail
</div>
<div class="item-thumbnail">empty-thumbnail</div>
<div class="item-title">
<a href="">Post title</a>
</div>
Expand All @@ -116,10 +108,13 @@
<h2>Categories</h2>
<div class="widget-content list-label-widget-content">
<ul>
<li><a dir="ltr" href=""
title="View all posts filed under Lorem 1">Category</a>
<span dir="ltr">(no#)</span>
</li>
{if $categories}
{foreach $categories as $cat}
<li><a href="?action=category&name={urlencode($cat.name)}">{$cat.name}</a></li>
{/foreach}
{else}
<li>No categories.</li>
{/if}
</ul>
<div class="clear"></div>
<div class="clear"></div>
Expand Down

0 comments on commit 9cdfab6

Please sign in to comment.