<%@ page contentType="text/html;charset=GBK" %>
<%
/*
 * $RCSfile: watches.jsp,v $
 * $Revision: 1.1.1.1 $
 * $Date: 2002/09/09 13:50:09 $
 */
%>

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

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

<%  // Get parameters
	long forumID = ParamUtils.getLongParameter(request,"forum",-1);
	long threadID = ParamUtils.getLongParameter(request,"thread",-1);
	int start = ParamUtils.getIntParameter(request,"start",0);
	int range = ParamUtils.getIntParameter(request,"range",10);
    int type = ParamUtils.getIntParameter(request,"type",WatchManager.NORMAL_WATCH);
    boolean add = ParamUtils.getBooleanParameter(request,"add");
    boolean remove = ParamUtils.getBooleanParameter(request,"remove");
    boolean deleteWatch = ParamUtils.getBooleanParameter(request,"deleteWatch");
    boolean flat = ParamUtils.getBooleanParameter(request,"flat",true);
    boolean edit = ParamUtils.getBooleanParameter(request,"edit");
    boolean save = ParamUtils.getBooleanParameter(request,"save");
    boolean doExpire = ParamUtils.getBooleanParameter(request,"doExpire");
    boolean expire = ParamUtils.getBooleanParameter(request,"expire");
    boolean back = ParamUtils.getBooleanParameter(request, "back");
    String referrer = ParamUtils.getParameter(request,"referrer");
    String redirect = ParamUtils.getParameter(request,"redirect");
    // thread IDs of threads we want to add email watches to
    String[] emailNotifyThreadIDs = request.getParameterValues("emailNotifyThreadID");
    // thread IDs of threads we want to add email watches to (the previous value)
    String[] oldEmailNotifyThreadIDs = request.getParameterValues("oldEmailNotifyThreadID");
    // thread IDs of the threads we want to remove all watches on
    String[] deleteWatchThreadIDs = request.getParameterValues("deleteWatchThreadID");
    // thread IDs of the threads we want to make un-deletable
    String[] saveWatchThreadIDs = request.getParameterValues("saveWatchThreadID");

    // null checks
    if (emailNotifyThreadIDs == null) { emailNotifyThreadIDs = new String[0]; }
    if (oldEmailNotifyThreadIDs == null) { oldEmailNotifyThreadIDs = new String[0]; }
    if (deleteWatchThreadIDs == null) { deleteWatchThreadIDs = new String[0]; }
    if (saveWatchThreadIDs == null) { saveWatchThreadIDs = new String[0]; }

    // set the referrer
    if (referrer == null) {
        referrer = request.getHeader("REFERER");
    }
    // go back if requested
    if (back) {
        if (referrer != null) {
            response.sendRedirect(referrer);
        }
        else {
            response.sendRedirect("index.jsp");
        }
        return;
   }

    // check to see if the user wants to automatically add an email notification
    boolean alwaysEmailNotify = false;
    try {
        alwaysEmailNotify = Boolean.valueOf(
            pageUser.getProperty("alwaysEmailNotify")).booleanValue();
    } catch (Exception e) {}

    // status message to the user
    String message = (String)session.getAttribute("message");
    if (message != null) {
        session.removeAttribute("message");
    }

    // The forum we're interested in
    Forum forum = null;
	// Get the forum
    if (forumID > -1L) {
        forum = myEnv.getForumFactory().getForum(forumID);
    }

    // The watch manager lets us work with the watches
    WatchManager watchManager = myEnv.getForumFactory().getWatchManager();

    // Get an iterator of watched threads for this user
    Iterator watchedThreads = null;
    if (forumID > -1L) {
        watchedThreads = watchManager.getWatchedForumThreads(forum, pageUser, WatchManager.NORMAL_WATCH);
    }
    else {
        watchedThreads = watchManager.getWatchedForumThreads(pageUser, WatchManager.NORMAL_WATCH);
    }

    // All forums
    Iterator forums = myEnv.getForumFactory().forums();

    // total number of watches
    int watchCount = 0;
    if (forumID > -1L) {
        watchCount = watchManager.getWatchCount(forum, pageUser, WatchManager.NORMAL_WATCH);
    }
    else {
        watchCount = watchManager.getWatchCount(pageUser, WatchManager.NORMAL_WATCH);
    }

    boolean errors = false;
    String errorMessage = "";
    if (add) {
        ForumThread thread = forum.getThread(threadID);
        watchManager.createWatch(pageUser, thread, type);
        if (type == WatchManager.NORMAL_WATCH) {
            if (alwaysEmailNotify) {
                watchManager.createWatch(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH);
            }
        }
        if (redirect != null) {
            response.sendRedirect(redirect);
        }
        else {
            response.sendRedirect("thread.jsp?forum="+forumID+"&thread="+thread.getID());
        }
        return;
    }
    else if (doExpire) {
        ForumThread thread = forum.getThread(threadID);
        watchManager.setWatchExpirable(pageUser, thread, type, expire);
        if (watchManager.isWatchedThread(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH)) {
            watchManager.setWatchExpirable(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH, expire);
        }
        if (redirect != null) {
            response.sendRedirect(redirect);
        }
        else {
            response.sendRedirect("thread.jsp?forum="+forumID+"&thread="+thread.getID());
        }
        return;
    }
    else if (remove) {
        ForumThread thread = forum.getThread(threadID);
        watchManager.deleteWatch(pageUser, thread, type);
        if (type == WatchManager.NORMAL_WATCH) {
            if (watchManager.isWatchedThread(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH)) {
                watchManager.deleteWatch(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH);
            }
        }
        if (redirect != null) {
            response.sendRedirect(redirect);
        }
        else {
            response.sendRedirect("thread.jsp?forum="+forumID+"&thread="+thread.getID());
        }
        return;
    }
    else if (deleteWatch) {
        for (int i=0; i<deleteWatchThreadIDs.length; i++) {
            forumID = ParamUtils.getLongParameter(request, "thread"+deleteWatchThreadIDs[i]+"forumID", -1L);
            forum = myEnv.getForumFactory().getForum(forumID);
            ForumThread thread = forum.getThread(Long.parseLong(deleteWatchThreadIDs[i]));
            watchManager.deleteWatch(pageUser, thread, WatchManager.NORMAL_WATCH);
            if (watchManager.isWatchedThread(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH)) {
                watchManager.deleteWatch(pageUser, thread, WatchManager.EMAIL_NOTIFY_WATCH);
            }
        }
        if (deleteWatchThreadIDs.length > 0) {
            // Get a watch manager
            watchManager = myEnv.getForumFactory().getWatchManager();

            // Get an iterator of watched threads for this user
            watchedThreads = watchManager.getWatchedForumThreads(pageUser, WatchManager.NORMAL_WATCH);

            // total number of watches
            watchCount = watchManager.getWatchCount(pageUser, WatchManager.NORMAL_WATCH);
        }
    }
