<?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: index.php,v 1.18.2.3.2.5 2004/02/28 02:48:34 ceb Exp $ */

	/* Is this really needed, eh? */
	$GLOBALS['phpgw_info']['flags'] = array(
		'currentapp'              => 'chat',
		'navbar_target'           => '_top',
		'enable_nextmatchs_class' => True,
		'noheader'                => True
	);
	include('../header.inc.php');

	$channel  = $GLOBALS['phpgw']->db->db_addslashes( get_var('channel',array('POST','GET')) );
	$action   = get_var('action',array('POST','GET'));
	$location = get_var('location',array('POST','GET'));

	$loginid = $GLOBALS['phpgw']->db->db_addslashes( $GLOBALS['phpgw_info']['user']['userid']);
	if ($action=='part')
	{
		if ($location == 'public')
		{
			$GLOBALS['phpgw']->db->query("DELETE FROM phpgw_chat_currentin WHERE loginid='$loginid' AND channel='$channel'", __LINE__, __FILE__);
		}
		if ($location == 'private')
		{
			$user2 = $channel;
			$GLOBALS['phpgw']->db->query("DELETE FROM phpgw_chat_privatechat WHERE ((user1='$loginid' AND user2='$user2') OR (user1='$user2' AND user2='$loginid'))", __LINE__, __FILE__);
			$GLOBALS['phpgw']->db->query("INSERT INTO phpgw_chat_privatechat (user1,user2,sentby,message,messagetype,timesent,closed) VALUES ('$loginid','$user2','System','This chat has been closed by $loginid.',0," . time() . ",1)", __LINE__, __FILE__);
		}
		$GLOBALS['phpgw']->db->query("SELECT loginid FROM phpgw_chat_currentin WHERE loginid='$loginid'", __LINE__, __FILE__);
		if(!$GLOBALS['phpgw']->db->next_record())
		{
?>
<html>
<head></head>
<script>
window.close();
</script>
</html>
<?php
		}
		else
		{
			Header('Location: ' . $GLOBALS['phpgw']->link('/chat/load.php'));
		}
	}

	$GLOBALS['phpgw']->common->phpgw_header();
	echo parse_navbar();
	
	$tpl = &$GLOBALS['phpgw']->template;
	$tpl->set_file(array('index' => 'index.tpl'));	
  
  $tplvars['link']     = $GLOBALS['phpgw']->link('/chat/load.php');
	$tplvars['var']      = isset($GLOBALS['phpgw_info']['server']['usecookies']) && $GLOBALS['phpgw_info']['server']['usecookies']?'?':'&';
  $tplvars['rooms']    = lang('rooms');
  $tplvars['whowhere'] = lang('whowhere');
  
	// get all valid groups for an account for the html option tag
	$groups = $GLOBALS['phpgw']->accounts->membership();
	while(list($key,$group_info) = each($groups))
	{
		if($group_info['account_name'] != '')
		{
			$tplvars['options'] .= ' <option value="'.$group_info['account_name'].'">'.$group_info['account_name']."</option>\n";
		}
	}

	// get all privat chat rooms for the html option tag
	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')", __LINE__, __FILE__);
	while ($GLOBALS['phpgw']->db->next_record())
	{
		if ($GLOBALS['phpgw']->db->f('user1') == $loginid && $GLOBALS['phpgw']->db->f('user2') != '')
		{
			$tplvars['options'] .= '<option value="~' . $GLOBALS['phpgw']->db->f('user2') . '">&lt ' . $GLOBALS['phpgw']->db->f('user2') . ' &gt</option>'."\n";
		}
		else
		{
			if($GLOBALS['phpgw']->db->f('user1') != '')
			{
				$tplvars['options'] .= '<option value="~' . $GLOBALS['phpgw']->db->f('user1') . '">&lt ' . $GLOBALS['phpgw']->db->f('user1') . ' &gt</option>'."\n";
			}
		}
		$GLOBALS['phpgw']->db->next_record();
	}
	
  $tplvars['enterchat'] = lang('Enter Chat');
  
	$ordermethod = 'ORDER BY channel, loginid ASC';
	
	$deadline = $GLOBALS['phpgw']->datetime->gmtnow - (15 * 60);
	$GLOBALS['phpgw']->db->limit_query("SELECT * FROM phpgw_chat_currentin WHERE lastmessage > $deadline $ordermethod",$start,__LINE__,__FILE__);

	while ($GLOBALS['phpgw']->db->next_record())
	{
		$tr_color = $GLOBALS['phpgw']->nextmatchs->alternate_row_color($tr_color);
		$tplvars['cols'] .=
		 '<tr bgcolor="' . $tr_color . '">' . "\n" .
		   '<td>' . $GLOBALS['phpgw']->db->f('loginid') . ' - ' . $GLOBALS['phpgw']->db->f('channel') . "</td>\n" .
		 '</tr>' . "\n";
	}

	$tpl->set_var($tplvars);
	$tpl->pfp('out','index');
	$GLOBALS['phpgw']->common->phpgw_footer();
?>
