eidolon/Handler/AdminAlbumSettings.hs

158 lines
5.5 KiB
Haskell
Raw Normal View History

2014-09-07 04:56:34 +00:00
module Handler.AdminAlbumSettings where
import Import
2014-09-07 23:46:47 +00:00
import qualified Data.Text as T
import System.FilePath
import System.Directory
2014-09-07 04:56:34 +00:00
getAdminAlbumsR :: Handler Html
getAdminAlbumsR = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
albums <- runDB $ selectList [] [Desc AlbumTitle]
defaultLayout $ do
$(widgetFile "adminAlbums")
False -> do
setMessage "You are no admin"
redirect $ HomeR
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
2014-09-09 02:25:47 +00:00
getAdminAlbumMediaR :: AlbumId -> Handler Html
getAdminAlbumMediaR albumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
media <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle]
defaultLayout $ do
$(widgetFile "adminAlbumMedia")
Nothing -> do
setMessage "This album does not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
2014-09-07 04:56:34 +00:00
getAdminAlbumSettingsR :: AlbumId -> Handler Html
2014-09-07 23:46:47 +00:00
getAdminAlbumSettingsR albumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
2014-09-07 23:46:47 +00:00
(adminAlbumSettingsWidget, enctype) <- generateFormPost $ adminAlbumSettingsForm album albumId
defaultLayout $ do
$(widgetFile "adminAlbumSet")
Nothing -> do
setMessage "This album does not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
2014-09-07 23:46:47 +00:00
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
2014-09-07 04:56:34 +00:00
postAdminAlbumSettingsR :: AlbumId -> Handler Html
2014-09-07 23:46:47 +00:00
postAdminAlbumSettingsR albumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
2014-09-07 23:46:47 +00:00
((res, adminAlbumSettingsWidget), enctype) <- runFormPost $ adminAlbumSettingsForm album albumId
case res of
FormSuccess temp -> do
aId <- runDB $ update albumId
[ AlbumTitle =. albumTitle temp
, AlbumSamplePic =. albumSamplePic temp
]
setMessage "Album settings changed successfully"
redirect $ AdminR
_ -> do
setMessage "There was an error while changing the settings"
redirect $ AdminAlbumSettingsR albumId
Nothing -> do
setMessage "This album does not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
2014-09-07 23:46:47 +00:00
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
2014-09-07 23:46:47 +00:00
adminAlbumSettingsForm :: Album -> AlbumId -> Form Album
adminAlbumSettingsForm album albumId = renderDivs $ Album
<$> areq textField "Title" (Just $ albumTitle album)
<*> pure (albumOwner album)
<*> pure (albumContent album)
<*> aopt (selectField media) "Sample picture" (Just $ albumSamplePic album)
where
media = do
entities <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle]
optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumPath (entityVal med))) entities
2014-09-07 04:56:34 +00:00
getAdminAlbumDeleteR :: AlbumId -> Handler Html
2014-09-07 23:46:47 +00:00
getAdminAlbumDeleteR albumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
2014-09-07 23:46:47 +00:00
-- remove reference from owner
ownerId <- return $ albumOwner album
owner <- runDB $ getJust ownerId
albumList <- return $ userAlbums owner
newAlbumList <- return $ removeItem albumId albumList
2014-09-09 02:26:12 +00:00
runDB $ update ownerId [UserAlbums =. newAlbumList]
2014-09-07 23:46:47 +00:00
-- delete album content
mapM (\a -> runDB $ delete a) (albumContent album)
-- delete album
runDB $ delete albumId
-- delete files
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> (T.unpack $ extractKey ownerId) </> (T.unpack $ extractKey albumId)
-- outro
setMessage "Album deleted successfully"
redirect $ AdminR
Nothing -> do
setMessage "This album dies not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
2014-09-07 23:46:47 +00:00
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR