X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FEditProfileFieldPage.java;h=e5bee788cc4f1ff034336bea90df0669c28c48da;hp=2b72a818579229b612e54daacc34bbe3d315e115;hb=7b55e0be6a3283e43a9bbab98f82aebdd948eb33;hpb=396fc01133dbcb6dfe48bec4e5263b6240407449 diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java index 2b72a81..e5bee78 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java @@ -1,5 +1,5 @@ /* - * Sone - EditProfileFieldPage.java - Copyright © 2011 David Roden + * Sone - EditProfileFieldPage.java - Copyright © 2011–2016 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,11 +18,12 @@ package net.pterodactylus.sone.web; 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.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.web.Method; /** * Page that lets the user edit the name of a profile field. @@ -51,21 +52,15 @@ public class EditProfileFieldPage extends SoneTemplatePage { * {@inheritDoc} */ @Override - protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException { - super.processTemplate(request, dataProvider); + protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { Sone currentSone = getCurrentSone(request.getToadletContext()); Profile profile = currentSone.getProfile(); /* get parameters from request. */ - int fieldIndex = Numbers.safeParseInteger(request.getHttpRequest().getParam("field"), -1); - if (fieldIndex >= currentSone.getProfile().getFieldNames().size()) { - fieldIndex = -1; - } - String fieldName = null; - String fieldValue = null; - if (fieldIndex > -1) { - fieldName = profile.getFieldNames().get(fieldIndex); - fieldValue = profile.getField(fieldName); + String fieldId = request.getHttpRequest().getParam("field"); + Field field = profile.getFieldById(fieldId); + if (field == null) { + throw new RedirectException("invalid.html"); } /* process the POST request. */ @@ -73,24 +68,23 @@ public class EditProfileFieldPage extends SoneTemplatePage { if (request.getHttpRequest().getPartAsStringFailsafe("cancel", 4).equals("true")) { throw new RedirectException("editProfile.html#profile-fields"); } - fieldIndex = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("field", 11), -1); - String name = request.getHttpRequest().getPartAsStringFailsafe("name", 256); - if (fieldIndex == -1) { + fieldId = request.getHttpRequest().getPartAsStringFailsafe("field", 36); + field = profile.getFieldById(fieldId); + if (field == null) { throw new RedirectException("invalid.html"); } - int existingFieldIndex = profile.getFieldNames().indexOf(name); - if ((existingFieldIndex == -1) || (existingFieldIndex == fieldIndex)) { - profile.setFieldName(fieldIndex, name); + String name = request.getHttpRequest().getPartAsStringFailsafe("name", 256); + Field existingField = profile.getFieldByName(name); + if ((existingField == null) || (existingField.equals(field))) { + field.setName(name); currentSone.setProfile(profile); throw new RedirectException("editProfile.html#profile-fields"); } - dataProvider.set("duplicateFieldName", true); + templateContext.set("duplicateFieldName", true); } /* store current values in template. */ - dataProvider.set("fieldIndex", fieldIndex); - dataProvider.set("fieldName", fieldName); - dataProvider.set("fieldValue", fieldValue); + templateContext.set("field", field); } }