<?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 bug_tracker_module
{
	protected $item;
	protected $version;
	protected $post;
	protected $mapper;

	public function __construct()
	{
		$this->mapper=Factory::create('bug_tracker.mapper');
	}

	/******* темы ********/
	public function getItemsByFilter($filter, $page, $atPage)
	{
		return $this->_getItem()->getItemsByFilter($filter, $page, $atPage);
	}

	public function getItemById($id)
	{
		return $this->_getItem()->getById($id);
	}

	public function getItemsById($id)
	{
		return $this->_getItem()->getByArrayId($id);
	}

	public function addItem($data)
	{
		$item=Factory::create('bug_tracker.item_entity', $data);
		$item->author=User::i()->getCurrentUser();
		$item->last_update=time();
		if (access('approve')) {
			$statuses=config::get('statuses');
			$item->status=$statuses[20];
			$item->approver=User::i()->getCurrentUser();
		}
		$this->_getItem()->add($item);
		$data['bug']=$item;
		$data['user']=User::i()->getCurrentUser();
		$this->addPost($data);
		return $item;
	}
	
	public function deleteItem($item)
	{	
		$posts=$this->getPostsByItem($item);
		if ($posts['count']) {
			$this->_getPost()->delete($posts['collection']);
		}
		return $this->_getItem()->delete($item);
	}

	public function updateItem($item)
	{
		return $this->_getItem()->update($item);
	}
	
	public function getItemAddForm()
	{
		return $this->_getPost()->buildEditForm(null, true);
	}

	/******* посты ********/
	public function getPostsByItem($item)
	{
		return $this->_getPost()->getPostsByItem($item);
	}

	public function getLastPosts()
	{
		return $this->_getPost()->getLastPosts(50);
	}

	public function getPostEntity($data=array())
	{
		return Factory::create('bug_tracker.post_entity', $data);
	}

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

	public function addPost($data)
	{
		$post=Factory::create('bug_tracker.post_entity', $data);
		$post->putdate=time();
		$this->_getPost()->add($post);

		$this->addFilesToPost($_FILES['shot'], $post);

		$post->bug->last_update=$post->putdate;
		$this->_getItem()->update($post->bug);

		$this->_addEmailMsg($post);
		
		return $post;
	}

	public function updatePost($post, $data)
	{
		if ($post->isFirst()) {
			$post->bug->summary=$data['summary'];
			$post->bug->version_id=$data['version_id'];
			$post->bug->type=$data['type'];
			$this->_getItem()->update($post->bug);
			unset($data['summary'], $data['version_id']);
		}

		if (!empty($data['del']) && $keys=array_keys($data['del'], 1)) {
			$this->_getPost()->deleteFilesByIds($post, $keys);
			unset($data['del']);
		}
		$post->set($data);
		$this->addFilesToPost($_FILES['shot'], $post);

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

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

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

	public function addFilesToPost($files, $post)
	{
		return $this->_getPost()->addFilesToPost($files, $post);
	}

	/******* версии ********/
	public function getVersionById($id)
	{
		return $this->_getVersion()->getVersionById($id);
	}

	public function getVersionEntity($data=array())
	{
		return Factory::create('bug_tracker.version_entity', $data);
	}

	public function getAllVersions()
	{
		return $this->_getVersion()->getAllVersions();
	}

	public function getSupportedVersions()
	{
		return $this->_getVersion()->getSupportedVersions();
	}

	public function getSupportedVersionsArray()
	{
		return $this->_getVersion()->getSupportedVersionsArray();
	}

	public function getAllVersionsForAdmin()
	{
		return $this->_getVersion()->getAllVersionsForAdmin();
	}

	public function getVersionEditForm($version=null)
	{
		return $this->_getVersion()->buildEditForm($version);
	}

	public function updateVersion($version)
	{
		return $this->_getVersion()->update($version);
	}

	public function addVersion($version)
	{
		return $this->_getVersion()->add($version);
	}

	public function deleteVersion($version)
	{
		return $this->_getVersion()->delete($version);
	}


	/******* служебные ********/

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

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

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

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

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

	protected function _addEmailMsg($post)
	{
		$message=array();
		$message['isHtml']=true;
		$message['exclude']=$post->user_id;
		$message['subject']='Новое сообщение в багтрекере';
		$message['from']='bugs@'.config::get('core.domain');
		$message['fromName']='Робот багтрекера';
		
		$template=LabCMS::i()->getModuleName().'/email_new_message.tpl';
		Tpl::i()->assign('post', $post);
		$message['text']=Tpl::i()->fetch($template);

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