%>

<%  String title ="论坛 - 监控信息"; %>
<%@ include file="header.jsp" %>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
    <td width="99%" valign="top">

    <font face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">
    <b>监控信息</b>
    </font>
    <br>

    <%-- Breadcrumbs --%>

    <font size="-1" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">
    <b>
    <a href="<%= JiveGlobals.getJiveProperty("skin.default.homeURL") %>"
    >首页</a>
    &raquo;
    <a href="index.jsp" title="返回论坛列表"
    >论坛</a>
    &raquo;
    <a href="watches.jsp">您的监控信息</a>
    <%  if (forumID != -1) { %>
    &raquo;
    <a href="forum.jsp?forum=<%= forumID %>"><%= forum.getName() %></a>
    <%  } %>
    </b>
    </font>
    <p>
    <font size="-1" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
    color="<%= JiveGlobals.getJiveProperty("skin.default.textColor") %>">
	管理或者删除监控信息,主题监控可以允许您跟踪任何您感兴趣的主题,您可以通过电子邮件
	收到主题更新的监控信息,为了方便您使用,智能监控在一个主题超过30天没有更新的的情况下就
	会自动的从您的列表中删除,您可以点击“保存自动监控”来激活此功能.
    </font>
    </td>
    <td valign="top" width="1%" align="center">
    <%@ include file="loginbox.jsp" %>
    </td>
</tr>
</table>

<p>

<form action="watches.jsp">
<font size="-1" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
论坛监控:
</font>
<select size="1" name="forum" onchange="location.href='watches.jsp?forum='+this.options[this.selectedIndex].value;">
<option value="-1">所有论坛 (默认)
<%  for (Iterator iter=myEnv.getForumFactory().forums(); iter.hasNext();) {
        Forum f = (Forum)iter.next();
        String selected = "";
        if (f.getID() == forumID) {
            selected = " selected";
        }
%>
    <option value="<%= f.getID() %>"<%= selected %>>&nbsp;&#149;&nbsp; <%= f.getName() %>
<%  } %>
</select>
<noscript>
<input type="submit" value="Go">
</noscript>
</form>
<%  if (forumID != -1) { %>
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    下面是 <b><%= forum.getName() %></b> 论坛的监控.
    [<a href="watches.jsp">显示全部监控</a>]
    </font>
<%  } %>

