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

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

<%@ include file="include/branding/style.jsp" %>

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

<%! // global variables
    static final int DEFAULT_RESULT_COUNT = 10;
    static final int[] RESULT_COUNTS = { 10, 15, 30 };
%>

<%  // Get parameters
    long forumID = ParamUtils.getLongParameter(request,"forum",-1L);
    long[] forumIDs = ParamUtils.getLongParameters(request,"forums",-1L);
    long threadID = ParamUtils.getLongParameter(request,"thread",-1L);
    long userID = ParamUtils.getLongParameter(request,"user",-1L);
    String username = ParamUtils.getParameter(request,"user");
    String queryText = ParamUtils.getParameter(request,"q");
    long dateLo = ParamUtils.getLongParameter(request,"dateLo",-1L);
    long dateHi = ParamUtils.getLongParameter(request,"dateHi",-1L);
    boolean search = ParamUtils.getBooleanParameter(request,"search");
    boolean options = ParamUtils.getBooleanParameter(request,"options");
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",DEFAULT_RESULT_COUNT);
    
    // Optionally load a forum
    Forum forum = null;
    if (forumID != -1L) {
        forum = forumFactory.getForum(forumID);
    }
    
    // Optionally load a thread
    ForumThread thread = null;
    if (threadID != -1L && forum != null) {
        thread = forum.getThread(threadID);
    }
    
    // Try to load a user (try by userID first, then try username)
    UserManager userManager = forumFactory.getUserManager();
    User user = null;
    try {
        user = userManager.getUser(userID);
    }
    catch (UserNotFoundException unfe) {
        try {
            user = userManager.getUser(username);
        }
        catch (UserNotFoundException ignored) {}
    }
    if (user != null) {
        userID = user.getID();
        username = user.getUsername();
    }
%>

<%  String title = titlePrefix + ": Search"; %>
<%@ include file="include/header.jsp" %>

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

<span class="header">
    Search
    <br>
    <font size="-1">
    <%  if (rootBreadcrumbText != null && rootBreadcrumbLink != null) { %>
    <a href="<%= rootBreadcrumbLink %>" class="header"
    ><%= rootBreadcrumbText %></a>
    &raquo;
    <%  } %>
    <a href="index.jsp" class="header" title="Go back to the forum listing"
    >Forums</a>
<%  if (forum != null) { %>
    &raquo;
    <a href="forum.jsp?forum=<%= forumID %>" class="header" title="Go back to the topic list"
    ><%= forum.getName() %></a>
<%  } %>
    </font>
</span>
<p>

<center>

<form action="search.jsp" name="searchForm">
<input type="hidden" name="forum" value="<%= forumID %>">
<input type="hidden" name="search" value="true">

<table bgcolor="#cccccc" cellpadding="0" cellspacing="0" border="0">
<tr><td>
    <table bgcolor="#cccccc" cellpadding="5" cellspacing="1" border="0">
    <tr bgcolor="#ffffff">
        <td>
        <input type="text" name="q" size="40" maxlength="60"
         value="<%= ((queryText!=null)?queryText:"") %>">
        <input type="submit" value="Search">
        </td>
    </tr>
    <tr bgcolor="#eeeeee">
        <td>
        <font size="-1">
        <a href="search.jsp?&forum=<%=forumID%>&thread=<%=threadID%>&q=<%= (queryText!=null)?java.net.URLEncoder.encode(queryText):"" %>&options=<%= (options?false:true) %>"
        ><%= (options?"&laquo; Hide":"&raquo; Show") %> Options</a>
        </font>
        </td>
    </tr>
    <%  if (options) { %>
    <tr bgcolor="#eeeeee">
        <td>
        <font size="-1">
        Show
        <select size="1" name="range">
<%      for (int i=0; i<RESULT_COUNTS.length; i++) {
            String selected = "";
            if (range == RESULT_COUNTS[i]) {
                selected = " selected";
            }
%>
        <option value="<%= RESULT_COUNTS[i] %>"<%= selected %>><%= RESULT_COUNTS[i] %>
        <%  } %>
        </select>
        results per page.
        </font>
        </td>
    </tr>
    <%  } %>
    </table>
</td></tr>
</table>

</form>

</center>

<font size="-1">

