Location: PHPKode > projects > Terra-Terra, A Virtual Operating System > terra-terra.0.0.5/workareas/Terra-Terra/TTmessages.php
<?php
/****m*
 * NAME
 *  TTmessages.php --- Workarea of the TT Terra-Terra application
 *
 * DESCRIPTION
 *  This area constantly refreshes in the background to check if there's a message
 *  for this user. If so, the area pops up.
 *  This page can *not* to be displayed as an embedded area
 *
 * USAGE
 *  This page can *not* to be displayed as an embedded area
 *
 * AUTHOR
 *  Oscar van Eijk, Oveas Functionality Provider
 *
 * COPYRIGHT
 *  (c) 2004-2005 by Oscar van Eijk/Oveas Functionality Provider 
 ***/
/*
 * This module is part of Terra-Terra, the Virtual Operating System
 * http://terra-terra.com
 * ------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License,
 * or any later version.
 * This library 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ------------------------------------------------------------------------
 * $Id: TTmessages.php,v 1.2 2005/07/19 18:17:40 tt_oscar Exp $ 
 */

if (!defined ('TT_MASTER_PROCESS')) {
	if (function_exists ('version_compare')) {
		$__TTroot = $_COOKIE['TTroot'];
	} else {
		$__TTroot = $HTTP_COOKIE_VARS['TTroot'];
	}
	require_once ($__TTroot . '/kernel/TTinit.pinc');
}

$__metatag = '<meta http-equiv=refresh content="60; url=' . $TT['env_server']['PHP_SELF'] 
	   . '?AreaObj=' . urlencode(serialize($ThisArea)) . '">';
echo (TT_header($__metatag));


function TTKRNL_MSG_list_msg () {
/*
 * Get a list of all existing messages meant for this user or any
 * of it's groups in the current session.
 */
	global $TT_db;
	global $TT_user;
	global $TT_session;

	$TT_db->query =  'SELECT mid '
	.		 'FROM   ' . $TT_db->db_prefix . 'link_msgs '
	.		 'WHERE  uid   = ' . $TT_user->uid . ' '
	.		 'OR     gid  IN (' . $TT_session->group_list() . ') '
	.		 'ORDER BY mid DESC '
	;

	$__list = $TT_db->read (DBHANDLE_DATA);
	if ($TT_db->severity() == TT_ERROR) {
		TT_signal ($TT_db);
	}
	if ($TT_db->status == DBHANDLE_NODATA) {
		return (array());
	} else {
		return ($__list);
	}
}

function TTKRNL_MSG_get_msg ($msg_id) {
/*
 * Get the message with the give id. If that message is not yet expired, return
 * it, otherwise return null.
 */
	global $TT_db;

	$TT_db->query =  'SELECT  ms.id '
	.		 ',       ms.text '
	.		 ',       ms.date '
	.		 ',       ui.first_name '
	.		 ",       IFNULL(ui.middle_name, '') "
	.		 ',       ui.last_name '
	.		 'FROM   ' . $TT_db->db_prefix . 'messages ms '
	.		 ',      ' . $TT_db->db_prefix . 'userinfo ui '
	.		 "WHERE  ms.id = $msg_id "
	.		 'AND    (ms.expires >= NOW() '
	.		 '   OR   ms.expires is NULL) '
	.		 'AND    ms.posted_by = ui.uid '
	;

	$__message = $TT_db->read (DBHANDLE_SINGLEROW);
	if ($TT_db->severity() == TT_ERROR) {
		TT_signal ($TT_db);
	}
	if ($TT_db->status == DBHANDLE_NODATA) {
		return (null);
	}

	return ($__message);
}

function TTKRNL_MSG_read_msg ($msg_id) {
/*
 * Check if this is a read message. If so, return True. Otherwise, mark the
 * mesage read and return false.
 */
	global $TT_db;
	global $TT_user;

	$TT_db->query =  'SELECT * '
	.		 'FROM   ' . $TT_db->db_prefix . 'read_msgs '
	.		 'WHERE  uid  = ' . $TT_user->uid . ' '
	.		 'AND    mid  = ' . $msg_id . ' '
	;

	$__read = $TT_db->read (DBHANDLE_DATA);
	if ($TT_db->severity() == TT_ERROR) {
		TT_signal ($TT_db);
	}

	if ($TT_db->status != DBHANDLE_NODATA) {
		return (True);
	}
	
	$TT_db->query =  'INSERT INTO ' . $TT_db->db_prefix . 'read_msgs ('
	.		 '    uid '
	.		 ',   mid '
	.		 ',   readtm '
	.		 ') VALUES ( '
	.		 '    ' . $TT_user->uid . ' '
	.		 ',   ' . $msg_id . ' '
	.		 ',   NOW() '
	.		 ')'
	;

	$TT_db->write ();
	if ($TT_db->severity() == TT_ERROR) {
		TT_signal ($TT_db);
	}
	return (False);
}
?>

<table align="center" width="95%">
<thead>
<tr>
	<th><?php echo ($TT_workspace->get_prompt('DATETIME')); ?></th>
	<th><?php echo ($TT_workspace->get_prompt('MESSAGE')); ?></th>
	<th><?php echo ($TT_workspace->get_prompt('POSTER')); ?></th>
</tr>
</thead>
<tbody>

<?php
$__messages = TTKRNL_MSG_list_msg ();
$__signalled = False;
foreach ($__messages as $__msg) {
	if (($__message =  TTKRNL_MSG_get_msg ($__msg[0])) == null) {
		continue 1;
	}
	if (!TTKRNL_MSG_read_msg ($__msg[0])) {
		if (!$__signalled) {
			echo ("<script language=\"JavaScript\">\n");
			echo ("<!-- \n");
			echo ("alert ('" . $TT_workspace->get_prompt('NEWMSG') . "');\n");
			echo ("top.PopUp('" . $ThisArea->name . ");\n");
			echo ("}\n");
			echo ("-->\n");
			echo ("</script>\n");
			$__signalled = True;
		}
		$__cssclass = ' class="emphased"';
	} else {
		$__cssclass = '';
	}

	$__text		= $__message[1];
	$__date		= $__message[2];
	$__posted_by	= $__message[3] . ' ' . $__message[4] . ' ' . $__message[5];

	echo ("<tr>\n");
	echo ('	<td valign="top"' . $__cssclass . '>' . $__date . "</td>\n");
	echo ('	<td valign="top"' . $__cssclass . '>' . $__text . "</td>\n");
	echo ('	<td valign="top"' . $__cssclass . '>' . $__posted_by . "</td>\n");
	echo ("</tr>\n");
	echo ('<tr><td colspan="3"><hr /></td></tr>' . "\n");
}

echo ('<tr><td colspan="3">&nbsp;</td></tr>' . "\n");
echo ('<tr><td colspan="3" align="center"><form><input type="button" value="' . $TT_workspace->get_prompt('CLOSEWA') . '" ' . "onClick=\"parent.WAVisibility('" . $ThisArea->id . "','c');\"></form></td></tr>\n");

echo ('</tbody>');
echo ('</table>');

echo (TT_footer());

?>
Return current item: Terra-Terra, A Virtual Operating System