2015-08-09 19:16:33 +00:00
|
|
|
-- yammat - Yet Another MateMAT
|
|
|
|
-- Copyright (C) 2015 Amedeo Molnár
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published
|
|
|
|
-- by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-04-04 04:46:33 +00:00
|
|
|
module Handler.NewUser where
|
|
|
|
|
2015-04-07 20:03:21 +00:00
|
|
|
import Import as I
|
2015-04-09 21:03:49 +00:00
|
|
|
import Handler.Common
|
2015-04-04 04:46:33 +00:00
|
|
|
import Text.Read
|
2015-04-09 21:03:49 +00:00
|
|
|
import Text.Shakespeare.Text
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
getNewUserR :: Handler Html
|
|
|
|
getNewUserR = do
|
|
|
|
time <- liftIO getCurrentTime
|
2015-09-14 22:49:13 +00:00
|
|
|
let secs = read $ formatTime defaultTimeLocale "%s" time
|
2015-04-04 04:46:33 +00:00
|
|
|
(newUserWidget, enctype) <- generateFormPost $ newUserForm secs
|
2015-09-14 22:49:13 +00:00
|
|
|
defaultLayout $
|
2015-04-04 04:46:33 +00:00
|
|
|
$(widgetFile "newUser")
|
|
|
|
|
|
|
|
postNewUserR :: Handler Html
|
|
|
|
postNewUserR = do
|
|
|
|
time <- liftIO getCurrentTime
|
2015-09-14 22:49:13 +00:00
|
|
|
let secs = read $ formatTime defaultTimeLocale "%s" time
|
2015-04-04 04:46:33 +00:00
|
|
|
((res, _), _) <- runFormPost $ newUserForm secs
|
|
|
|
case res of
|
|
|
|
FormSuccess user -> do
|
2015-09-14 22:49:13 +00:00
|
|
|
runDB $ insert_ user
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgUserCreated
|
2015-09-14 22:49:13 +00:00
|
|
|
redirect HomeR
|
2015-04-04 04:46:33 +00:00
|
|
|
_ -> do
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgUserNotCreated
|
2015-09-14 22:49:13 +00:00
|
|
|
redirect NewUserR
|
2015-04-04 04:46:33 +00:00
|
|
|
|
|
|
|
newUserForm :: Int -> Form User
|
|
|
|
newUserForm secs = renderDivs $ User
|
2015-04-09 22:40:58 +00:00
|
|
|
<$> areq textField (fieldSettingsLabel MsgName) Nothing
|
2015-04-04 04:46:33 +00:00
|
|
|
<*> pure 0
|
|
|
|
<*> pure secs
|
2015-04-10 16:34:58 +00:00
|
|
|
<*> aopt emailField (fieldSettingsLabel MsgEmailNotify) Nothing
|
2015-04-16 00:12:03 +00:00
|
|
|
<*> aopt (selectField avatars) (fieldSettingsLabel MsgSelectAvatar) Nothing
|
|
|
|
where
|
|
|
|
avatars = do
|
|
|
|
ents <- runDB $ selectList [] [Asc AvatarIdent]
|
|
|
|
optionsPairs $ map (\ent -> ((avatarIdent $ entityVal ent), entityKey ent)) ents
|
2015-04-07 20:03:21 +00:00
|
|
|
|
|
|
|
data UserConf = UserConf
|
|
|
|
{ userConfEmail :: Maybe Text
|
2015-04-16 00:51:08 +00:00
|
|
|
, userConfAvatar :: Maybe AvatarId
|
2015-07-21 07:14:38 +00:00
|
|
|
, userConfBarcode :: Maybe [Text]
|
2015-04-07 20:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getModifyUserR :: UserId -> Handler Html
|
|
|
|
getModifyUserR uId = do
|
|
|
|
mUser <- runDB $ I.get uId
|
|
|
|
case mUser of
|
|
|
|
Just user -> do
|
2015-07-21 07:14:38 +00:00
|
|
|
p <- lookupGetParam "barcode"
|
|
|
|
_ <- handleGetParam p (Left uId)
|
|
|
|
rawbs <- runDB $ selectList [BarcodeUser ==. Just uId] []
|
2015-09-14 22:49:13 +00:00
|
|
|
let bs = map (barcodeCode . entityVal) rawbs
|
2015-07-21 07:14:38 +00:00
|
|
|
(modifyUserWidget, enctype) <- generateFormPost $ modifyUserForm user bs
|
2015-09-14 22:49:13 +00:00
|
|
|
defaultLayout $
|
2015-07-21 07:14:38 +00:00
|
|
|
$(widgetFile "modifyUser")
|
2015-04-07 20:03:21 +00:00
|
|
|
Nothing -> do
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgUserUnknown
|
2015-09-14 22:49:13 +00:00
|
|
|
redirect HomeR
|
2015-04-07 20:03:21 +00:00
|
|
|
|
|
|
|
postModifyUserR :: UserId -> Handler Html
|
|
|
|
postModifyUserR uId = do
|
|
|
|
mUser <- runDB $ I.get uId
|
|
|
|
case mUser of
|
|
|
|
Just user -> do
|
2015-07-21 07:14:38 +00:00
|
|
|
rawbs <- runDB $ selectList [BarcodeUser ==. Just uId] []
|
2015-09-14 22:49:13 +00:00
|
|
|
let bs = map (barcodeCode . entityVal) rawbs
|
2015-07-21 07:14:38 +00:00
|
|
|
((res, _), _) <- runFormPost $ modifyUserForm user bs
|
2015-04-07 20:03:21 +00:00
|
|
|
case res of
|
2015-04-16 00:51:08 +00:00
|
|
|
FormSuccess uc -> do
|
2015-04-07 20:03:21 +00:00
|
|
|
runDB $ update uId
|
2015-04-16 00:51:08 +00:00
|
|
|
[ UserEmail =. userConfEmail uc
|
|
|
|
, UserAvatar =. userConfAvatar uc
|
2015-04-07 20:03:21 +00:00
|
|
|
]
|
2015-04-16 00:51:08 +00:00
|
|
|
liftIO $ notify user (userConfEmail uc)
|
2015-07-21 07:14:38 +00:00
|
|
|
handleBarcodes (Left uId) (fromMaybe [] $ userConfBarcode uc)
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgUserEdited
|
2015-04-07 20:03:21 +00:00
|
|
|
redirect $ SelectR uId
|
|
|
|
_ -> do
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgUserNotEdited
|
2015-04-07 20:03:21 +00:00
|
|
|
redirect $ SelectR uId
|
|
|
|
Nothing -> do
|
2015-04-09 22:40:58 +00:00
|
|
|
setMessageI MsgUserUnknown
|
2015-09-14 22:49:13 +00:00
|
|
|
redirect HomeR
|
2015-04-07 20:03:21 +00:00
|
|
|
|
2015-07-21 07:14:38 +00:00
|
|
|
modifyUserForm :: User -> [Text] -> Form UserConf
|
|
|
|
modifyUserForm user bs = renderDivs $ UserConf
|
2015-04-16 00:51:08 +00:00
|
|
|
<$> aopt emailField (fieldSettingsLabel MsgEmailNotify) (Just $ userEmail user)
|
|
|
|
<*> aopt (selectField avatars) (fieldSettingsLabel MsgSelectAvatar) (Just $ userAvatar user)
|
2015-07-21 07:14:38 +00:00
|
|
|
<*> aopt barcodeField (fieldSettingsLabel MsgBarcodeField) (Just $ Just bs)
|
2015-04-16 00:51:08 +00:00
|
|
|
where
|
|
|
|
avatars = do
|
|
|
|
ents <- runDB $ selectList [] [Asc AvatarIdent]
|
|
|
|
optionsPairs $ map (\ent -> ((avatarIdent $ entityVal ent), entityKey ent)) ents
|
2015-04-09 21:03:49 +00:00
|
|
|
|
2015-04-10 15:00:19 +00:00
|
|
|
notify :: User -> Maybe Text -> IO ()
|
|
|
|
notify user email
|
|
|
|
| (userEmail user) == email = return ()
|
2015-04-09 21:18:47 +00:00
|
|
|
| otherwise = case userEmail user of
|
2015-04-10 15:00:19 +00:00
|
|
|
Just address -> sendMail address "Profiländerung"
|
2015-04-09 21:18:47 +00:00
|
|
|
[stext|
|
2015-04-09 21:03:49 +00:00
|
|
|
Hallo #{userIdent user},
|
|
|
|
|
|
|
|
deine Profileinstellungen wurden geändert.
|
|
|
|
Nur damit du Bescheid weißt.
|
|
|
|
|
|
|
|
Grüße,
|
|
|
|
|
|
|
|
der Matemat
|
2015-04-09 21:18:47 +00:00
|
|
|
|]
|
|
|
|
Nothing -> return ()
|