<?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_forum extends DefaultObject
{
	protected $cache_key='forum_forum_';

	public function __construct($mapper=null)
	{
		if (is_object($mapper)) {
			$this->mapper=$mapper;
		} else {
			$this->mapper=Factory::create('forum.mapper');
		}
		Factory::load('forum.forum_entity');
	}

	public function add($forum)
	{
		$this->mapper->addForum($forum);
		TCache::clean('MATCH_TAG', array($this->_getCacheKeyCommon()));
	}

	public function update($forum)
	{
		$this->mapper->updateForum($forum);
		TCache::remove($this->_getCacheKey().$forum->id);
		TCache::clean('MATCH_TAG', array($this->_getCacheKeyCommon()));
	}

	public function delete($forum)
	{
		$this->_getTheme()->hideThemesByForum($forum);
		$this->updateStat($forum, -$forum->total_posts, -$forum->total_themes);
		$this->_getTheme()->deleteThemesByForum($forum);
		$this->mapper->deleteForum($forum);
		TCache::remove($this->_getCacheKey().$forum->id);
		$this->resetCommonCache();
	}

	public function getAllForums()
	{
		$key='forum_all_forums';
		if (false === ($data=TCache::get($key))) {
			$data = array(
				'collection'=>$this->mapper->getAllForums($count),
				'count'=>$count
			);
			TCache::set($data, $key, array($this->_getCacheKeyCommon()));
		}

		$data['collection'] = $this->getForumsByArrayId($data['collection']);

		return $data;
	}
	public function getAllForumsArray()
	{
		$data=array();
		if ($result=$this->getAllForums()) {
			foreach ($result['collection'] as $item) {
				$data[$item->id]=$item->name;
			}
		}
		return $data;
	}

	public function getForumById($id)
	{
		$data=$this->getForumsByArrayId(array($id));
		if (isset($data[$id])) {
			return $data[$id];
		}
		return null;
	}

	public function getForumsByArrayId($id)
	{
		$data=$this->_getItemsByArrayId($id);
		if ($data) {
			$this->_setAddtitionalData($data);
		}
		return $data;
	}

	public function _getItemsByArrayIdFromStorage($id)
	{
		return $this->mapper->getForumsByArrayId($id);
	}

	protected function _setAddtitionalData(&$forums)
	{
		$users=array();
		foreach ($forums as $forum) {
			$users[]=$forum->last_user_id;
		}

		//добавляем пользователей
		if ($users=User::i()->getUsersByIds($users)) {
			foreach ($forums as $forum) {
				$user_data=array('login'=>$forum->last_user_name);
				$forum->last_user=$this->getUserObj($forum->last_user_id, $users, $user_data);
			}
		}
	}

	public function buildEditForumForm($forum=null)
	{
		$form = Factory::create('HTML_QuickForm2', 'edit_forum', 'post', array('action'=>''));
		$token=$form->addElement('hidden', 'token', array('value' => User::i()->token));
		
		$name=$form->addElement('text', 'name', null, array('label' => _m('Название форума:')));
		$describe=$form->addElement('textarea', 'describe', null, array('label' => _m('Описание форума:')));
		$closed=$form->addElement('checkbox', 'closed', null, array('label' => _m('Закрытый форум')));
		
		$name->addRule('required', _m('Название форума не может быть пустым'), null, $this->validation);

		if ($forum) {
			$form->addElement('submit', null, array('value'=>_m('Сохранить')));
			$form->addDataSource(
				Factory::create('HTML_QuickForm2_DataSource_Array', $forum->getArray())
			);			
		} else {
			$form->addElement('submit', null, array('value'=>_m('Изменить')));
		}

		return $form;
	}

	public function updateStat($forum, $count_post, $count_theme=0, $last_user=null, $last_post_date=null)
	{
		if (!is_object($last_user)) {
			if($theme=$this->_getTheme()->getLastThemeByForum($forum)) {
				$last_user=$theme->last_user;
				$last_post_date=$theme->last_post_date;
			}
		}

		$forum->total_posts+=$count_post;
		$forum->total_themes+=$count_theme;
		$forum->last_user=$last_user;
		$forum->last_post_date=$last_post_date;
		$this->update($forum);
	}

	public function resetCommonCache()
	{
		TCache::clean('MATCH_TAG', array($this->_getCacheKeyCommon()));
	}

	protected function _getTheme()
	{
		return Factory::create('forum.theme');
	}

	protected function _getPost()
	{
		return Factory::create('forum.post');
	}
}