Search in sources :

Example 11 with Forum

use of net.jforum.entities.Forum 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 12 with Forum

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

the class RSSAction method forumTopics.

/**
	 * RSS for all N first topics for some given forum
	 */
public void forumTopics() {
    int forumId = this.request.getIntParameter("forum_id");
    if (!TopicsCommon.isTopicAccessible(forumId)) {
        JForumExecutionContext.requestBasicAuthentication();
        return;
    }
    List posts = DataAccessDriver.getInstance().newPostDAO().selectLatestByForumForRSS(forumId, SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE));
    Forum forum = ForumRepository.getForum(forumId);
    String[] p = { forum.getName() };
    RSSAware rss = new TopicRSS(I18n.getMessage("RSS.ForumTopics.title", p), I18n.getMessage("RSS.ForumTopics.description", p), forumId, posts);
    this.context.put("rssContents", rss.createRSS());
}
Also used : TopicRSS(net.jforum.util.rss.TopicRSS) List(java.util.List) Forum(net.jforum.entities.Forum) RSSAware(net.jforum.util.rss.RSSAware)

Example 13 with Forum

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

the class RecentTopicsAction method showTopicsByUser.

public void showTopicsByUser() {
    DataAccessDriver da = DataAccessDriver.getInstance();
    UserDAO udao = da.newUserDAO();
    User u = udao.selectById(this.request.getIntParameter("user_id"));
    if (u.getId() == 0) {
        this.context.put("message", I18n.getMessage("User.notFound"));
        this.setTemplateName(TemplateKeys.USER_NOT_FOUND);
        return;
    }
    TopicsCommon.topicListingBase();
    int start = ViewCommon.getStartPage();
    int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
    int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
    this.setTemplateName(TemplateKeys.RECENT_USER_TOPICS_SHOW);
    int totalTopics = da.newTopicDAO().countUserTopics(u.getId());
    this.context.put("u", u);
    this.context.put("pageTitle", I18n.getMessage("ForumListing.userTopics") + " " + u.getUsername());
    this.context.put("postsPerPage", new Integer(postsPerPage));
    List topics = da.newTopicDAO().selectByUserByLimit(u.getId(), start, topicsPerPage);
    List l = TopicsCommon.prepareTopics(topics);
    Map forums = new HashMap();
    for (Iterator iter = l.iterator(); iter.hasNext(); ) {
        Topic t = (Topic) iter.next();
        Forum f = ForumRepository.getForum(t.getForumId());
        if (f == null) {
            iter.remove();
            totalTopics--;
            continue;
        }
        forums.put(new Integer(t.getForumId()), f);
    }
    this.context.put("topics", l);
    this.context.put("forums", forums);
    ViewCommon.contextToPagination(start, totalTopics, topicsPerPage);
}
Also used : User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) HashMap(java.util.HashMap) DataAccessDriver(net.jforum.dao.DataAccessDriver) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Topic(net.jforum.entities.Topic) HashMap(java.util.HashMap) Map(java.util.Map) Forum(net.jforum.entities.Forum)

Example 14 with Forum

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

the class ForumCommon method getAllCategoriesAndForums.

/**
	 * Gets all forums available to the user.
	 * 
	 * @param us An <code>UserSession</code> instance with user information
	 * @param anonymousUserId The id which represents the anonymous user
	 * @param tracking <code>Map</code> instance with information 
	 * about the topics read by the user
	 * @param checkUnreadPosts <code>true</code> if is to search for unread topics inside the forums, 
	 * or <code>false</code> if this action is not needed. 
	 * @return A <code>List</code> instance where each record is an instance of a <code>Category</code>
	 * object
	 */
public static List getAllCategoriesAndForums(UserSession us, int anonymousUserId, Map tracking, boolean checkUnreadPosts) {
    long lastVisit = 0;
    int userId = anonymousUserId;
    if (us != null) {
        lastVisit = us.getLastVisit().getTime();
        userId = us.getUserId();
    }
    // Do not check for unread posts if the user is not logged in
    checkUnreadPosts = checkUnreadPosts && (userId != anonymousUserId);
    List categories = ForumRepository.getAllCategories(userId);
    if (!checkUnreadPosts) {
        return categories;
    }
    List returnCategories = new ArrayList();
    for (Iterator iter = categories.iterator(); iter.hasNext(); ) {
        Category c = new Category((Category) iter.next());
        for (Iterator tmpIterator = c.getForums().iterator(); tmpIterator.hasNext(); ) {
            Forum f = (Forum) tmpIterator.next();
            ForumCommon.checkUnreadPosts(f, tracking, lastVisit);
        }
        returnCategories.add(c);
    }
    return returnCategories;
}
Also used : Category(net.jforum.entities.Category) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Forum(net.jforum.entities.Forum)

Example 15 with Forum

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

the class ForumAction method editSave.

public void editSave() {
    ForumDAO forumDao = DataAccessDriver.getInstance().newForumDAO();
    Forum f = forumDao.selectById(this.request.getIntParameter("forum_id"));
    boolean moderated = f.isModerated();
    int categoryId = f.getCategoryId();
    f.setDescription(this.request.getParameter("description"));
    f.setIdCategories(this.request.getIntParameter("categories_id"));
    f.setName(this.request.getParameter("forum_name"));
    f.setModerated("1".equals(this.request.getParameter("moderate")));
    forumDao.update(f);
    if (moderated != f.isModerated()) {
        new ModerationCommon().setTopicModerationStatus(f.getId(), f.isModerated());
    }
    if (categoryId != f.getCategoryId()) {
        f.setIdCategories(categoryId);
        ForumRepository.removeForum(f);
        f.setIdCategories(this.request.getIntParameter("categories_id"));
        ForumRepository.addForum(f);
    } else {
        ForumRepository.reloadForum(f.getId());
    }
    //this.handleMailIntegration();
    this.list();
}
Also used : ForumDAO(net.jforum.dao.ForumDAO) ModerationCommon(net.jforum.view.admin.common.ModerationCommon) Forum(net.jforum.entities.Forum)

Aggregations

Forum (net.jforum.entities.Forum)28 List (java.util.List)14 Iterator (java.util.Iterator)13 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Topic (net.jforum.entities.Topic)8 Category (net.jforum.entities.Category)7 ForumDAO (net.jforum.dao.ForumDAO)5 TopicDAO (net.jforum.dao.TopicDAO)5 User (net.jforum.entities.User)5 AttachmentCommon (net.jforum.view.forum.common.AttachmentCommon)4 PostDAO (net.jforum.dao.PostDAO)3 UserDAO (net.jforum.dao.UserDAO)3 UserSession (net.jforum.entities.UserSession)3 PermissionControl (net.jforum.security.PermissionControl)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Set (java.util.Set)2