<%  if (!watchedThreads.hasNext()) { %>

    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
    color="<%= JiveGlobals.getJiveProperty("skin.default.textColor") %>">
    <%  if (forumID == -1) { %>
您没有监控任何主题,请点击每个主题页面中的"监控主体"链接.
    <%  } else { %>
您没有监控任何主题,请点击<a href="forum.jsp?forum=<%= forumID %>">主题页面</a>中的"监控主体"链接.
    <%  } %>
    </font>
    <p>

<%  } else { %>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<form action="watches.jsp">
<td width="99%">
    &nbsp;
</td>
</form>
<form action="watches.jsp" method="post">
<input type="hidden" name="deleteWatch" value="true">
<input type="hidden" name="start" value="<%= start %>">
<input type="hidden" name="range" value="<%= range %>">
<%  if (referrer != null) { %>
<input type="hidden" name="referrer" value="<%= referrer %>">
<%  } %>
<%  if (watchedThreads.hasNext()) { %>
<td width="1%" valign="bottom">&nbsp;</td>
<%  } %>
</tr>
</table>
<%  if (watchedThreads.hasNext()) { %>
<table bgcolor="<%= JiveGlobals.getJiveProperty("skin.default.tableBorderColor") %>"
 cellpadding="0" cellspacing="0" border="0" width="100%">
<tr><td>
<table bgcolor="<%= JiveGlobals.getJiveProperty("skin.default.tableBorderColor") %>"
 cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="<%= JiveGlobals.getJiveProperty("skin.default.tableHeaderBgColor") %>">
    <td width="1%">&nbsp;</td>
    <td width="97%"><font class=p2 color="<%= JiveGlobals.getJiveProperty("skin.default.tableHeaderTextColor") %>" face="<%= JiveGlobals.getJiveProperty("skin.default.headerFontFace") %>"><b>&nbsp; 主 题</b></font></td>
    <td width="1%" nowrap align="center"><font class=p2 color="<%= JiveGlobals.getJiveProperty("skin.default.tableHeaderTextColor") %>" face="<%= JiveGlobals.getJiveProperty("skin.default.headerFontFace") %>"><b>&nbsp; 选 项 &nbsp;</b></font></td>
    <td width="1%" align="center" nowrap><font class=p2 color="<%= JiveGlobals.getJiveProperty("skin.default.tableHeaderTextColor") %>" face="<%= JiveGlobals.getJiveProperty("skin.default.headerFontFace") %>"><b>&nbsp; 删 除 &nbsp;</b></font></td>
</tr>
<%      int watchStart = start;
        while (watchStart-- > 0 && watchedThreads.hasNext()) {
            watchedThreads.next();
        }
        int watchRange = range;
        while (watchRange-- > 0 && watchedThreads.hasNext()) {
            ForumThread t = (ForumThread)watchedThreads.next();
            Forum f = t.getForum();
            long tID = t.getID();
            long fID = f.getID();
            User author = t.getRootMessage().getUser();
            boolean isEmailUpdatable = watchManager.isWatchedThread(pageUser, t, WatchManager.EMAIL_NOTIFY_WATCH);
            boolean isExpirable = watchManager.isWatchExpirable(pageUser, t, WatchManager.NORMAL_WATCH);
            boolean isNew = (t.getModifiedDate().getTime() > lastVisited.getTime());
%>
<tr bgcolor="<%= JiveGlobals.getJiveProperty("skin.default.tableRowColor1") %>">
    <td nowrap width="1%" valign="top">
    <%  if (isNew) { %>
        <img src="images/topic_new.gif" width="8" height="8" vspace="4" hspace="2" border="0">
    <%  } else { %>
        <img src="images/topic_old.gif" width="8" height="8" vspace="4" hspace="2" border="0">
    <%  } %>
    </td>
    <td width="97%">
        <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
        color="<%= JiveGlobals.getJiveProperty("skin.default.textColor") %>">
        <a href="thread.jsp?forum=<%= fID %>&thread=<%= tID %>" title="<%= f.getName() %>"
         ><b><%= t.getName() %></b></a>
        </font>
        <br>
        <font class=p2 color="#333333" face="<%= JiveGlobals.getJiveProperty("skin.default.headerFontFace") %>">
        <%  if (author != null) { %>
        &nbsp;&nbsp; 发贴人: <a href="profile.jsp?user=<%= author.getID() %>"><%= author.getUsername() %></a>
        <%  } else { %>
        发贴人: <i>客人</i>
        <%  } %>
        在<a href="forum.jsp?forum=<%= f.getID() %>"><%= f.getName() %></a>论坛.
        <br>
        &nbsp;&nbsp; (最后更新: <%= SkinUtils.formatDate(request, response, pageUser, t.getModifiedDate()) %>)
        </font>
    </td>
    <td width="1%" nowrap>
        <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
<%  if (isEmailUpdatable) { %>
&#149; <a href="watches.jsp?forum=<%= fID %>&thread=<%= tID %>&remove=true&type=<%= WatchManager.EMAIL_NOTIFY_WATCH %>&redirect=<%= "watches.jsp?forum="+forumID %>"
>删除邮件提醒</a
<%  } else { %>
&#149; <a href="watches.jsp?forum=<%= fID %>&thread=<%= tID %>&add=true&type=<%= WatchManager.EMAIL_NOTIFY_WATCH %>&redirect=<%= "watches.jsp?forum="+forumID %>"
>加入邮件提醒</a
<%  } %>
<%  if (isExpirable) { %>
><br>&#149; <a href="watches.jsp?forum=<%= fID %>&thread=<%= tID %>&doExpire=true&expire=false&redirect=<%= "watches.jsp?forum="+forumID %>"
>保存自动监控</a>
<%  } else { %>
><br>&#149; <a href="watches.jsp?forum=<%= fID %>&thread=<%= tID %>&doExpire=true&expire=true&redirect=<%= "watches.jsp?forum="+forumID %>"
>删除自动监控</a>
<%  } %>
        </font>
    </td>
    <td width="1%" align="center">
        <table cellpadding="0" cellspacing="0" border="0" width="60">
        <tr><td align="center">
        <input type="hidden" name="<%= "thread"+tID+"forumID" %>" value="<%= fID %>">
        <input type="checkbox" name="deleteWatchThreadID" value="<%= tID %>">
        </td></tr>
        </table>
    </td>
</tr>
<%      } // end while %>
</table>
</td></tr>
</table>

<p>

<table cellpadding="3" cellspacing="0" border="0" width="100%">
<tr><td width="1%">
    <img src="images/topic_new.gif" width="8" height="8" vspace="2" hspace="2" border="0">
</td><td width="98%">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
    color="<%= JiveGlobals.getJiveProperty("skin.default.textColor") %>">
    <i>表明在您最后访问有新消息发表.</i>
    </font>
</td><td width="1%">
    <input type="submit" value="删 除">
</td></tr>
</table>

<p>

<%  if (start > 0 || ((start+range) < watchCount)) { %>
<table cellpadding="2" cellspacing="0" border="0" width="100%">
<tr>
    <td width="1%" nowrap>
    <%  if (start > 0) { %>
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.headerFontFace") %>">
    <a href="watches.jsp?start=<%= (start-range) %>&range=<%= range %>"
     ><img src="images/prev.gif" width="10" height="10" alt="Previous <%= range %> watches" hspace="3" border="0"></a>
    <a href="watches.jsp?start=<%= (start-range) %>&range=<%= range %>"
     >上 <%= range %> 个监控</a>
    </font>
    <%  } %>
    </td>
    <td width="98%">&nbsp;</td>
    <td width="1%" nowrap>
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.headerFontFace") %>">
    <%  if ((start+range) < watchCount) { %>
    <a href="watches.jsp?start=<%= (start+range) %>&range=<%= range %>"
     >下 <%= range %> 个监控</a>
    <a href="watches.jsp?start=<%= (start+range) %>&range=<%= range %>"
     ><img src="images/next.gif" width="10" height="10" alt="Next <%= range %> watches" hspace="3" border="0"></a>
    <%  } %>
    </font>
    </td>
</tr>
</table>
<%  } %>

<p>

<%  } // end else %>

</form>

<%  } // end if no watches %>

<%  if (referrer != null) { %>
<br>
<center>
    <form action="watches.jsp">
    <input type="hidden" name="back" value="true">
    <%  if (referrer != null) { %>
    <input type="hidden" name="referrer" value="<%= referrer %>">
    <%  } %>
    <input type="submit" value="返 回">
    </form>
</center>
<%  } %>

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