eidolon/Handler/AdminMediumSettings.hs

139 lines
4.9 KiB
Haskell
Raw Normal View History

2014-09-09 02:25:47 +00:00
module Handler.AdminMediumSettings where
import Import
import System.FilePath
import System.Directory
import Data.List (tail)
getAdminMediaR :: Handler Html
getAdminMediaR = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
2014-09-21 16:42:07 +00:00
media <- runDB $ selectList [] [Asc MediumTitle]
2014-09-09 02:25:47 +00:00
defaultLayout $ do
2014-09-24 21:03:18 +00:00
setTitle "Administration: Media"
2014-09-09 02:25:47 +00:00
$(widgetFile "adminMedia")
False -> do
setMessage "You are no admin"
redirect $ HomeR
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
getAdminMediumSettingsR :: MediumId -> Handler Html
getAdminMediumSettingsR mediumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
(adminMediumSetWidget, enctype) <- generateFormPost $ adminMediumSetForm medium
defaultLayout $ do
2014-09-24 21:03:18 +00:00
setTitle "Administration: Medium Settings"
2014-09-09 02:25:47 +00:00
$(widgetFile "adminMediumSet")
Nothing -> do
setMessage "This medium does not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
postAdminMediumSettingsR :: MediumId -> Handler Html
postAdminMediumSettingsR mediumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
((res, adminMediumSetWidget), enctype) <- runFormPost $ adminMediumSetForm medium
case res of
FormSuccess temp -> do
runDB $ update mediumId
[ MediumTitle =. mediumTitle temp
, MediumDescription =. mediumDescription temp
, MediumTags =. mediumTags temp
]
setMessage "Medium settings changed successfully"
redirect $ AdminR
_ -> do
setMessage "There was an error while changing the settings"
redirect $ AdminMediumSettingsR mediumId
Nothing -> do
setMessage "This medium does not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR
adminMediumSetForm :: Medium -> Form Medium
adminMediumSetForm medium = renderDivs $ Medium
<$> areq textField "Title" (Just $ mediumTitle medium)
<*> pure (mediumPath medium)
2014-10-01 22:58:36 +00:00
<*> pure (mediumThumb medium)
2014-12-14 19:21:23 +00:00
<*> pure (mediumMime medium)
2014-09-09 02:25:47 +00:00
<*> pure (mediumTime medium)
<*> pure (mediumOwner medium)
<*> areq textareaField "Description" (Just $ mediumDescription medium)
<*> areq tagField "Tags" (Just $ mediumTags medium)
<*> pure (mediumAlbum medium)
getAdminMediumDeleteR :: MediumId -> Handler Html
getAdminMediumDeleteR mediumId = do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
userId <- return $ getUserIdFromText tempUserId
user <- runDB $ getJust userId
case userAdmin user of
True -> do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
-- remove reference from album
albumId <- return $ mediumAlbum medium
album <- runDB $ getJust albumId
mediaList <- return $ albumContent album
newMediaList <- return $ removeItem mediumId mediaList
runDB $ update albumId [AlbumContent =. newMediaList]
-- delete comments
commEnts <- runDB $ selectList [CommentOrigin ==. mediumId] []
mapM (\ent -> runDB $ delete $ entityKey ent) commEnts
2014-09-09 02:25:47 +00:00
-- delete medium
runDB $ delete mediumId
-- delete files
liftIO $ removeFile (normalise $ tail $ mediumPath medium)
2014-09-09 02:25:47 +00:00
liftIO $ removeFile (normalise $ tail $ mediumPath medium)
-- outro
setMessage "Medium deleted successfully"
redirect $ AdminR
Nothing -> do
setMessage "This medium does not exist"
redirect $ AdminR
False -> do
setMessage "You are no admin"
redirect $ HomeR
Nothing -> do
setMessage "You must be logged in"
redirect $ LoginR