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-08-30 18:25:08 +00:00
|
|
|
module Handler.MediumSettings where
|
|
|
|
|
|
|
|
import Import
|
2014-12-28 00:09:19 +00:00
|
|
|
import Handler.Commons
|
2014-08-30 19:19:41 +00:00
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
2015-03-26 19:25:55 +00:00
|
|
|
import qualified Data.Text as T
|
2014-08-30 19:19:41 +00:00
|
|
|
import Data.List (tail)
|
2014-08-30 18:25:08 +00:00
|
|
|
|
|
|
|
getMediumSettingsR :: MediumId -> Handler Html
|
|
|
|
getMediumSettingsR mediumId = do
|
2014-12-28 00:09:19 +00:00
|
|
|
checkRes <- mediumCheck mediumId
|
|
|
|
case checkRes of
|
|
|
|
Right medium -> do
|
2015-10-19 06:32:47 +00:00
|
|
|
(mediumSettingsWidget, enctype) <- generateFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
mediumSettingsForm medium
|
2016-09-07 15:32:56 +00:00
|
|
|
defaultLayout $ do
|
2014-12-28 00:09:19 +00:00
|
|
|
setTitle "Eidolon :: Medium Settings"
|
|
|
|
$(widgetFile "mediumSettings")
|
|
|
|
Left (errorMsg, route) -> do
|
|
|
|
setMessage errorMsg
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect route
|
2014-08-30 18:25:08 +00:00
|
|
|
|
|
|
|
postMediumSettingsR :: MediumId -> Handler Html
|
|
|
|
postMediumSettingsR mediumId = do
|
2014-12-28 00:09:19 +00:00
|
|
|
checkRes <- mediumCheck mediumId
|
|
|
|
case checkRes of
|
|
|
|
Right medium -> do
|
2015-10-19 06:32:47 +00:00
|
|
|
((result, _), _) <- runFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
mediumSettingsForm medium
|
2014-12-28 00:09:19 +00:00
|
|
|
case result of
|
|
|
|
FormSuccess temp -> do
|
2014-12-28 06:12:25 +00:00
|
|
|
_ <- runDB $ update mediumId
|
2014-12-28 00:09:19 +00:00
|
|
|
[ MediumTitle =. mediumTitle temp
|
|
|
|
, MediumDescription =. mediumDescription temp
|
|
|
|
, MediumTags =. mediumTags temp
|
|
|
|
]
|
|
|
|
setMessage "Medium settings changed succesfully"
|
|
|
|
redirect $ MediumR mediumId
|
|
|
|
_ -> do
|
|
|
|
setMessage "There was an error changing the settings"
|
|
|
|
redirect $ MediumSettingsR mediumId
|
|
|
|
Left (errorMsg, route) -> do
|
|
|
|
setMessage errorMsg
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect route
|
2014-08-30 18:25:08 +00:00
|
|
|
|
2015-10-19 06:32:47 +00:00
|
|
|
mediumSettingsForm :: Medium -> AForm Handler Medium
|
|
|
|
mediumSettingsForm medium = Medium
|
|
|
|
<$> areq textField (bfs ("Title" :: T.Text)) (Just $ mediumTitle medium)
|
2014-08-30 18:25:08 +00:00
|
|
|
<*> 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-08-30 18:25:08 +00:00
|
|
|
<*> pure (mediumTime medium)
|
|
|
|
<*> pure (mediumOwner medium)
|
2015-10-19 06:32:47 +00:00
|
|
|
<*> aopt textareaField (bfs ("Description" :: T.Text)) (Just $ mediumDescription medium)
|
|
|
|
<*> areq tagField (bfs ("tags" :: T.Text)) (Just $ mediumTags medium)
|
2014-08-30 18:25:08 +00:00
|
|
|
<*> pure (mediumAlbum medium)
|
2015-09-27 22:40:05 +00:00
|
|
|
<*> pure (mediumPreview medium)
|
2015-10-19 06:32:47 +00:00
|
|
|
<* bootstrapSubmit ("Change settings" :: BootstrapSubmit T.Text)
|
2014-08-30 19:19:41 +00:00
|
|
|
|
|
|
|
getMediumDeleteR :: MediumId -> Handler Html
|
|
|
|
getMediumDeleteR mediumId = do
|
2014-12-28 00:09:19 +00:00
|
|
|
checkRes <- mediumCheck mediumId
|
|
|
|
case checkRes of
|
2015-09-14 16:54:46 +00:00
|
|
|
Right medium ->
|
2016-09-07 15:32:56 +00:00
|
|
|
defaultLayout $ do
|
2014-12-28 00:09:19 +00:00
|
|
|
setTitle "Eidolon :: Delete Medium"
|
|
|
|
$(widgetFile "mediumDelete")
|
|
|
|
Left (errorMsg, route) -> do
|
|
|
|
setMessage errorMsg
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect route
|
2014-08-30 19:19:41 +00:00
|
|
|
|
|
|
|
postMediumDeleteR :: MediumId -> Handler Html
|
|
|
|
postMediumDeleteR mediumId = do
|
2014-12-28 00:09:19 +00:00
|
|
|
checkRes <- mediumCheck mediumId
|
|
|
|
case checkRes of
|
|
|
|
Right medium -> do
|
|
|
|
confirm <- lookupPostParam "confirm"
|
|
|
|
case confirm of
|
|
|
|
Just "confirm" -> do
|
2016-10-19 12:14:42 +00:00
|
|
|
-- -- delete comments
|
|
|
|
-- commEnts <- runDB $ selectList [CommentOrigin ==. mediumId] []
|
|
|
|
-- _ <- mapM (runDB . delete . entityKey) commEnts
|
|
|
|
-- -- delete references first
|
|
|
|
-- let albumId = mediumAlbum medium
|
|
|
|
-- album <- runDB $ getJust albumId
|
|
|
|
-- let mediaList = albumContent album
|
|
|
|
-- let newMediaList = removeItem mediumId mediaList
|
|
|
|
-- -- update reference List
|
|
|
|
-- runDB $ update albumId [AlbumContent =. newMediaList]
|
|
|
|
-- liftIO $ removeFile (normalise $ tail $ mediumPath medium)
|
|
|
|
-- liftIO $ removeFile (normalise $ tail $ mediumThumb medium)
|
|
|
|
-- liftIO $ removeFile (normalise $ tail $ mediumPreview medium)
|
|
|
|
-- runDB $ delete mediumId
|
|
|
|
deleteMedium mediumId medium
|
2014-12-28 00:09:19 +00:00
|
|
|
setMessage "Medium succesfully deleted"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect HomeR
|
2014-12-28 00:09:19 +00:00
|
|
|
_ -> do
|
|
|
|
setMessage "You must confirm the deletion"
|
|
|
|
redirect $ MediumSettingsR mediumId
|
|
|
|
Left (errorMsg, route) -> do
|
|
|
|
setMessage errorMsg
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect route
|
2016-10-19 12:14:42 +00:00
|
|
|
|
|
|
|
getMediumMoveR :: MediumId -> Handler Html
|
|
|
|
getMediumMoveR mId = do
|
|
|
|
checkRes <- mediumCheck mId
|
|
|
|
case checkRes of
|
|
|
|
Right medium ->
|
|
|
|
(mediumSettingsWidget, enctype) <- generateFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
mediumMoveForm medium
|
|
|
|
Left (err, route) -> do
|
|
|
|
setMessage err
|
|
|
|
redirect route
|
|
|
|
|
|
|
|
postMediumMoveR :: MediumOd -> Handler Html
|
|
|
|
postMediumMoveR mId = do
|
|
|
|
checkRes <- mediumCheck mId
|
|
|
|
case checkres of
|
|
|
|
Right medium -> do
|
|
|
|
((res, _), _) <- runFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
mediumMoveForm medium
|
|
|
|
case res of
|
|
|
|
FormSuccess aId ->
|
|
|
|
moveMedium medium mId aId
|
|
|
|
|
|
|
|
mediumMoveForm :: Medium -> AForm Handler AlbumId
|
|
|
|
mediumMoveForm medium = id
|
|
|
|
<$> areq (selectField albums) (bfs ("Destination album" :: T.Text)) (mediumAlbum medium)
|
|
|
|
<* bootstrapSubmit ("Move medium" :: BootstrapSubmit Text)
|
|
|
|
where
|
|
|
|
albums = do
|
|
|
|
allEnts <- runDB $ selectList [] [Asc AlbumTitle]
|
|
|
|
let ents = catMaybes $ map (\ent ->
|
|
|
|
if uId == albumOwner (entityVal ent) || uId `elem` albumShares (entityVal nt)
|
|
|
|
then Just ent
|
|
|
|
else Nothing
|
|
|
|
) allEnts
|
|
|
|
optionsPairs $ map (\alb -> ((albumTitle $ entityVal alb), entityKey alb) ents
|
|
|
|
uId = mediumOwner medium
|