<%  if (search) { %>

    <p>
    
<%      // Create a query object, do a search
        Query query = forum.createQuery();
        query.setQueryString(queryText);
        // execute the query
        Iterator results = query.results(start,range);
        
        // display results
        if (!results.hasNext()) {
%>
        <center><i>No results</i></center>
        <p>
<%      }
        else {
            int numResults = query.resultCount();
%>
        <%= numResults %> search results for "<b><%= queryText %></b>".
        Showing <%= range %> results per page.
        <p>

<%      int count = start + 1;
        String[] queryWords = StringUtils.toLowerCaseWordArray(queryText);
        int messageRange = myEnv.du.getMessageRange(request, response, pageUser);
        while (results.hasNext()) {
            ForumMessage message = (ForumMessage)results.next();
		    String body = StringUtils.escapeHTMLTags(message.getUnfilteredBody());
		    body = StringUtils.chopAtWord(body, 150) + " ...";
		    body = StringUtils.highlightWords(body, queryWords, "<font style='background-color:#ffff00'><b>", "</b></font>");
            long mID = message.getID();
            long tID = message.getForumThread().getID();
            long fID = message.getForumThread().getForum().getID();
            Forum f = forumFactory.getForum(fID);
            ForumThread t = f.getThread(tID);
            int messageCount = t.getMessageCount();
            int numPages = (int)Math.ceil((double)messageCount/(double)messageRange);
            int msgStart = (numPages-1) * messageRange;
            User author = message.getUser();
            String authorUsername = "<i>Guest</i>";
            if (author != null) {
                authorUsername = author.getUsername();
            }
%>
        <table cellpadding="2" cellspacing="0" border="0">
        <tr>
        <td valign="top">
            <font size="-1">
            <%= count++ %>)
            </font>
        </td>
        <td>
            <font size="-1">
            <a href="thread.jsp?forum=<%= fID %>&thread=<%= tID %>&message=<%= mID %>&redirect=true&hilite=true&q=<%= java.net.URLEncoder.encode(queryText) %>"
            ><%= message.getSubject() %></a>
            <br>
            <font color="#666666">
            Posted on <%= SkinUtils.formatDate(request,response,pageUser,message.getCreationDate()) %>
            by <%= authorUsername %>
            </font>
            </font>
        </td>
        </tr>
        </table>
        <ul>
        <%= body %>
        </ul>
<%      } // end while loop
%>

</font>

<%
	int total = (numResults/range)+1;
	int i = (start/range)+1;
	int lTotal = i-1;
	int rTotal = total-i;
	int lCount, rCount;
	
	if (i < 5) { lCount = lTotal; }
	else { lCount = 5; }
	
	if (i+5 > total) {
		rCount = total-i;
		//now, add as much as we can to other side
		lCount += 5-(total-i) > lTotal-lCount? lTotal-lCount: 5-(total-i);
	}
	else {
		rCount = 5;
		//Add in more to right if possible;
		if (lCount < 5) {
			rCount += 5-lCount > rTotal-rCount? rTotal-rCount: 5-lCount;
		}
	}
    String href = "search.jsp?forum=" + forumID + "&thread=" + threadID
        + "&q=" + java.net.URLEncoder.encode(queryText) + "&range=" + range
        + "&search=true";
%>
<center>
<table cellpadding="4" cellspacing="2" border="0">
<tr>
    <td>
    <font size="-1">
    <%	if( (start-range) >= 0 ) { %>
    <a href="<%= href %>&start=<%= (start-range) %>">&laquo; Previous Page</a>
    <%	} %>
    &nbsp;
    </font>
    </td>
<%      for( int idx=(i-lCount); idx<=(i+rCount); idx++ ) {
            boolean onCurrent = !((range*(idx-1))!=start);
            String color = (onCurrent)?"blue":"red";
%>
    <td align="center">
    <font size="-1">
    <%  if (onCurrent) { %>
    <a href="<%= href %>&start=<%= range*(idx-1) %>"
     style="color:#000000;background-color:#cccccc;"><%= idx %></a>
    <%  } else { %>
    <a href="<%= href %>&start=<%= range*(idx-1) %>"
     ><%= idx %></a>
    <%  } %>
    </font>
    </td>
    <%	} %>
    <td>
    <font size="-1">
    &nbsp;
    <%	if( (start+range) <= numResults ) { %>
    <a href="<%= href %>&start=<%= (start+range) %>">Next Page &raquo;</a>
    <%	} %>
    </font>
    </td>
</tr>
</table>
</center>

<%      } // end else there are results
%>

<%  } // end search block %>

<!--
<ul>
    <li>Ability to search by all forums
    <li>Ability to search one forum
    <li>Ability to search a set of forums
    <li>Ability to search within a given thread
    <li>Search within date range
    <li>Search by a user
</ul>
-->

<script language="JavaScript" type="text/javascript">
<!--
document.searchForm.q.focus();
//-->
</script>

<br>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
    <td align="right">
    <a href="http://www.jivesoftware.com/poweredby/" target="_blank"
    ><img src="images/powered_by_jive.gif" width="100" height="30" alt="Powered by Jive" border="0"></a>
    </td>
</tr>
</table>

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

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