⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postaction.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

	public void edit() throws Exception {
		this.edit(false, null);
	}

	private void edit(boolean preview, Post p) throws Exception {
		int userId = SessionFacade.getUserSession().getUserId();
		int aId = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
		boolean canAccess = false;

		if (!preview) {
			PostModel pm = DataAccessDriver.getInstance().newPostModel();
			p = pm.selectById(this.request.getIntParameter("post_id"));

			// The post exist?
			if (p.getId() == 0) {
				this.postNotFound();
				return;
			}
		}

		boolean isModerator = SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT);
		canAccess = (isModerator || p.getUserId() == userId);

		if ((userId != aId) && canAccess) {
			Topic topic = DataAccessDriver.getInstance().newTopicModel().selectById(p.getTopicId());

			if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
				return;
			}

			if (topic.getStatus() == Topic.STATUS_LOCKED && !isModerator) {
				this.topicLocked();
				return;
			}

			if (preview && this.request.getParameter("topic_type") != null) {
				topic.setType(this.request.getIntParameter("topic_type"));
			}
			
			if (p.hasAttachments()) {
				this.context.put("attachments", 
						DataAccessDriver.getInstance().newAttachmentModel().selectAttachments(p.getId()));
			}

			this.context.put("attachmentsEnabled", SecurityRepository.canAccess(
					SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(p.getForumId())));
			
			QuotaLimit ql = new AttachmentCommon(this.request).getQuotaLimit(userId);
			this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
			
			this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
			this.context.put("forum", ForumRepository.getForum(p.getForumId()));
			this.context.put("action", "editSave");
			this.context.put("post", p);
			this.context.put("setType", p.getId() == topic.getFirstPostId());
			this.context.put("topic", topic);
			this.context.put("moduleAction", "post_form.htm");
			this.context.put("start", this.request.getParameter("start"));
			this.context.put("htmlAllowed", SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, 
					Integer.toString(topic.getForumId())));
			this.context.put("canCreateStickyOrAnnouncementTopics",
					SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS));
		}
		else {
			this.context.put("moduleAction", "message.htm");
			this.context.put("message", I18n.getMessage("CannotEditPost"));
		}

		User u = PostCommon.getUserForDisplay(userId);

		if (preview) {
			u.setNotifyOnMessagesEnabled(this.request.getParameter("notify") != null);
			
			if (u.getId() != p.getUserId()) {
				// Probably a moderator is editing the message
				this.context.put("previewUser", PostCommon.getUserForDisplay(p.getUserId()));
			}
		}

		this.context.put("user", u);
	}
	
	public void quote() throws Exception {
		PostModel pm = DataAccessDriver.getInstance().newPostModel();
		Post p = pm.selectById(this.request.getIntParameter("post_id"));

		if (!this.anonymousPost(p.getForumId())) {
			return;
		}

		Topic t = DataAccessDriver.getInstance().newTopicModel().selectById(p.getTopicId());

		if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
			return;
		}

		if (t.getStatus() == Topic.STATUS_LOCKED) {
			this.topicLocked();
			return;
		}

		if (p.getId() == 0) {
			this.postNotFound();
			return;
		}

		this.context.put("forum", ForumRepository.getForum(p.getForumId()));
		this.context.put("action", "insertSave");
		this.context.put("post", p);

		UserModel um = DataAccessDriver.getInstance().newUserModel();
		User u = um.selectById(p.getUserId());

		Topic topic = DataAccessDriver.getInstance().newTopicModel().selectById(p.getTopicId());
		int userId = SessionFacade.getUserSession().getUserId();
		
		this.context.put("attachmentsEnabled", SecurityRepository.canAccess(
				SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(topic.getForumId())));
		
		QuotaLimit ql = new AttachmentCommon(this.request).getQuotaLimit(userId);
		this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
		
		this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
		this.context.put("isNewPost", true);
		this.context.put("topic", topic);
		this.context.put("quote", "true");
		this.context.put("quoteUser", u.getUsername());
		this.context.put("moduleAction", "post_form.htm");
		this.context.put("setType", false);
		this.context.put("htmlAllowed", SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, 
				Integer.toString(topic.getForumId())));
		this.context.put("start", this.request.getParameter("start"));
		this.context.put("user", DataAccessDriver.getInstance().newUserModel().selectById(userId));
	}

	public void editSave() throws Exception {
		PostModel pm = DataAccessDriver.getInstance().newPostModel();
		TopicModel tm = DataAccessDriver.getInstance().newTopicModel();

		Post p = pm.selectById(this.request.getIntParameter("post_id"));
		p = PostCommon.fillPostFromRequest(p, true);

		// The user wants to preview the message before posting it?
		if (this.request.getParameter("preview") != null) {
			this.context.put("preview", true);

			Post postPreview = new Post(p);
			this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));

			this.edit(true, p);
		}
		else {
			Topic t = tm.selectById(p.getTopicId());

			if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
				return;
			}

			if (t.getStatus() == Topic.STATUS_LOCKED
					&& !SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT)) {
				this.topicLocked();
				return;
			}

			pm.update(p);
			
			// Attachments
			AttachmentCommon ac = new AttachmentCommon(this.request);
			ac.editAttachments(p.getId(), p.getForumId());
			
			try {
				ac.insertAttachments(p.getId(), p.getForumId());
			}
			catch (AttachmentException e) {
				JForum.enableCancelCommit();
				p.setText(this.request.getParameter("message"));
				this.context.put("errorMessage", e.getMessage());
				this.context.put("post", p);
				this.edit(false, p);
				return;
			}

			// Updates the topic title
			if (t.getFirstPostId() == p.getId()) {
				t.setTitle(p.getSubject());
				t.setType(this.request.getIntParameter("topic_type"));
				
				tm.update(t);
				
				ForumRepository.reloadForum(t.getForumId());
				TopicRepository.clearCache(t.getForumId());
			}

			if (this.request.getParameter("notify") == null) {
				tm.removeSubscription(p.getTopicId(), SessionFacade.getUserSession().getUserId());
			}

			String path = this.request.getContextPath() + "/posts/list/";
			String start = this.request.getParameter("start");
			if (start != null && !start.equals("0")) {
				path += start + "/";
			}

			path += p.getTopicId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#" + p.getId();
			JForum.setRedirect(path);
			
			if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
				PostRepository.update(p.getTopicId(), PostCommon.preparePostForDisplay(p));
			}
		}
	}
	
	public void waitingModeration()
	{
		this.context.put("moduleAction", "message.htm");
		
		int topicId = this.request.getIntParameter("topic_id");
		String path = this.request.getContextPath();
		
		if (topicId == 0) {
			path += "/forums/show/" + this.request.getParameter("forum_id");
		}
		else {
			path += "/posts/list/" + topicId;
		}
		
		this.context.put("message", I18n.getMessage("PostShow.waitingModeration", 
				new String[] { path + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) }));
	}
	
	private void notModeratedYet()
	{
		this.context.put("moduleAction", "message.htm");
		this.context.put("message", I18n.getMessage("PostShow.notModeratedYet"));
	}

	public void insertSave() throws Exception 
	{
		int forumId = this.request.getIntParameter("forum_id");
		boolean firstPost = false;

		if (!this.anonymousPost(forumId)) {
			SessionFacade.setAttribute(ConfigKeys.REQUEST_DUMP, this.request.dumpRequest());
			return;
		}
		
		Topic t = new Topic();
		t.setId(-1);
		t.setForumId(forumId);

		if (!TopicsCommon.isTopicAccessible(t.getForumId())
				|| this.isForumReadonly(t.getForumId(), this.request.getParameter("topic_id") != null)) {
			return;
		}

		TopicModel tm = DataAccessDriver.getInstance().newTopicModel();
		PostModel pm = DataAccessDriver.getInstance().newPostModel();
		ForumModel fm = DataAccessDriver.getInstance().newForumModel();

		if (this.request.getParameter("topic_id") != null) {
			t = tm.selectById(this.request.getIntParameter("topic_id"));

			// Cannot insert new messages on locked topics
			if (t.getStatus() == Topic.STATUS_LOCKED) {
				this.topicLocked();
				return;
			}
		}
		else {
			if (this.isReplyOnly(forumId)) {
				this.replyOnly();
				return;
			}
		}

		if (this.request.getParameter("topic_type") != null) {
			t.setType(this.request.getIntParameter("topic_type"));
		}

		UserSession us = SessionFacade.getUserSession();
		User u = new User();
		u.setId(us.getUserId());
		u.setUsername(us.getUsername());

		t.setPostedBy(u);

		// Set the Post
		Post p = PostCommon.fillPostFromRequest();
		p.setForumId(this.request.getIntParameter("forum_id"));
		
		if (p.getSubject() == null || p.getSubject() == "") {
			p.setSubject(t.getTitle());
		}

		boolean preview = (this.request.getParameter("preview") != null);
		boolean moderate = false;
		if (!preview) {
			// If topic_id is -1, then is the first post
			if (t.getId() == -1) {
				t.setTime(new Date());
				t.setTitle(this.request.getParameter("subject"));
				t.setModerated(ForumRepository.getForum(forumId).isModerated());

				t.setId(tm.addNew(t));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -