comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1
2 package nabble.view.web.catalog;
3
4 import fschmidt.util.servlet.AuthorizingServlet;
5 import nabble.model.Node;
6 import nabble.model.NodeIterator;
7 import nabble.model.Person;
8 import nabble.model.Site;
9 import nabble.view.lib.Jtp;
10 import nabble.view.lib.Shared;
11 import nabble.view.lib.help.Help;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.util.ArrayList;
20 import java.util.List;
21
22
23 public class ChangePinOrder extends HttpServlet implements AuthorizingServlet {
24
25 public String getAuthorizationKey(HttpServletRequest request) throws ServletException {
26 return Jtp.getReadAuthorizationKey( Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request,"forum")) );
27 }
28
29 public boolean authorize(String key,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
30 return Jtp.authorizeForRead(key,request,response);
31 }
32
33 protected void service(HttpServletRequest request, HttpServletResponse response)
34 throws ServletException, IOException
35 {
36 PrintWriter out = response.getWriter();
37 String context = request.getContextPath();
38 Site site = Jtp.getSiteNotNull(request);
39 Node forum = site.getNode(Jtp.getLong(request,"forum"));
40 if (forum == null) {
41 response.sendError(HttpServletResponse.SC_GONE, "This app has been deleted.");
42 return;
43 }
44
45 String what = request.getParameter("what");
46
47 boolean pinnedThreads = "threads".equals(what);
48 String title = pinnedThreads? "Pinned Threads" : "Manage " + Jtp.childName(forum, true);
49
50 Person visitor = Jtp.getVisitor(request, response);
51
52 if (!Jtp.canBeEditedBy(forum,visitor)) {
53 Jtp.login("Only administrators can proceed in this area.", request, response);
54 return;
55 }
56
57 List<Node> others = new ArrayList<Node>();
58 List<Node> pinned = new ArrayList<Node>();
59
60 NodeIterator<? extends Node> children = forum.getChildren();
61 for (Node node : children) {
62 if( !node.isPinned() )
63 break;
64
65 if (node.getKind()==Node.Kind.POST && pinnedThreads)
66 pinned.add(node);
67 else if (node.getKind()==Node.Kind.APP && !pinnedThreads)
68 pinned.add(node);
69 else
70 others.add(node);
71 }
72 children.close();
73
74 String action = request.getParameter("action");
75 boolean up = "up".equals(action);
76 boolean down = "down".equals(action);
77 if (up || down) {
78 long id = Long.parseLong(request.getParameter("id"));
79 Node node = site.getNode(id);
80 if (pinned.contains(node)) {
81 int index = pinned.indexOf(node);
82 pinned.remove(node);
83 index = up? index-1 : index+1;
84 if (index < 0)
85 index = 0;
86 else if (index > pinned.size())
87 index = pinned.size();
88 pinned.add(index, node);
89
90 List<Node> all = new ArrayList<Node>();
91 // Ensure that forums come first
92 if (pinnedThreads) {
93 all.addAll(others);
94 all.addAll(pinned);
95 } else {
96 all.addAll(pinned);
97 all.addAll(others);
98 }
99 forum.pin(all.toArray(new Node[0]));
100
101 Jtp.sendRedirect(request,response,"/catalog/ChangePinOrder.jtp?forum=" + forum.getId() + "&what=" + what);
102 return;
103 }
104 }
105
106
107 out.print( "\r\n<html>\r\n <head>\r\n " );
108 Shared.title(request, response, "Manage " + title);
109 out.print( "\r\n </head>\r\n <body>\r\n " );
110 Shared.minHeader(request,response, forum);
111 out.print( "\r\n " );
112 Shared.editHeader(forum.getSubjectHtml(), title, out);
113 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 " );
114 out.print( (pinnedThreads? "Pinned Topics" : "Pinned " + Jtp.childName(forum, true)) );
115 out.print( "\r\n </div>\r\n\r\n " );
116 if (!pinnedThreads) {
117 out.print( "\r\n <div class=\"weak-color\" style=\"margin:0 0 .3em .2em\">Pinned " );
118 out.print( (Jtp.childName(forum, true).toLowerCase()) );
119 out.print( " are created or approved by a forum owner to always appear under a forum.</div>\r\n " );
120 }
121 out.print( "\r\n <div style=\"padding-left:2em\">\r\n " );
122
123 if (pinned.size() > 1) {
124
125 out.print( "\r\n<table id=\"table-pin\" class=\"pin\">\r\n " );
126
127 for (int i = 0; i < pinned.size(); i++) {
128 Node node = pinned.get(i);
129 boolean isFirst = i == 0;
130 boolean isLast = i == pinned.size() - 1;
131
132 out.print( "\r\n<tr>\r\n <td style=\"width:24px\"><img src=\"/images/" );
133 out.print( (pinnedThreads? "pin.png" : "forum_pin.png") );
134 out.print( "\"></td>\r\n <td style=\"padding-left: .4em;width:100%\"><a href=\"" );
135 out.print( (Jtp.path(node)) );
136 out.print( "\">" );
137 out.print( (node.getSubjectHtml()) );
138 out.print( "</a></td>\r\n <td style=\"width:24px\">\r\n <a class=\"up\" style=\"" );
139 out.print( (isFirst? "display:none" : "") );
140 out.print( "\" href=\"/catalog/ChangePinOrder.jtp?forum=" );
141 out.print( (forum.getId()) );
142 out.print( "&what=" );
143 out.print( (what) );
144 out.print( "&action=up&id=" );
145 out.print( (node.getId()) );
146 out.print( "\"><img src=\"" );
147 out.print( (context) );
148 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=\"" );
149 out.print( (isLast? "display:none" : "") );
150 out.print( "\" href=\"/catalog/ChangePinOrder.jtp?forum=" );
151 out.print( (forum.getId()) );
152 out.print( "&what=" );
153 out.print( (what) );
154 out.print( "&action=down&id=" );
155 out.print( (node.getId()) );
156 out.print( "\"><img src=\"" );
157 out.print( (context) );
158 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=" );
159 out.print( (node.getId()) );
160 out.print( "&value=unpin\">Unpin</a></td>\r\n</tr>\r\n" );
161
162 }
163
164 out.print( "\r\n</table>\r\n" );
165
166 } else if (pinned.size() == 1) {
167 Node node = pinned.get(0);
168
169 out.print( "\r\n<table class=\"pin\">\r\n <tr>\r\n <td style=\"width:24px\"><img src=\"/images/" );
170 out.print( (pinnedThreads? "pin.png" : "forum_pin.png") );
171 out.print( "\"></td>\r\n <td><a href=\"" );
172 out.print( (Jtp.path(node)) );
173 out.print( "\">" );
174 out.print( (node.getSubjectHtml()) );
175 out.print( "</a></td>\r\n <td style=\"width:5em\"><a href=\"/catalog/SetPin.jtp?node=" );
176 out.print( (node.getId()) );
177 out.print( "&value=unpin\">Unpin</a></td>\r\n </tr>\r\n</table>\r\n" );
178
179 } else {
180
181 out.print( "\r\n<div style=\"margin-top:1em\">\r\n No " );
182 out.print( (pinnedThreads? "pinned topics" : "pinned " + Jtp.childName(forum, true).toLowerCase()) );
183 out.print( ".\r\n</div>\r\n" );
184
185 }
186
187 out.print( "\r\n</div>\r\n\r\n" );
188
189 if (!pinnedThreads) {
190 // Search for unpinned child forums
191 List<Node> childForums = forum.getChildApps().get(0, 1000);
192 List<Node> unpinned = new ArrayList<Node>();
193 for (Node child : childForums) {
194 if (!child.isPinned())
195 unpinned.add(child);
196 }
197
198 if (!unpinned.isEmpty()) {
199
200 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 " );
201
202 for (Node node : unpinned) {
203
204 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=\"" );
205 out.print( (Jtp.path(node)) );
206 out.print( "\">" );
207 out.print( (node.getSubjectHtml()) );
208 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=" );
209 out.print( (node.getId()) );
210 out.print( "&value=pin\">Pin</a></td>\r\n</tr>\r\n" );
211
212 }
213
214 out.print( "\r\n</table>\r\n</div>\r\n" );
215
216 }
217 }
218
219 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 " );
220 out.print( (Help.pinned_subapps.link()) );
221 out.print( "\r\n</div>\r\n\r\n" );
222 Shared.footer(request, response);
223 out.print( "\r\n" );
224 Shared.analytics(request,response);
225 out.print( "\r\n</body>\r\n</html>\r\n" );
226
227 }
228 }
229