eidolon/Handler/AdminAlbumSettings.hs

148 lines
5.5 KiB
Haskell
Raw Normal View History

2014-09-07 04:56:34 +00:00
module Handler.AdminAlbumSettings where
import Import
2014-12-28 00:08:35 +00:00
import Handler.Commons
2014-09-07 23:46:47 +00:00
import qualified Data.Text as T
import qualified Data.List as L
2015-01-11 19:06:44 +00:00
import Data.Maybe
2014-09-07 23:46:47 +00:00
import System.FilePath
import System.Directory
2014-09-07 04:56:34 +00:00
getAdminAlbumsR :: Handler Html
getAdminAlbumsR = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
albums <- runDB $ selectList [] [Asc AlbumTitle]
defaultLayout $ do
setTitle "Administration: Albums"
$(widgetFile "adminAlbums")
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-09 02:25:47 +00:00
getAdminAlbumMediaR :: AlbumId -> Handler Html
getAdminAlbumMediaR albumId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
media <- runDB $ selectList [MediumAlbum ==. albumId] [Asc MediumTitle]
defaultLayout $ do
setTitle "Administration: Album media"
$(widgetFile "adminAlbumMedia")
Nothing -> do
setMessage "This album does not exist"
redirect $ AdminR
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-07 04:56:34 +00:00
getAdminAlbumSettingsR :: AlbumId -> Handler Html
2014-09-07 23:46:47 +00:00
getAdminAlbumSettingsR albumId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
users <- return $ map (\u -> (userName $ entityVal u, entityKey u)) entities
(adminAlbumSettingsWidget, enctype) <- generateFormPost $ adminAlbumSettingsForm album albumId users
2015-01-11 19:06:44 +00:00
formLayout $ do
2014-12-27 22:09:51 +00:00
setTitle "Administration: Album settings"
$(widgetFile "adminAlbumSet")
Nothing -> do
setMessage "This album does not exist"
redirect $ AdminR
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-07 04:56:34 +00:00
postAdminAlbumSettingsR :: AlbumId -> Handler Html
2014-09-07 23:46:47 +00:00
postAdminAlbumSettingsR albumId = do
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
users <- return $ map (\u -> (userName $ entityVal u, entityKey u)) entities
2014-12-28 06:12:25 +00:00
((res, _), _) <- runFormPost $ adminAlbumSettingsForm album albumId users
2014-12-27 22:09:51 +00:00
case res of
FormSuccess temp -> do
2015-01-11 19:06:44 +00:00
width <- getThumbWidth $ Just $ L.tail $ fromMaybe ['a'] $ albumSamplePic temp
2014-12-28 06:12:25 +00:00
_ <- runDB $ update albumId
2014-12-27 22:09:51 +00:00
[ AlbumTitle =. albumTitle temp
, AlbumShares =. albumShares temp
, AlbumSamplePic =. albumSamplePic temp
, AlbumSampleWidth =. width
2014-12-27 22:09:51 +00:00
]
setMessage "Album settings changed successfully"
redirect $ AdminR
2014-12-27 22:09:51 +00:00
_ -> do
setMessage "There was an error while changing the settings"
redirect $ AdminAlbumSettingsR albumId
Nothing -> do
setMessage "This album does not exist"
redirect $ AdminR
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route
2014-09-07 23:46:47 +00:00
adminAlbumSettingsForm :: Album -> AlbumId -> [(Text, UserId)] -> Form Album
adminAlbumSettingsForm album albumId users = renderDivs $ Album
2014-09-07 23:46:47 +00:00
<$> areq textField "Title" (Just $ albumTitle album)
<*> pure (albumOwner album)
<*> areq (userField users) "This album shared with" (Just $ albumShares album)
2014-09-07 23:46:47 +00:00
<*> pure (albumContent album)
<*> aopt (selectField media) "Sample picture" (Just $ albumSamplePic album)
2015-01-11 16:06:43 +00:00
<*> pure 230
2014-09-07 23:46:47 +00:00
where
media = do
entities <- runDB $ selectList [MediumAlbum ==. albumId] [Asc MediumTitle]
optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumThumb (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
2014-12-27 22:09:51 +00:00
adminCheck <- loginIsAdmin
case adminCheck of
Right _ -> do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
-- remove reference from owner
ownerId <- return $ albumOwner album
owner <- runDB $ getJust ownerId
albumList <- return $ userAlbums owner
newAlbumList <- return $ removeItem albumId albumList
runDB $ update ownerId [UserAlbums =. newAlbumList]
-- delete album content and its comments
2014-12-28 06:12:25 +00:00
_ <- mapM (\a -> do
2014-12-27 22:09:51 +00:00
-- delete files
medium <- runDB $ getJust a
liftIO $ removeFile (normalise $ L.tail $ mediumPath medium)
liftIO $ removeFile (normalise $ L.tail $ mediumThumb medium)
-- delete comments
commEnts <- runDB $ selectList [CommentOrigin ==. a] []
2014-12-28 06:12:25 +00:00
_ <- mapM (\ent -> runDB $ delete $ entityKey ent) commEnts
2014-12-27 22:09:51 +00:00
-- delete album database entry
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
Left (errorMsg, route) -> do
setMessage errorMsg
redirect $ route