Search in sources :

Example 1 with KarmaDAO

use of net.jforum.dao.KarmaDAO in project jforum2 by rafaelsteil.

the class KarmaAction method insert.

public void insert() {
    if (!SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED)) {
        this.error("Karma.featureDisabled", null);
        return;
    }
    int postId = this.request.getIntParameter("post_id");
    int fromUserId = SessionFacade.getUserSession().getUserId();
    PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
    Post p = pm.selectById(postId);
    if (fromUserId == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
        this.error("Karma.anonymousIsDenied", p);
        return;
    }
    if (p.getUserId() == fromUserId) {
        this.error("Karma.cannotSelfVote", p);
        return;
    }
    KarmaDAO km = DataAccessDriver.getInstance().newKarmaDAO();
    if (!km.userCanAddKarma(fromUserId, postId)) {
        this.error("Karma.alreadyVoted", p);
        return;
    }
    // Check range
    int points = this.request.getIntParameter("points");
    if (points < SystemGlobals.getIntValue(ConfigKeys.KARMA_MIN_POINTS) || points > SystemGlobals.getIntValue(ConfigKeys.KARMA_MAX_POINTS)) {
        this.error("Karma.invalidRange", p);
        return;
    }
    Karma karma = new Karma();
    karma.setFromUserId(fromUserId);
    karma.setPostUserId(p.getUserId());
    karma.setPostId(postId);
    karma.setTopicId(p.getTopicId());
    karma.setPoints(points);
    km.addKarma(karma);
    p.setKarma(new KarmaStatus(p.getId(), points));
    if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
        PostRepository.update(p.getTopicId(), PostCommon.preparePostForDisplay(p));
    }
    JForumExecutionContext.setRedirect(this.urlToTopic(p));
}
Also used : PostDAO(net.jforum.dao.PostDAO) Post(net.jforum.entities.Post) KarmaDAO(net.jforum.dao.KarmaDAO) Karma(net.jforum.entities.Karma) KarmaStatus(net.jforum.entities.KarmaStatus)

Example 2 with KarmaDAO

use of net.jforum.dao.KarmaDAO in project jforum2 by rafaelsteil.

the class PostAction method delete.

public void delete() {
    if (!SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) {
        this.setTemplateName(TemplateKeys.POSTS_CANNOT_DELETE);
        this.context.put("message", I18n.getMessage("CannotRemovePost"));
        return;
    }
    // Post
    PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
    Post p = postDao.selectById(this.request.getIntParameter("post_id"));
    if (p.getId() == 0) {
        this.postNotFound();
        return;
    }
    TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
    Topic t = topicDao.selectRaw(p.getTopicId());
    if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
        return;
    }
    postDao.delete(p);
    DataAccessDriver.getInstance().newUserDAO().decrementPosts(p.getUserId());
    // Karma
    KarmaDAO karmaDao = DataAccessDriver.getInstance().newKarmaDAO();
    karmaDao.updateUserKarma(p.getUserId());
    // Attachments
    new AttachmentCommon(this.request, p.getForumId()).deleteAttachments(p.getId(), p.getForumId());
    // It was the last remaining post in the topic?
    int totalPosts = topicDao.getTotalPosts(p.getTopicId());
    if (totalPosts > 0) {
        // Topic
        topicDao.decrementTotalReplies(p.getTopicId());
        int maxPostId = topicDao.getMaxPostId(p.getTopicId());
        if (maxPostId > -1) {
            topicDao.setLastPostId(p.getTopicId(), maxPostId);
        }
        int minPostId = topicDao.getMinPostId(p.getTopicId());
        if (minPostId > -1) {
            topicDao.setFirstPostId(p.getTopicId(), minPostId);
        }
        // Forum
        ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
        maxPostId = fm.getMaxPostId(p.getForumId());
        if (maxPostId > -1) {
            fm.setLastPost(p.getForumId(), maxPostId);
        }
        String returnPath = this.request.getContextPath() + "/posts/list/";
        int page = ViewCommon.getStartPage();
        if (page > 0) {
            int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
            if (totalPosts % postsPerPage == 0) {
                page -= postsPerPage;
            }
            returnPath += page + "/";
        }
        JForumExecutionContext.setRedirect(returnPath + p.getTopicId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        // Update the cache
        if (TopicRepository.isTopicCached(t)) {
            t = topicDao.selectById(t.getId());
            TopicRepository.updateTopic(t);
        }
    } else {
        // Ok, all posts were removed. Time to say goodbye
        TopicsCommon.deleteTopic(p.getTopicId(), p.getForumId(), false);
        JForumExecutionContext.setRedirect(this.request.getContextPath() + "/forums/show/" + p.getForumId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
    }
    this.request.addOrReplaceParameter("log_original_message", p.getText());
    ModerationHelper moderationHelper = new ModerationHelper();
    ModerationLog moderationLog = moderationHelper.buildModerationLogFromRequest();
    moderationLog.getPosterUser().setId(p.getUserId());
    moderationHelper.saveModerationLog(moderationLog);
    PostRepository.remove(t.getId(), p.getId());
    TopicRepository.loadMostRecentTopics();
    TopicRepository.loadHottestTopics();
    ForumRepository.reloadForum(p.getForumId());
}
Also used : ForumDAO(net.jforum.dao.ForumDAO) PostDAO(net.jforum.dao.PostDAO) Post(net.jforum.entities.Post) ModerationLog(net.jforum.entities.ModerationLog) KarmaDAO(net.jforum.dao.KarmaDAO) TopicDAO(net.jforum.dao.TopicDAO) Topic(net.jforum.entities.Topic) AttachmentCommon(net.jforum.view.forum.common.AttachmentCommon)

Aggregations

KarmaDAO (net.jforum.dao.KarmaDAO)2 PostDAO (net.jforum.dao.PostDAO)2 Post (net.jforum.entities.Post)2 ForumDAO (net.jforum.dao.ForumDAO)1 TopicDAO (net.jforum.dao.TopicDAO)1 Karma (net.jforum.entities.Karma)1 KarmaStatus (net.jforum.entities.KarmaStatus)1 ModerationLog (net.jforum.entities.ModerationLog)1 Topic (net.jforum.entities.Topic)1 AttachmentCommon (net.jforum.view.forum.common.AttachmentCommon)1