<?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_version extends DefaultObject
{
	protected $cache_key='bt_version_';
	public function __construct($mapper=null)
	{
		if (is_object($mapper)) {
			$this->mapper=$mapper;
		} else {
			$this->mapper=Factory::create('bug_tracker.mapper');
		}
		Factory::load('bug_tracker.version_entity');
	}
	
	public function getAllVersions()
	{
		$key='bug_tracker_all_versions';
		if (false === ($data=TCache::get($key))) {
			$data = array(
				'collection'=>$this->mapper->getAllVersions($count),
				'count'=>$count
			);
			TCache::set($data, $key, array($this->_getCacheKeyCommon()));
		}
		$data['collection'] = $this->getVersionsByArrayId($data['collection']);
		return $data;
	}

	public function getAllVersionsForAdmin()
	{
		$data = array(
				'collection'=>$this->mapper->getAllVersions($count),
				'count'=>$count
			);
		$data['collection'] = $this->getVersionsByArrayId($data['collection']);
		if ($result=$this->mapper->isVersionsHasItems(array_keys($data['collection']))) {
			foreach ($data['collection'] as $version) {
				$version->has_items=isset($result[$version->id]);
			}
		}
		return $data;
	}

	public function getSupportedVersions()
	{
		$key='bug_tracker_all_supported_versions';
		if (false === ($data=TCache::get($key))) {
			$data = array(
				'collection'=>$this->mapper->getSupportedVersions($count),
				'count'=>$count
			);
			TCache::set($data, $key, array($this->_getCacheKeyCommon()));
		}
		$data['collection'] = $this->getVersionsByArrayId($data['collection']);
		return $data;
	}

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

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

	public function getVersionsByArrayId($id)
	{
		return $this->_getItemsByArrayId($id);
	}

	protected function _getItemsByArrayIdFromStorage($id)
	{
		return $this->mapper->getVersionsByArrayId($id);
	}

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

	public function delete($version)
	{
		TCache::remove($this->_getCacheKey().$version->id);
		TCache::clean('MATCH_TAG', array($this->_getCacheKeyCommon()));
		return $this->mapper->deleteVersion($version);
	}
	

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

	public function buildEditForm($version=null)
	{
		$form = Factory::create('HTML_QuickForm2', null, 'version', array('action'=>''));

		if ($version) {
			$form->addDataSource(Factory::create('HTML_QuickForm2_DataSource_Array', $version->getArray()));
		}

		$token=$form->addElement('hidden', 'token', array('value' => User::i()->token));
		
		$name=$form->addElement('text', 'name', null, array('label' => _m('Номер версии')));
		$unsupported=$form->addElement('checkbox', 'unsupported', null, array('label' => _m('Снаята с поддержки')));

		$name->addRule('required', _m('Номер не может быть пустым'), null, $this->validation);
		
		$form->addElement('submit', null, array('value'=>_m('Сохранить')));

		return $form;
	}
}