<?php
/*
 * Copyright (c) 2004-2009 Luiz Otavio O Souza <loos.br@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* 
 * $Id: chat-to.php 102 2009-02-05 18:37:40Z loos-br $
 */

    require('mysql.inc.php');
    require('header.inc.php');

?>

<body bgcolor="#FFFFFF">

<div align=center>
<?php
    if (isset($_REQUEST['back']) && $_REQUEST['back'] == "on") {
	echo "<a href=\"javascript: history.back()\">Voltar</a> | ";
    }
?>
 <a href="javascript: window.close()">Fechar a janela</a><br><br>
 <table border=0 width=500
        style="border-color: gray; border-style: solid; border-width: 1;">
  <tr>
   <td align="center" bgcolor="#EBEBEB" width=120>Data</td>
   <td align="center" bgcolor="#EBEBEB" width=150>De</td>
   <td align="center" bgcolor="#EBEBEB" width=150>Para</td>
   <td align="center" bgcolor="#EBEBEB">Visualizar</td>
   <td align="center" bgcolor="#EBEBEB">Visualizar<br>Todas</td>
  </tr>
<?php
    $sql  = "SELECT DISTINCT l1.sb_id, l1.date, l1.display_name as dn1, ";
    $sql .= "l1.type as t1, l2.display_name as dn2, l2.type as t2 ";
    $sql .= "FROM log as l1, log as l2 WHERE ";
    $sql .= "(l1.email = '".$_REQUEST['email']."' AND l1.type = 'start' AND ";
    $sql .= "l2.email = '".$_REQUEST['to']."' AND l2.type = 'join' AND ";
    $sql .= "l1.sb_id = l2.sb_id) OR ";
    $sql .= "(l1.email = '".$_REQUEST['email']."' AND l1.type = 'join' AND ";
    $sql .= "l2.email = '".$_REQUEST['to']."' AND l2.type = 'start' AND ";
    $sql .= "l1.sb_id = l2.sb_id) ";
    $sql .= "ORDER BY l1.date DESC ";

    $rs = mysql_query($sql, $mysql);
    $rsCount = mysql_num_rows($rs);

    while($rs && ($row = mysql_fetch_array($rs))) {

	$sql  = "SELECT email, display_name FROM log WHERE ";
	$sql .= "sb_id = ".$row['sb_id']." AND ";
	$sql .= "type = 'join' ";

	$rsTo = mysql_query($sql, $mysql);
?>
  <tr>
   <td bgcolor="#EBEBEB" align="center"><?php echo $row['date'] ?></td>
<?php
	if ($row['t1'] == "start") {
?>
   <td bgcolor="#EBEBEB"><?php echo $row['dn1'] ?></td>
   <td bgcolor="#EBEBEB">
<?php
	    while($rsTo && ($rowTo = mysql_fetch_array($rsTo))) {
		echo $rowTo['display_name'] . "<BR>\n";
	    }
	} else {
?>
   <td bgcolor="#EBEBEB"><?php echo $row['dn2'] ?></td>
   <td bgcolor="#EBEBEB">
<?php
            while($rsTo && ($rowTo = mysql_fetch_array($rsTo))) {
                echo $rowTo['display_name'] . "<BR>\n";
            }
        }
?>
   </td>
   <td align="center" bgcolor="#EBEBEB">
	<a href="view.php?sb_id=<?php echo $row['sb_id'] ?>&back=on"><img src="imagens/send_im.gif" border=0></a></td>
<?php
	if ($rsCount > 0) {
?>
   <td align="center" bgcolor="#EBEBEB" rowspan="<?php echo $rsCount ?>">
	<a href="view-to.php?email=<?php echo $_REQUEST['email'] ?>&to=<?php echo $_REQUEST['to'] ?>"><img src="imagens/send_im.gif" border=0></a></td>
<?php
	    $rsCount = 0;
	}
?>
  </tr>
<?php
    }
    mysql_close($mysql);
?>
</table><br>
</div>

</body>
</html>

