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