1 43 package net.jforum.view.forum; 44 45 import net.jforum.Command; 46 import net.jforum.JForumExecutionContext; 47 import net.jforum.SessionFacade; 48 import net.jforum.dao.DataAccessDriver; 49 import net.jforum.dao.KarmaDAO; 50 import net.jforum.dao.PostDAO; 51 import net.jforum.entities.Karma; 52 import net.jforum.entities.KarmaStatus; 53 import net.jforum.entities.Post; 54 import net.jforum.repository.PostRepository; 55 import net.jforum.repository.SecurityRepository; 56 import net.jforum.security.SecurityConstants; 57 import net.jforum.util.I18n; 58 import net.jforum.util.preferences.ConfigKeys; 59 import net.jforum.util.preferences.SystemGlobals; 60 import net.jforum.util.preferences.TemplateKeys; 61 import net.jforum.view.forum.common.PostCommon; 62 import net.jforum.view.forum.common.ViewCommon; 63 64 68 public class KarmaAction extends Command 69 { 70 public void insert() throws Exception 71 { 72 if (!SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED)) { 73 this.error("Karma.featureDisabled", null); 74 return; 75 } 76 77 int postId = this.request.getIntParameter("post_id"); 78 int fromUserId = SessionFacade.getUserSession().getUserId(); 79 80 PostDAO pm = DataAccessDriver.getInstance().newPostDAO(); 81 Post p = pm.selectById(postId); 82 83 if (fromUserId == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) { 84 this.error("Karma.anonymousIsDenied", p); 85 return; 86 } 87 88 if (p.getUserId() == fromUserId) { 89 this.error("Karma.cannotSelfVote", p); 90 return; 91 } 92 93 KarmaDAO km = DataAccessDriver.getInstance().newKarmaDAO(); 94 95 if (!km.userCanAddKarma(fromUserId, postId)) { 96 this.error("Karma.alreadyVoted", p); 97 return; 98 } 99 100 int points = this.request.getIntParameter("points"); 102 103 if (points < SystemGlobals.getIntValue(ConfigKeys.KARMA_MIN_POINTS) 104 || points > SystemGlobals.getIntValue(ConfigKeys.KARMA_MAX_POINTS)) { 105 this.error("Karma.invalidRange", p); 106 return; 107 } 108 109 Karma karma = new Karma(); 110 karma.setFromUserId(fromUserId); 111 karma.setPostUserId(p.getUserId()); 112 karma.setPostId(postId); 113 karma.setTopicId(p.getTopicId()); 114 karma.setPoints(points); 115 116 km.addKarma(karma); 117 118 p.setKarma(new KarmaStatus(p.getId(), points)); 119 120 if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) { 121 PostRepository.update(p.getTopicId(), PostCommon.preparePostForDisplay(p)); 122 } 123 124 JForumExecutionContext.setRedirect(this.urlToTopic(p)); 125 } 126 127 private void error(String message, Post p) 128 { 129 this.setTemplateName(TemplateKeys.KARMA_ERROR); 130 131 if (p != null) { 132 this.context.put("message", I18n.getMessage(message, new String [] { this.urlToTopic(p) })); 133 } 134 else { 135 this.context.put("message", I18n.getMessage(message)); 136 } 137 } 138 139 private String urlToTopic(Post p) 140 { 141 return JForumExecutionContext.getRequest().getContextPath() + "/posts/list/" 142 + ViewCommon.getStartPage() 143 + "/" + p.getTopicId() 144 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) 145 + "#" + p.getId(); 146 } 147 148 151 public void list() throws Exception 152 { 153 this.setTemplateName(TemplateKeys.KARMA_LIST); 154 this.context.put("message", I18n.getMessage("invalidAction")); 155 } 156 157 164 198 199 207 238 } 239 | Popular Tags |