{- This file is part of Vervis. - - Written in 2016 by fr33domlover . - - ♡ Copying is an act of love. Please copy, reuse and share. - - The author(s) have dedicated all copyright and related and neighboring - rights to this software to the public domain worldwide. This software is - distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along - with this software. If not, see - . -} module Vervis.Form.Project ( NewProject (..) , newProjectForm , NewProjectCollab (..) , newProjectCollabForm , editProjectForm ) where import Vervis.Import hiding (on, isNothing) import Database.Esqueleto hiding ((==.)) import qualified Database.Esqueleto as E ((==.)) import Vervis.Field.Project import Vervis.Model import Vervis.Model.Ident import Vervis.Model.Repo data NewProject = NewProject { npIdent :: PrjIdent , npName :: Maybe Text , npDesc :: Maybe Text , npWflow :: WorkflowId , npRole :: ProjectRoleId } newProjectAForm :: SharerId -> AForm Handler NewProject newProjectAForm sid = NewProject <$> areq (newProjectIdentField sid) "Identifier*" Nothing <*> aopt textField "Name" Nothing <*> aopt textField "Description" Nothing <*> areq selectWorkflow "Workflow*" Nothing <*> areq selectRole "Your role*" Nothing where selectRole = selectField $ optionsPersistKey [ProjectRoleSharer ==. sid] [] $ rl2text . projectRoleIdent selectWorkflow = selectField $ optionsPersistKey [WorkflowSharer ==. sid] [] $ \ w -> fromMaybe (wfl2text $ workflowIdent w) $ workflowName w newProjectForm :: SharerId -> Form NewProject newProjectForm sid = renderDivs $ newProjectAForm sid data NewProjectCollab = NewProjectCollab { ncPerson :: PersonId , ncRole :: ProjectRoleId } newProjectCollabAForm :: SharerId -> ProjectId -> AForm Handler NewProjectCollab newProjectCollabAForm sid jid = NewProjectCollab <$> areq selectPerson "Person*" Nothing <*> areq selectRole "Role*" Nothing where selectPerson = selectField $ do l <- runDB $ select $ from $ \ (collab `RightOuterJoin` person `InnerJoin` sharer) -> do on $ person ^. PersonIdent E.==. sharer ^. SharerId on $ collab ?. ProjectCollabProject E.==. just (val jid) &&. collab ?. ProjectCollabPerson E.==. just (person ^. PersonId) where_ $ isNothing $ collab ?. ProjectCollabId return (sharer ^. SharerIdent, person ^. PersonId) optionsPairs $ map (shr2text . unValue *** unValue) l selectRole = selectField $ optionsPersistKey [ProjectRoleSharer ==. sid] [] $ rl2text . projectRoleIdent newProjectCollabForm :: SharerId -> ProjectId -> Form NewProjectCollab newProjectCollabForm sid jid = renderDivs $ newProjectCollabAForm sid jid editProjectAForm :: Entity Project -> AForm Handler Project editProjectAForm (Entity jid project) = Project <$> pure (projectIdent project) <*> pure (projectSharer project) <*> aopt textField "Name" (Just $ projectName project) <*> aopt textField "Description" (Just $ projectDesc project) <*> pure (projectWorkflow project) <*> pure (projectNextTicket project) <*> aopt selectWiki "Wiki" (Just $ projectWiki project) where selectWiki = selectField $ optionsPersistKey [RepoProject ==. Just jid] [] $ rp2text . repoIdent editProjectForm :: Entity Project -> Form Project editProjectForm p = renderDivs $ editProjectAForm p