<?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: channels.php,v 1.1.1.2.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'];
	$channel  = urldecode(get_var('channel',array('POST','GET')));

	$tpl = &$GLOBALS['phpgw']->template;
	$tpl->set_file(array('channels' => 'channels.tpl'));
	
	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')",$start,__LINE__,__FILE__);
	$size = $GLOBALS['phpgw']->db->nf();

	$groups = $GLOBALS['phpgw']->accounts->membership();
	$size = $size + count($groups);
	if (($size == 0) || ($size == 1))
	{
		$size = 2;
	}
 	$tplvars['langChannels']     = lang('Channels');

	$GLOBALS['phpgw']->db->query("SELECT account_lid AS group_name FROM phpgw_accounts where account_type = 'g'");
	$GLOBALS['phpgw']->db->next_record();

	
	$tplvars['load']     = $GLOBALS['phpgw']->link('/chat/load.php');
	$tplvars['chan']     = $channel;
	$tplvars['size']     = $size;

	while(list($key,$group_info) = each($groups))
	{
		$tplvars['users']     .= '<option value="'.$group_info['account_name'].'"'.($group_info['account_name']==$channel?' selected':'')
								. '>'.$group_info['account_name'].'</option>' . "\n";
	}

	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	$GLOBALS['phpgw']->db->next_record();

	for ($i = 0; $i < $GLOBALS['phpgw']->db->nf(); $i++)
	{
		if ($GLOBALS['phpgw']->db->f('user1') == $loginid)
		{
			$tplvars['users'] .=	'<option value="'.$GLOBALS['phpgw']->db->f('user2').'"'.($GLOBALS['phpgw']->db->f('user2')==$channel?' selected':'')
									. '>'.$GLOBALS['phpgw']->db->f('user2').'</option>';
		}
		else
		{
			$tplvars['users'] .= '<option value="'.$GLOBALS['phpgw']->db->f('user1').'"'.($GLOBALS['phpgw']->db->f('user2')==$channel?' selected':'')
								.'>'.$GLOBALS['phpgw']->db->f('user1').'</option>';
		}
		$GLOBALS['phpgw']->db->next_record();
	}

	$ordermethod = 'ORDER BY channel';

	$GLOBALS['phpgw']->db->query("SELECT * FROM phpgw_chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	$GLOBALS['phpgw']->db->next_record();
	$size = $GLOBALS['phpgw']->db->nf();

	$GLOBALS['phpgw']->db->query("select * FROM phpgw_chat_currentin WHERE loginid='$loginid' $ordermethod",$start,__LINE__,__FILE__);
	$GLOBALS['phpgw']->db->next_record();

	$size = $size + $GLOBALS['phpgw']->db->nf();
	if (($size == 0) || ($size == 1))
	{
		$size = 2;
	}

	$tplvars['lang_switch_room']    = lang('Switch Room');
	$tplvars['index']     					= $GLOBALS['phpgw']->link('/chat/index.php');
	$tplvars['location']     				= $location;
	$tplvars['chan2'] 		    			= $channel;
	$tplvars['size']	 		    			= $size;

	for ($i = 0; $i < $GLOBALS['phpgw']->db->nf(); $i++)
	{
		$tplvars['channel']	.= '<option value="'.$GLOBALS['phpgw']->db->f('channel').'">'.$GLOBALS['phpgw']->db->f('channel').'</option>';
		$GLOBALS['phpgw']->db->next_record();
	}
	if(!$i)
	{
		$tplvars['js'] = ' onClick="window.close();"';
	}

	$GLOBALS['phpgw']->db->query("SELECT * from phpgw_chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	$GLOBALS['phpgw']->db->next_record();

	for ($i = 0; $i < $GLOBALS['phpgw']->db->nf(); $i++)
	{
		if ($GLOBALS['phpgw']->db->f('user1') == $loginid)
		{
			$tplvars['channel'] = $GLOBALS['phpgw']->db->f('user2'); 
		}
		else
		{
			$tplvars['channel'] =  $GLOBALS['phpgw']->db->f('user1');
		}
		$GLOBALS['phpgw']->db->next_record();
	}

	$tplvars['lang_part_channel']	 = lang('Part Channel'); 
	
	$tpl->set_var($tplvars);
	$tpl->pfp('out','channels');
?>
