f4baadc0415317be526dbfdf142ee836152258e7
[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.*
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.*
11 import javax.inject.*
12
13 /**
14  * This page lets the user edit her profile.
15  */
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("editProfile.html", "Page.EditProfile.Title", webInterface, loaders, templateRenderer) {
21
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())
43                                         }
44                                         currentSone.profile = profile
45                                         soneRequest.core.touchConfiguration()
46                                         throw RedirectException("editProfile.html")
47                                 } else if (soneRequest.httpRequest.getPartAsStringFailsafe("add-field", 4) == "true") {
48                                         val fieldName = soneRequest.httpRequest.getPartAsStringFailsafe("field-name", 100)
49                                         try {
50                                                 profile.addField(fieldName)
51                                                 currentSone.profile = profile
52                                                 soneRequest.core.touchConfiguration()
53                                                 throw RedirectException("editProfile.html#profile-fields")
54                                         } catch (e: DuplicateField) {
55                                                 templateContext["fieldName"] = fieldName
56                                                 templateContext["duplicateFieldName"] = true
57                                         }
58                                 } else profile.fields.forEach { field ->
59                                         if (soneRequest.httpRequest.getPartAsStringFailsafe("delete-field-${field.id}", 4) == "true") {
60                                                 throw RedirectException("deleteProfileField.html?field=${field.id}")
61                                         } else if (soneRequest.httpRequest.getPartAsStringFailsafe("edit-field-${field.id}", 4) == "true") {
62                                                 throw RedirectException("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                                                 throw RedirectException("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                                                 throw RedirectException("editProfile.html#profile-fields")
71                                         }
72                                 }
73                         }
74                 }
75         }
76
77 }