<?php
/*=====================================================================*\
|| ###################################################################
|| # Bugdar
|| # Copyright (c)2004-2009 Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version 2 of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/


if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'manage';
}

$focus[ ($_REQUEST['do'] == 'handle' ? 'showreport' : 'favorites') ] = 'focus';

$fetchtemplates = array(
	'favorites',
	'trackerhome_bits',
	'list_head'
);

require_once('./global.php');
require_once('./includes/class_sort.php');

// ###################################################################

if ($_REQUEST['do'] == 'handle')
{
	$input->inputClean('bugid', TYPE_UINT);
	$bug = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . $input->in['bugid']);
	if (!check_bug_permissions($bug))
	{
		$message->errorPermission();
	}

	if (!can_perform('cansubscribe', $bug['product']))
	{
		$message->errorPermission();
	}

	if ($db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . bugdar::$userinfo['userid'] . " AND bugid = " . $input->in['bugid']))
	{
		$db->query("DELETE FROM " . TABLE_PREFIX . "favorite WHERE userid = " . bugdar::$userinfo['userid'] . " AND bugid = " . $input->in['bugid']);
		$message->redirect(T('This bug has been removed from your favorites list.'), "showreport.php?bugid=" . $input->in['bugid']);
	}
	else
	{
		$db->query("INSERT INTO " . TABLE_PREFIX . "favorite (userid, bugid) VALUES (" . bugdar::$userinfo['userid'] . ", " . $input->in['bugid'] . ")");
		$message->redirect(T('This bug has been added to your favorites list.'), "showreport.php?bugid=" . $input->in['bugid']);
	}
}

// ###################################################################

if ($_REQUEST['do'] == 'manage')
{
	if (!can_perform('canviewbugs'))
	{
		$message->errorPermission();
	}

	$favorites = $db->query("
		SELECT favorite.bugid, bug.* FROM " . TABLE_PREFIX . "favorite AS favorite
		RIGHT JOIN " . TABLE_PREFIX . "bug AS bug
			ON (favorite.bugid = bug.bugid)
		WHERE favorite.userid = " . bugdar::$userinfo['userid'] . "
		AND (!bug.hidden OR (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ? " OR (bug.hidden AND bug.userid = " . bugdar::$userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewownhidden') . "))" : "") . ")
	");

	if ($favorites->size() < 1)
	{
		$message->error(T('You do not have any favorites in your list. To add a bug to your list, while viewing the report, click the [Add to Favorites] link next to the bug\'s ID.'));
	}

	$sort = new ListSorter('favorite');

	$headers = $sort->constructColumnHeaders(false);

	foreach ($favorites as $bug)
	{
		BSFunctions::swap_css_classes('altcolor', '');
		$bug = ProcessBugDataForDisplay($bug, BSFunctions::$cssClass);
		$bugs .= $sort->constructRow($bug);
	}

	$tpl = new BSTemplate('favorites');
	$tpl->vars = array(
		'bugs'		=> $bugs,
		'headers'	=> $headers
	);
	$tpl->evaluate()->flush();
}

?>