<?php
/** 
* @package		uForum2
* @file		inc/models/ForumsModel.php
* @copyright	2007-2015 (c) PioDer <piotrek@pioder.pl>
* @link    		http://www.pioder.pl/
* @license		see LICENSE.txt
**/

class ForumsModel extends Model
{
	private $forum_info = null;
	private $cat_info = null;
	public function getForums()
	{
		return $this->select (FORUMS_VIEW);
	}
	
	public function getCats()
	{
		return $this->select (CATS_TABLE);
	}
	
	public function getForumsNames()
	{
		$out = $this->select (FORUMS_TABLE, 'forum_id, name', '', 'forum_id ASC');
		if (count($out) > 0)
			return $out;
		else
			return array();
	}
	
	public function getForum($forum_id)
	{
		if ($this->forum_info == null)
		{
			$out = $this->select(FORUMS_TABLE, '*', 'forum_id=\''.$forum_id.'\'');
			
			if (count($out) > 0)
				$this->forum_info = $out[0];
		}
		
		return $this->forum_info;
	}
	
	public function getTopics($forum_id)	
	{
		$out = $this->select(TOPICS_VIEW, '*', 'forum_id=\''.$forum_id.'\'', 'sticky DESC,lastpost_post_id DESC');
		if (count($out) > 0)
			return $out;
		else
			return array();
	}
	
	public function getCat($cat_id)
	{
		if ($this->cat_info == null)
		{
			$out = $this->select(CATS_TABLE, '*', 'category_id=\''.$cat_id.'\'');
			
			if (count($out) > 0)
				$this->cat_info = $out[0];
		}
		
		return $this->cat_info;
	}
	
	public function changeCat($cat_id, $cat_name)
	{
		$query = 'UPDATE '.CATS_TABLE.' 
		SET `name`=\''.$cat_name.'\' 
		WHERE `category_id`=\''.$cat_id.'\'';
		
		$this->db->query($query);
	}
	
	public function addCat($cat_name)
	{
		$query = 'INSERT INTO '.CATS_TABLE.' 
		(category_id, name) 
		VALUES (NULL, \''.$cat_name.'\')';
		
		$this->db->query($query);
	}
	
	public function deleteCat($cat_id)
	{
		$query = 'DELETE FROM '.CATS_TABLE.' 
		WHERE `category_id`=\''.$cat_id.'\'';
		
		$this->db->query($query);
	}
	
	public function changeForum($forum_id, $forum_name, $forum_desc, $forum_category_id, $forum_locked)
	{
		$query = 'UPDATE '.FORUMS_TABLE.' 
		SET `name`=\''.$forum_name.'\', 
		`desc`=\''.$forum_desc.'\', 
		`category_id`=\''.$forum_category_id.'\', 
		`locked`=\''.$forum_locked.'\' 
		WHERE `forum_id`=\''.$forum_id.'\'';
		
		$this->db->query($query);
	}
	
	public function addForum($forum_name, $forum_desc, $forum_category_id, $forum_locked)
	{
		$query = 'INSERT INTO '.FORUMS_TABLE.' 
		(`forum_id`, `name`, `desc`, `category_id`, `locked`) 
		VALUES (NULL, \''.$forum_name.'\', \''.$forum_desc.'\', \''.$forum_category_id.'\', \''.$forum_locked.'\')';
		
		$this->db->query($query);
	}
	
	public function deleteForum($forum_id)
	{
		$query = 'DELETE FROM '.FORUMS_TABLE.' 
		WHERE `forum_id`=\''.$forum_id.'\'';
		
		$this->db->query($query);
	}
}
?>