1 43 package net.jforum.view.forum; 44 45 import java.text.SimpleDateFormat ; 46 import java.util.GregorianCalendar ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import java.util.Map ; 51 52 import net.jforum.Command; 53 import net.jforum.JForumExecutionContext; 54 import net.jforum.SessionFacade; 55 import net.jforum.dao.DataAccessDriver; 56 import net.jforum.dao.ForumDAO; 57 import net.jforum.dao.ModerationDAO; 58 import net.jforum.dao.SearchData; 59 import net.jforum.entities.Forum; 60 import net.jforum.entities.MostUsersEverOnline; 61 import net.jforum.entities.Topic; 62 import net.jforum.entities.UserSession; 63 import net.jforum.repository.ForumRepository; 64 import net.jforum.repository.SecurityRepository; 65 import net.jforum.security.SecurityConstants; 66 import net.jforum.util.I18n; 67 import net.jforum.util.preferences.ConfigKeys; 68 import net.jforum.util.preferences.SystemGlobals; 69 import net.jforum.util.preferences.TemplateKeys; 70 import net.jforum.view.admin.ModerationAction; 71 import net.jforum.view.forum.common.ForumCommon; 72 import net.jforum.view.forum.common.PostCommon; 73 import net.jforum.view.forum.common.TopicsCommon; 74 import net.jforum.view.forum.common.ViewCommon; 75 79 public class ForumAction extends Command 80 { 81 84 public void list() throws Exception 85 { 86 this.setTemplateName(TemplateKeys.FORUMS_LIST); 87 88 this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(true)); 89 this.context.put("topicsPerPage", new Integer (SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE))); 90 this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED)); 91 92 this.context.put("totalMessages", I18n.getMessage("ForumListing.totalMessagesInfo", 93 new Object [] {new Integer ( ForumRepository.getTotalMessages() )})); 94 95 this.context.put("totalUsers", I18n.getMessage("ForumListing.registeredUsers", 96 new Object [] { ForumRepository.totalUsers() })); 97 this.context.put("lastUser", ForumRepository.lastRegisteredUser()); 98 99 SimpleDateFormat df = new SimpleDateFormat (SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT)); 100 GregorianCalendar gc = new GregorianCalendar (); 101 this.context.put("now", df.format(gc.getTime())); 102 103 this.context.put("lastVisit", df.format(SessionFacade.getUserSession().getLastVisit())); 104 this.context.put("fir", new ForumRepository()); 105 106 this.context.put("totalOnlineUsers", new Integer (SessionFacade.size())); 108 int aid = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID); 109 110 List onlineUsersList = SessionFacade.getLoggedSessions(); 111 112 UserSession currentUser = SessionFacade.getUserSession(); 114 115 if (currentUser.getUserId() == aid) { 116 String lang = this.request.getParameter("lang"); 117 118 if (lang != null && I18n.languageExists(lang)) { 119 currentUser.setLang(lang); 120 } 121 } 122 123 if (onlineUsersList.size() == 0) { 127 UserSession us = new UserSession(); 128 129 us.setUserId(aid); 130 us.setUsername(I18n.getMessage("Guest")); 131 132 onlineUsersList.add(us); 133 } 134 135 int registeredSize = SessionFacade.registeredSize(); 136 int anonymousSize = SessionFacade.anonymousSize(); 137 int totalUsers = registeredSize + anonymousSize; 138 139 this.context.put("userSessions", onlineUsersList); 140 this.context.put("usersOnline", I18n.getMessage("ForumListing.numberOfUsersOnline", 141 new Object [] { 142 new Integer (totalUsers), 143 new Integer (registeredSize), 144 new Integer (anonymousSize) 145 })); 146 147 MostUsersEverOnline mostUsersEverOnline = ForumRepository.getMostUsersEverOnline(); 149 150 if (totalUsers > mostUsersEverOnline.getTotal()) { 151 mostUsersEverOnline.setTotal(totalUsers); 152 mostUsersEverOnline.setTimeInMillis(System.currentTimeMillis()); 153 154 ForumRepository.updateMostUsersEverOnline(mostUsersEverOnline); 155 } 156 157 this.context.put("mostUsersEverOnline", I18n.getMessage("ForumListing.mostUsersEverOnline", 158 new String [] { Integer.toString(mostUsersEverOnline.getTotal()), mostUsersEverOnline.getDate() })); 159 } 160 161 public void moderation() throws Exception 162 { 163 this.context.put("openModeration", true); 164 this.show(); 165 } 166 167 171 public void show() throws Exception 172 { 173 int forumId = this.request.getIntParameter("forum_id"); 174 ForumDAO fm = DataAccessDriver.getInstance().newForumDAO(); 175 176 Forum forum = ForumRepository.getForum(forumId); 178 179 if (forum == null || !ForumRepository.isCategoryAccessible(forum.getCategoryId())) { 180 new ModerationHelper().denied(I18n.getMessage("ForumListing.denied")); 181 return; 182 } 183 184 int start = ViewCommon.getStartPage(); 185 186 List tmpTopics = TopicsCommon.topicsByForum(forumId, start); 187 188 this.setTemplateName(TemplateKeys.FORUMS_SHOW); 189 190 boolean canApproveMessages = (SessionFacade.isLogged() 192 && SessionFacade.getUserSession().isModerator(forumId) 193 && SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_APPROVE_MESSAGES)); 194 195 Map topicsToApprove = new HashMap (); 196 197 if (canApproveMessages) { 198 ModerationDAO mdao = DataAccessDriver.getInstance().newModerationDAO(); 199 topicsToApprove = mdao.topicsByForum(forumId); 200 this.context.put("postFormatter", PostCommon.getInstance()); 201 } 202 203 this.context.put("topicsToApprove", topicsToApprove); 204 205 this.context.put("attachmentsEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, 206 Integer.toString(forumId)) 207 || SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD)); 208 209 this.context.put("topics", TopicsCommon.prepareTopics(tmpTopics)); 210 this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false)); 211 this.context.put("forum", forum); 212 this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED)); 213 this.context.put("pageTitle", forum.getName()); 214 this.context.put("canApproveMessages", canApproveMessages); 215 this.context.put("replyOnly", !SecurityRepository.canAccess(SecurityConstants.PERM_REPLY_ONLY, 216 Integer.toString(forum.getId()))); 217 218 this.context.put("readonly", !SecurityRepository.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS, 219 Integer.toString(forumId))); 220 221 this.context.put("watching", fm.isUserSubscribed(forumId, SessionFacade.getUserSession().getUserId())); 222 223 int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE); 225 int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE); 226 int totalTopics = ForumRepository.getTotalTopics(forumId); 227 228 ViewCommon.contextToPagination(start, totalTopics, topicsPerPage); 229 this.context.put("postsPerPage", new Integer (postsPerPage)); 230 231 TopicsCommon.topicListingBase(); 232 } 233 234 private String makeRedirect(String action) 236 { 237 String path = this.request.getContextPath() + "/forums/" + action + "/"; 238 String thisPage = this.request.getParameter("start"); 239 240 if (thisPage != null && !thisPage.equals("0")) { 241 path += thisPage + "/"; 242 } 243 244 String forumId = this.request.getParameter("forum_id"); 245 246 if (forumId == null) { 247 forumId = this.request.getParameter("persistData"); 248 } 249 250 path += forumId + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION); 251 252 return path; 253 } 254 255 public void readAll() throws Exception 257 { 258 SearchData sd = new SearchData(); 259 sd.setTime(SessionFacade.getUserSession().getLastVisit()); 260 261 String forumId = this.request.getParameter("forum_id"); 262 if (forumId != null) { 263 sd.setForumId(Integer.parseInt(forumId)); 264 } 265 266 List allTopics = DataAccessDriver.getInstance().newSearchDAO().search(sd); 267 for (Iterator iter = allTopics.iterator(); iter.hasNext(); ) { 268 Topic t = (Topic)iter.next(); 269 270 ((Map )SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING)).put(new Integer (t.getId()), 271 new Long (t.getLastPostDate().getTime())); 272 } 273 274 if (forumId != null) { 275 JForumExecutionContext.setRedirect(this.makeRedirect("show")); 276 } 277 else { 278 JForumExecutionContext.setRedirect(this.request.getContextPath() + "/forums/list" 279 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)); 280 } 281 } 282 283 public void newMessages() throws Exception 285 { 286 this.request.addParameter("post_time", Long.toString(SessionFacade.getUserSession().getLastVisit().getTime())); 287 this.request.addParameter("clean", "true"); 288 this.request.addParameter("sort_by", "t." + SystemGlobals.getValue(ConfigKeys.TOPIC_TIME_FIELD)); 289 this.request.addParameter("sort_dir", "DESC"); 290 291 new SearchAction(this.request, this.response, this.context).search(); 292 this.setTemplateName(TemplateKeys.SEARCH_NEW_MESSAGES); 293 } 294 295 public void approveMessages() throws Exception 296 { 297 if (SessionFacade.getUserSession().isModerator(this.request.getIntParameter("forum_id"))) { 298 new ModerationAction(this.context, this.request).doSave(); 299 } 300 301 JForumExecutionContext.setRedirect(this.request.getContextPath() 302 + "/forums/show/" + this.request.getParameter("forum_id") 303 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)); 304 } 305 306 313 public void watchForum() throws Exception { 314 int forumId = this.request.getIntParameter("forum_id"); 315 int userId = SessionFacade.getUserSession().getUserId(); 316 317 this.watchForum(DataAccessDriver.getInstance().newForumDAO(), forumId, userId); 318 319 JForumExecutionContext.setRedirect(this.redirectLinkToShowAction(forumId)); 320 } 321 322 private String redirectLinkToShowAction(int forumId) 323 { 324 int start = ViewCommon.getStartPage(); 325 326 return this.request.getContextPath() 327 + "/forums/show/" 328 + (start > 0 ? start + "/" : "") 329 + forumId 330 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION); 331 } 332 333 340 private void watchForum(ForumDAO dao, int forumId, int userId) throws Exception { 341 if (SessionFacade.isLogged() && !dao.isUserSubscribed(forumId, userId)) { 342 dao.subscribeUser(forumId, userId); 343 } 344 } 345 346 350 public void unwatchForum() throws Exception { 351 if (SessionFacade.isLogged()) { 352 int forumId = this.request.getIntParameter("forum_id"); 353 int userId = SessionFacade.getUserSession().getUserId(); 354 355 DataAccessDriver.getInstance().newForumDAO().removeSubscription(forumId, userId); 356 357 String returnPath = this.redirectLinkToShowAction(forumId); 358 359 this.setTemplateName(TemplateKeys.POSTS_UNWATCH); 360 this.context.put("message", I18n.getMessage("ForumBase.forumUnwatched", new String [] { returnPath })); 361 } 362 else { 363 this.setTemplateName(ViewCommon.contextToLogin()); 364 } 365 } 366 } 367 | Popular Tags |