<?php
if (!defined("ADMIN_FILE") || !is_admin_modul($aid, "Comments")) die("Illegal File Access");

if (!is_admin_god($aid)) {

require_once("config/config_comments.php");

function CommentsNavi() {
	global $admin_file, $chng_user, $search;
	GraphicAdmin();
	OpenTable();
	echo "<center><font class=\"title\"><b>"._EDITCOMMENTS."</b></font><br /><br />"
	."<font class=\"content\">[ <a href=\"".$admin_file.".php?op=ShowComments\">"._HOME."</a>"
	." ]</center>";
	CloseTable();
}
	
function ShowComments() {
	global $prefix, $db, $pagenum, $admin_file;
	head();
	CommentsNavi();
	OpenTable();
	if ($pagenum == "") $pagenum = 1;
	$storynum = 50;
	$offset = ($pagenum-1) * $storynum ;
	$result = $db->sql_query("SELECT a.id, a.cid, a.modul, a.name, a.comment, b.user_name FROM ".$prefix."_comment AS a LEFT JOIN ".$prefix."_users AS b ON (a.uid=b.user_id) ORDER BY date DESC LIMIT $offset, $storynum");
	echo "<center><font class=\"option\"><b>"._COMMENTS."</b></font><br><br>"
	."<script language=\"JavaScript\" type=\"text/javascript\" src=\"javascripts/SortTable.js\"></script>"
	."<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" class=\"sort\" id=\"sort_id\"><tr>"
	."<th>"._ID."</th><th>"._MODUL."</th><th>"._NICKNAME."</th><th width=\"60%\">"._COMMENT."</th><th>"._FUNCTIONS."</th></tr>";
	while (list($id, $cid, $com_modul, $com_name, $com_text, $user_name) = $db->sql_fetchrow($result)) {
		$com_name = ($user_name) ? $user_name : $com_name;
		echo "<tr class=\"bgcolor1\">"
		."<td align=\"center\">$id</td>"
		."<td align=\"center\">$com_modul</td>"
		."<td>$com_name</td>"
		."<td>".bb_decode($com_text)."</td>";
		echo "<td align=\"center\">";
		#©Serg_pnz - открываем в новом окне новость или файл, которому принадлежит данный комментарий
        if ($com_modul=="News") {echo " <a href=\"index.php?name=News&op=article&sid=$cid\" target=\"_blank\"><img src=\"images/blocks/News.gif\" border=\"0\" alt=\"Смотреть новость\"> </a> ";}
        elseif ($com_modul=="Files") {echo " <a href=\"index.php?name=Files&op=view_file&lid=$cid\" target=\"_blank\"><img src=\"images/blocks/Files.gif\" border=\"0\" alt=\"Смотреть файл\"> </a> ";}
        elseif ($com_modul=="obmenik") {echo " <a href=\"index.php?name=obmenik&op=view_file&lid=$cid\" target=\"_blank\"><img src=\"images/blocks/Files.gif\" border=\"0\" alt=\"Смотреть файл\"> </a> ";}
		elseif ($com_modul=="Pages") {echo " <a href=\"index.php?name=Pages&op=page&pid=$cid&pagenum=1\" target=\"_blank\"><img src=\"images/blocks/Pages.gif\" border=\"0\" alt=\"Смотреть файл\"> </a> ";}
		elseif ($com_modul=="Voting") {echo " <a href=\"index.php?name=Voting&op=poll_result&poll_id=$cid&pagenum=1\" target=\"_blank\"><img src=\"images/blocks/Voting.gif\" border=\"0\" alt=\"Смотреть файл\"> </a> ";}
		echo "<a href=\"".$admin_file.".php?op=EditComments&id=$id\" title=\""._EDIT."\"><img src=\"images/edit.gif\" border=\"0\" alt=\""._EDIT."\"></a> <a href=\"".$admin_file.".php?op=DeleteComments&id=$id&cid=$cid&module_name=$com_modul&com_text=".htmlspecialchars($com_text)."\" title=\""._DELETE."\"><img src=images/delete.gif border=\"0\" alt=\""._DELETE."\"></a></td>";
		echo "</tr>";
	}
	echo "</table>";
	CloseTable();
	list($numstories) = $db->sql_fetchrow($db->sql_query("SELECT Count(id) FROM ".$prefix."_comment"));
	$numpages = ceil($numstories / $storynum);
	num_page("", $numstories, $numpages, $storynum, "op=ShowComments&");
	foot();
}

function EditComments($id) {
    global $admin, $db, $prefix, $admin_file;
    head();
    CommentsNavi();
    $result = $db->sql_query("SELECT id, comment FROM ".$prefix."_comment WHERE id='$id'");
    list($id, $com_text) = $db->sql_fetchrow($result);
    OpenTable();
    echo "<center><font class=\"option\"><b>"._EDIT."</b></font><br>"
	."<form name=\"post\" action=\"".$admin_file.".php\" method=\"post\"><input type=\"hidden\" name=\"id\" value=\"$id\">"
	."<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" align=\"center\">"
	."<tr><td>"._COMMENT.":</td><td><textarea id=\"area\" name=\"comment\" cols=\"65\" rows=\"5\" style=\"width:400px\" OnKeyPress=\"TransliteFeld(this, event)\" OnSelect=\"FieldName(this, this.name)\" OnClick=\"FieldName(this, this.name)\" OnKeyUp=\"FieldName(this, this.name)\">".replace_break($com_text)."</textarea>".get_code("area")."</td></tr>"
	."<tr><td colspan=\"2\" align=\"center\"><br /><input type=\"hidden\" name=\"op\" value=\"EditCommentsSave\"> <input type=\"submit\" value=\""._SAVE."\"> "._GOBACK."</td></tr></form></table>";
    CloseTable();
    foot();
}

function EditCommentsSave($id, $comment) {
    global $prefix, $db, $admin_file;
	$id = intval($id);
	$com_text = nl2br(text_filter($comment, 2));
	$db->sql_query("UPDATE ".$prefix."_comment SET id='$id', comment='$com_text' WHERE id='$id'");
    Header("Location: ".$admin_file.".php?op=ShowComments");
}

switch($op) {
	case "ShowComments":
	ShowComments();
    break;

	case "EditComments":
	EditComments($id);
    break;

	case "EditCommentsSave":
	EditCommentsSave($id, $comment);
    break;

    case "DeleteComments":
    head();
    CommentsNavi();
    OpenTable();
    echo "<center>"._DELETE." \"".htmlspecialchars($com_text)."\"?<br><br>[ <a href=\"".$admin_file.".php?op=DeleteCommentsOk&id=$id&cid=$cid&modul=$module_name\">"._YES."</a> | <a href=\"".$admin_file.".php?op=ShowComments\">"._NO."</a> ]</center>";
    CloseTable();
    foot();
    break;

    case "DeleteCommentsOk":
	$id = intval($id);
	$cid = intval($cid);
	if ($id != "" && $cid != "" && $modul != "") {
		$db->sql_query("DELETE FROM ".$prefix."_comment WHERE id='$id'");
		if ($modul == "Files") {
			$db->sql_query("UPDATE ".$prefix."_files SET totalcomments=totalcomments-1 WHERE lid='$cid'");
		} elseif ($modul == "Links") {
			$db->sql_query("UPDATE ".$prefix."_links SET totalcomments=totalcomments-1 WHERE lid='$cid'");
		} elseif ($modul == "Voting") {
			$db->sql_query("UPDATE ".$prefix."_survey SET pool_comments=pool_comments-1 WHERE poll_id='$cid'");
		} elseif ($modul == "News") {
			$db->sql_query("UPDATE ".$prefix."_stories SET comments=comments-1 WHERE sid='$cid'");
		}
	}
    Header("Location: ".$admin_file.".php?op=ShowComments");
    break;

}
}
?>