_CommentController.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. import java.util.List;
  35. @RouterMapping(url = "/admin/comment", viewPath = "/WEB-INF/admin/comment")
  36. @Before(ActionCacheClearInterceptor.class)
  37. @RouterNotAllowConvert
  38. public class _CommentController extends JBaseCRUDController<Comment> {
  39. private String getModule() {
  40. return getPara("m");
  41. }
  42. private String getType() {
  43. return getPara("t");
  44. }
  45. private boolean getSon() {
  46. return Boolean.parseBoolean(getPara("son"));
  47. }
  48. @Override
  49. public void index() {
  50. keepPara();
  51. setAttr("module", TemplateManager.me().currentTemplateModule(getModule()));
  52. if ("uuhelper".equals(getModule())) {
  53. setAttr("answered_count", CommentQuery.me().findCountByModuleAndSonAndContentId(getParaToBigInteger("cid"), getModule(), true));
  54. setAttr("unanswered_count", CommentQuery.me().findCountByModuleAndSonAndContentId(getParaToBigInteger("cid"), getModule(), false));
  55. setAttr("draft_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_DRAFT));
  56. setAttr("normal_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_NORMAL));
  57. setAttr("delete_count", CommentQuery.me().findCountByModuleAndStatusAndContentId(getParaToBigInteger("cid"), getModule(), Comment.STATUS_DELETE));
  58. } else {
  59. setAttr("delete_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_DELETE));
  60. setAttr("draft_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_DRAFT));
  61. setAttr("normal_count", CommentQuery.me().findCountByModuleAndStatus(getModule(), Comment.STATUS_NORMAL));
  62. setAttr("count", CommentQuery.me().findCountInNormalByModule(getModule()));
  63. }
  64. super.index();
  65. }
  66. @Override
  67. public Page<Comment> onIndexDataLoad(int pageNumber, int pageSize) {
  68. Page<Comment> page = null;
  69. BigInteger contentId = getParaToBigInteger("cid");
  70. BigInteger parentCommentId = getParaToBigInteger("pid");
  71. if (StringUtils.isNotBlank(getPara("s"))) {
  72. page = CommentQuery.me().paginateWithContent(pageNumber, pageSize, getModule(), getType(), contentId,
  73. parentCommentId, getPara("s"));
  74. } else if (StringUtils.isNotBlank(getPara("son"))) {
  75. page = CommentQuery.me().paginateWithContentAndSon(pageNumber, pageSize, getModule(), getType(), contentId,
  76. parentCommentId,getSon());
  77. }
  78. page = CommentQuery.me().paginateWithContentNotInDelete(pageNumber, pageSize, getModule(), getType(), contentId,
  79. parentCommentId);
  80. List<Comment> comments = page.getList();
  81. for (Comment comment : comments) {
  82. String userAvater = comment.getUuUserAvatar();
  83. if (!(StringUtils.isNotBlank(userAvater) && comment.isImageExist(userAvater))) {
  84. comment.setUuUserAvatar(null);
  85. }
  86. }
  87. return page;
  88. }
  89. @Override
  90. public void edit() {
  91. BigInteger id = getParaToBigInteger("id");
  92. Comment comment = CommentQuery.me().findById(id);
  93. setAttr("comment", comment);
  94. }
  95. @Before(UCodeInterceptor.class)
  96. public void trash() {
  97. Comment c = CommentQuery.me().findById(getParaToBigInteger("id"));
  98. if (c != null) {
  99. c.setStatus(Comment.STATUS_DELETE);
  100. if (c.saveOrUpdate()) {
  101. renderAjaxResultForSuccess();
  102. } else {
  103. renderAjaxResultForError("restore error!");
  104. }
  105. } else {
  106. renderAjaxResultForError("trash error!");
  107. }
  108. }
  109. @Before(UCodeInterceptor.class)
  110. public void restore() {
  111. BigInteger id = getParaToBigInteger("id");
  112. Comment c = CommentQuery.me().findById(id);
  113. if (c != null && c.isDelete()) {
  114. c.setStatus(Content.STATUS_DRAFT);
  115. if (c.saveOrUpdate()) {
  116. renderAjaxResultForSuccess("success");
  117. } else {
  118. renderAjaxResultForError("restore error!");
  119. }
  120. } else {
  121. renderAjaxResultForError("restore error!");
  122. }
  123. }
  124. @Before(UCodeInterceptor.class)
  125. public void pub() {
  126. BigInteger id = getParaToBigInteger("id");
  127. Comment c = CommentQuery.me().findById(id);
  128. if (c != null) {
  129. c.setStatus(Content.STATUS_NORMAL);
  130. if (c.saveOrUpdate()) {
  131. renderAjaxResultForSuccess("success");
  132. } else {
  133. renderAjaxResultForError("pub fail!");
  134. }
  135. } else {
  136. renderAjaxResultForError("pub error!");
  137. }
  138. }
  139. @Before(UCodeInterceptor.class)
  140. public void draft() {
  141. BigInteger id = getParaToBigInteger("id");
  142. Comment c = CommentQuery.me().findById(id);
  143. if (c != null) {
  144. c.setStatus(Content.STATUS_DRAFT);
  145. if (c.saveOrUpdate()) {
  146. renderAjaxResultForSuccess("success");
  147. } else {
  148. renderAjaxResultForError("draft fail!");
  149. }
  150. } else {
  151. renderAjaxResultForError("draft error!");
  152. }
  153. }
  154. @Before(UCodeInterceptor.class)
  155. public void delete() {
  156. BigInteger id = getParaToBigInteger("id");
  157. final Comment c = CommentQuery.me().findById(id);
  158. if (c != null) {
  159. if (c.delete()) {
  160. renderAjaxResultForSuccess();
  161. return;
  162. }
  163. }
  164. renderAjaxResultForError();
  165. }
  166. @Override
  167. public void save() {
  168. Comment comment = getModel(Comment.class);
  169. String username = getPara("username");
  170. if (StringUtils.isNotBlank(username)) {
  171. User user = UserQuery.me().findUserByUsername(username);
  172. if (user == null) {
  173. renderAjaxResultForError("系统没有该用户:" + username);
  174. return;
  175. }
  176. comment.setUserId(user.getId());
  177. }
  178. if (comment.saveOrUpdate()) {
  179. comment.updateCommentCount();
  180. renderAjaxResultForSuccess();
  181. } else {
  182. renderAjaxResultForError();
  183. }
  184. }
  185. public void reply_layer() {
  186. BigInteger id = getParaToBigInteger("id");
  187. setAttr("comment", CommentQuery.me().findById(id));
  188. }
  189. public void reply() {
  190. Comment comment = getModel(Comment.class);
  191. comment.setType(Comment.TYPE_COMMENT);
  192. comment.setIp(getIPAddress());
  193. comment.setAgent(getUserAgent());
  194. User user = getLoginedUser();
  195. String author = StringUtils.isNotBlank(user.getNickname()) ? user.getNickname() : user.getUsername();
  196. comment.setAuthor(author);
  197. comment.setEmail(user.getEmail());
  198. comment.setStatus(Comment.STATUS_NORMAL);
  199. comment.setUserId(user.getId());
  200. comment.setCreated(new Date());
  201. comment.setText(JsoupUtils.getBodyHtml(comment.getText()));
  202. comment.save();
  203. renderAjaxResultForSuccess();
  204. }
  205. @Before(UCodeInterceptor.class)
  206. public void changeCommentTopSataus() {
  207. BigInteger id = getParaToBigInteger("id");
  208. Comment c = CommentQuery.me().findById(id);
  209. String commentTopStatus = getPara("commentTopStatus");
  210. if (c != null) {
  211. if ("top".equals(commentTopStatus)) {
  212. c.setOrderNumber(Long.parseLong("1"));
  213. } else {
  214. c.setOrderNumber(Long.parseLong("0"));
  215. }
  216. if (c.saveOrUpdate()) {
  217. renderAjaxResultForSuccess("success");
  218. } else {
  219. renderAjaxResultForError("restore error!");
  220. }
  221. } else {
  222. renderAjaxResultForError("restore error!");
  223. }
  224. }
  225. }