♻️ Add freenet client interface
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / EditProfileFieldPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.*
9 import javax.inject.*
10
11 /**
12  * Page that lets the user edit the name of a profile field.
13  */
14 @TemplatePath("/templates/editProfileField.html")
15 @ToadletPath("editProfileField.html")
16 class EditProfileFieldPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
17                 LoggedInPage("Page.EditProfileField.Title", webInterface, loaders, templateRenderer) {
18
19         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 currentSone.profile.let { profile ->
21                         if (soneRequest.isPOST) {
22                                 if (soneRequest.httpRequest.getPartAsStringFailsafe("cancel", 4) == "true") {
23                                         redirectTo("editProfile.html#profile-fields")
24                                 }
25                                 val field = profile.getFieldById(soneRequest.httpRequest.getPartAsStringFailsafe("field", 36)) ?: redirectTo("invalid.html")
26                                 soneRequest.httpRequest.getPartAsStringFailsafe("name", 256).let { name ->
27                                         try {
28                                                 if (name != field.name) {
29                                                         field.name = name
30                                                         currentSone.profile = profile
31                                                 }
32                                                 redirectTo("editProfile.html#profile-fields")
33                                         } catch (e: IllegalArgumentException) {
34                                                 templateContext["duplicateFieldName"] = true
35                                                 return
36                                         }
37                                 }
38                         }
39                         templateContext["field"] = profile.getFieldById(soneRequest.httpRequest.getParam("field")) ?: redirectTo("invalid.html")
40                 }
41         }
42
43 }