_CommentController.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
  3. *
  4. * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.gnu.org/licenses/lgpl-3.0.txt
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.jpress.admin.controller;
  17. import com.jfinal.aop.Before;
  18. import com.jfinal.plugin.activerecord.Page;
  19. import io.jpress.core.JBaseCRUDController;
  20. import io.jpress.core.interceptor.ActionCacheClearInterceptor;
  21. import io.jpress.interceptor.UCodeInterceptor;
  22. import io.jpress.model.Comment;
  23. import io.jpress.model.Content;
  24. import io.jpress.model.User;
  25. import io.jpress.model.query.CommentQuery;
  26. import io.jpress.model.query.UserQuery;
  27. import io.jpress.router.RouterMapping;
  28. import io.jpress.router.RouterNotAllowConvert;
  29. import io.jpress.template.TemplateManager;
  30. import io.jpress.utils.JsoupUtils;
  31. import io.jpress.utils.StringUtils;
  32. import java.math.BigInteger;
  33. import java.util.Date;
  34. @RouterMapping(url = "/admin/comment", viewPath = "/WEB-INF/admin/comment")
  35. @Before(ActionCacheClearInterceptor.class)
  36. @RouterNotAllowConvert
  37. public class _CommentController extends JBaseCRUDController<Comment> {
  38. private String getModule() {
  39. return getPara("m");
  40. }
  41. private String getType() {
  42. return getPara("t");
  43. }
  44. private boolean getSon() {
  45. return Boolean.parseBoolean(getPara("son"));
  46. }
  47. @Override
  48. public void index() {
  49. keepPara();
  50. setAttr("module", TemplateManager.me().currentTemplateModule(getModule()));
  51. if ("uuhelper".equals(getModule())) {
  52. setAttr("answered_count", CommentQuery.me().findCountByModuleAndSonAndContentId(getParaToBigInteger("cid"), getModule(), true));
  53. setAttr("unanswered_count", CommentQuery.me().findCountByModuleAndSonAndContentId(getParaToBigInteger("cid"), getModule(), false));
  54. setAttr("draft_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_DRAFT));
  55. setAttr("normal_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_NORMAL));
  56. setAttr("delete_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_DELETE));
  57. } else {
  58. setAttr("delete_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_DELETE));
  59. setAttr("draft_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_DRAFT));
  60. setAttr("normal_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_NORMAL));
  61. setAttr("count", CommentQuery.me().findCountInNormalByModule(getModule()));
  62. }
  63. super.index();
  64. }
  65. @Override
  66. public Page<Comment> onIndexDataLoad(int pageNumber, int pageSize) {
  67. BigInteger contentId = getParaToBigInteger("cid");
  68. BigInteger parentCommentId = getParaToBigInteger("pid");
  69. if (StringUtils.isNotBlank(getPara("s"))) {
  70. return CommentQuery.me().paginateWithContent(pageNumber, pageSize, getModule(), getType(), contentId,
  71. parentCommentId, getPara("s"));
  72. } else if (StringUtils.isNotBlank(getPara("son"))) {
  73. return CommentQuery.me().paginateWithContentAndSon(pageNumber, pageSize, getModule(), getType(), contentId,
  74. parentCommentId,getSon());
  75. }
  76. return CommentQuery.me().paginateWithContentNotInDelete(pageNumber, pageSize, getModule(), getType(), contentId,
  77. parentCommentId);
  78. }
  79. @Override
  80. public void edit() {
  81. BigInteger id = getParaToBigInteger("id");
  82. Comment comment = CommentQuery.me().findById(id);
  83. setAttr("comment", comment);
  84. }
  85. @Before(UCodeInterceptor.class)
  86. public void trash() {
  87. Comment c = CommentQuery.me().findById(getParaToBigInteger("id"));
  88. if (c != null) {
  89. c.setStatus(Comment.STATUS_DELETE);
  90. if (c.saveOrUpdate()) {
  91. renderAjaxResultForSuccess();
  92. } else {
  93. renderAjaxResultForError("restore error!");
  94. }
  95. } else {
  96. renderAjaxResultForError("trash error!");
  97. }
  98. }
  99. @Before(UCodeInterceptor.class)
  100. public void restore() {
  101. BigInteger id = getParaToBigInteger("id");
  102. Comment c = CommentQuery.me().findById(id);
  103. if (c != null && c.isDelete()) {
  104. c.setStatus(Content.STATUS_DRAFT);
  105. if (c.saveOrUpdate()) {
  106. renderAjaxResultForSuccess("success");
  107. } else {
  108. renderAjaxResultForError("restore error!");
  109. }
  110. } else {
  111. renderAjaxResultForError("restore error!");
  112. }
  113. }
  114. @Before(UCodeInterceptor.class)
  115. public void pub() {
  116. BigInteger id = getParaToBigInteger("id");
  117. Comment c = CommentQuery.me().findById(id);
  118. if (c != null) {
  119. c.setStatus(Content.STATUS_NORMAL);
  120. if (c.saveOrUpdate()) {
  121. renderAjaxResultForSuccess("success");
  122. } else {
  123. renderAjaxResultForError("pub fail!");
  124. }
  125. } else {
  126. renderAjaxResultForError("pub error!");
  127. }
  128. }
  129. @Before(UCodeInterceptor.class)
  130. public void draft() {
  131. BigInteger id = getParaToBigInteger("id");
  132. Comment c = CommentQuery.me().findById(id);
  133. if (c != null) {
  134. c.setStatus(Content.STATUS_DRAFT);
  135. if (c.saveOrUpdate()) {
  136. renderAjaxResultForSuccess("success");
  137. } else {
  138. renderAjaxResultForError("draft fail!");
  139. }
  140. } else {
  141. renderAjaxResultForError("draft error!");
  142. }
  143. }
  144. @Before(UCodeInterceptor.class)
  145. public void delete() {
  146. BigInteger id = getParaToBigInteger("id");
  147. final Comment c = CommentQuery.me().findById(id);
  148. if (c != null) {
  149. if (c.delete()) {
  150. renderAjaxResultForSuccess();
  151. return;
  152. }
  153. }
  154. renderAjaxResultForError();
  155. }
  156. @Override
  157. public void save() {
  158. Comment comment = getModel(Comment.class);
  159. String username = getPara("username");
  160. if (StringUtils.isNotBlank(username)) {
  161. User user = UserQuery.me().findUserByUsername(username);
  162. if (user == null) {
  163. renderAjaxResultForError("系统没有该用户:" + username);
  164. return;
  165. }
  166. comment.setUserId(user.getId());
  167. }
  168. if (comment.saveOrUpdate()) {
  169. comment.updateCommentCount();
  170. renderAjaxResultForSuccess();
  171. } else {
  172. renderAjaxResultForError();
  173. }
  174. }
  175. public void reply_layer() {
  176. BigInteger id = getParaToBigInteger("id");
  177. setAttr("comment", CommentQuery.me().findById(id));
  178. }
  179. public void reply() {
  180. Comment comment = getModel(Comment.class);
  181. comment.setType(Comment.TYPE_COMMENT);
  182. comment.setIp(getIPAddress());
  183. comment.setAgent(getUserAgent());
  184. User user = getLoginedUser();
  185. String author = StringUtils.isNotBlank(user.getNickname()) ? user.getNickname() : user.getUsername();
  186. comment.setAuthor(author);
  187. comment.setEmail(user.getEmail());
  188. comment.setStatus(Comment.STATUS_NORMAL);
  189. comment.setUserId(user.getId());
  190. comment.setCreated(new Date());
  191. comment.setText(JsoupUtils.getBodyHtml(comment.getText()));
  192. comment.save();
  193. renderAjaxResultForSuccess();
  194. }
  195. }