From c5b338c6fc4394a6f3de26c13beeb025a3228932 Mon Sep 17 00:00:00 2001
From: e107steved <steved@e107.org>
Date: Fri, 8 May 2009 21:50:19 +0000
Subject: [PATCH] Bugtracker #4651 - news comment counting

---
 e107_admin/comment.php                        | 41 +++++++---
 e107_admin/modcomment.php                     | 45 ++++++-----
 e107_admin/newspost.php                       | 77 +++++++++++++++++--
 e107_languages/English/admin/lan_newspost.php | 13 +++-
 4 files changed, 134 insertions(+), 42 deletions(-)

diff --git a/e107_admin/comment.php b/e107_admin/comment.php
index 2729a1a38..ddf11ec21 100644
--- a/e107_admin/comment.php
+++ b/e107_admin/comment.php
@@ -11,36 +11,53 @@
 |     GNU General Public License (http://gnu.org).
 |
 |     $Source: /cvs_backup/e107_0.8/e107_admin/comment.php,v $
-|     $Revision: 1.1.1.1 $
-|     $Date: 2006-12-02 04:33:12 $
-|     $Author: mcfly_e107 $
+|     $Revision: 1.2 $
+|     $Date: 2009-05-08 21:50:19 $
+|     $Author: e107steved $
 +----------------------------------------------------------------------------+
 */
 require_once("../class2.php");
-if (!getperms("B")) {
+if (!getperms("B")) 
+{
 	header("location:".e_BASE."index.php");
 	exit;
 }
 	
-if (e_QUERY) {
+if (e_QUERY) 
+{
 	$temp = explode("-", e_QUERY);
 	$action = $temp[0];
 	$id = intval($temp[1]);
 	$item = $temp[2];
 	$c_item = $temp[3];
-	if ($action == "block") {
-		$sql->db_Update("comments", "comment_blocked='1' WHERE comment_id='$id' ");
+	if ($sql->select('comments','*', 'comment_id='.$id))
+	{
+		$comment = $sql->db_Fetch();
+		if ($action == "block") 
+		{
+			$sql->db_Update("comments", "comment_blocked='1' WHERE comment_id=".$id);
 	}
-	if ($action == "unblock") {
-		$sql->db_Update("comments", "comment_blocked='0' WHERE comment_id='$id' ");
+		if ($action == "unblock") 
+		{
+			$sql->db_Update("comments", "comment_blocked='0' WHERE comment_id=".$id);
+		}
+		if ($action == "delete") 
+		{
+			$sql->db_Delete("comments", "comment_id=".$id);
+			switch ($comment['comment_type'])
+			{
+				case '0' :
+				case 'news' :		// Need to update count in news record as well
+					$sql2->db_Update('news', 'news_comment_total = CAST(GREATEST(CAST(news_comment_total AS SIGNED) - 1, 0) AS UNSIGNED) WHERE news_id='.$comment['comment_item_id']);
+					break;
 	}
-	if ($action == "delete") {
-		$sql->db_Delete("comments", "comment_id='$id' ");
 	}
-	if (!$e107cache->clear($item)) {
+		if (!$e107cache->clear($item)) 
+		{
 		$tmp = explode("?", $item);
 		$item = $tmp[0]."?news.".$c_item;
 		$e107cache->clear($item);
+		}
 	}
 }
 echo "<script type='text/javascript'>window.history.go(-1);</script>\n";
diff --git a/e107_admin/modcomment.php b/e107_admin/modcomment.php
index 2bf68db15..506704fca 100644
--- a/e107_admin/modcomment.php
+++ b/e107_admin/modcomment.php
@@ -11,8 +11,8 @@
 |     GNU General Public License (http://gnu.org).
 |
 |     $Source: /cvs_backup/e107_0.8/e107_admin/modcomment.php,v $
-|     $Revision: 1.6 $
-|     $Date: 2009-04-16 20:50:38 $
+|     $Revision: 1.7 $
+|     $Date: 2009-05-08 21:50:19 $
 |     $Author: e107steved $
 +----------------------------------------------------------------------------+
 */
@@ -50,19 +50,14 @@ if (isset($_POST['moderate']))
 	{
 		$sql->db_Update('comments', "comment_lock='0' WHERE `comment_item_id`=".$id." AND `comment_type`='".$tp -> toDB($type, true)."' ");
 	}
-
-	if (is_array($_POST['comment_blocked'])) 
-	{
-		while (list ($key, $cid) = each ($_POST['comment_blocked'])) 
-		{
-			$sql->db_Update('comments', "comment_blocked='1' WHERE `comment_id`=".$cid);
+	if (is_array($_POST['comment_blocked'])) {
+		while (list ($key, $cid) = each ($_POST['comment_blocked'])) {
+			$sql->db_Update("comments", "comment_blocked='1' WHERE comment_id='$cid' ");
 		}
 	}
-	if (is_array($_POST['comment_unblocked'])) 
-	{
-		while (list ($key, $cid) = each ($_POST['comment_unblocked'])) 
-		{
-			$sql->db_Update('comments', "comment_blocked='0' WHERE `comment_id`=".$cid);
+	if (is_array($_POST['comment_unblocked'])) {
+		while (list ($key, $cid) = each ($_POST['comment_unblocked'])) {
+			$sql->db_Update("comments", "comment_blocked='0' WHERE comment_id='$cid' ");
 		}
 	}
 	if (is_array($_POST['comment_delete'])) 
@@ -172,24 +167,32 @@ $ns->tablerender(MDCLAN_8, $text);
 
 require_once("footer.php");
 
-function delete_children($row, $cid) {
+
+
+function delete_children($row, $cid) 
+{
 	global $sql, $sql2, $table;
 
 	$tmp = explode(".", $row['comment_author']);
-	$u_id = $tmp[0];
-	if ($u_id >= 1) {
-		$sql->db_Update("user", "user_comments=user_comments-1 WHERE user_id='$u_id'");
+	$u_id = intval($tmp[0]);
+	if ($u_id >= 1) 
+	{
+		$sql->db_Update("user", "user_comments=user_comments-1 WHERE user_id=".$u_id);
 	}
-	if($table == "news"){
+	if (($table == "news") || ($table == '0'))
+	{
 		$sql->db_Update("news", "news_comment_total=news_comment_total-1 WHERE news_id='".$row['comment_item_id']."'");
 	}
-	if ($sql2->db_Select("comments", "*", "comment_pid='".$row['comment_id']."'")) {
-		while ($row2 = $sql2->db_Fetch()) {
+	if ($sql2->db_Select("comments", "*", "comment_pid='".$row['comment_id']."'")) 
+	{
+		while ($row2 = $sql2->db_Fetch()) 
+		{
 			delete_children($row2, $row2['comment_id']);
 		}
 	}
 	$c_del[] = $cid;
-	while (list ($key, $cid) = each ($c_del)) {
+	while (list ($key, $cid) = each ($c_del)) 
+	{
 		$sql->db_Delete("comments", "comment_id='$cid'");
 	}
 }
diff --git a/e107_admin/newspost.php b/e107_admin/newspost.php
index 7dc818df0..0d14584f7 100644
--- a/e107_admin/newspost.php
+++ b/e107_admin/newspost.php
@@ -9,9 +9,9 @@
  * News Administration
  *
  * $Source: /cvs_backup/e107_0.8/e107_admin/newspost.php,v $
- * $Revision: 1.36 $
- * $Date: 2009-05-06 20:40:16 $
- * $Author: bugrain $
+ * $Revision: 1.37 $
+ * $Date: 2009-05-08 21:50:19 $
+ * $Author: e107steved $
 */
 require_once("../class2.php");
 
@@ -106,10 +106,10 @@ function headerjs()
    	return $ret;
 }
 $e_sub_cat = 'news';
-$e_wysiwyg = "data,news_extended";
+$e_wysiwyg = 'data,news_extended';
 
-require_once("auth.php");
-require_once (e_HANDLER."message_handler.php");
+require_once('auth.php');
+require_once (e_HANDLER.'message_handler.php');
 
 /*
  * Observe for delete action
@@ -247,6 +247,10 @@ class admin_newspost
 		{
 			$this->_observe_upload();
 		}
+		elseif(isset($_POST['news_comments_recalc']))
+		{
+			$this->_observe_newsCommentsRecalc();
+		}
 	}
 
 	function show_page()
@@ -276,6 +280,10 @@ class admin_newspost
 				$this->show_news_prefs();
 			break;
 
+			case 'maint' :
+				$this->showMaintenance();
+				break;
+
 			default:
 				$this->show_existing_items();
 			break;
@@ -1421,6 +1429,7 @@ class admin_newspost
 
 		echo $frm->selectbox('newsposts_archive', $this->_optrange(intval($this->getSubAction()) - 1), intval($pref['newsposts_archive']), 'class=tbox&tabindex='.intval($this->getId()));
 	}
+	
 
 	function show_news_prefs()
 	{
@@ -1551,6 +1560,7 @@ class admin_newspost
 		$e107->ns->tablerender(NWSLAN_90, $emessage->render().$text);
 	}
 
+
 	function show_submitted_news()
 	{
 
@@ -1624,6 +1634,54 @@ class admin_newspost
 	}
 
 
+
+	function showMaintenance() 
+	{
+		require_once(e_HANDLER."form_handler.php");
+		$frm = new e_form(true); //enable inner tabindex counter
+
+		$e107 = &e107::getInstance();
+
+		$text = "
+			<form method='post' action='".e_SELF."?maint' id='core-newspost-maintenance-form'>
+			<div style='text-align:center'>
+		<table class='fborder' style='".ADMIN_WIDTH."'>
+
+		<tr><td class='forumheader3'>".LAN_NEWS_56."</td><td style='text-align:center' class='forumheader3'>";
+		$text .= "<input class='button' type='submit' name='news_comments_recalc' value='".LAN_NEWS_57."' /></td></tr>";
+
+		$text .= "</table></div>
+		</form>";
+
+		$emessage = &eMessage::getInstance();
+		$e107->ns->tablerender(LAN_NEWS_59, $emessage->render().$text);
+	}
+
+
+	function _observe_newsCommentsRecalc()
+	{
+		global $sql2;
+
+		$e107 = &e107::getInstance();
+		$qry = "SELECT 
+			COUNT(`comment_id`) AS c_count,
+			`comment_item_id`
+			FROM `#comments`
+			WHERE (`comment_type`='0') OR (`comment_type`='news')
+			GROUP BY `comment_item_id`";
+			
+		if ($e107->sql->db_Select_gen($qry))
+		{
+			while ($row = $e107->sql->db_Fetch(MYSQL_ASSOC))
+			{
+				$sql2->db_Update('news', 'news_comment_total = '.$row['c_count'].' WHERE news_id='.$row['comment_item_id']);
+			}
+		}
+		$this->show_message(LAN_NEWS_58, E_MESSAGE_SUCCESS);
+	}
+
+
+
 	function show_message($message, $type = E_MESSAGE_INFO, $session = false)
 	{
 		// ##### Display comfort ---------
@@ -1656,6 +1714,13 @@ class admin_newspost
 			$var['sn']['perm'] = "N";
 		}
 
+		if (getperms('0'))
+		{
+			$var['maint']['text'] = LAN_NEWS_55;
+			$var['maint']['link'] = e_SELF."?maint";
+			$var['maint']['perm'] = "N";
+		}
+
 		e_admin_menu(NWSLAN_48, $this->getAction(), $var);
 	}
 
diff --git a/e107_languages/English/admin/lan_newspost.php b/e107_languages/English/admin/lan_newspost.php
index 49106972c..064c45752 100644
--- a/e107_languages/English/admin/lan_newspost.php
+++ b/e107_languages/English/admin/lan_newspost.php
@@ -4,9 +4,9 @@
 |     e107 website system - Language File.
 |
 |     $Source: /cvs_backup/e107_0.8/e107_languages/English/admin/lan_newspost.php,v $
-|     $Revision: 1.9 $
-|     $Date: 2009-01-18 19:02:07 $
-|     $Author: secretr $
+|     $Revision: 1.10 $
+|     $Date: 2009-05-08 21:50:19 $
+|     $Author: e107steved $
 +----------------------------------------------------------------------------+
 */
 define("NWSLAN_1", "News story deleted.");
@@ -185,4 +185,11 @@ define("LAN_NEWS_53", "Advanced Options");
 define("LAN_NEWS_54", "stay in edit mode");
 
 
+define('LAN_NEWS_55', 'Maintenance');				// Was LAN_NEWS_50 in 0.7
+define('LAN_NEWS_56', 'Recalculate comment counts');
+define('LAN_NEWS_57', 'Proceed');
+define('LAN_NEWS_58', 'Update complete');
+define('LAN_NEWS_59', 'News Maintenance');
+
+
 ?>