
<%
/**
 *	$RCSfile: settings.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" %>

<%! private static Locale[] locales = Locale.getAvailableLocales();
%>

<%  // Parameters
    long userID = ParamUtils.getLongParameter(request,"user",-1L);
    String username = ParamUtils.getParameter(request,"user");
	String referer = ParamUtils.getParameter(request,"referer");
	boolean save = ParamUtils.getBooleanParameter(request,"doSave");
	String saveButton = ParamUtils.getParameter(request,"saveButton");
	String localeCode = ParamUtils.getParameter(request,"locale");
	String timeZoneID = ParamUtils.getParameter(request,"timeZone");
	int threadRange = ParamUtils.getIntParameter(request,"threadRange",DEFAULT_THREAD_RANGE);
	int messageRange = ParamUtils.getIntParameter(request,"messageRange",DEFAULT_MESSAGE_RANGE);
	String name = ParamUtils.getParameter(request,"name");
	String email = ParamUtils.getParameter(request,"email");
	String occupation = ParamUtils.getParameter(request,"occupation",true);
	String location = ParamUtils.getParameter(request,"location",true);
	String homepage =  ParamUtils.getParameter(request,"homepage",true);
	boolean nameVisible = ParamUtils.getBooleanParameter(request,"nameVisible");
	boolean emailVisible = ParamUtils.getBooleanParameter(request,"emailVisible");
    
    // 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) {}
    }
    
    // Get the default locale
    Locale locale = JiveGlobals.getLocale();
    
	// Setup the correct values for the referer
	if ("Cancel".equals(saveButton) && referer == null) {
		referer = null;
	}
	else if (referer == null) {
		referer = request.getHeader("REFERER");
	}
    
	// Security check: make sure the user using this page is the same as the
	// loaded user. If they're different, set user=null so the rest of the
    // page will think that we're working with a guest user.
	if ((user != null) && (pageUser != null) && (user.getID() != pageUser.getID())) {
		user = null;
		isGuest = true;
	}
    
	// save user settings
	if (save) {
		// Save user settings
		if (user != null) {
            // Save the thread range, message range and timezone
			if (threadRange > -1) {
				user.setProperty("jive.threadRange",new Integer(threadRange).toString());
			}
			if (messageRange > -1) {
				user.setProperty("jive.messageRange",new Integer(messageRange).toString());
			}
			if (timeZoneID != null) {
				user.setProperty("jive.timeZoneID",timeZoneID);
			}
            
			// Save all other user profile properties (for users only)
			if (name != null) {
				user.setName(name);
			}
			if (email != null) {
				user.setEmail(email);
			}
            if (occupation != null && occupation.equals("")) {
                user.deleteProperty("occupation");
            } else if (occupation != null) {
				user.setProperty("occupation",occupation);
			}
            if (location != null && location.equals("")) {
                user.deleteProperty("location");
            } else if (location != null) {
				user.setProperty("location",location);
			}
            if (homepage != null && homepage.equals("")) {
                user.deleteProperty("homepage");
            } else if (homepage != null) {
				user.setProperty("homepage",homepage);
			}
			user.setNameVisible(nameVisible);
			user.setEmailVisible(emailVisible);
		}
        // Save guest settings
		else {
			if (threadRange > -1) {
				SkinUtils.store(request,response,"jive.threadRange",
					new Integer(threadRange).toString(),(int)MONTH);
			}
			if (messageRange > -1) {
				SkinUtils.store(request,response,"jive.messageRange",
					new Integer(messageRange).toString(),(int)MONTH);
			}
			if (timeZoneID != null) {
				SkinUtils.store(request,response,"jive.timeZoneID",timeZoneID,(int)MONTH);
			}
		}
		// Redirect to the appropriate page:
		String redirectURL = null;
		if (referer != null) {
			redirectURL = "settings.jsp?referer=" + java.net.URLEncoder.encode(referer);
		}
		if ("Save and Return".equals(saveButton)) {
			redirectURL = referer;
		}
		else if ("Cancel".equals(saveButton) && referer == null) {
			redirectURL = "index.jsp";
		}
		else if ("Cancel".equals(saveButton) && referer != null) {
			redirectURL = referer;
		}
		else {
			if (user != null) {
				redirectURL += "&user=" + user.getID();
			}
		}
		response.sendRedirect(redirectURL);
		return;
	} // end save
%>

<%  String title = "Jive Software: Communities: Settings"; %>
<%@ include file="include/header.jsp" %>

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

<%	// Getting to this point means there were no user settings to save, so
	// we should load up settings from the user object or from the session 
	// (in the case of a guest)
    
    // User settings
	if (user != null) {
		try {
            threadRange = Integer.parseInt(user.getProperty("jive.threadRange"));
        } catch (Exception e) {}
		try { 
            messageRange = Integer.parseInt(user.getProperty("jive.messageRange"));
        } catch (Exception e) {}
		timeZoneID = user.getProperty("jive.timeZoneID");
		// load up other user profile properties
		String nameProp = user.getName();
		if (nameProp != null) {
			name = nameProp;
		}
		String emailProp = user.getEmail();
		if (emailProp != null) {
			email = emailProp;
		}
		String occupationProp = user.getProperty("occupation");
		if (occupationProp != null) {
			occupation = occupationProp;
		}
		String locationProp = user.getProperty("location");
		if (locationProp != null) {
			location = locationProp;
		}
		String homepageProp = user.getProperty("homepage");
		if (homepageProp != null) {
			homepage = homepageProp;
		}
		nameVisible = user.isNameVisible();
		emailVisible = user.isEmailVisible();
	}
    // Guest settings
	else if (isGuest) {
		try { 
			threadRange = Integer.parseInt(
				SkinUtils.retrieve(request,response,"jive.threadRange").trim()
			); 
		} catch (Exception e) {}
		try {
			messageRange = Integer.parseInt(
				SkinUtils.retrieve(request,response,"jive.messageRange").trim()
			);
		} catch (Exception e) {}
		timeZoneID = SkinUtils.retrieve(request,response,"jive.timeZoneID");
	}
	// one more check for the timezone -- if the code is null, that means no
	// value was retrieved, so set it to the default:
	if (timeZoneID == null) {
		timeZoneID = JiveGlobals.getTimeZone().getID();
	}
%>

<span class="header">
<%  if (rootBreadcrumbText != null && rootBreadcrumbLink != null) { %>
<a href="<%= rootBreadcrumbLink %>" class="header"
><%= rootBreadcrumbText %></a>
&raquo;
<%  } %>
<a href="index.jsp" class="header" title="Refresh the forum listing"
><%= indexPageHeaderText %></a>
&raquo;
Your Settings
</span>
<p>

<font size="-1" color="<%= deckTextColor %>">
Change your viewing preferences to control how you see forum data.
<% if (user != null) { %>
Change your profile to set the information you would like others to see about
you -- you're free to leave any of the information blank.
<%  } %>
</font>
<p>

<form action="settings.jsp">
<input type="hidden" name="referer" value="<%= ((referer!=null)?referer:"") %>">
<input type="hidden" name="doSave" value="true">
<%	if (user != null) { %>
<input type="hidden" name="user" value="<%= userID %>">
<%	} %>

<table bgcolor="<%= tableBorderColor %>" cellspacing="0" cellpadding="0" border="0">
<td>
<table bgcolor="<%= tableBorderColor %>" cellspacing="1" cellpadding="3" border="0" width="100%">
<tr bgcolor="#eeeeee">
	<td>
	<font size="-1">
	<b>Viewing Preferences</b>
	</font>
	</td>
</tr>
<tr bgcolor="#ffffff">
	<td align="center">
	<table cellpadding="3" cellspacing="0" border="0">
	<tr>
		<td align="right"><font size="-1">Topics per forum listing:</font></td>
		<td>
        <font size="-1">
		<select size="1" name="threadRange">
		<%	for (int i=0; i<THREAD_RANGES.length; i++) {
				String selected = ((THREAD_RANGES[i]==threadRange)?" selected":"");
		%>
		<option value="<%= THREAD_RANGES[i] %>"<%= selected %>><%= THREAD_RANGES[i] %>
		<%	} %>
		</select>
        </font>
		</td>
	</tr>
	<tr>
		<td align="right"><font size="-1">Messages per topic lisiting:</font></td>
		<td>
        <font size="-1">
		<select size="1" name="messageRange">
		<%	for (int i=0; i<MESSAGE_RANGES.length; i++) {
				String selected = ((MESSAGE_RANGES[i]==messageRange)?" selected":"");
		%>
		<option value="<%= MESSAGE_RANGES[i] %>"<%= selected %>><%= MESSAGE_RANGES[i] %>
		<%	} %>
		</select>
        </font>
		</td>
	</tr>
	<tr>
		<td align="right"><font size="-1">Time zone:</font></td>
		<td>
        <font size="-1">
		<select size="1" name="timeZone">
		<%	for (int i=0; i<timeZones.length; i++) {
				String selected = (timeZones[i][0].equals(timeZoneID))?" selected":"";
		%>
		<option value="<%= timeZones[i][0] %>"<%= selected %>><%= timeZones[i][1] %>
		<%	} %>
		</select>
        </font>
		</td>
	</tr>
	</table>
	</td>
</tr>
</table>
</td>
</table>

<%	if (user != null && pageUser != null && (user.getID() == pageUser.getID())) { %>

<p>

<table bgcolor="<%= tableBorderColor %>" cellspacing="0" cellpadding="0" border="0">
<td>
<table bgcolor="<%= tableBorderColor %>" cellspacing="1" cellpadding="3" border="0" width="100%">
<tr bgcolor="#eeeeee">
	<td>
	<font size="-1">
	<b>Your Profile</b>
	</font>
	</td>
</tr>
<tr bgcolor="#ffffff">
	<td align="center">
	<table cellpadding="3" cellspacing="0" border="0">
	<tr>
		<td align="right"><font size="-1">Name visible to others:</font></td>
		<td>
        <font size="-1">
		Yes <input type="radio" name="nameVisible" value="true"
		<%= ((nameVisible)?"checked":"") %>>
		No <input type="radio" name="nameVisible" value="false"
		<%= ((!nameVisible)?"checked":"") %>>
        </font>
		</td>
	</tr>
	<tr>
		<td align="right"><font size="-1">Email address visible to others:</font></td>
		<td>
        <font size="-1">
		Yes <input type="radio" name="emailVisible" value="true"
		<%= ((emailVisible)?"checked":"") %>>
		No <input type="radio" name="emailVisible" value="false"
		<%= ((!emailVisible)?"checked":"") %>>
        </font>
		</td>
	</tr>
	<tr>
		<td align="right"><font size="-1">Occupation:</font></td>
		<td>
        <font size="-1">
		<input type="text" name="occupation" value="<%= ((occupation!=null)?occupation:"") %>"
		 size="30" maxlength="255">
        </font>
		</td>
	</tr>
	<tr>
		<td align="right"><font size="-1">Location:</font></td>
		<td>
        <font size="-1">
		<input type="text" name="location" value="<%= ((location!=null)?location:"") %>"
		 size="30" maxlength="255">
        </font>
		</td>
	</tr>
	<tr>
		<td align="right"><font size="-1">Homepage:</font></td>
		<td>
        <font size="-1">
		<input type="text" name="homepage" value="<%= ((homepage!=null)?homepage:"") %>"
		 size="30" maxlength="255">
        </font>
		</td>
	</tr>
	</table>
	</td>
</tr>
</table>
</td>
</table>

<%	} // end if user != null %>

<p>

<center>
<input type="submit" value="Save" name="saveButton">
<%	if (referer != null) { %>
<input type="submit" value="Save and Return" name="saveButton">
<%	} %>
<input type="submit" value="Cancel" name="saveButton">
</center>

</form>

<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" %>
