Mercurial Hosting > nabble
diff src/nabble/view/web/catalog/ChangePinOrder.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 18cf4872fd7f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/web/catalog/ChangePinOrder.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,229 @@ + +package nabble.view.web.catalog; + +import fschmidt.util.servlet.AuthorizingServlet; +import nabble.model.Node; +import nabble.model.NodeIterator; +import nabble.model.Person; +import nabble.model.Site; +import nabble.view.lib.Jtp; +import nabble.view.lib.Shared; +import nabble.view.lib.help.Help; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + + +public class ChangePinOrder extends HttpServlet implements AuthorizingServlet { + + public String getAuthorizationKey(HttpServletRequest request) throws ServletException { + return Jtp.getReadAuthorizationKey( Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request,"forum")) ); + } + + public boolean authorize(String key,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { + return Jtp.authorizeForRead(key,request,response); + } + + protected void service(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException + { + PrintWriter out = response.getWriter(); + String context = request.getContextPath(); + Site site = Jtp.getSiteNotNull(request); + Node forum = site.getNode(Jtp.getLong(request,"forum")); + if (forum == null) { + response.sendError(HttpServletResponse.SC_GONE, "This app has been deleted."); + return; + } + + String what = request.getParameter("what"); + + boolean pinnedThreads = "threads".equals(what); + String title = pinnedThreads? "Pinned Threads" : "Manage " + Jtp.childName(forum, true); + + Person visitor = Jtp.getVisitor(request, response); + + if (!Jtp.canBeEditedBy(forum,visitor)) { + Jtp.login("Only administrators can proceed in this area.", request, response); + return; + } + + List<Node> others = new ArrayList<Node>(); + List<Node> pinned = new ArrayList<Node>(); + + NodeIterator<? extends Node> children = forum.getChildren(); + for (Node node : children) { + if( !node.isPinned() ) + break; + + if (node.getKind()==Node.Kind.POST && pinnedThreads) + pinned.add(node); + else if (node.getKind()==Node.Kind.APP && !pinnedThreads) + pinned.add(node); + else + others.add(node); + } + children.close(); + + String action = request.getParameter("action"); + boolean up = "up".equals(action); + boolean down = "down".equals(action); + if (up || down) { + long id = Long.parseLong(request.getParameter("id")); + Node node = site.getNode(id); + if (pinned.contains(node)) { + int index = pinned.indexOf(node); + pinned.remove(node); + index = up? index-1 : index+1; + if (index < 0) + index = 0; + else if (index > pinned.size()) + index = pinned.size(); + pinned.add(index, node); + + List<Node> all = new ArrayList<Node>(); + // Ensure that forums come first + if (pinnedThreads) { + all.addAll(others); + all.addAll(pinned); + } else { + all.addAll(pinned); + all.addAll(others); + } + forum.pin(all.toArray(new Node[0])); + + Jtp.sendRedirect(request,response,"/catalog/ChangePinOrder.jtp?forum=" + forum.getId() + "&what=" + what); + return; + } + } + + + out.print( "\r\n<html>\r\n <head>\r\n " ); + Shared.title(request, response, "Manage " + title); + out.print( "\r\n </head>\r\n <body>\r\n " ); + Shared.minHeader(request,response, forum); + out.print( "\r\n " ); + Shared.editHeader(forum.getSubjectHtml(), title, out); + out.print( "\r\n <style>\r\n table.pin {\r\n width: 40em;\r\n border-collapse:collapse;\r\n }\r\n\r\n table.pin td {\r\n padding: .4em;\r\n }\r\n \r\n .title-row {\r\n margin-top:2em;\r\n padding:.2em;\r\n border-bottom-width:2px;\r\n border-bottom-style:solid;\r\n font-weight:bold;\r\n }\r\n </style>\r\n\r\n <div class=\"title-row light-border-color\">\r\n " ); + out.print( (pinnedThreads? "Pinned Topics" : "Pinned " + Jtp.childName(forum, true)) ); + out.print( "\r\n </div>\r\n\r\n " ); + if (!pinnedThreads) { + out.print( "\r\n <div class=\"weak-color\" style=\"margin:0 0 .3em .2em\">Pinned " ); + out.print( (Jtp.childName(forum, true).toLowerCase()) ); + out.print( " are created or approved by a forum owner to always appear under a forum.</div>\r\n " ); + } + out.print( "\r\n <div style=\"padding-left:2em\">\r\n " ); + + if (pinned.size() > 1) { + + out.print( "\r\n<table id=\"table-pin\" class=\"pin\">\r\n " ); + + for (int i = 0; i < pinned.size(); i++) { + Node node = pinned.get(i); + boolean isFirst = i == 0; + boolean isLast = i == pinned.size() - 1; + + out.print( "\r\n<tr>\r\n <td style=\"width:24px\"><img src=\"/images/" ); + out.print( (pinnedThreads? "pin.png" : "forum_pin.png") ); + out.print( "\"></td>\r\n <td style=\"padding-left: .4em;width:100%\"><a href=\"" ); + out.print( (Jtp.path(node)) ); + out.print( "\">" ); + out.print( (node.getSubjectHtml()) ); + out.print( "</a></td>\r\n <td style=\"width:24px\">\r\n <a class=\"up\" style=\"" ); + out.print( (isFirst? "display:none" : "") ); + out.print( "\" href=\"/catalog/ChangePinOrder.jtp?forum=" ); + out.print( (forum.getId()) ); + out.print( "&what=" ); + out.print( (what) ); + out.print( "&action=up&id=" ); + out.print( (node.getId()) ); + out.print( "\"><img src=\"" ); + out.print( (context) ); + out.print( "/images/icon_up.png\" style=\"border:none;margin:5px\" title=\"Move up\"/></a>\r\n </td>\r\n <td style=\"width:24px\">\r\n <a class=\"down\" style=\"" ); + out.print( (isLast? "display:none" : "") ); + out.print( "\" href=\"/catalog/ChangePinOrder.jtp?forum=" ); + out.print( (forum.getId()) ); + out.print( "&what=" ); + out.print( (what) ); + out.print( "&action=down&id=" ); + out.print( (node.getId()) ); + out.print( "\"><img src=\"" ); + out.print( (context) ); + out.print( "/images/icon_down.png\" style=\"border:none;margin:5px\" title=\"Move down\"/></a>\r\n </td>\r\n <td style=\"width:5em\"><a href=\"/catalog/SetPin.jtp?node=" ); + out.print( (node.getId()) ); + out.print( "&value=unpin\">Unpin</a></td>\r\n</tr>\r\n" ); + + } + + out.print( "\r\n</table>\r\n" ); + + } else if (pinned.size() == 1) { + Node node = pinned.get(0); + + out.print( "\r\n<table class=\"pin\">\r\n <tr>\r\n <td style=\"width:24px\"><img src=\"/images/" ); + out.print( (pinnedThreads? "pin.png" : "forum_pin.png") ); + out.print( "\"></td>\r\n <td><a href=\"" ); + out.print( (Jtp.path(node)) ); + out.print( "\">" ); + out.print( (node.getSubjectHtml()) ); + out.print( "</a></td>\r\n <td style=\"width:5em\"><a href=\"/catalog/SetPin.jtp?node=" ); + out.print( (node.getId()) ); + out.print( "&value=unpin\">Unpin</a></td>\r\n </tr>\r\n</table>\r\n" ); + + } else { + + out.print( "\r\n<div style=\"margin-top:1em\">\r\n No " ); + out.print( (pinnedThreads? "pinned topics" : "pinned " + Jtp.childName(forum, true).toLowerCase()) ); + out.print( ".\r\n</div>\r\n" ); + + } + + out.print( "\r\n</div>\r\n\r\n" ); + + if (!pinnedThreads) { + // Search for unpinned child forums + List<Node> childForums = forum.getChildApps().get(0, 1000); + List<Node> unpinned = new ArrayList<Node>(); + for (Node child : childForums) { + if (!child.isPinned()) + unpinned.add(child); + } + + if (!unpinned.isEmpty()) { + + out.print( "\r\n<div class=\"title-row light-border-color\">Unpinned Sub-forums</div>\r\n<div class=\"weak-color\" style=\"margin:0 0 .3em .2em\">Unpinned sub-forums are user-created categories which float under a forum like a thread.</div>\r\n<div style=\"padding-left:2em\">\r\n <table class=\"pin\">\r\n " ); + + for (Node node : unpinned) { + + out.print( "\r\n<tr>\r\n <td style=\"width:30px\"><img src=\"/images/forum.png\"></td> \r\n <td style=\"width:100%\"><a href=\"" ); + out.print( (Jtp.path(node)) ); + out.print( "\">" ); + out.print( (node.getSubjectHtml()) ); + out.print( "</a></td>\r\n <td style=\"width:24px\"><img src=\"/images/unpin.png\"/></td>\r\n <td style=\"width:5em\"><a href=\"/catalog/SetPin.jtp?node=" ); + out.print( (node.getId()) ); + out.print( "&value=pin\">Pin</a></td>\r\n</tr>\r\n" ); + + } + + out.print( "\r\n</table>\r\n</div>\r\n" ); + + } + } + + out.print( "\r\n\r\n<div class=\"light-bg-color\" style=\"padding: .5em;margin:1.5em 0 0\">\r\n <div class=\"second-font field-title\" style=\"margin-top:0\">Related Help Article</div>\r\n " ); + out.print( (Help.pinned_subapps.link()) ); + out.print( "\r\n</div>\r\n\r\n" ); + Shared.footer(request, response); + out.print( "\r\n" ); + Shared.analytics(request,response); + out.print( "\r\n</body>\r\n</html>\r\n" ); + + } +} +