<?php
/*=====================================================================*\
|| ###################################################################
|| # Bugdar
|| # Copyright ©2002-2007 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
|| ###################################################################
\*=====================================================================*/

$fetchtemplates = array(
	'history',
	'history_bit'
);

define('SVN', '$Id$');

$focus['showreport'] = 'focus';

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

$bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . $bugsys->input_clean('bugid', TYPE_UINT));
if (!$bug)
{
	$message->error(L_INVALID_ID);
}

if (!check_bug_permissions($bug))
{
	$message->errorPermission();
}

$lookupfields = array(
	'status'		=> 'status',
	'priority'		=> 'priority',
	'severity'		=> 'severity',
	'resolution'	=> 'resolution',
	'version'		=> 'version',
	'product'		=> 'title',
	'component'		=> 'title'
);

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

$customfields = $db->query("
	SELECT bugfield.*, MAX(permission.mask) AS mask
	FROM " . TABLE_PREFIX . "bugfield AS bugfield
	LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
		ON (bugfield.fieldid = permission.fieldid)
	WHERE mask <> 0
	AND permission.usergroupid IN ({$bugsys->userinfo['usergroupid']}" . (sizeof($bugsys->userinfo['groupids']) != 0 ? ',' . implode(',', $bugsys->userinfo['groupids']) : '') . ")
	GROUP BY (bugfield.fieldid)"
);

while ($field = $db->fetch_array($customfields))
{
	$fieldlist["$field[fieldid]"] = sprintf(_('Custom Field %1$s (%2$s)'), $field['fieldid'], $field['name']);
}

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

$logs = array();
$logs_fetch = $db->query("
	SELECT history.*, user.userid, user.email, user.displayname, user.showemail
	FROM " . TABLE_PREFIX . "history AS history
	LEFT JOIN " . TABLE_PREFIX . "user AS user
		ON (user.userid = history.userid)
	WHERE bugid = $bug[bugid]"
);
while ($log = $db->fetch_array($logs_fetch))
{
	$log['date'] = $datef->format($bugsys->options['dateformat'], $log['dateline']);
	$log['user'] = construct_user_display($log);

	$logs["$log[dateline]"]["$log[historyid]"] = $log;
	ksort($logs["$log[dateline]"]);
}

ksort($logs);

$toKill = array();

foreach ($logs AS $dateline => $logitems)
{
	$show['group'] = true;

	foreach ($logitems AS $log)
	{
		if ($log['field'] == '.' OR ($log['original'] == '' AND $log['changed'] == ''))
		{
			$toKill[] = $log['historyid'];
			continue;
		}

		if (preg_match('#^(comment|attachment)\.(.*)#', $log['field'], $matches))
		{
			if ($matches[1] == 'comment')
			{
				$log['field'] = sprintf(_('Comment #%1$s %2$s'), $log['commentid'], ucwords($matches[2]));
			}
			else if ($matches[1] == 'attachment')
			{
				$log['field'] = sprintf(_('Attachment #2%1$s %2$s'), $log['attachmentid'], ucwords($matches[2]));
			}
		}
		else if (preg_match('#^.?custom(.field)?([0-9]+?)#', $log['field'], $matches))
		{
			if ($fieldlist["$matches[2]"])
			{
				$log['field'] = $fieldlist["$matches[2]"];
			}
			else
			{
				continue;
			}
		}
		else if (preg_match('#^\.(.*)#', $log['field'], $matches))
		{
			$log['field'] = ucwords($matches[1]);

			if (isset($lookupfields["$matches[1]"]))
			{
				$lookup = $matches[1];
				$log['original'] = $bugsys->datastore["$lookup"]["$log[original]"][ $lookupfields["$lookup"] ];
				$log['changed'] = $bugsys->datastore["$lookup"]["$log[changed]"][ $lookupfields["$lookup"] ];
			}
			else if ($matches[1] == 'assignto' OR $matches[1] == 'assignedto')
			{
				if ($log['original'])
				{
					$user = new UserAPI($bugsys);
					$user->set('userid', $log['original']);
					$user->set_condition();
					$user->fetch();
					$log['original'] = construct_user_display($user->objdata);
				}

				if ($log['changed'])
				{
					$user = new UserAPI($bugsys);
					$user->set('userid', $log['changed']);
					$user->set_condition();
					$user->fetch();
					$log['changed'] = construct_user_display($user->objdata);
				}
			}
		}

		$funct->exec_swap_bg('', 'altcolor');
		$bgcolor = $funct->bgcolour;

		eval('$history .= "' . $template->fetch('history_bit') . '";');
		$show['group'] = false;
	}
}

// we can now remove all useless logs
if (sizeof($toKill) > 0)
{
	$db->query("DELETE FROM " . TABLE_PREFIX . "history WHERE historyid IN (" . implode(',', $toKill) . ")");
}

eval('$template->flush("' . $template->fetch('history') . '");');

/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>