1 43 package net.jforum.view.forum; 44 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 import java.util.List ; 48 49 import net.jforum.ActionServletRequest; 50 import net.jforum.JForumExecutionContext; 51 import net.jforum.dao.DataAccessDriver; 52 import net.jforum.dao.ForumDAO; 53 import net.jforum.dao.TopicDAO; 54 import net.jforum.entities.Forum; 55 import net.jforum.entities.Topic; 56 import net.jforum.repository.ForumRepository; 57 import net.jforum.repository.PostRepository; 58 import net.jforum.repository.SecurityRepository; 59 import net.jforum.repository.TopicRepository; 60 import net.jforum.security.SecurityConstants; 61 import net.jforum.util.I18n; 62 import net.jforum.util.preferences.TemplateKeys; 63 import net.jforum.view.forum.common.ForumCommon; 64 65 import org.apache.log4j.Logger; 66 67 import freemarker.template.SimpleHash; 68 69 73 public class ModerationHelper 74 { 75 private static Logger logger = Logger.getLogger(ModerationHelper.class); 76 77 public static final int SUCCESS = 1; 78 public static final int FAILURE = 2; 79 public static final int IGNORE = 3; 80 81 public int doModeration(String returnUrl) throws Exception 82 { 83 int status = FAILURE; 84 85 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION)) { 86 ActionServletRequest request = JForumExecutionContext.getRequest(); 88 89 if (request.getParameter("topicRemove") != null) { 90 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) { 91 this.removeTopics(); 92 93 status = SUCCESS; 94 } 95 } 96 else if (request.getParameter("topicMove") != null) { 97 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)) { 98 this.moveTopics(); 99 100 status = IGNORE; 101 } 102 } 103 else if (request.getParameter("topicLock") != null) { 104 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)) { 105 this.lockUnlockTopics(Topic.STATUS_LOCKED); 106 107 status = SUCCESS; 108 } 109 } 110 else if (request.getParameter("topicUnlock") != null) { 111 if (SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK)) { 112 this.lockUnlockTopics(Topic.STATUS_UNLOCKED); 113 114 status = SUCCESS; 115 } 116 } 117 } 118 119 if (status == ModerationHelper.FAILURE) { 120 this.denied(); 121 } 122 else if (status == ModerationHelper.SUCCESS && returnUrl != null) { 123 JForumExecutionContext.setRedirect(returnUrl); 124 } 125 126 return status; 127 } 128 129 public int doModeration() throws Exception 130 { 131 return this.doModeration(null); 132 } 133 134 private void removeTopics() throws Exception 135 { 136 String [] topics = JForumExecutionContext.getRequest().getParameterValues("topic_id"); 137 138 List forumsList = new ArrayList (); 139 TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO(); 140 141 List topicsToDelete = new ArrayList (); 142 143 if (topics != null && topics.length > 0) { 144 for (int i = 0; i < topics.length; i++) { 145 Topic t = tm.selectById(Integer.parseInt(topics[i])); 146 147 if (!forumsList.contains(new Integer (t.getForumId()))) { 148 forumsList.add(new Integer (t.getForumId())); 149 } 150 151 topicsToDelete.add(t); 152 PostRepository.clearCache(t.getId()); 153 } 154 155 tm.deleteTopics(topicsToDelete); 156 157 ForumDAO fm = DataAccessDriver.getInstance().newForumDAO(); 158 TopicRepository.loadMostRecentTopics(); 159 160 for (Iterator iter = forumsList.iterator(); iter.hasNext(); ) { 162 int forumId = ((Integer )iter.next()).intValue(); 163 164 TopicRepository.clearCache(forumId); 165 166 int postId = fm.getMaxPostId(forumId); 167 if (postId > -1) { 168 fm.setLastPost(forumId, postId); 169 } 170 else { 171 logger.warn("Could not find last post id for forum " + forumId); 172 } 173 174 ForumRepository.reloadForum(forumId); 175 } 176 } 177 } 178 179 private void lockUnlockTopics(int status) throws Exception 180 { 181 String [] topics = JForumExecutionContext.getRequest().getParameterValues("topic_id"); 182 183 if (topics != null && topics.length > 0) { 184 int[] ids = new int[topics.length]; 185 186 for (int i = 0; i < topics.length; i++) { 187 ids[i] = Integer.parseInt(topics[i]); 188 } 189 190 DataAccessDriver.getInstance().newTopicDAO().lockUnlock(ids, status); 191 192 Topic t = DataAccessDriver.getInstance().newTopicDAO().selectById(ids[0]); 194 TopicRepository.clearCache(t.getForumId()); 195 } 196 } 197 198 private void moveTopics() throws Exception 199 { 200 SimpleHash context = JForumExecutionContext.getTemplateContext(); 201 202 context.put("persistData", JForumExecutionContext.getRequest().getParameter("persistData")); 203 context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false)); 204 205 String [] topics = JForumExecutionContext.getRequest().getParameterValues("topic_id"); 206 207 if (topics.length > 0) { 208 String forumId = JForumExecutionContext.getRequest().getParameter("forum_id"); 210 211 if (forumId == null) { 212 int topicId = Integer.parseInt(topics[0]); 213 214 Topic topic = TopicRepository.getTopic(new Topic(topicId)); 215 216 if (topic == null) { 217 topic = DataAccessDriver.getInstance().newTopicDAO().selectRaw(topicId); 218 } 219 220 forumId = Integer.toString(topic.getForumId()); 221 } 222 223 context.put("forum_id", forumId); 224 225 StringBuffer sb = new StringBuffer (128); 226 227 for (int i = 0; i < topics.length - 1; i++) { 228 sb.append(topics[i]).append(","); 229 } 230 231 sb.append(topics[topics.length - 1]); 232 233 context.put("topics", sb.toString()); 234 } 235 } 236 237 public int moveTopicsSave(String successUrl) throws Exception 238 { 239 int status = SUCCESS; 240 241 if (!SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE)) { 242 status = FAILURE; 243 } 244 else { 245 ActionServletRequest request = JForumExecutionContext.getRequest(); 246 String topics = request.getParameter("topics"); 247 248 if (topics != null) { 249 int fromForumId = Integer.parseInt(request.getParameter("forum_id")); 250 int toForumId = Integer.parseInt(request.getParameter("to_forum")); 251 252 DataAccessDriver.getInstance().newForumDAO().moveTopics(topics.split(","), fromForumId, toForumId); 253 254 Forum fromForum = ForumRepository.getForum(fromForumId); 255 256 ForumRepository.reloadForum(fromForumId); 257 ForumRepository.reloadForum(toForumId); 258 259 TopicRepository.clearCache(fromForumId); 260 TopicRepository.clearCache(toForumId); 261 262 TopicRepository.loadMostRecentTopics(); 263 } 264 } 265 266 if (status == FAILURE) { 267 this.denied(); 268 } 269 else { 270 this.moderationDone(successUrl); 271 } 272 273 return status; 274 } 275 276 public String moderationDone(String redirectUrl) 277 { 278 JForumExecutionContext.getRequest().setAttribute("template", TemplateKeys.MODERATION_DONE); 279 JForumExecutionContext.getTemplateContext().put("message", I18n.getMessage("Moderation.ModerationDone", new String [] { redirectUrl })); 280 281 return TemplateKeys.MODERATION_DONE; 282 } 283 284 public void denied() 285 { 286 this.denied(I18n.getMessage("Moderation.Denied")); 287 } 288 289 public void denied(String message) 290 { 291 JForumExecutionContext.getRequest().setAttribute("template", TemplateKeys.MODERATION_DENIED); 292 JForumExecutionContext.getTemplateContext().put("message", message); 293 } 294 } 295 | Popular Tags |