<?php

	$userid = isset($_GET["id"]) ? $_GET["id"] : (me("id")!=="" ? me("id") : NULL);

	$tpl = new template;
	$tpl -> Load("usercomments");
	$tpl -> AssignUser($userid);
	
	/* Handle removal */
	if (isset($_GET["rmcomment"]) && (is_mop() || $CONF["COMMENTS_USERDEL"])) {

		if(is_mop()){

		myQ("DELETE FROM `[x]comments` WHERE `id`='{$_GET["rmcomment"]}'");

		} else myQ("DELETE FROM `[x]comments` WHERE `id`='{$_GET["rmcomment"]}' and `relative` = '".me("id")."'");
	}
	

	/* Get RECEIVED comments */
	$select = myQ("SELECT * FROM `[x]comments` WHERE `relative`='{$userid}' AND `type`='user' ORDER BY `date` DESC");
	$sentCommentsCounter = 0;
	$receivedCommentsCounter = 0;

	if(myNum($select) > 0){

	$tpl->Zone("comments","enabled");

	if(is_mop() || $CONF["COMMENTS_USERDEL"]) $tpl->Zone("commentdel","enabled");
	
	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`='{$userid}' 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
	));

	} else $tpl->Zone("comments","disabled");

	$tpl -> AssignUser($userid);
	$tpl-> CleanZones();	
	$tpl -> Flush();
	
?>