X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FEditProfilePage.java;h=43dd15b973fd91f78a943c351c7979b42747046e;hp=4f7e1f3b0ae0006f4b09a78e182e21ac5711d9b1;hb=89a905395984ec35e313b07594e21773ffd3d9d8;hpb=0ac0337b61614cf23833f866fd8f0568d0f7bf58 diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 4f7e1f3..43dd15b 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -17,12 +17,15 @@ package net.pterodactylus.sone.web; +import java.util.List; + import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.Page.Request.Method; import net.pterodactylus.util.number.Numbers; -import net.pterodactylus.util.template.DataProvider; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; import freenet.clients.http.ToadletContext; /** @@ -52,8 +55,8 @@ public class EditProfilePage extends SoneTemplatePage { * {@inheritDoc} */ @Override - protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException { - super.processTemplate(request, dataProvider); + protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { + super.processTemplate(request, templateContext); ToadletContext toadletContenxt = request.getToadletContext(); Sone currentSone = getCurrentSone(toadletContenxt); Profile profile = currentSone.getProfile(); @@ -63,6 +66,7 @@ public class EditProfilePage extends SoneTemplatePage { Integer birthDay = profile.getBirthDay(); Integer birthMonth = profile.getBirthMonth(); Integer birthYear = profile.getBirthYear(); + List fields = profile.getFields(); if (request.getMethod() == Method.POST) { if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) { firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim(); @@ -75,16 +79,86 @@ public class EditProfilePage extends SoneTemplatePage { profile.setMiddleName(middleName.length() > 0 ? middleName : null); profile.setLastName(lastName.length() > 0 ? lastName : null); profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear); + for (Field field : fields) { + String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400); + field.setValue(value); + } currentSone.setProfile(profile); - throw new RedirectException("index.html"); + webInterface.getCore().touchConfiguration(); + throw new RedirectException("editProfile.html"); + } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) { + String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim(); + try { + profile.addField(fieldName); + currentSone.setProfile(profile); + fields = profile.getFields(); + webInterface.getCore().touchConfiguration(); + throw new RedirectException("editProfile.html#profile-fields"); + } catch (IllegalArgumentException iae1) { + templateContext.set("fieldName", fieldName); + templateContext.set("duplicateFieldName", true); + } + } else { + String id = getFieldId(request, "delete-field-"); + if (id != null) { + throw new RedirectException("deleteProfileField.html?field=" + id); + } + id = getFieldId(request, "move-up-field-"); + if (id != null) { + Field field = profile.getFieldById(id); + if (field == null) { + throw new RedirectException("invalid.html"); + } + profile.moveFieldUp(field); + currentSone.setProfile(profile); + throw new RedirectException("editProfile.html#profile-fields"); + } + id = getFieldId(request, "move-down-field-"); + if (id != null) { + Field field = profile.getFieldById(id); + if (field == null) { + throw new RedirectException("invalid.html"); + } + profile.moveFieldDown(field); + currentSone.setProfile(profile); + throw new RedirectException("editProfile.html#profile-fields"); + } + id = getFieldId(request, "edit-field-"); + if (id != null) { + throw new RedirectException("editProfileField.html?field=" + id); + } } } - dataProvider.set("firstName", firstName); - dataProvider.set("middleName", middleName); - dataProvider.set("lastName", lastName); - dataProvider.set("birthDay", birthDay); - dataProvider.set("birthMonth", birthMonth); - dataProvider.set("birthYear", birthYear); + templateContext.set("firstName", firstName); + templateContext.set("middleName", middleName); + templateContext.set("lastName", lastName); + templateContext.set("birthDay", birthDay); + templateContext.set("birthMonth", birthMonth); + templateContext.set("birthYear", birthYear); + templateContext.set("fields", fields); } + // + // PRIVATE METHODS + // + + /** + * Searches for a part whose names starts with the given {@code String} and + * extracts the ID from the located name. + * + * @param request + * The request to get the parts from + * @param partNameStart + * The start of the name of the requested part + * @return The parsed ID, or {@code null} if there was no part matching the + * given string + */ + private String getFieldId(Request request, String partNameStart) { + for (String partName : request.getHttpRequest().getParts()) { + if (partName.startsWith(partNameStart)) { + return partName.substring(partNameStart.length()); + } + } + return null; + } }