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
|
2014-12-21 20:55:23 +00:00
|
|
|
import qualified Data.List as L
|
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
|
|
|
|
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
|
|
|
albums <- runDB $ selectList [] [Asc AlbumTitle]
|
2014-09-07 04:56:34 +00:00
|
|
|
defaultLayout $ do
|
2014-09-24 21:03:18 +00:00
|
|
|
setTitle "Administration: Albums"
|
2014-09-07 04:56:34 +00:00
|
|
|
$(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
|
2014-09-21 16:42:07 +00:00
|
|
|
media <- runDB $ selectList [MediumAlbum ==. albumId] [Asc MediumTitle]
|
2014-09-09 02:25:47 +00:00
|
|
|
defaultLayout $ do
|
2014-09-24 21:03:18 +00:00
|
|
|
setTitle "Administration: Album media"
|
2014-09-09 02:25:47 +00:00
|
|
|
$(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
|
2014-09-08 01:56:30 +00:00
|
|
|
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-12-06 03:39:24 +00:00
|
|
|
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
|
2014-09-07 23:46:47 +00:00
|
|
|
defaultLayout $ do
|
2014-09-24 21:03:18 +00:00
|
|
|
setTitle "Administration: Album settings"
|
2014-09-07 23:46:47 +00:00
|
|
|
$(widgetFile "adminAlbumSet")
|
2014-09-08 01:56:30 +00:00
|
|
|
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
|
2014-09-08 01:56:30 +00:00
|
|
|
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
|
2014-09-08 01:56:30 +00:00
|
|
|
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-12-06 03:39:24 +00:00
|
|
|
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
|
|
|
|
users <- return $ map (\u -> (userName $ entityVal u, entityKey u)) entities
|
|
|
|
((res, adminAlbumSettingsWidget), enctype) <- runFormPost $ adminAlbumSettingsForm album albumId users
|
2014-09-07 23:46:47 +00:00
|
|
|
case res of
|
|
|
|
FormSuccess temp -> do
|
|
|
|
aId <- runDB $ update albumId
|
|
|
|
[ AlbumTitle =. albumTitle temp
|
2014-12-06 03:39:24 +00:00
|
|
|
, AlbumShares =. albumShares temp
|
2014-09-07 23:46:47 +00:00
|
|
|
, AlbumSamplePic =. albumSamplePic temp
|
|
|
|
]
|
|
|
|
setMessage "Album settings changed successfully"
|
|
|
|
redirect $ AdminR
|
|
|
|
_ -> do
|
|
|
|
setMessage "There was an error while changing the settings"
|
|
|
|
redirect $ AdminAlbumSettingsR albumId
|
2014-09-08 01:56:30 +00:00
|
|
|
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
|
2014-09-08 01:56:30 +00:00
|
|
|
setMessage "You must be logged in"
|
|
|
|
redirect $ LoginR
|
2014-09-07 23:46:47 +00:00
|
|
|
|
2014-12-06 03:39:24 +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)
|
2014-12-06 03:39:24 +00:00
|
|
|
<*> 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)
|
|
|
|
where
|
|
|
|
media = do
|
|
|
|
entities <- runDB $ selectList [MediumAlbum ==. albumId] [Desc MediumTitle]
|
|
|
|
optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumPath (entityVal med))) entities
|
2014-12-06 03:39:24 +00:00
|
|
|
-- userNames =
|
|
|
|
-- let entities = runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
|
|
|
|
-- in map (\ent -> (userName $ entityVal ent, entityKey ent)) 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-09-08 01:56:30 +00:00
|
|
|
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-12-21 20:55:23 +00:00
|
|
|
-- delete album content and its comments
|
|
|
|
mapM (\a -> do
|
|
|
|
-- 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] []
|
|
|
|
mapM (\ent -> runDB $ delete $ entityKey ent) commEnts
|
|
|
|
-- delete album database entry
|
|
|
|
runDB $ delete a
|
|
|
|
) (albumContent album)
|
2014-09-07 23:46:47 +00:00
|
|
|
-- 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
|
2014-09-08 01:56:30 +00:00
|
|
|
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
|
2014-09-08 01:56:30 +00:00
|
|
|
setMessage "You must be logged in"
|
|
|
|
redirect $ LoginR
|