Add page that always requires a logged-in user
[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.FreenetRequest
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11
12 /**
13  * This page lets the user edit her profile.
14  */
15 class EditProfilePage(template: Template, webInterface: WebInterface) :
16                 LoggedInPage("editProfile.html", template, "Page.EditProfile.Title", webInterface) {
17
18         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 currentSone.profile.let { profile ->
20                         templateContext["firstName"] = profile.firstName
21                         templateContext["middleName"] = profile.middleName
22                         templateContext["lastName"] = profile.lastName
23                         templateContext["birthDay"] = profile.birthDay
24                         templateContext["birthMonth"] = profile.birthMonth
25                         templateContext["birthYear"] = profile.birthYear
26                         templateContext["avatarId"] = profile.avatar
27                         templateContext["fields"] = profile.fields
28                         if (freenetRequest.isPOST) {
29                                 if (freenetRequest.httpRequest.getPartAsStringFailsafe("save-profile", 4) == "true") {
30                                         profile.firstName = freenetRequest.httpRequest.getPartAsStringFailsafe("first-name", 256).trim()
31                                         profile.middleName = freenetRequest.httpRequest.getPartAsStringFailsafe("middle-name", 256).trim()
32                                         profile.lastName = freenetRequest.httpRequest.getPartAsStringFailsafe("last-name", 256).trim()
33                                         profile.birthDay = freenetRequest.httpRequest.getPartAsStringFailsafe("birth-day", 256).trim().toIntOrNull()
34                                         profile.birthMonth = freenetRequest.httpRequest.getPartAsStringFailsafe("birth-month", 256).trim().toIntOrNull()
35                                         profile.birthYear = freenetRequest.httpRequest.getPartAsStringFailsafe("birth-year", 256).trim().toIntOrNull()
36                                         profile.setAvatar(webInterface.core.getImage(freenetRequest.httpRequest.getPartAsStringFailsafe("avatarId", 256).trim(), false))
37                                         profile.fields.forEach { field ->
38                                                 field.value = TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), freenetRequest.httpRequest.getPartAsStringFailsafe("field-${field.id}", 400).trim())
39                                         }
40                                         webInterface.core.touchConfiguration()
41                                         throw RedirectException("editProfile.html")
42                                 } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("add-field", 4) == "true") {
43                                         val fieldName = freenetRequest.httpRequest.getPartAsStringFailsafe("field-name", 100)
44                                         try {
45                                                 profile.addField(fieldName)
46                                                 currentSone.profile = profile
47                                                 webInterface.core.touchConfiguration()
48                                                 throw RedirectException("editProfile.html#profile-fields")
49                                         } catch (e: DuplicateField) {
50                                                 templateContext["fieldName"] = fieldName
51                                                 templateContext["duplicateFieldName"] = true
52                                         }
53                                 } else profile.fields.forEach { field ->
54                                         if (freenetRequest.httpRequest.getPartAsStringFailsafe("delete-field-${field.id}", 4) == "true") {
55                                                 throw RedirectException("deleteProfileField.html?field=${field.id}")
56                                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("edit-field-${field.id}", 4) == "true") {
57                                                 throw RedirectException("editProfileField.html?field=${field.id}")
58                                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("move-down-field-${field.id}", 4) == "true") {
59                                                 profile.moveFieldDown(field)
60                                                 currentSone.profile = profile
61                                                 throw RedirectException("editProfile.html#profile-fields")
62                                         } else if (freenetRequest.httpRequest.getPartAsStringFailsafe("move-up-field-${field.id}", 4) == "true") {
63                                                 profile.moveFieldUp(field)
64                                                 currentSone.profile = profile
65                                                 throw RedirectException("editProfile.html#profile-fields")
66                                         }
67                                 }
68                         }
69                 }
70         }
71
72 }