eidolon/Handler/AdminAlbumSettings.hs

174 lines
6.6 KiB
Haskell
Raw Normal View History

2015-01-18 19:44:41 +00:00
-- eidolon -- A simple gallery in Haskell and Yesod
-- Copyright (C) 2015 Amedeo Molnár
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
2015-01-21 09:00:18 +00:00
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-01-18 19:44:41 +00:00
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
2015-09-14 16:54:46 +00:00
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"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
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
2015-09-14 16:54:46 +00:00
entities <- runDB $ selectList [UserId !=. albumOwner album] [Desc UserName]
let users = map (\u -> (userName $ entityVal u, entityKey u)) entities
2015-10-19 06:32:47 +00:00
(adminAlbumSettingsWidget, enctype) <- generateFormPost $
renderBootstrap3 BootstrapBasicForm $
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"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
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
2015-09-14 16:54:46 +00:00
entities <- runDB $ selectList [UserId !=. albumOwner album] [Desc UserName]
let users = map (\u -> (userName $ entityVal u, entityKey u)) entities
2015-10-19 06:32:47 +00:00
((res, _), _) <- runFormPost $
renderBootstrap3 BootstrapBasicForm $
adminAlbumSettingsForm album albumId users
2014-12-27 22:09:51 +00:00
case res of
FormSuccess temp -> do
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
]
setMessage "Album settings changed successfully"
2015-09-14 16:54:46 +00:00
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"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route
2014-09-07 23:46:47 +00:00
2015-10-19 06:32:47 +00:00
adminAlbumSettingsForm :: Album -> AlbumId -> [(Text, UserId)] -> AForm Handler Album
adminAlbumSettingsForm album albumId users = Album
<$> areq textField (bfs ("Title" :: T.Text)) (Just $ albumTitle album)
2014-09-07 23:46:47 +00:00
<*> pure (albumOwner album)
2015-10-19 06:32:47 +00:00
<*> areq (userField users) (bfs ("This album shared with" :: T.Text)) (Just $ albumShares album)
2014-09-07 23:46:47 +00:00
<*> pure (albumContent album)
2015-10-19 06:32:47 +00:00
<*> aopt (selectField media) (bfs ("Sample picture" :: T.Text)) (Just $ albumSamplePic album)
<* bootstrapSubmit ("Change settings" :: BootstrapSubmit Text)
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
2015-09-14 16:54:46 +00:00
let ownerId = albumOwner album
2014-12-27 22:09:51 +00:00
owner <- runDB $ getJust ownerId
2015-09-14 16:54:46 +00:00
let albumList = userAlbums owner
let newAlbumList = removeItem albumId albumList
2014-12-27 22:09:51 +00:00
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)
2016-05-04 00:26:48 +00:00
liftIO $ removeFile (normalise $ L.tail $ mediumPreview medium)
2014-12-27 22:09:51 +00:00
-- delete comments
commEnts <- runDB $ selectList [CommentOrigin ==. a] []
_ <- mapM (\c -> do
2016-09-05 14:01:49 +00:00
-- delete comment
children <- runDB $ selectList [CommentParent ==. (Just $ entityKey c)] []
_ <- mapM (\child -> do
2016-09-05 14:01:49 +00:00
-- delete comment children from db
runDB $ delete $ entityKey child
) children
runDB $ delete $ entityKey c) 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
2015-09-14 16:54:46 +00:00
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> T.unpack (extractKey ownerId) </> T.unpack (extractKey albumId)
2014-12-27 22:09:51 +00:00
-- outro
setMessage "Album deleted successfully"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Nothing -> do
setMessage "This album dies not exist"
2015-09-14 16:54:46 +00:00
redirect AdminR
2014-12-27 22:09:51 +00:00
Left (errorMsg, route) -> do
setMessage errorMsg
2015-09-14 16:54:46 +00:00
redirect route