<?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 forum_module
{
	protected $forum;
	protected $theme;
	protected $post;
	protected $mapper;


	public function __construct()
	{
		$this->mapper=Factory::create('forum.mapper');
	}
	/******* форумы ********/
	public function getAllForums()
	{
		return $this->_getForum()->getAllForums();
	}

	public function getForumById($id)
	{
		return $this->_getForum()->getForumById($id);
	}
	
	public function addForum($data)
	{
		$forum=Factory::create('forum.forum_entity', $data);
		return $this->_getForum()->add($forum);
	}
	
	public function updateForum($forum, $data)
	{
		$forum->set($data);
		return $this->_getForum()->update($forum);
	}

	public function deleteForum($forum)
	{
		return $this->_getForum()->delete($forum);
	}

	public function getEditForumForm($forum=null)
	{
		return $this->_getForum()->buildEditForumForm($forum);
	}
	/******* темы ********/
	public function getThemeById($id)
	{
		return $this->_getTheme()->getThemeById($id);
	}

	public function getThemesByForum($forum, $page, $atPage)
	{
		return $this->_getTheme()->getThemesByForum($forum, $page, $atPage);
	}

	public function getThemesByHidden($page, $atPage)
	{
		$filter=array(
			'show_only_hidden'=>true,
			'show_hidden'=>true
		);
		return $this->_getTheme()->getThemesByFilter($filter, $page, $atPage);
	}

	public function getThemesByUser($user, $page, $atPage)
	{
		$filter=array(
			'show_hidden'=>access('admin'),
			'user_id'=>$user->id
		);
		return $this->_getTheme()->getThemesByFilter($filter, $page, $atPage);
	}

	public function getThemesByString($string, $forum=null, $page, $atPage)
	{
		$filter=array(
			'show_hidden'=>access('admin'),
			'string'=>$string
		);
		if ($forum) $filter['forum_id']=$forum->id;

		return $this->_getTheme()->getThemesByString($string, $page, $atPage, $filter);
	}

	public function addTheme($data)
	{
		$data['author']=$data['user'];
		$theme=Factory::create('forum.theme_entity', $data);
		$this->_getTheme()->add($theme);
		$data['theme']=$theme;
		$data['putdate']=time();
		$data['first']=1;
		$post=Factory::create('forum.post_entity', $data);
		$this->_getPost()->add($post);
		return $theme;
	}

	public function deleteTheme($theme)
	{
		return $this->_getTheme()->delete($theme);
	}

	public function toggleTheme($theme)
	{
		return $this->_getTheme()->toggle($theme);
	}

	public function hideTheme($theme)
	{
		return $this->_getTheme()->hide($theme);
	}

	public function unhideTheme($theme)
	{
		return $this->_getTheme()->unhide($theme);
	}

	public function getExtractForm()
	{
		return $this->_getTheme()->buildExtractForm();
	}

	public function extractTheme($data, $post)
	{
		return $this->_getTheme()->extract($data, $post);
	}

	public function getAddThemeForm()
	{
		return $this->_getPost()->buildAddThemeForm();
	}

	/******* посты ********/
	public function getPostsByTheme($theme, $page, $atPage)
	{
		return $this->_getPost()->getPostsByTheme($theme, $page, $atPage);
	}

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

	public function getLastPosts($page, $atPage)
	{
		$filter=array(
			'show_hidden'=>access('admin'),
			'order'=>'putdate DESC'
		);

		$posts=$this->_getPost()->getPostsByFilter($filter,  $page, $atPage);
		foreach ($posts['collection'] as $key=>$post) {
			if (!access('read_closed') && $post->theme->forum->closed) unset($data['collection'][$key]);
		}
		return $posts;
	}

	public function getPostsByHidden($page, $atPage)
	{
		$filter=array(
			'show_only_hidden'=>true,
			'show_hidden'=>true
		);
		return $this->_getPost()->getPostsByFilter($filter, $page, $atPage);
	}

	public function getPostsByUser($user, $page, $atPage)
	{
		$filter=array(
			'show_hidden'=>access('admin'),
			'user_id'=>$user->id
		);
		return $this->_getPost()->getPostsByFilter($filter, $page, $atPage);
	}

	public function getLastPostsOfTheme($theme, $page, $atPage)
	{
		$filter=array(
			'theme_id'=>$theme->id,
			'order'=>'putdate DESC'
		);

		$posts=$this->_getPost()->getPostsByFilter($filter,  $page, $atPage);
		$posts['collection']=array_reverse($posts['collection']);
		return $posts;
	}

	public function addPost($data)
	{
		$post=Factory::create('forum.post_entity', $data);
		$this->_getPost()->add($post);
		$this->_addEmailMsg($post);
		return $post;
	}
	public function updatePost($post, $data)
	{
		$post->msg=$data['msg'];
		if ($post->user->isGuest()) {
			$post->user_name=$data['user_name'];
			$post->email=$data['email'];
		}
	
		if (!$post->isAuthor()) $post->edited=User::i()->id;
		
		$this->_getPost()->update($post);

		if ($post->first) {
			if ($data['subject']!=$post->theme->subject) {
				$post->theme->subject=$data['subject'];
				$this->_getTheme()->update($post->theme);
			}
			if (access('moderator') && $data['forum_id']!=$post->theme->forum->id) {
				if (!$forum=$this->getForumById($data['forum_id'])) return;
				$this->_getTheme()->moveToForum($post->theme, $forum);
			}
		}
	}

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

	public function hidePost($post)
	{
		return $this->_getPost()->hide($post);
	}

	public function unhidePost($post)
	{
		return $this->_getPost()->unhide($post);
	}

	public function votePost($post, $vote)
	{
		return $this->_getPost()->vote($post, $vote);
	}

	public function getEditPostForm($post=null)
	{
		return $this->_getPost()->buildEditPostForm($post);
	}


	public function getAddPostFormQuote($post)
	{
		return $this->_getPost()->buildPostQuoteForm($post);
	}

	public function getVoteType()
	{
		return $this->_getPost()->vote_type;
	}

	/******* служебные ********/
	public function getPager($count, $page, $perPage)
	{
		return Factory::create('Paginator', $count, $perPage, $page)->getPages();
	}

	protected function _getForum()
	{
		if (!is_object($this->forum)) $this->forum=Factory::create('forum.forum', $this->mapper);
		return $this->forum;
	}

	protected function _getTheme()
	{
		if (!is_object($this->theme)) $this->theme=Factory::create('forum.theme', $this->mapper);
		return $this->theme;
	}

	protected function _getPost()
	{
		if (!is_object($this->post)) $this->post=Factory::create('forum.post', $this->mapper);
		return $this->post;
	}

	protected function _addEmailMsg($post)
	{
		$message=array();
		$message['isHtml']=true;
		if ($post->user->isNotGuest()) $message['exclude']=$post->user->id;
		$message['from']='forum@'.config::get('core.domain');
		$message['fromName']='Робот форума';

		if ($post->first) {
			$message['subject'] = 'Новая тема в форуме '.config::get('core.domain');
			$template=LabCMS::i()->getModuleName().'/email_notify_new.tpl';
		} else {
			$message['subject'] = 'Новый ответ форуме '.config::get('core.domain');
			$template=LabCMS::i()->getModuleName().'/email_notify_reply.tpl';
		}

		Tpl::i()->assign('post', $post);
		$message['text']=Tpl::i()->fetch($template);

		$subscriber=Factory::create('Subscriber');
		$subscriber->addMessage($message, $post->theme->getSubsribeId());

		if (config::get('forum_email_notify') && config::get('core.admin_mail')) {
			$mail=Factory::create('Mailer', config::get('core.admin_mail'));
			$mail->From = $message['from'];
			$mail->FromName = $message['fromName'];
			$mail->AddReplyTo(
				$message['from'],
				"Робот форума"
			);
			$mail->WordWrap = 80;
			
			$mail->Subject = $message['subject'];
			$mail->Body=$message['text'];

			$mail->Send();
		}
	}

}