<?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
*/
	$mod_template="bugs_list.tpl";
	
	$order		=	intval($_GET['var2']);
	$bug_type	=	intval($_GET['var3']);
	$importance	=	intval($_GET['var4']);
	$status		=	intval($_GET['var5']);
	$version_id	=	intval($_GET['var6']);
	
	Tpl::i()->assign('filter', array('order'=>$order,
											'bug_type'=>$bug_type,
											'importance'=>$importance,
											'status'=>$status,
											'version_id'=>$version_id
											));

	
	$orderby=array(
	0=>'b.status, importance DESC, `type`, bug_date DESC',
	1=>'bug_id, b.status, importance DESC, `type`, bug_date DESC',
	2=>'bug_id DESC, b.status, importance DESC, `type`, bug_date DESC',
	3=>'summary, b.status, importance DESC, `type`, bug_date DESC',
	4=>'summary DESC, b.status, importance DESC, `type`, bug_date DESC',
	5=>'author, b.status, importance DESC, `type`, bug_date DESC',
	6=>'author DESC, b.status, importance DESC, `type`, bug_date DESC',
	7=>'bug_date, b.status, importance DESC, `type`',
	8=>'bug_date DESC, b.status, importance DESC, `type`',
	9=>'version, b.status, importance DESC, `type`, bug_date DESC',
	10=>'version DESC, b.status, importance DESC, `type`, bug_date DESC',
	11=>'approver, b.status, importance DESC, `type`, bug_date DESC',
	12=>'approver DESC, b.status, importance DESC, `type`, bug_date DESC',
	13=>'performer, b.status, importance DESC, `type`, bug_date DESC',
	14=>'performer DESC, b.status, importance DESC, `type`, bug_date DESC',
	15=>'`type`, b.status, importance DESC, bug_date DESC',
	16=>'`type` DESC, b.status, importance DESC, bug_date DESC',
	);
	
	$order=array_key_exists($order, $orderby)?$orderby[$order]:$orderby[0];
	
	$versions=Db::i()->selectCol("SELECT version_id AS ARRAY_KEY, version FROM ?_bt_versions WHERE project_id=? ORDER BY version_id", $project_id);
	$versions[-1]='HEAD';
	Tpl::i()->assign('versions', $versions);
	
	$count=Db::i()->selectCell("SELECT COUNT(bug_id) FROM ?_bt_bugs
										WHERE project_id=?
										{AND `type`=?}
										{AND importance=?}
										{AND `status`=?}
										{AND version_id=?}",
										$project_id, 
										($bug_type?$bug_type:DBSIMPLE_SKIP),
										($importance?$importance:DBSIMPLE_SKIP),
										($status?$status:DBSIMPLE_SKIP),
										($version_id?$version_id:DBSIMPLE_SKIP));
	
	$pager=new Paginator($count, LabConfig::$mcfg['bugs_at_page'], $_GET['var1']);
	$pager=$pager->getPages();
	
	$bugs=Db::i()->select("SELECT bug_id, summary, UNIX_TIMESTAMP(bug_date) as bug_date, `type`, IF(b.version_id=-1, 'HEAD', version) version, author_id, a.login as author, approver_id, ap.login as approver, b.status, performer_id, p.login as performer, importance
										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 b.version_id=v.version_id
										WHERE b.project_id=?
										{AND `type`=?}
										{AND importance=?}
										{AND b.`status`=?}
										{AND b.version_id=?}
										ORDER BY ".$order."
										LIMIT ?d, ?d", $project_id, 
										($bug_type?$bug_type:DBSIMPLE_SKIP),
										($importance?$importance:DBSIMPLE_SKIP),
										($status?$status:DBSIMPLE_SKIP),
										($version_id?$version_id:DBSIMPLE_SKIP),
										$pager['sql_start'], $pager['sql_limit']);
	if (is_array($bugs))
	foreach($bugs as $key=>$bug)
	{
		$bugs[$key]['summary_html']=getbb($bug['summary'], 'nolinks');
	}
	
	Tpl::i()->assign('bugs', $bugs);
	Tpl::i()->assign('pager', $pager);
	