2017-05-17 07:58:40 +02:00
package router
2017-05-13 00:28:56 +02:00
import (
2017-05-13 02:01:17 +02:00
"encoding/json"
2017-05-13 00:28:56 +02:00
"net/http"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/service/user"
2017-05-27 19:08:47 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
2017-05-21 02:12:29 +02:00
"github.com/NyaaPantsu/nyaa/util/timeHelper"
2017-05-13 00:28:56 +02:00
)
2017-05-25 21:54:58 +02:00
// LanguagesJSONResponse : Structure containing all the languages to parse it as a JSON response
2017-05-13 02:01:17 +02:00
type LanguagesJSONResponse struct {
2017-05-17 07:58:40 +02:00
Current string ` json:"current" `
2017-05-13 02:01:17 +02:00
Languages map [ string ] string ` json:"languages" `
}
2017-05-27 19:08:47 +02:00
// SeePublicSettingsHandler : Controller to view the languages and themes
func SeePublicSettingsHandler ( w http . ResponseWriter , r * http . Request ) {
_ , Tlang := publicSettings . GetTfuncAndLanguageFromRequest ( r )
availableLanguages := publicSettings . GetAvailableLanguages ( )
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-19 04:55:59 +02:00
contentType := r . Header . Get ( "Content-Type" )
if contentType == "application/json" {
2017-05-13 02:01:17 +02:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
err := json . NewEncoder ( w ) . Encode ( LanguagesJSONResponse { Tlang . Tag , availableLanguages } )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
} else {
2017-05-25 21:54:58 +02:00
clv := changeLanguageVariables {
commonTemplateVariables : newCommonVariables ( r ) ,
2017-05-24 09:11:13 +02:00
Language : Tlang . Tag ,
Languages : availableLanguages ,
2017-05-19 04:55:59 +02:00
}
2017-05-27 19:08:47 +02:00
err := changePublicSettingsTemplate . ExecuteTemplate ( w , "index.html" , clv )
2017-05-13 02:01:17 +02:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2017-05-13 00:28:56 +02:00
}
}
2017-05-27 19:08:47 +02:00
// ChangePublicSettingsHandler : Controller for changing the current language and theme
func ChangePublicSettingsHandler ( w http . ResponseWriter , r * http . Request ) {
theme := r . FormValue ( "theme" )
2017-05-13 00:28:56 +02:00
lang := r . FormValue ( "language" )
2017-05-27 19:08:47 +02:00
availableLanguages := publicSettings . GetAvailableLanguages ( )
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-05-13 00:28:56 +02:00
if _ , exists := availableLanguages [ lang ] ; ! exists {
http . Error ( w , "Language not available" , http . StatusInternalServerError )
return
}
2017-05-27 19:08:47 +02:00
// If logged in, update user language.
2017-05-21 02:12:29 +02:00
user , _ := userService . CurrentUser ( r )
2017-05-24 09:11:13 +02:00
if user . ID > 0 {
2017-05-13 00:28:56 +02:00
user . Language = lang
2017-05-27 19:08:47 +02:00
user . Theme = theme
2017-05-13 00:28:56 +02:00
// I don't know if I should use this...
userService . UpdateUserCore ( & user )
}
2017-05-27 19:08:47 +02:00
// Set cookie
2017-05-21 02:12:29 +02:00
http . SetCookie ( w , & http . Cookie { Name : "lang" , Value : lang , Expires : timeHelper . FewDaysLater ( 365 ) } )
2017-05-27 19:08:47 +02:00
http . SetCookie ( w , & http . Cookie { Name : "theme" , Value : theme , Expires : timeHelper . FewDaysLater ( 365 ) } )
2017-05-13 00:28:56 +02:00
url , _ := Router . Get ( "home" ) . URL ( )
http . Redirect ( w , r , url . String ( ) , http . StatusSeeOther )
}