Search in sources :

Example 6 with Poll

use of net.jforum.entities.Poll in project jforum2 by rafaelsteil.

the class PostAction method insertSave.

public void insertSave() {
    int forumId = this.request.getIntParameter("forum_id");
    boolean firstPost = false;
    if (!this.anonymousPost(forumId)) {
        return;
    }
    Topic t = new Topic(-1);
    t.setForumId(forumId);
    boolean newTopic = (this.request.getParameter("topic_id") == null);
    if (!TopicsCommon.isTopicAccessible(t.getForumId()) || this.isForumReadonly(t.getForumId(), newTopic)) {
        return;
    }
    TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
    PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
    PollDAO poolDao = DataAccessDriver.getInstance().newPollDAO();
    ForumDAO forumDao = DataAccessDriver.getInstance().newForumDAO();
    if (!newTopic) {
        int topicId = this.request.getIntParameter("topic_id");
        t = TopicRepository.getTopic(new Topic(topicId));
        if (t == null) {
            t = topicDao.selectById(topicId);
        }
        // Could not find the topic. The topicId sent was invalid
        if (t == null || t.getId() == 0) {
            newTopic = true;
        } else {
            if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
                return;
            }
            // Cannot insert new messages on locked topics
            if (t.getStatus() == Topic.STATUS_LOCKED) {
                this.topicLocked();
                return;
            }
        }
    }
    // checking above set the newTopic var to true
    if (newTopic) {
        if (this.isReplyOnly(forumId)) {
            this.replyOnly();
            return;
        }
        if (this.request.getParameter("topic_type") != null) {
            t.setType(this.request.getIntParameter("topic_type"));
            if (t.getType() != Topic.TYPE_NORMAL && !SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS)) {
                t.setType(Topic.TYPE_NORMAL);
            }
        }
    }
    UserSession us = SessionFacade.getUserSession();
    User u = DataAccessDriver.getInstance().newUserDAO().selectById(us.getUserId());
    if ("1".equals(this.request.getParameter("quick")) && SessionFacade.isLogged()) {
        this.request.addParameter("notify", u.isNotifyOnMessagesEnabled() ? "1" : null);
        this.request.addParameter("attach_sig", u.getAttachSignatureEnabled() ? "1" : "0");
    } else {
        u.setId(us.getUserId());
        u.setUsername(us.getUsername());
    }
    // Set the Post
    Post p = PostCommon.fillPostFromRequest();
    if (p.getText() == null || p.getText().trim().equals("")) {
        this.insert();
        return;
    }
    // Check the elapsed time since the last post from the user
    int delay = SystemGlobals.getIntValue(ConfigKeys.POSTS_NEW_DELAY);
    if (delay > 0) {
        Long lastPostTime = (Long) SessionFacade.getAttribute(ConfigKeys.LAST_POST_TIME);
        if (lastPostTime != null) {
            if (System.currentTimeMillis() < (lastPostTime.longValue() + delay)) {
                this.context.put("post", p);
                this.context.put("start", this.request.getParameter("start"));
                this.context.put("error", I18n.getMessage("PostForm.tooSoon"));
                this.insert();
                return;
            }
        }
    }
    p.setForumId(this.request.getIntParameter("forum_id"));
    if (StringUtils.isBlank(p.getSubject())) {
        p.setSubject(t.getTitle());
    }
    boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS) && request.getSessionContext().getAttribute(ConfigKeys.REQUEST_IGNORE_CAPTCHA) == null;
    if (needCaptcha) {
        if (!us.validateCaptchaResponse(this.request.getParameter("captcha_anwser"))) {
            this.context.put("post", p);
            this.context.put("start", this.request.getParameter("start"));
            this.context.put("error", I18n.getMessage("CaptchaResponseFails"));
            this.insert();
            return;
        }
    }
    boolean preview = "1".equals(this.request.getParameter("preview"));
    if (!preview) {
        AttachmentCommon attachments = new AttachmentCommon(this.request, forumId);
        try {
            attachments.preProcess();
        } catch (AttachmentException e) {
            JForumExecutionContext.enableRollback();
            p.setText(this.request.getParameter("message"));
            p.setId(0);
            this.context.put("errorMessage", e.getMessage());
            this.context.put("post", p);
            this.insert();
            return;
        }
        Forum forum = ForumRepository.getForum(forumId);
        PermissionControl pc = SecurityRepository.get(us.getUserId());
        // Moderators and admins don't need to have their messages moderated
        boolean moderate = (forum.isModerated() && !pc.canAccess(SecurityConstants.PERM_MODERATION) && !pc.canAccess(SecurityConstants.PERM_ADMINISTRATION));
        if (newTopic) {
            t.setTime(new Date());
            t.setTitle(this.request.getParameter("subject"));
            t.setModerated(moderate);
            t.setPostedBy(u);
            t.setFirstPostTime(ViewCommon.formatDate(t.getTime()));
            int topicId = topicDao.addNew(t);
            t.setId(topicId);
            firstPost = true;
        }
        if (!firstPost && pc.canAccess(SecurityConstants.PERM_REPLY_WITHOUT_MODERATION, Integer.toString(t.getForumId()))) {
            moderate = false;
        }
        // Topic watch
        if (this.request.getParameter("notify") != null) {
            this.watch(topicDao, t.getId(), u.getId());
        }
        p.setTopicId(t.getId());
        // add a poll
        Poll poll = PollCommon.fillPollFromRequest();
        if (poll != null && newTopic) {
            poll.setTopicId(t.getId());
            if (poll.getOptions().size() < 2) {
                //it is not a valid poll, cancel the post
                JForumExecutionContext.enableRollback();
                p.setText(this.request.getParameter("message"));
                p.setId(0);
                this.context.put("errorMessage", I18n.getMessage("PostForm.needMorePollOptions"));
                this.context.put("post", p);
                this.context.put("poll", poll);
                this.insert();
                return;
            }
            poolDao.addNew(poll);
            t.setVoteId(poll.getId());
        }
        // Save the remaining stuff
        p.setModerate(moderate);
        int postId = postDao.addNew(p);
        if (newTopic) {
            t.setFirstPostId(postId);
        }
        if (!moderate) {
            t.setLastPostId(postId);
            t.setLastPostBy(u);
            t.setLastPostDate(p.getTime());
            t.setLastPostTime(p.getFormatedTime());
        }
        topicDao.update(t);
        attachments.insertAttachments(p);
        if (!moderate) {
            StringBuffer path = new StringBuffer(512);
            path.append(this.request.getContextPath()).append("/posts/list/");
            int start = ViewCommon.getStartPage();
            path.append(this.startPage(t, start)).append("/").append(t.getId()).append(SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)).append('#').append(postId);
            JForumExecutionContext.setRedirect(path.toString());
            if (newTopic) {
                // Notify "forum new topic" users
                ForumCommon.notifyUsers(forum, t, p);
            } else {
                t.setTotalReplies(t.getTotalReplies() + 1);
                TopicsCommon.notifyUsers(t, p);
            }
            // Update forum stats, cache and etc
            t.setTotalViews(t.getTotalViews() + 1);
            DataAccessDriver.getInstance().newUserDAO().incrementPosts(p.getUserId());
            TopicsCommon.updateBoardStatus(t, postId, firstPost, topicDao, forumDao);
            ForumRepository.updateForumStats(t, u, p);
            int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
            if (u.getId() != anonymousUser) {
                SessionFacade.getTopicsReadTime().put(new Integer(t.getId()), new Long(p.getTime().getTime()));
            }
            if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
                SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
                p.setFormatedTime(df.format(p.getTime()));
                PostRepository.append(p.getTopicId(), PostCommon.preparePostForDisplay(p));
            }
        } else {
            JForumExecutionContext.setRedirect(this.request.getContextPath() + "/posts/waitingModeration/" + (firstPost ? 0 : t.getId()) + "/" + t.getForumId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        }
        if (delay > 0) {
            SessionFacade.setAttribute(ConfigKeys.LAST_POST_TIME, new Long(System.currentTimeMillis()));
        }
    } else {
        this.context.put("preview", true);
        this.context.put("post", p);
        this.context.put("start", this.request.getParameter("start"));
        Post postPreview = new Post(p);
        this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
        this.insert();
    }
}
Also used : ForumDAO(net.jforum.dao.ForumDAO) User(net.jforum.entities.User) PermissionControl(net.jforum.security.PermissionControl) Post(net.jforum.entities.Post) TopicDAO(net.jforum.dao.TopicDAO) Date(java.util.Date) Forum(net.jforum.entities.Forum) AttachmentException(net.jforum.exceptions.AttachmentException) PollDAO(net.jforum.dao.PollDAO) PostDAO(net.jforum.dao.PostDAO) UserSession(net.jforum.entities.UserSession) Poll(net.jforum.entities.Poll) Topic(net.jforum.entities.Topic) AttachmentCommon(net.jforum.view.forum.common.AttachmentCommon) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with Poll

