<?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
*/
	
	if ($_SERVER['REQUEST_METHOD']=='POST' && (access('approve, perform')))
	{
		User::checkToken();
		Db::i()->query("UPDATE ?_bt_bugs SET `type`=?, 	importance=?, version_id=? WHERE bug_id=?", $_POST['type'], $_POST['importance'], $_POST['version_id'], $_GET['var1']);

		//если изменился статус, то ставим текущего пользователя
		if (Db::i()->query("UPDATE ?_bt_bugs SET `status`=? WHERE  bug_id=?", $_POST['status'], $_GET['var1']))
		{
			if ($_POST['status']==10) Db::i()->query("UPDATE ?_bt_bugs SET approver_id=? WHERE  bug_id=?", User::i()->id, $_GET['var1']);
			if ($_POST['status']==30) Db::i()->query("UPDATE ?_bt_bugs SET performer_id=? WHERE  bug_id=?", User::i()->id, $_GET['var1']);
		}
		refresh('параметры применены', LabCMS::i()->url($_GET['var1'], 'item'));
	}
	
	
	$bug_info=Db::i()->selectRow("SELECT bug_id, b.project_id, IF(b.version_id,b.version_id,-1)  version_id, IF(b.version_id=-1, 'HEAD',version) version, summary, type, author_id, a.login author, bug_date, importance, approver_id, ap.login approver, b.status, performer_id, p.login performer
													FROM ?_bt_bugs b
													JOIN ?_users a ON a.user_id=b.author_id
													LEFT JOIN ?_users ap ON ap.user_id=b.approver_id
													LEFT JOIN ?_users p ON p.user_id=b.performer_id
													LEFT JOIN ?_bt_versions v ON v.version_id=b.version_id
													WHERE bug_id=?", $_GET['var1']);
	if (!$bug_info) return LabCMS::i()->error('Ветка не найдена');
	
	$mod_template="item.tpl";
	
	$bug_info['summary_html']=getbb($bug_info['summary'], 'nolinks');
	
	addBreadcrumb($bug_info['summary_html'], LabCMS::i()->url($bug_info['bug_id'], 'item'), true);
	
	$comments=Db::i()->select("SELECT post_id AS ARRAY_KEY, msg, putdate, p.user_id, login, u.email FROM ?_bt_posts p JOIN ?_users u ON p.user_id=u.user_id WHERE bug_id=? ORDER BY putdate", $bug_info['bug_id']);
	
	$first_post=true;
	if (is_array($comments) && count($comments))
	{
		$files=Db::i()->select("SELECT file_id, file_type, post_id, file_name FROM ?_bt_files WHERE post_id IN (?a)", array_keys($comments));
		while (count($files)):
			$file=array_shift($files);
			if (in_array($file['file_type'], array('gif', 'jpg', 'jpeg', 'png'))) $comments[$file['post_id']]['images'][$file['file_id']]=array('type'=>$file['file_type'], 'name'=>$file['file_name']);
			else $comments[$file['post_id']]['files'][$file['file_id']]=array('type'=>$file['file_type'], 'name'=>$file['file_name']);
		endwhile; 
		foreach($comments as $key=>$comment)
		{
			$comments[$key]['msg_html']=getbb($comment['msg']);
			if (access('moderator') || $comment['user_id']==User::i()->id) $comments[$key]['access_edit']=1;
			if ((access('moderator') || $comment['user_id']==User::i()->id) && !$first_post) $comments[$key]['access_delete']=1;
			$first_post=false;
		}
	}
	
	$versions=Db::i()->selectCol("SELECT v.version_id AS ARRAY_KEY, version
					FROM ?_bt_versions v
					WHERE v.project_id=?
					ORDER BY v.version_id", $project_id);
	$versions[-1]='HEAD';
	
	if (access('approve,perform')) Tpl::i()->assign('all_versions', $versions);
	
	Tpl::i()->append('feed_rss', array('path'=>LabCMS::i()->url($bug_info['bug_id'], 'item', 'xml'), 'name'=>'Подписаться на тему "'.strip_tags($bug_info['summary_html']).'"'));
	
	Tpl::i()->assign('bug_info', $bug_info);
	Tpl::i()->assign('comments', $comments);
