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