<%
/**
 *	$RCSfile: moderation.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:29 $
 */
%>

<%@ page import="java.util.*,
                 com.jivesoftware.forum.*,
				 com.jivesoftware.forum.util.*"
    errorPage="error.jsp"
%>

<%! // Global vars, methods, etc
    
    // moderation presets
    static final int NONE = 1;
    static final int THREAD_MODERATION = 2;
    static final int THREAD_AND_MESSAGE_MODERATION = 3;
    static final int[] PRESETS = {
        NONE, THREAD_MODERATION, THREAD_AND_MESSAGE_MODERATION
    };
    static final String[] PRESET_LABELS = {
        "None",
        "Threads Only",
        "Threads and Messages"
    };
    
    // A method to get an iterator of non-root message messages
    private Iterator getNonRootMessageIterator(Iterator messages) {
        List list = new ArrayList();
        while (messages.hasNext()) {
            ForumMessage message = (ForumMessage)messages.next();
            if (message.getID() != message.getForumThread().getRootMessage().getID()) {
                list.add(message);
            }
        }
        return list.iterator();
    }
%>

<%@	include file="include/global.jsp" %>

<%  // get parameters
    long forumID = ParamUtils.getLongParameter(request,"forum",-1L);
    long threadID = ParamUtils.getLongParameter(request,"thread",-1L);
    long messageID = ParamUtils.getLongParameter(request,"message",-1L);
    boolean savePreset = ParamUtils.getBooleanParameter(request,"savePreset");
    int moderationPreset = ParamUtils.getIntParameter(request,"moderationPreset",NONE);
    boolean approveThread = ParamUtils.getBooleanParameter(request,"approveThread");
    boolean approveMessage = ParamUtils.getBooleanParameter(request,"approveMessage");
    boolean deleteThread = ParamUtils.getBooleanParameter(request,"deleteThread");
    boolean deleteMessage = ParamUtils.getBooleanParameter(request,"deleteMessage");
    
    // Load up requested forum
    Forum forum = forumFactory.getForum(forumID);
    
    // Change what is being moderated by altering the moderationPreset value
    if (savePreset) {
        // based on the moderation preset, switch:
        switch (moderationPreset) {
            case NONE:
                forum.setModerationDefaultThreadValue(1);
                forum.setModerationMinThreadValue(1);
                forum.setModerationDefaultMessageValue(1);
                forum.setModerationMinMessageValue(1);
                break;
            case THREAD_MODERATION:
                forum.setModerationDefaultThreadValue(0);
                forum.setModerationMinThreadValue(1);
                forum.setModerationDefaultMessageValue(1);
                forum.setModerationMinMessageValue(1);
                break;
            case THREAD_AND_MESSAGE_MODERATION:
                forum.setModerationDefaultThreadValue(0);
                forum.setModerationMinThreadValue(1);
                forum.setModerationDefaultMessageValue(0);
                forum.setModerationMinMessageValue(1);
                break;
            default:
        }
        // done saving, redirect back to this page:
        response.sendRedirect("moderation.jsp?forum="+forumID);
        return;
    }
    
    // approve a thread or message if requested
    if (approveThread || approveMessage) {
        // Get the thread
        ForumThread thread = forum.getThread(threadID);
        if (approveThread) {
            thread.setModerationValue(
                forum.getModerationMinThreadValue(), authToken
            );
			// Also approve the thread's root message
			ForumMessage message = thread.getRootMessage();
			message.setModerationValue(
				forum.getModerationMinMessageValue(), authToken
			);
        }
        if (approveMessage) {
            // Get the message (only if a we're approving a message
            ForumMessage message = thread.getMessage(messageID);
            // set this message's moderation value
            message.setModerationValue(
                forum.getModerationMinMessageValue(), authToken
            );
        }
        response.sendRedirect("moderation.jsp?forum="+forumID);
        return;
    }
    
    // delete a thread or message if requested
    if (deleteThread || deleteMessage) {
        // Get the thread
        ForumThread thread = forum.getThread(threadID);
        if (deleteThread) {
            forum.deleteThread(thread);
        }
        if (deleteMessage) {
            // Get the message
            ForumMessage message = thread.getMessage(messageID);
            thread.deleteMessage(message);
        }
        response.sendRedirect("moderation.jsp?forum="+forumID);
        return;
    }
    
    // determine what type of moderation is turned on
    boolean isThreadModOn = (
        forum.getModerationDefaultThreadValue() < forum.getModerationMinThreadValue()
    );
    boolean isMsgModOn = (
        forum.getModerationDefaultMessageValue() < forum.getModerationMinMessageValue()
    );
    
    // determine the correct value of the moderation preset (the radio button)
    if (isThreadModOn) {
        moderationPreset = THREAD_MODERATION;
    }
    if (isMsgModOn) {
        moderationPreset = THREAD_AND_MESSAGE_MODERATION;
    }
    
    // Get Iterators of messages for each moderation type. Start with a result
    // filter to grab only pending threads/messages
    ResultFilter filter = new ResultFilter();
    filter.setModerationRangeMin(0);
    filter.setModerationRangeMax(0);
    Iterator pendingThreads = forum.threads(filter);
    // This iterator is just of non-root message messages
    Iterator pendingMessages = getNonRootMessageIterator(forum.messages(filter));
%>

<%@ include file="include/header.jsp" %>

<%  // Title of this page and breadcrumbs
    String title = "Moderation";
    String[][] breadcrumbs = {
        {"Main", "main.jsp"},
        {"Moderation", "moderation.jsp?forum="+forumID}
    };
