Location: PHPKode > projects > Mivec Framework > mivec/demo/managers/WeblogManager.class.php
<?php

require_once "demo/GlobalConfig.inc.php";
require_once "demo/managers/DbConnectionManager.class.php";

class WeblogManager
{
	function addWeblogEntry($title, $content, $categories = null)
	{
		$connection = DbConnectionManager::openDbConnection();
		mysql_query("INSERT INTO weblog_entries (title, time, content) VALUES ('$title', NOW(), '$content')", $connection);
		$newEntryID = mysql_insert_id($connection);
		if(($categories != null) && (is_array($categories)))
		{
			foreach($categories as $catID)
			{
				mysql_query("INSERT INTO weblog_entry_categories(entryID, categoryID) VALUES ('$newEntryID', '$catID')");
			}
		}
		DbConnectionManager::closeDbConnection($connection);
	}
	
	function updateWeblogEntry($entryID, $title, $content)
	{
		$connection = DbConnectionManager::openDbConnection();
		mysql_query("UPDATE weblog_entries SET title = '$title', content = '$content' WHERE entryID = '$entryID'", $connection);
		DbConnectionManager::closeDbConnection($connection);		
	}
	
	function removeWeblogEntry($entryID)
	{
		$connection = DbConnectionManager::openDbConnection();
		mysql_query("DELETE FROM weblog_entries WHERE entryID = '$entryID'", $connection);
		mysql_query("DELETE FROM weblog_entry_categories WHERE entryID = '$entryID'", $connection);
		DbConnectionManager::closeDbConnection($connection);		
	}
	
	function getWeblogEntries($offset = 0)
	{
		$connection = DbConnectionManager::openDbConnection();
		$result = mysql_query("SELECT entryID, title, UNIX_TIMESTAMP(time) as time, content FROM weblog_entries ORDER BY time, entryID LIMIT $offset, 15");
		$entries = array();
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{
				$entries[] = array(
					"entryID" => $row["entryID"],
					"title" => $row["title"],
					"time" => $row["time"],
					"content" => $row["content"],
					"categories" =>	WeblogManager::getCategoryNames($row["entryID"], $connection));
			}
			mysql_free_result($result);
		}
		DbConnectionManager::closeDbConnection($connection);
		return $entries;
	}
	
	function getWeblogEntry($entryID)
	{
		$connection = DbConnectionManager::openDbConnection();
		$result = mysql_query("SELECT * FROM weblog_entries WHERE entryID = '$entryID'");
		$entry = array();
		if(($result) && (mysql_num_rows($result) > 0))
		{
			$entry = array(
				"entryID" => mysql_result($result, 0, "entryID"),
				"title" => mysql_result($result, 0, "title"),
				"time" => mysql_result($result, 0, "time"),
				"content" => mysql_result($result, 0, "content"),
				"categories" =>	WeblogManager::getCategoryNames(mysql_result($result, 0, "entryID"), $connection));
		}
		mysql_free_result($result);
		DbConnectionManager::closeDbConnection($connection);
		return $entry;		
	}
	
	function getWeblogLastBuildDate()
	{
		$connection = DbConnectionManager::openDbConnection();
		$result = mysql_query("SELECT time FROM weblog_entries ORDER BY time DESC LIMIT 0, 1");
		$time = "";
		if($result)
		{
			$time = mysql_result($result, 0, "time");
			mysql_free_result($result);
		}
		DbConnectionManager::closeDbConnection($connection);
		return $time;
	}
	
	function getAvailableCategories()
	{
		$connection = DbConnectionManager::openDbConnection();
		$result = mysql_query("SELECT * FROM categories ORDER BY categoryName");
		$categories = array();
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{
				$categories[] = array("id" => $row["categoryID"], "categoryName" => $row["categoryName"]);
			}
			mysql_free_result($result);
		}
		DbConnectionManager::closeDbConnection($connection);
		return $categories;
	}
	
	function getCategoryNames($entryID, $connection)
	{
		$result = mysql_query("SELECT categories.categoryName FROM categories, weblog_entry_categories WHERE weblog_entry_categories.categoryID = categories.categoryID AND weblog_entry_categories.entryID = '$entryID' ORDER BY categories.categoryName");
		if($result)
		{
			$categoryNames = array();
			while($row = mysql_fetch_array($result))
			{
				$categoryNames[] = $row["categoryName"];
			}
			mysql_free_result($result);
		}
		return $categoryNames;
	}
}

?>
Return current item: Mivec Framework