<?php
	/**************************************************************************\
	* phpGroupWare - Chat                                                      *
	* http://www.phpgroupware.org                                              *
	* This application written by Joseph Engo <jengo@phpgroupware.org>         *
	* --------------------------------------------                             *
	*  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; either version 2 of the License, or (at your  *
	*  option) any later version.                                              *
	\**************************************************************************/

	/* $Id: messages.php,v 1.4.8.1 2004/09/26 10:21:27 kazuyan Exp $ */

	$GLOBALS['phpgw_info']['flags'] = array(
		'currentapp' => 'chat',
		'noheader'   => True,
		'nonavbar'   => True
	);
	include('../header.inc.php');
	$GLOBALS['phpgw']->common->phpgw_header();

	$loginid = $GLOBALS['phpgw_info']['user']['userid'];
	$refresh = 3;

	$channel   = urldecode(get_var('channel',array('POST','GET')));
	$action   = get_var('action',array('POST','GET'));
	$location = get_var('location',array('POST','GET'));
	$user2    = get_var('user2',array('POST','GET'));
	
	/*
	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_chat_messages WHERE channel='$channel'");
	$GLOBALS['phpgw']->db->next_record();
	$status = $GLOBALS['phpgw']->db->f('status');
	$title = $GLOBALS['phpgw']->db->f('title');
	$user2 = $channel;
	*/

	// below line is abusively bad HTML form, but works in NS so far.
	
	$tpl = &$GLOBALS['phpgw']->template;
	$tpl->set_file(array('messages' => 'messages.tpl'));
	
  $tplvars['message']   = $GLOBALS['phpgw']->link('/chat/messages.php',array(
		'channel'  => urlencode($channel),
		'action'   => $action,
		'location' => $location,
		'unique'    => $GLOBALS['phpgw']->datetime->gmtnow
	));

	$tplvars['login_id'] = $loginid; 
	$tplvars['chan'] 		 = $channel;

	$deadline = $GLOBALS['phpgw']->datetime->gmtnow - (60 * 60);
	if ($location == 'private')
	{
		$databasefield = 'sentby';
		$sql = "SELECT * FROM phpgw_chat_privatechat WHERE (user1='$loginid' AND user2='$user2') OR (user1='$user2' AND user2='$loginid')";
	}
	else
	{
		$datenbankfield = 'loginid';
		$sql = "SELECT * FROM phpgw_chat_messages WHERE channel='$channel' AND timesent > '" . $deadline . "'";
	}
	$GLOBALS['phpgw']->db->query($sql.' ORDER BY timesent',__FILE__,__LINE__);

	while($GLOBALS['phpgw']->db->next_record())
	{
		$friendlyTime = $GLOBALS['phpgw']->common->show_date($GLOBALS['phpgw']->db->f('timesent')+$GLOBALS['phpgw_info']['server']['tz_offset']*3600,'H:i.s');
		
		$tplvars['messageline'] .= $GLOBALS['phpgw']->db->f($databasefield) .
		                           ' (' . $friendlyTime . ')';
		$tplvars['messageline'] .= '&nbsp;';
		$tplvars['messageline'] .= htmlspecialchars(stripslashes($GLOBALS['phpgw']->db->f('message'))).'<br />'; 
	}

	$tpl->set_var($tplvars);
	$tpl->pfp('out','messages');
?>
