<?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_post_entity extends Entity
{
	public function __construct($data)
	{
		$this->_data['id']=&$this->_data['post_id'];
		parent::__construct($data);
	}
	
	public function set($data)
	{
		parent::set($data);
		$this->isAccessEdit();
		$this->isAccessDelete();
	}

	public function __set($k, $v)
	{
		parent::__set($k, $v);
		if ($k=='user') {
			$this->_data['user_id']=$this->_data['user']->id;
		}
		if ($k=='bug') {
			$this->_data['bug_id']=&$this->_data['bug']->id;
		}
		if ($k=='msg') {
			parent::__set('msg_html', getbb($v));
			//Hook::call('bug_tracker_msg', $post->msg_html);
		}
	}

	public function isAccessEdit()
	{
		if (!isset($this->access_edit)) {
			$this->access_edit=(access('moderator') || $this->user_id==User::i()->id);
		}
		return $this->access_edit;
	}

	public function isAccessDelete()
	{
		if (!isset($this->access_delete)) {
			$this->access_delete=((access('moderator') || $this->user_id==User::i()->id) && !$this->isFirst());
		}
		return $this->access_delete;
	}

	public function isFirst()
	{
		if (!$this->id) return;
		return $this->first;
	}

	public function addFile($file)
	{
		$this->_data['files'][]=$file;
	}
}