module HalbumEdit where import CGIUtils import Config import HtmlView import ImageInfo import ImageStore import Manage import Utils import Data.List import Data.Maybe import Network.NewCGI hiding (Html) import Text.XHtml data Action = UploadForm | AddImages [(String,Buffer)] | AddTag ImageID Tag | RemoveImage ImageID | RemoveTag ImageID Tag deriving (Show,Eq) getAction :: CGI (Maybe Action) getAction = do a <- getInput "action" case a of Nothing -> return Nothing Just "uploadimages" -> return $ Just UploadForm Just "addimages" -> do is <- getInputNames let fs = filter ("file" `isPrefixOf`) is getImage f = do name <- getInputFilename f >>= maybeM "No filename given" file <- getInputBufferOrFail f return $ (name,file) xs <- mapM getImage fs -- logCGI $ "Image files: " ++ show (map fst xs) return $ Just $ AddImages xs Just "addtag" -> do id <- getInputOrFail "image" >>= readImageID tag <- getInputOrFail "tag" return $ Just $ AddTag id tag Just "removeimage" -> do id <- getInputOrFail "image" >>= readImageID return $ Just $ RemoveImage id Just "removetag" -> do id <- getInputOrFail "image" >>= readImageID tag <- getInputOrFail "tag" return $ Just $ RemoveTag id tag Just c -> return Nothing doAction :: Config -> Action -> CGI CGIResult doAction conf action = case action of AddImages is -> do liftIO $ mapM_ (uncurry (doAddImage (metaDataStore conf))) is next "Image(s) added" AddTag img tag -> do liftIO $ doAddTag (metaDataStore conf) img tag next "Tag added" RemoveImage img -> do liftIO $ doRemoveImage (metaDataStore conf) img next "Image removed" RemoveTag img tag -> do liftIO $ doRemoveTag (metaDataStore conf) img tag next "Tag removed" UploadForm -> outputHtml $ uploadPage (viewConfig conf) where next t = do g <- getInput "goto" case g of Just u -> redirect u Nothing -> cont t cont t = do let bdy = thediv << t +++ thediv (hlink (adminUrl (viewConfig conf)) << "Continue...") outputHtml $ mkHtml (thetitle << "Done") bdy