/** * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com). * * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.gnu.org/licenses/lgpl-3.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.jpress.admin.controller; import com.jfinal.aop.Before; import com.jfinal.plugin.activerecord.Page; import io.jpress.core.JBaseCRUDController; import io.jpress.core.interceptor.ActionCacheClearInterceptor; import io.jpress.interceptor.UCodeInterceptor; import io.jpress.model.Comment; import io.jpress.model.Content; import io.jpress.model.User; import io.jpress.model.query.CommentQuery; import io.jpress.model.query.UserQuery; import io.jpress.router.RouterMapping; import io.jpress.router.RouterNotAllowConvert; import io.jpress.template.TemplateManager; import io.jpress.utils.JsoupUtils; import io.jpress.utils.StringUtils; import java.math.BigInteger; import java.util.Date; import java.util.List; @RouterMapping(url = "/admin/comment", viewPath = "/WEB-INF/admin/comment") @Before(ActionCacheClearInterceptor.class) @RouterNotAllowConvert public class _CommentController extends JBaseCRUDController { private String getModule() { return getPara("m"); } private String getType() { return getPara("t"); } private boolean getSon() { return Boolean.parseBoolean(getPara("son")); } @Override public void index() { keepPara(); setAttr("module", TemplateManager.me().currentTemplateModule(getModule())); if ("uuhelper".equals(getModule())) { setAttr("answered_count", CommentQuery.me().findCountByModuleAndSonAndContentId(getParaToBigInteger("cid"), getModule(), true)); setAttr("unanswered_count", CommentQuery.me().findCountByModuleAndSonAndContentId(getParaToBigInteger("cid"), getModule(), false)); setAttr("draft_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_DRAFT)); setAttr("normal_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_NORMAL)); setAttr("delete_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_DELETE)); } else { setAttr("delete_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_DELETE)); setAttr("draft_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_DRAFT)); setAttr("normal_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_NORMAL)); setAttr("count", CommentQuery.me().findCountInNormalByModule(getModule())); } super.index(); } @Override public Page onIndexDataLoad(int pageNumber, int pageSize) { Page page = null; BigInteger contentId = getParaToBigInteger("cid"); BigInteger parentCommentId = getParaToBigInteger("pid"); if (StringUtils.isNotBlank(getPara("s"))) { page = CommentQuery.me().paginateWithContent(pageNumber, pageSize, getModule(), getType(), contentId, parentCommentId, getPara("s")); } else if (StringUtils.isNotBlank(getPara("son"))) { page = CommentQuery.me().paginateWithContentAndSon(pageNumber, pageSize, getModule(), getType(), contentId, parentCommentId,getSon()); } page = CommentQuery.me().paginateWithContentNotInDelete(pageNumber, pageSize, getModule(), getType(), contentId, parentCommentId); List comments = page.getList(); for (Comment comment : comments) { String userAvater = comment.getUuUserAvatar(); if (!(StringUtils.isNotBlank(userAvater) && comment.isImageExist(userAvater))) { comment.setUuUserAvatar(null); } } return page; } @Override public void edit() { BigInteger id = getParaToBigInteger("id"); Comment comment = CommentQuery.me().findById(id); setAttr("comment", comment); } @Before(UCodeInterceptor.class) public void trash() { Comment c = CommentQuery.me().findById(getParaToBigInteger("id")); if (c != null) { c.setStatus(Comment.STATUS_DELETE); if (c.saveOrUpdate()) { renderAjaxResultForSuccess(); } else { renderAjaxResultForError("restore error!"); } } else { renderAjaxResultForError("trash error!"); } } @Before(UCodeInterceptor.class) public void restore() { BigInteger id = getParaToBigInteger("id"); Comment c = CommentQuery.me().findById(id); if (c != null && c.isDelete()) { c.setStatus(Content.STATUS_DRAFT); if (c.saveOrUpdate()) { renderAjaxResultForSuccess("success"); } else { renderAjaxResultForError("restore error!"); } } else { renderAjaxResultForError("restore error!"); } } @Before(UCodeInterceptor.class) public void pub() { BigInteger id = getParaToBigInteger("id"); Comment c = CommentQuery.me().findById(id); if (c != null) { c.setStatus(Content.STATUS_NORMAL); if (c.saveOrUpdate()) { renderAjaxResultForSuccess("success"); } else { renderAjaxResultForError("pub fail!"); } } else { renderAjaxResultForError("pub error!"); } } @Before(UCodeInterceptor.class) public void draft() { BigInteger id = getParaToBigInteger("id"); Comment c = CommentQuery.me().findById(id); if (c != null) { c.setStatus(Content.STATUS_DRAFT); if (c.saveOrUpdate()) { renderAjaxResultForSuccess("success"); } else { renderAjaxResultForError("draft fail!"); } } else { renderAjaxResultForError("draft error!"); } } @Before(UCodeInterceptor.class) public void delete() { BigInteger id = getParaToBigInteger("id"); final Comment c = CommentQuery.me().findById(id); if (c != null) { if (c.delete()) { renderAjaxResultForSuccess(); return; } } renderAjaxResultForError(); } @Override public void save() { Comment comment = getModel(Comment.class); String username = getPara("username"); if (StringUtils.isNotBlank(username)) { User user = UserQuery.me().findUserByUsername(username); if (user == null) { renderAjaxResultForError("系统没有该用户:" + username); return; } comment.setUserId(user.getId()); } if (comment.saveOrUpdate()) { comment.updateCommentCount(); renderAjaxResultForSuccess(); } else { renderAjaxResultForError(); } } public void reply_layer() { BigInteger id = getParaToBigInteger("id"); setAttr("comment", CommentQuery.me().findById(id)); } public void reply() { Comment comment = getModel(Comment.class); comment.setType(Comment.TYPE_COMMENT); comment.setIp(getIPAddress()); comment.setAgent(getUserAgent()); User user = getLoginedUser(); String author = StringUtils.isNotBlank(user.getNickname()) ? user.getNickname() : user.getUsername(); comment.setAuthor(author); comment.setEmail(user.getEmail()); comment.setStatus(Comment.STATUS_NORMAL); comment.setUserId(user.getId()); comment.setCreated(new Date()); comment.setText(JsoupUtils.getBodyHtml(comment.getText())); comment.save(); renderAjaxResultForSuccess(); } @Before(UCodeInterceptor.class) public void changeCommentTopSataus() { BigInteger id = getParaToBigInteger("id"); Comment c = CommentQuery.me().findById(id); String commentTopStatus = getPara("commentTopStatus"); if (c != null) { if ("top".equals(commentTopStatus)) { c.setOrderNumber(Long.parseLong("1")); } else { c.setOrderNumber(Long.parseLong("0")); } if (c.saveOrUpdate()) { renderAjaxResultForSuccess("success"); } else { renderAjaxResultForError("restore error!"); } } else { renderAjaxResultForError("restore error!"); } } }