001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: Apr 24, 2003 / 10:15:07 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import java.text.SimpleDateFormat;
046: import java.util.Date;
047: import java.util.GregorianCalendar;
048: import java.util.HashMap;
049: import java.util.List;
050: import java.util.Map;
051:
052: import net.jforum.Command;
053: import net.jforum.JForumExecutionContext;
054: import net.jforum.SessionFacade;
055: import net.jforum.dao.DataAccessDriver;
056: import net.jforum.dao.ForumDAO;
057: import net.jforum.dao.ModerationDAO;
058: import net.jforum.entities.Forum;
059: import net.jforum.entities.MostUsersEverOnline;
060: import net.jforum.entities.UserSession;
061: import net.jforum.repository.ForumRepository;
062: import net.jforum.repository.SecurityRepository;
063: import net.jforum.security.SecurityConstants;
064: import net.jforum.util.I18n;
065: import net.jforum.util.preferences.ConfigKeys;
066: import net.jforum.util.preferences.SystemGlobals;
067: import net.jforum.util.preferences.TemplateKeys;
068: import net.jforum.view.admin.ModerationAction;
069: import net.jforum.view.forum.common.ForumCommon;
070: import net.jforum.view.forum.common.PostCommon;
071: import net.jforum.view.forum.common.TopicsCommon;
072: import net.jforum.view.forum.common.ViewCommon;
073:
074: /**
075: * @author Rafael Steil
076: * @version $Id: ForumAction.java,v 1.76 2007/09/03 12:24:32 rafaelsteil Exp $
077: */
078: public class ForumAction extends Command {
079: /**
080: * List all the forums (first page of forum index)?
081: */
082: public void list() {
083: this .setTemplateName(TemplateKeys.FORUMS_LIST);
084:
085: this .context.put("allCategories", ForumCommon
086: .getAllCategoriesAndForums(true));
087: this .context.put("topicsPerPage", new Integer(SystemGlobals
088: .getIntValue(ConfigKeys.TOPICS_PER_PAGE)));
089: this .context.put("rssEnabled", SystemGlobals
090: .getBoolValue(ConfigKeys.RSS_ENABLED));
091:
092: this .context.put("totalMessages", new Integer(ForumRepository
093: .getTotalMessages()));
094: this .context.put("totalRegisteredUsers", ForumRepository
095: .totalUsers());
096: this .context.put("lastUser", ForumRepository
097: .lastRegisteredUser());
098:
099: SimpleDateFormat df = new SimpleDateFormat(SystemGlobals
100: .getValue(ConfigKeys.DATE_TIME_FORMAT));
101: GregorianCalendar gc = new GregorianCalendar();
102: this .context.put("now", df.format(gc.getTime()));
103:
104: this .context.put("lastVisit", df.format(SessionFacade
105: .getUserSession().getLastVisit()));
106: this .context.put("forumRepository", new ForumRepository());
107:
108: // Online Users
109: this .context.put("totalOnlineUsers", new Integer(SessionFacade
110: .size()));
111: int aid = SystemGlobals
112: .getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
113:
114: List onlineUsersList = SessionFacade.getLoggedSessions();
115:
116: // Check for an optional language parameter
117: UserSession currentUser = SessionFacade.getUserSession();
118:
119: if (currentUser.getUserId() == aid) {
120: String lang = this .request.getParameter("lang");
121:
122: if (lang != null && I18n.languageExists(lang)) {
123: currentUser.setLang(lang);
124: }
125: }
126:
127: // If there are only guest users, then just register
128: // a single one. In any other situation, we do not
129: // show the "guest" username
130: if (onlineUsersList.size() == 0) {
131: UserSession us = new UserSession();
132:
133: us.setUserId(aid);
134: us.setUsername(I18n.getMessage("Guest"));
135:
136: onlineUsersList.add(us);
137: }
138:
139: int registeredSize = SessionFacade.registeredSize();
140: int anonymousSize = SessionFacade.anonymousSize();
141: int totalOnlineUsers = registeredSize + anonymousSize;
142:
143: this .context.put("userSessions", onlineUsersList);
144: this .context.put("totalOnlineUsers", new Integer(
145: totalOnlineUsers));
146: this .context.put("totalRegisteredOnlineUsers", new Integer(
147: registeredSize));
148: this .context.put("totalAnonymousUsers", new Integer(
149: anonymousSize));
150:
151: // Most users ever online
152: MostUsersEverOnline mostUsersEverOnline = ForumRepository
153: .getMostUsersEverOnline();
154:
155: if (totalOnlineUsers > mostUsersEverOnline.getTotal()) {
156: mostUsersEverOnline.setTotal(totalOnlineUsers);
157: mostUsersEverOnline.setTimeInMillis(System
158: .currentTimeMillis());
159:
160: ForumRepository
161: .updateMostUsersEverOnline(mostUsersEverOnline);
162: }
163:
164: this .context.put("mostUsersEverOnline", mostUsersEverOnline);
165: }
166:
167: public void moderation() {
168: this .context.put("openModeration", true);
169: this .show();
170: }
171:
172: /**
173: * Display all topics in a forum
174: */
175: public void show() {
176: int forumId = this .request.getIntParameter("forum_id");
177: ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
178:
179: // The user can access this forum?
180: Forum forum = ForumRepository.getForum(forumId);
181:
182: if (forum == null
183: || !ForumRepository.isCategoryAccessible(forum
184: .getCategoryId())) {
185: new ModerationHelper().denied(I18n
186: .getMessage("ForumListing.denied"));
187: return;
188: }
189:
190: int start = ViewCommon.getStartPage();
191:
192: List tmpTopics = TopicsCommon.topicsByForum(forumId, start);
193:
194: this .setTemplateName(TemplateKeys.FORUMS_SHOW);
195:
196: // Moderation
197: UserSession userSession = SessionFacade.getUserSession();
198: boolean isLogged = SessionFacade.isLogged();
199: boolean isModerator = userSession.isModerator(forumId);
200:
201: boolean canApproveMessages = (isLogged && isModerator && SecurityRepository
202: .canAccess(SecurityConstants.PERM_MODERATION_APPROVE_MESSAGES));
203:
204: Map topicsToApprove = new HashMap();
205:
206: if (canApproveMessages) {
207: ModerationDAO mdao = DataAccessDriver.getInstance()
208: .newModerationDAO();
209: topicsToApprove = mdao.topicsByForum(forumId);
210: this .context.put("postFormatter", new PostCommon());
211: }
212:
213: this .context.put("topicsToApprove", topicsToApprove);
214:
215: this .context
216: .put(
217: "attachmentsEnabled",
218: SecurityRepository
219: .canAccess(
220: SecurityConstants.PERM_ATTACHMENTS_ENABLED,
221: Integer.toString(forumId))
222: || SecurityRepository
223: .canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD));
224:
225: this .context.put("topics", TopicsCommon
226: .prepareTopics(tmpTopics));
227: this .context.put("allCategories", ForumCommon
228: .getAllCategoriesAndForums(false));
229: this .context.put("forum", forum);
230: this .context.put("rssEnabled", SystemGlobals
231: .getBoolValue(ConfigKeys.RSS_ENABLED));
232: this .context.put("pageTitle", forum.getName());
233: this .context.put("canApproveMessages", canApproveMessages);
234: this .context.put("replyOnly", !SecurityRepository.canAccess(
235: SecurityConstants.PERM_REPLY_ONLY, Integer
236: .toString(forum.getId())));
237:
238: this .context.put("readonly", !SecurityRepository.canAccess(
239: SecurityConstants.PERM_READ_ONLY_FORUMS, Integer
240: .toString(forumId)));
241:
242: this .context.put("watching", fm.isUserSubscribed(forumId,
243: userSession.getUserId()));
244:
245: // Pagination
246: int topicsPerPage = SystemGlobals
247: .getIntValue(ConfigKeys.TOPICS_PER_PAGE);
248: int postsPerPage = SystemGlobals
249: .getIntValue(ConfigKeys.POSTS_PER_PAGE);
250: int totalTopics = forum.getTotalTopics();
251:
252: ViewCommon.contextToPagination(start, totalTopics,
253: topicsPerPage);
254: this .context.put("postsPerPage", new Integer(postsPerPage));
255:
256: TopicsCommon.topicListingBase();
257: this .context.put("moderator", isLogged && isModerator);
258: }
259:
260: // Make an URL to some action
261: private String makeRedirect(String action) {
262: String path = this .request.getContextPath() + "/forums/"
263: + action + "/";
264: String this Page = this .request.getParameter("start");
265:
266: if (this Page != null && !this Page.equals("0")) {
267: path += this Page + "/";
268: }
269:
270: String forumId = this .request.getParameter("forum_id");
271:
272: if (forumId == null) {
273: forumId = this .request.getParameter("persistData");
274: }
275:
276: path += forumId
277: + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);
278:
279: return path;
280: }
281:
282: // Mark all topics as read
283: public void readAll() {
284: String forumId = this .request.getParameter("forum_id");
285:
286: if (forumId != null) {
287: Map tracking = SessionFacade.getTopicsReadTimeByForum();
288:
289: if (tracking == null) {
290: tracking = new HashMap();
291: }
292:
293: tracking.put(new Integer(forumId), new Long(System
294: .currentTimeMillis()));
295: SessionFacade.setAttribute(
296: ConfigKeys.TOPICS_READ_TIME_BY_FORUM, tracking);
297: }
298:
299: if (forumId != null) {
300: JForumExecutionContext.setRedirect(this
301: .makeRedirect("show"));
302: } else {
303: JForumExecutionContext.setRedirect(this .request
304: .getContextPath()
305: + "/forums/list"
306: + SystemGlobals
307: .getValue(ConfigKeys.SERVLET_EXTENSION));
308: }
309: }
310:
311: // Messages since last visit
312: public void newMessages() {
313: this .request.addParameter("from_date", SessionFacade
314: .getUserSession().getLastVisit());
315: this .request.addParameter("to_date", new Date());
316:
317: SearchAction searchAction = new SearchAction(this .request,
318: this .response, this .context);
319: searchAction.newMessages();
320:
321: this .setTemplateName(TemplateKeys.SEARCH_NEW_MESSAGES);
322: }
323:
324: public void approveMessages() {
325: if (SessionFacade.getUserSession().isModerator(
326: this .request.getIntParameter("forum_id"))) {
327: new ModerationAction(this .context, this .request).doSave();
328: }
329:
330: JForumExecutionContext.setRedirect(this .request
331: .getContextPath()
332: + "/forums/show/"
333: + this .request.getParameter("forum_id")
334: + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
335: }
336:
337: /**
338: * Action when users click on "watch this forum"
339: * It gets teh forum_id and userId, and put them into a watch_forum table in the database;
340: */
341: public void watchForum() {
342: int forumId = this .request.getIntParameter("forum_id");
343: int userId = SessionFacade.getUserSession().getUserId();
344:
345: this .watchForum(DataAccessDriver.getInstance().newForumDAO(),
346: forumId, userId);
347:
348: JForumExecutionContext.setRedirect(this
349: .redirectLinkToShowAction(forumId));
350: }
351:
352: public void banned() {
353: this .setTemplateName(TemplateKeys.FORUMS_BANNED);
354: this .context.put("message", I18n
355: .getMessage("ForumBanned.banned"));
356: }
357:
358: private String redirectLinkToShowAction(int forumId) {
359: int start = ViewCommon.getStartPage();
360:
361: return this .request.getContextPath() + "/forums/show/"
362: + (start > 0 ? start + "/" : "") + forumId
363: + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);
364: }
365:
366: /**
367: *
368: * @param dao ForumDAO
369: * @param forumId int
370: * @param userId int
371: */
372: private void watchForum(ForumDAO dao, int forumId, int userId) {
373: if (SessionFacade.isLogged()
374: && !dao.isUserSubscribed(forumId, userId)) {
375: dao.subscribeUser(forumId, userId);
376: }
377: }
378:
379: /**
380: * Unwatch the forum watched.
381: */
382: public void unwatchForum() {
383: if (SessionFacade.isLogged()) {
384: int forumId = this .request.getIntParameter("forum_id");
385: int userId = SessionFacade.getUserSession().getUserId();
386:
387: DataAccessDriver.getInstance().newForumDAO()
388: .removeSubscription(forumId, userId);
389:
390: String returnPath = this .redirectLinkToShowAction(forumId);
391:
392: this .setTemplateName(TemplateKeys.POSTS_UNWATCH);
393: this .context.put("message", I18n.getMessage(
394: "ForumBase.forumUnwatched",
395: new String[] { returnPath }));
396: } else {
397: this.setTemplateName(ViewCommon.contextToLogin());
398: }
399: }
400: }
|