1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.data.Profile.*
5 import net.pterodactylus.sone.main.*
6 import net.pterodactylus.sone.text.*
7 import net.pterodactylus.sone.utils.*
8 import net.pterodactylus.sone.web.*
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.template.*
14 * This page lets the user edit her profile.
16 @MenuName("EditProfile")
17 @TemplatePath("/templates/editProfile.html")
18 @ToadletPath("editProfile.html")
19 class EditProfilePage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
20 LoggedInPage("Page.EditProfile.Title", webInterface, loaders, templateRenderer) {
22 override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
23 currentSone.profile.let { profile ->
24 templateContext["firstName"] = profile.firstName
25 templateContext["middleName"] = profile.middleName
26 templateContext["lastName"] = profile.lastName
27 templateContext["birthDay"] = profile.birthDay
28 templateContext["birthMonth"] = profile.birthMonth
29 templateContext["birthYear"] = profile.birthYear
30 templateContext["avatarId"] = profile.avatar
31 templateContext["fields"] = profile.fields
32 if (soneRequest.isPOST) {
33 if (soneRequest.httpRequest.getPartAsStringFailsafe("save-profile", 4) == "true") {
34 profile.firstName = soneRequest.httpRequest.getPartAsStringFailsafe("first-name", 256).trim()
35 profile.middleName = soneRequest.httpRequest.getPartAsStringFailsafe("middle-name", 256).trim()
36 profile.lastName = soneRequest.httpRequest.getPartAsStringFailsafe("last-name", 256).trim()
37 profile.birthDay = soneRequest.httpRequest.getPartAsStringFailsafe("birth-day", 256).trim().toIntOrNull()
38 profile.birthMonth = soneRequest.httpRequest.getPartAsStringFailsafe("birth-month", 256).trim().toIntOrNull()
39 profile.birthYear = soneRequest.httpRequest.getPartAsStringFailsafe("birth-year", 256).trim().toIntOrNull()
40 profile.setAvatar(soneRequest.core.getImage(soneRequest.httpRequest.getPartAsStringFailsafe("avatarId", 256).trim(), false))
41 profile.fields.forEach { field ->
42 field.value = TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), soneRequest.httpRequest.getPartAsStringFailsafe("field-${field.id}", 400).trim())
44 currentSone.profile = profile
45 soneRequest.core.touchConfiguration()
46 redirectTo("editProfile.html")
47 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("add-field", 4) == "true") {
48 val fieldName = soneRequest.httpRequest.getPartAsStringFailsafe("field-name", 100)
50 profile.addField(fieldName)
51 currentSone.profile = profile
52 soneRequest.core.touchConfiguration()
53 redirectTo("editProfile.html#profile-fields")
54 } catch (e: DuplicateField) {
55 templateContext["fieldName"] = fieldName
56 templateContext["duplicateFieldName"] = true
58 } else profile.fields.forEach { field ->
59 if (soneRequest.httpRequest.getPartAsStringFailsafe("delete-field-${field.id}", 4) == "true") {
60 redirectTo("deleteProfileField.html?field=${field.id}")
61 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("edit-field-${field.id}", 4) == "true") {
62 redirectTo("editProfileField.html?field=${field.id}")
63 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("move-down-field-${field.id}", 4) == "true") {
64 profile.moveFieldDown(field)
65 currentSone.profile = profile
66 redirectTo("editProfile.html#profile-fields")
67 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("move-up-field-${field.id}", 4) == "true") {
68 profile.moveFieldUp(field)
69 currentSone.profile = profile
70 redirectTo("editProfile.html#profile-fields")