%>
<%@ include file="include/title.jsp" %>

<font size="-1">
Moderation allows to screen threads and messages before they are viewable
in your forum. When moderation is turned on, you must approve or reject
each thread or message below.
</font>

<p>

<font size="-1"><b>Moderation Settings</b></font>
<ul>
    <form action="moderation.jsp">
    <input type="hidden" name="savePreset" value="true">
    <input type="hidden" name="forum" value="<%= forumID %>">
    <table cellpadding="2" cellspacing="0" border="0">
<%  for (int i=0; i<PRESETS.length; i++) {
        String checked = "";
        if (moderationPreset == PRESETS[i]) {
            checked = " checked";
        }
%>
    <tr>
    	<td><input type="radio" name="moderationPreset" value="<%= PRESETS[i] %>" id="rb<%= i %>"<%= checked %>></td>
    	<td><label for="rb<%= i %>"><font size="-1"><%= PRESET_LABELS[i] %></font></label></td>
    </tr>
<%  } %>
    <tr>
        <td>&nbsp;</td>
    	<td><input type="submit" value="Save"></td>
    </tr>
    </table>
    </form>
</ul>

<p>

<%  if (!pendingThreads.hasNext()) { %>
<font size="-1"><b>No Pending Threads</b></font>
<p>
<%  } else { %>
<font size="-1"><b>Pending Threads</b></font>
<ul>
<%  while (pendingThreads.hasNext()) {
        ForumThread pendingThread = (ForumThread)pendingThreads.next();
        User author = pendingThread.getRootMessage().getUser();
%>
    <table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="90%">
    <tr><td>
    <table bgcolor="<%= tableBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
    <tr>
        <td bgcolor="#eeeeee">
            <table cellspacing="2" cellpadding="0" border="0" width="100%">
            <tr>
            	<td width="99%">
                <font size="-1">
                <b><%= pendingThread.getName() %></b>
                <br>
                posted by:
                <%  if (author != null) { %>
                    <b><%= author.getUsername() %></b>
                <%  } else { %>
                    <b><i>Guest</i></b>
                <%  } %>
                on
                <%= JiveGlobals.formatDateTime(pendingThread.getCreationDate()) %>
                </font>
                </td>
            	<td width="1%" nowrap>
                    <a href="moderation.jsp?forum=<%=forumID%>&approveThread=true&thread=<%=pendingThread.getID()%>"
                    ><img src="images/button_approve.gif" width="17" height="17" alt="Approve this thread" border="0"
                    ><a href="moderation.jsp?forum=<%=forumID%>&deleteThread=true&thread=<%=pendingThread.getID()%>"
                    ><img src="images/button_delete.gif" width="17" height="17" alt="Delete this thread" border="0"
                    ></a>
                </td>
            </tr>
            </table>
        </td>
    </tr>
    <tr bgcolor="#ffffff">
        <td>
            <font size="-1">
            <%= pendingThread.getRootMessage().getBody() %>
            </font>
        </td>
    </tr>
    </table>
    </td></tr>
    </table>
    <br>
<%  } %>
</ul>
<%  } %>

<p>

<%  if (!pendingMessages.hasNext()) { %>
<font size="-1"><b>No Pending Messages</b></font>
<p>
<%  } else { %>
<font size="-1"><b>Pending Messages</b></font>
<ul>
<%  while (pendingMessages.hasNext()) {
        ForumMessage pendingMessage = (ForumMessage)pendingMessages.next();
        // If this message is not a thread's root message
        if (pendingMessage.getID() != pendingMessage.getForumThread().getRootMessage().getID()) {
            User author = pendingMessage.getUser();
%>
    <table bgcolor="<%= tableBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="90%">
    <tr><td>
    <table bgcolor="<%= tableBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
    <tr>
        <td bgcolor="#eeeeee">
            <table cellspacing="2" cellpadding="0" border="0" width="100%">
            <tr>
            	<td width="99%">
                <font size="-1">
                <b><%= pendingMessage.getSubject() %></b>
                <br>
                posted by:
                <%  if (author != null) { %>
                    <b><%= author.getUsername() %></b>
                <%  } else { %>
                    <b><i>Guest</i></b>
                <%  } %>
                on
                <%= JiveGlobals.formatDateTime(pendingMessage.getCreationDate()) %>
                </font>
                </td>
            	<td width="1%" nowrap>
                    <a href="moderation.jsp?forum=<%=forumID%>&approveMessage=true&thread=<%=pendingMessage.getForumThread().getID()%>&message=<%=pendingMessage.getID()%>"
                    ><img src="images/button_approve.gif" width="17" height="17" alt="Approve this thread" border="0"
                    ><a href="moderation.jsp?forum=<%=forumID%>&deleteMessage=true&thread=<%=pendingMessage.getForumThread().getID()%>&message=<%=pendingMessage.getID()%>"
                    ><img src="images/button_delete.gif" width="17" height="17" alt="Delete this thread" border="0"
                    ></a>
                </td>
            </tr>
            </table>
        </td>
    </tr>
    <tr bgcolor="#ffffff">
        <td>
            <font size="-1">
            <%= pendingMessage.getBody() %>
            </font>
        </td>
    </tr>
    </table>
    </td></tr>
    </table>
    <br>
<%      }
    }
%>
</ul>
<%  } %>

<p>

<%@ include file="include/footer.jsp" %>