<?php
/**
 * @name:        CommentModeration <http://jeenaparadies.net/projects/jlog/wiki/plugins/CommentModeration>
 * @author:      Robert Bienert <robertbienert@gmx.net>
 * @version:     0.3
 * @date:        2009-01-25
 *
 * Mit diesem Plugin landen alle neuen Kommentare erst einmal in der
 * Moderationswarteschlange und muessen vom Administrator genehmigt
 * werden.
 *
 * 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 of the License, or (at your option) 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
 */

define('COMMENT_MOD_KEY', 'inModeration');

class CommentModeration extends JlogPlugin {
	var $commSid;	// zwischengespeicherte Session-ID
	var $nWaiting;	// Anzahl wartender Kommentare

	// Ausgabe des Hinweises auf die Moderation
	function hook_commentForm($form) {
		return str_replace('<p class=\'xmp\'>',
			'<p><em>&#8230; wird moderiert</em></p><p class=\'xmp\'>',
			$form);
	}

	// Vor dem Speichern eines neuen Kommentars
	function hook_newComment($form) {
		// Idee vom AkismetPlugin: neuer Typ 'inModeration':
		$form['type'] = COMMENT_MOD_KEY;

		// Session-ID zwischenspeichern fuer die Anzeige
		$this->commSid = $form['sid'];

		return $form;
	}

	// Kommentare in der Warteschlange werden nicht angezeigt.
	function hook_showComment($comment, $data, $nr) {
		if (COMMENT_MOD_KEY == $data['type']) {
			++$this->nWaiting;

			if ($this->commSid != $data['sid'])
				return NULL;

			$comment .= '<p><em>Dein Kommentar wird moderiert.</em></p>';
		}

		return $comment;
	}

	// TODO: Was macht diese Methode genau?
	function hook_countComments($com) {
		$q = new Query('SELECT reference, COUNT(*) as count FROM ' .
			JLOG_DB_COMMENTS . ' WHERE type <> \'pingback\' ' .
			'AND type <> \''. COMMENT_MOD_KEY .
			'\' GROUP BY reference');

		if($q->error()) {
			echo "<pre>\n";
			echo $comments->getError();
			echo "</pre>\n";
			die();
		}

		$com = array();

		while($c = $q->fetch())
			$com[$c['reference']] = $c['count'];

		return $com;
	}

	/* Biete in der Mail an den Admin einen Direktlink zum Genehmigen
	 * des Kommentars an.
	 */
	function hook_adminMail($mail, $blogentry, $id) {
		$mail->appendText("\n\nKommentar genehmigen\n" .
			JLOG_PATH .
			'/admin/plugin.php?jplug=CommentModeration&allow=' .
			$id);

		return $mail;
	}

	/*
	function hook_commentorMail($mail, $blog) {
		if ($this->commSid)
			$mail['nomail'] = TRUE;

		return $mail;
	}
	*/

	// Anzeige aller Kommentare im Admin-Center:
	function hook_commentAdminList($comment, $data) {
		global $l;

		if (COMMENT_MOD_KEY == $data['type'])
			return str_replace('/img/JLOG_edit.png\' alt=\'' .
				$l['admin']['change'] . '\' /></a>',
			'/img/JLOG_edit.png\' alt=\'' .
				$l['admin']['change'] .
				'\' /></a> <a title="in der Moderation" href="plugin.php?jplug=CommentModeration&amp;allow='.$data['id'].'">Genehmigen</a>',
			$comment);

		return $comment;
	}

	function hook_adminContent($html) {
		if (isset($_GET['allow'])) {
			$id = mysql_real_escape_string($_GET['allow']);

			$q = new Query('UPDATE ' . JLOG_DB_COMMENTS .
				' SET type=\'\' WHERE id=\''. $id . '\'');

			global $categories;
			global $bbcode;
			global $plugins;

			include(JLOG_BASEPATH . 'scripts' .
				DIRECTORY_SEPARATOR . 'update.php');

			return '<p>Kommentar #' .
				htmlspecialchars($_GET['allow']) .
				' genehmigt.</p>';
		}

		return <<<EOF
<p>Version 0.3</p>
<p>Copyright &#169; 2008, 2009 Robert Bienert, <a href="http://jeenaparadies.net/projects/jlog/wiki/plugins/CommentModeration">Jlog-Plugin CommentModeration</a></p>
<p>Mit diesem Plugin werden alle neuen Kommentare in die Moderation eingestellt und nicht angezeigt. Aus dem Admin-Center heraus k&#246;nnen die Kommentare dann genehmigt oder gel&#246;scht werden.</p>
EOF;
	}
}

