<?php
/*
* News Module
* @package: PerfCMS
*/

class IndexController extends Controller
{
	public function actionIndex()
	{
		$db = PerfDb::init();
		if(isset($_GET['add']))
		{
			$text = Filters::input($_POST['text']);
			if(!empty($text))
			{
				$user_id	= User::Id();
				$time		= time();
				$db->query("INSERT INTO `minichat` SET `text` = '$text', `user_id` = '$user_id', `time` = '$time'");
				// print_r($db->errorInfo());
				$this->redirect('/minichat/index');
			}
		}
		elseif(isset($_GET['clear']) && User::level() > 1)
		{
			$db->query("TRUNCATE TABLE `minichat`");
			$this->redirect('/minichat/index');
		}
		elseif(isset($_GET['delete_message']) && !empty($_GET['this_id']) && User::level() > 1)
		{
			$this_id = Filters::num($_GET['this_id']);
			$db->query("DELETE FROM `minichat` WHERE `id` = '$this_id' LIMIT 1");
			$this->redirect('/minichat/index');
		}
		$this->getHeader(array('title' => Lang::get('minichat_title', 'minichat')), '/minichat::minichat_location');
		$mchatNum = $db->query("SELECT * FROM `minichat`")->rowCount();
		$pages = new Paginator($mchatNum, System::pages());
		global $start;
		$mchatArray = $db->query("SELECT * FROM `minichat` ORDER BY time DESC LIMIT $start, ".System::pages()."");
		$this->render('main', array('pages' => $pages, 'array' => $mchatArray));
		$this->getFooter();
	}
}