2 * Sone - EditProfilePage.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import static net.pterodactylus.sone.text.TextFilter.filter;
22 import java.util.List;
24 import net.pterodactylus.sone.data.Profile;
25 import net.pterodactylus.sone.data.Profile.Field;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.text.TextFilter;
28 import net.pterodactylus.sone.web.page.FreenetRequest;
29 import net.pterodactylus.util.number.Numbers;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
32 import net.pterodactylus.util.web.Method;
33 import freenet.clients.http.ToadletContext;
36 * This page lets the user edit her profile.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class EditProfilePage extends SoneTemplatePage {
43 * Creates a new “edit profile” page.
46 * The template to render
48 * The Sone web interface
50 public EditProfilePage(Template template, WebInterface webInterface) {
51 super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true);
55 // TEMPLATEPAGE METHODS
62 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
63 super.processTemplate(request, templateContext);
64 ToadletContext toadletContenxt = request.getToadletContext();
65 Sone currentSone = getCurrentSone(toadletContenxt);
66 Profile profile = currentSone.getProfile();
67 String firstName = profile.getFirstName();
68 String middleName = profile.getMiddleName();
69 String lastName = profile.getLastName();
70 Integer birthDay = profile.getBirthDay();
71 Integer birthMonth = profile.getBirthMonth();
72 Integer birthYear = profile.getBirthYear();
73 String avatarId = profile.getAvatar();
74 List<Field> fields = profile.getFields();
75 if (request.getMethod() == Method.POST) {
76 if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) {
77 firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim();
78 middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim();
79 lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim();
80 birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim());
81 birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
82 birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
83 avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
84 profile.setFirstName(firstName.length() > 0 ? firstName : null);
85 profile.setMiddleName(middleName.length() > 0 ? middleName : null);
86 profile.setLastName(lastName.length() > 0 ? lastName : null);
87 profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
88 profile.setAvatar(webInterface.getCore().getImage(avatarId, false));
89 for (Field field : fields) {
90 String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
91 String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value);
92 field.setValue(filteredValue);
94 currentSone.setProfile(profile);
95 webInterface.getCore().touchConfiguration();
96 throw new RedirectException("editProfile.html");
97 } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) {
98 String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim();
100 profile.addField(fieldName);
101 currentSone.setProfile(profile);
102 fields = profile.getFields();
103 webInterface.getCore().touchConfiguration();
104 throw new RedirectException("editProfile.html#profile-fields");
105 } catch (IllegalArgumentException iae1) {
106 templateContext.set("fieldName", fieldName);
107 templateContext.set("duplicateFieldName", true);
110 String id = getFieldId(request, "delete-field-");
112 throw new RedirectException("deleteProfileField.html?field=" + id);
114 id = getFieldId(request, "move-up-field-");
116 Field field = profile.getFieldById(id);
118 throw new RedirectException("invalid.html");
120 profile.moveFieldUp(field);
121 currentSone.setProfile(profile);
122 throw new RedirectException("editProfile.html#profile-fields");
124 id = getFieldId(request, "move-down-field-");
126 Field field = profile.getFieldById(id);
128 throw new RedirectException("invalid.html");
130 profile.moveFieldDown(field);
131 currentSone.setProfile(profile);
132 throw new RedirectException("editProfile.html#profile-fields");
134 id = getFieldId(request, "edit-field-");
136 throw new RedirectException("editProfileField.html?field=" + id);
140 templateContext.set("firstName", firstName);
141 templateContext.set("middleName", middleName);
142 templateContext.set("lastName", lastName);
143 templateContext.set("birthDay", birthDay);
144 templateContext.set("birthMonth", birthMonth);
145 templateContext.set("birthYear", birthYear);
146 templateContext.set("avatarId", avatarId);
147 templateContext.set("fields", fields);
155 * Searches for a part whose names starts with the given {@code String} and
156 * extracts the ID from the located name.
159 * The request to get the parts from
160 * @param partNameStart
161 * The start of the name of the requested part
162 * @return The parsed ID, or {@code null} if there was no part matching the
165 private static String getFieldId(FreenetRequest request, String partNameStart) {
166 for (String partName : request.getHttpRequest().getParts()) {
167 if (partName.startsWith(partNameStart)) {
168 return partName.substring(partNameStart.length());