<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class guest_module
{
	protected $post;

	public function getPostsByPage($perPage=10, $page=1)
	{
		return $this->getPost()->getPostsbyPage($perPage, $page);
	}

	public function updatePost($post, $data)
	{
		$post->msg=trim($data['msg']);
		if (isset($data['answer'])) $post->answer=trim($data['answer']);
		
		if (!$post->user_id) {
			$post->name=$data['name'];
			$post->email=$data['email'];
		}

		return $this->getPost()->update($post);
	}

	public function addPost($data)
	{
		$post=$this->getPostEntity($data);
		$post->msg=trim($data['msg']);
		$post->ip=User::getIP();
		$post->putdate=time();
		
		if (User::i()->isNotGuest()) $post->user=User::i();
		if (User::i()->isGuest()) $post->name=trim($data['name']);
		if (User::i()->isGuest()) $post->email=trim($data['email']);
		
		if (isset($data['answer'])) $post->answer=trim($data['answer']);
		return $this->getPost()->add($post);
	}

	public function deletePost($post)
	{
		return $this->getPost()->delete($post);
	}

	public function getPostEditForm($post=null)
	{
		return $this->getPost()->buildEditForm($post);
	}

	public function getPostById($id)
	{
		return $this->getPost()->getPostById($id);
	}

	public function getPostEntity()
	{
		return $this->getPost()->getPostEntity();
	}

	public function getPost()
	{
		if (!is_object($this->post)) $this->post=Factory::create('guest.post');
		return $this->post;
	}

	public function getPager($count, $page, $perPage)
	{
		return Factory::create('Paginator', $count, $perPage, $page)->getPages();
	}
}