use of net.jforum.entities.Poll in project jforum2 by rafaelsteil.

the class PollCommon method fillPollFromRequest.

public static Poll fillPollFromRequest() {
    RequestContext request = JForumExecutionContext.getRequest();
    String label = request.getParameter("poll_label");
    if (label == null || label.length() == 0) {
        return null;
    }
    Poll poll = new Poll();
    poll.setStartTime(new Date());
    poll.setLabel(label);
    int count = request.getIntParameter("poll_option_count");
    for (int i = 0; i <= count; i++) {
        String option = request.getParameter("poll_option_" + i);
        if (option == null) {
            continue;
        }
        option = option.trim();
        if (option.length() > 0) {
            PollOption po = new PollOption();
            po.setId(i);
            po.setText(option);
            poll.addOption(po);
        }
    }
    String pollLength = request.getParameter("poll_length");
    if (pollLength != null && pollLength.length() > 0) {
        poll.setLength(Integer.parseInt(pollLength));
    }
    return poll;
}
Also used : Poll(net.jforum.entities.Poll) PollOption(net.jforum.entities.PollOption) RequestContext(net.jforum.context.RequestContext) Date(java.util.Date)

Aggregations

Poll (net.jforum.entities.Poll)7 PollDAO (net.jforum.dao.PollDAO)4 PostDAO (net.jforum.dao.PostDAO)4 Topic (net.jforum.entities.Topic)4 AttachmentCommon (net.jforum.view.forum.common.AttachmentCommon)4 Date (java.util.Date)3 TopicDAO (net.jforum.dao.TopicDAO)3 Forum (net.jforum.entities.Forum)2 Post (net.jforum.entities.Post)2 User (net.jforum.entities.User)2 UserSession (net.jforum.entities.UserSession)2 AttachmentException (net.jforum.exceptions.AttachmentException)2 PermissionControl (net.jforum.security.PermissionControl)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1