<?php

	$tpl = new template;
	$tpl -> Load("usercomments");
	$tpl -> AssignUser($_GET["id"]);
	
	/* Handle removal */
	if (isset($_GET["rmcomment"]) and is_mop()) {
		myQ("DELETE FROM `[x]comments` WHERE `id`='{$_GET["rmcomment"]}'");
	}
	
	/* Get RECEIVED comments */
	$select = myQ("SELECT * FROM `[x]comments` WHERE `relative`='{$_GET["id"]}' AND `type`='user' ORDER BY `date` DESC");
	$sentCommentsCounter = 0;
	$receivedCommentsCounter = 0;
	
	while ($row = myF($select)) {
		
		$rxCommentsArray[] = array(
			"comment.id" => 			$row["id"],
			"comment.title" => 			_fnc("strtrim", $row["title"], 50),
			"comment.body" => 			_fnc("clearBodyCodes", _fnc("strtrim", $row["body"], 500)),
			"comment.username" => 		_fnc("user", $row["user"], "username"),
			"comment.userid" => 		$row["user"],
			"comment.mainpicture" => 	_fnc("user", $row["user"], "mainpicture"),
			"comment.date" => 			date($CONF["LOCALE_SHORT_DATE_TIME"], $row["date"])
		);

		/* Increment the received comments counter */
		$receivedCommentsCounter++;
	}
	$tpl -> Loop("usercomments", (isset($rxCommentsArray) ? $rxCommentsArray : array()));
	
	
	/* Get SENT comments */
	$select = myQ("SELECT * FROM `[x]comments` WHERE `user`='{$_GET["id"]}' AND `type`='user' ORDER BY `date` DESC");
	while ($row = myF($select)) {
		
		$txCommentsArray[] = array(
			"comment.id" => 			$row["id"],
			"comment.title" => 			_fnc("strtrim", $row["title"], 50),
			"comment.body" => 			_fnc("clearBodyCodes", _fnc("strtrim", $row["body"], 500)),
			"comment.username" => 		_fnc("user", $row["relative"], "username"),
			"comment.userid" => 		$row["relative"],
			"comment.mainpicture" => 	_fnc("user", $row["relative"], "mainpicture"),
			"comment.date" => 			date($CONF["LOCALE_SHORT_DATE_TIME"], $row["date"])
		);

		/* Increment the received comments counter */
		$sentCommentsCounter++;
	}
	
	$tpl -> Loop("sentcomments", (isset($txCommentsArray) ? $txCommentsArray : array()));

	$tpl -> AssignArray(array(
		"count.sent" => $sentCommentsCounter,
		"count.received" => $receivedCommentsCounter
	));

	$tpl -> AssignUser($_GET["id"]);	
	$tpl -> Flush();
	
?>