eidolon/Handler/AdminMediumSettings.hs

113 lines
3.8 KiB
Haskell
Raw Normal View History

2014-09-09 02:25:47 +00:00
module Handler.AdminMediumSettings where
import Import
2014-12-28 00:08:35 +00:00
import Handler.Commons
2014-09-09 02:25:47 +00:00
import System.FilePath
import System.Directory
import Data.List (tail)
getAdminMediaR :: Handler Html
getAdminMediaR = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
media <- runDB $ selectList [] [Asc MediumTitle]
defaultLayout $ do
setTitle "Administration: Media"
$(widgetFile "adminMedia")
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-09 02:25:47 +00:00
getAdminMediumSettingsR :: MediumId -> Handler Html
getAdminMediumSettingsR mediumId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
(adminMediumSetWidget, enctype) <- generateFormPost $ adminMediumSetForm medium
defaultLayout $ do
setTitle "Administration: Medium Settings"
$(widgetFile "adminMediumSet")
Nothing -> do
setMessage "This medium does not exist"
redirect $ AdminR
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-09 02:25:47 +00:00
postAdminMediumSettingsR :: MediumId -> Handler Html
postAdminMediumSettingsR mediumId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
2014-12-28 06:12:25 +00:00
((res, _), _) <- runFormPost $ adminMediumSetForm medium
2014-12-27 22:09:51 +00:00
case res of
FormSuccess temp -> do
runDB $ update mediumId
[ MediumTitle =. mediumTitle temp
, MediumDescription =. mediumDescription temp
, MediumTags =. mediumTags temp
]
setMessage "Medium settings changed successfully"
2014-09-09 02:25:47 +00:00
redirect $ AdminR
2014-12-27 22:09:51 +00:00
_ -> do
setMessage "There was an error while changing the settings"
redirect $ AdminMediumSettingsR mediumId
Nothing -> do
setMessage "This medium does not exist"
redirect $ AdminR
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-09 02:25:47 +00:00
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 (mediumThumbWidth medium)
2014-09-09 02:25:47 +00:00
<*> pure (mediumAlbum medium)
getAdminMediumDeleteR :: MediumId -> Handler Html
getAdminMediumDeleteR mediumId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> 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] []
2014-12-28 06:12:25 +00:00
_ <- mapM (\ent -> runDB $ delete $ entityKey ent) commEnts
2014-12-27 22:09:51 +00:00
-- delete medium
runDB $ delete mediumId
-- delete files
liftIO $ removeFile (normalise $ tail $ mediumPath medium)
liftIO $ removeFile (normalise $ tail $ mediumPath medium)
-- outro
setMessage "Medium deleted successfully"
redirect $ AdminR
Nothing -> do
setMessage "This medium does not exist"
redirect $ AdminR
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route