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 class EditProfilePage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
19 LoggedInPage("editProfile.html", "Page.EditProfile.Title", webInterface, loaders, templateRenderer) {
21 override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
22 currentSone.profile.let { profile ->
23 templateContext["firstName"] = profile.firstName
24 templateContext["middleName"] = profile.middleName
25 templateContext["lastName"] = profile.lastName
26 templateContext["birthDay"] = profile.birthDay
27 templateContext["birthMonth"] = profile.birthMonth
28 templateContext["birthYear"] = profile.birthYear
29 templateContext["avatarId"] = profile.avatar
30 templateContext["fields"] = profile.fields
31 if (soneRequest.isPOST) {
32 if (soneRequest.httpRequest.getPartAsStringFailsafe("save-profile", 4) == "true") {
33 profile.firstName = soneRequest.httpRequest.getPartAsStringFailsafe("first-name", 256).trim()
34 profile.middleName = soneRequest.httpRequest.getPartAsStringFailsafe("middle-name", 256).trim()
35 profile.lastName = soneRequest.httpRequest.getPartAsStringFailsafe("last-name", 256).trim()
36 profile.birthDay = soneRequest.httpRequest.getPartAsStringFailsafe("birth-day", 256).trim().toIntOrNull()
37 profile.birthMonth = soneRequest.httpRequest.getPartAsStringFailsafe("birth-month", 256).trim().toIntOrNull()
38 profile.birthYear = soneRequest.httpRequest.getPartAsStringFailsafe("birth-year", 256).trim().toIntOrNull()
39 profile.setAvatar(soneRequest.core.getImage(soneRequest.httpRequest.getPartAsStringFailsafe("avatarId", 256).trim(), false))
40 profile.fields.forEach { field ->
41 field.value = TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), soneRequest.httpRequest.getPartAsStringFailsafe("field-${field.id}", 400).trim())
43 currentSone.profile = profile
44 soneRequest.core.touchConfiguration()
45 throw RedirectException("editProfile.html")
46 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("add-field", 4) == "true") {
47 val fieldName = soneRequest.httpRequest.getPartAsStringFailsafe("field-name", 100)
49 profile.addField(fieldName)
50 currentSone.profile = profile
51 soneRequest.core.touchConfiguration()
52 throw RedirectException("editProfile.html#profile-fields")
53 } catch (e: DuplicateField) {
54 templateContext["fieldName"] = fieldName
55 templateContext["duplicateFieldName"] = true
57 } else profile.fields.forEach { field ->
58 if (soneRequest.httpRequest.getPartAsStringFailsafe("delete-field-${field.id}", 4) == "true") {
59 throw RedirectException("deleteProfileField.html?field=${field.id}")
60 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("edit-field-${field.id}", 4) == "true") {
61 throw RedirectException("editProfileField.html?field=${field.id}")
62 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("move-down-field-${field.id}", 4) == "true") {
63 profile.moveFieldDown(field)
64 currentSone.profile = profile
65 throw RedirectException("editProfile.html#profile-fields")
66 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("move-up-field-${field.id}", 4) == "true") {
67 profile.moveFieldUp(field)
68 currentSone.profile = profile
69 throw RedirectException("editProfile.html#profile-fields")