X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FEditProfilePage.java;h=fc1a0bbf620040d5bb664f0d81c890c12504456f;hb=1f7dda6c7d0b81059a5b8f70ebf6d25d90231bae;hp=4f7e1f3b0ae0006f4b09a78e182e21ac5711d9b1;hpb=bc29afc8fdd96b8c80c767148a917034c124d93e;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 4f7e1f3..fc1a0bb 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import java.util.Map; + import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.Page.Request.Method; @@ -63,6 +65,7 @@ public class EditProfilePage extends SoneTemplatePage { Integer birthDay = profile.getBirthDay(); Integer birthMonth = profile.getBirthMonth(); Integer birthYear = profile.getBirthYear(); + Map 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,8 +78,45 @@ 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 (int fieldIndex = 0; fieldIndex < profile.getFieldNames().size(); ++fieldIndex) { + String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + fieldIndex, 400); + profile.setField(fieldIndex, value); + } currentSone.setProfile(profile); + webInterface.getCore().saveSone(currentSone); throw new RedirectException("index.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().saveSone(currentSone); + } catch (IllegalArgumentException iae1) { + dataProvider.set("fieldName", fieldName); + dataProvider.set("duplicateFieldName", true); + } + } else { + int deleteFieldIndex = getFieldIndex(request, "delete-field-"); + if (deleteFieldIndex > -1) { + throw new RedirectException("deleteProfileField.html?field=" + deleteFieldIndex); + } + int moveUpFieldIndex = getFieldIndex(request, "move-up-field-"); + if (moveUpFieldIndex > -1) { + profile.moveFieldUp(moveUpFieldIndex); + currentSone.setProfile(profile); + throw new RedirectException("editProfile.html#profile-fields"); + } + int moveDownFieldIndex = getFieldIndex(request, "move-down-field-"); + if (moveDownFieldIndex > -1) { + profile.moveFieldDown(moveDownFieldIndex); + currentSone.setProfile(profile); + throw new RedirectException("editProfile.html#profile-fields"); + } + int editFieldIndex = getFieldIndex(request, "edit-field-"); + if (editFieldIndex > -1) { + throw new RedirectException("editProfileField.html?field=" + editFieldIndex); + } } } dataProvider.set("firstName", firstName); @@ -85,6 +125,30 @@ public class EditProfilePage extends SoneTemplatePage { dataProvider.set("birthDay", birthDay); dataProvider.set("birthMonth", birthMonth); dataProvider.set("birthYear", birthYear); + dataProvider.set("fields", fields); } + // + // PRIVATE METHODS + // + + /** + * Searches for a part whose names starts with the given {@code String} and + * extracts the number 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 number, or {@code -1} if the number could not be + * parsed + */ + private int getFieldIndex(Request request, String partNameStart) { + for (String partName : request.getHttpRequest().getParts()) { + if (partName.startsWith(partNameStart)) { + return Numbers.safeParseInteger(partName.substring(partNameStart.length()), -1); + } + } + return -1; + } }