Experimental changes to Vervis.
Clone
HTTPS:
darcs clone https://vervis.peers.community/repos/KrXYo
SSH:
darcs clone USERNAME@vervis.peers.community:KrXYo
Tags
TODO
Ticket.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | {- This file is part of Vervis.
-
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ 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
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
module Vervis.Form.Ticket
( NewTicket (..)
, newTicketForm
, editTicketContentForm
, assignTicketForm
, claimRequestForm
, ticketFilterForm
, ticketDepForm
)
where
import Prelude
import Control.Applicative (liftA2, liftA3)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Class (lift)
import Data.Maybe (catMaybes, mapMaybe)
import Data.Text (Text)
import Data.Time.Calendar (Day (..))
import Data.Time.Clock (getCurrentTime, UTCTime (..))
import Database.Persist
import Yesod.Form
import Yesod.Persist.Core (runDB)
import qualified Data.Text as T (snoc)
import Vervis.Field.Ticket
import Vervis.Foundation (App, Form, Handler)
import Vervis.Model
import Vervis.Model.Ticket
import Vervis.Model.Workflow
import Vervis.Ticket
import Vervis.TicketFilter (TicketFilter (..))
--TODO use custom fields to ensure uniqueness or other constraints?
data NewTicket = NewTicket
{ ntTitle :: Text
, ntDesc :: Text
, ntTParams :: [(WorkflowFieldId, Text)]
, ntEParams :: [(WorkflowFieldId, WorkflowFieldEnumCtorId)]
}
fieldSettings :: Text -> Bool -> FieldSettings App
fieldSettings name req =
fieldSettingsLabel $
if req
then name `T.snoc` '*'
else name
tfield :: Entity WorkflowField -> AForm Handler (Maybe (WorkflowFieldId, Text))
tfield (Entity fid f) =
let sets = fieldSettings (workflowFieldName f) (workflowFieldRequired f)
in fmap (fid, ) <$>
if workflowFieldRequired f
then Just <$> areq textField sets Nothing
else aopt textField sets Nothing
efield
:: Entity WorkflowField
-> Maybe (AForm Handler (Maybe (WorkflowFieldId, WorkflowFieldEnumCtorId)))
efield (Entity fid f) =
case workflowFieldEnm f of
Nothing -> Nothing
Just eid -> Just $
let sets =
fieldSettings
(workflowFieldName f)
(workflowFieldRequired f)
sel =
selectField $
optionsPersistKey
[WorkflowFieldEnumCtorEnum ==. eid]
[]
workflowFieldEnumCtorName
in fmap (fid, ) <$>
if workflowFieldRequired f
then Just <$> areq sel sets Nothing
else aopt sel sets Nothing
newTicketForm :: WorkflowId -> Form NewTicket
newTicketForm wid html = do
(tfs, efs) <- lift $ runDB $ do
tfs <- selectList
[ WorkflowFieldWorkflow ==. wid
, WorkflowFieldType ==. WFTText
, WorkflowFieldEnm ==. Nothing
, WorkflowFieldFilterNew ==. True
]
[]
efs <- selectList
[ WorkflowFieldWorkflow ==. wid
, WorkflowFieldType ==. WFTEnum
, WorkflowFieldFilterNew ==. True
]
[]
return (tfs, efs)
flip renderDivs html $ NewTicket
<$> areq textField "Title*" Nothing
<*> ( maybe "" unTextarea <$>
aopt textareaField "Description (Markdown)" Nothing
)
<*> (catMaybes <$> traverse tfield tfs)
<*> (fmap catMaybes $ sequenceA $ mapMaybe efield efs)
editTicketContentAForm :: Ticket -> AForm Handler Ticket
editTicketContentAForm ticket = Ticket
<$> pure (ticketProject ticket)
<*> pure (ticketNumber ticket)
<*> pure (ticketCreated ticket)
<*> pure (ticketCreator ticket)
<*> areq textField "Title*" (Just $ ticketTitle ticket)
<*> ( maybe "" unTextarea <$>
aopt
textareaField
"Description (Markdown)"
(Just $ Just $ Textarea $ ticketDesc ticket)
)
<*> pure (ticketAssignee ticket)
<*> pure (ticketStatus ticket)
<*> pure (ticketClosed ticket)
<*> pure (ticketCloser ticket)
<*> pure (ticketDiscuss ticket)
tEditField
:: TicketTextParam
-> AForm Handler (Maybe TicketParamTextId, Maybe (WorkflowFieldId, Text))
tEditField (TicketTextParam (WorkflowFieldSummary fid _ name req _ _) mv) =
let sets = fieldSettings name req
in (ttpvId <$> mv, ) . fmap (fid, ) <$>
if req
then Just <$> areq textField sets (ttpvVal <$> mv)
else aopt textField sets (Just . ttpvVal <$> mv)
eEditField
:: TicketEnumParam
-> AForm
Handler
( Maybe TicketParamEnumId
, Maybe (WorkflowFieldId, WorkflowFieldEnumCtorId)
)
eEditField (TicketEnumParam (WorkflowFieldSummary fid _ name req _ _) e mv) =
let sets = fieldSettings name req
sel =
selectField $
optionsPersistKey
[WorkflowFieldEnumCtorEnum ==. wesId e]
[]
workflowFieldEnumCtorName
in (tepvId <$> mv, ) . fmap (fid, ) <$>
if req
then Just <$> areq sel sets (tepvVal <$> mv)
else aopt sel sets (Just . tepvVal <$> mv)
editableField :: Ticket -> WorkflowFieldSummary -> Bool
editableField t f =
not (wfsConstant f) &&
case ticketStatus t of
TSNew -> wffNew $ wfsFilter f
TSTodo -> wffTodo $ wfsFilter f
TSClosed -> wffClosed $ wfsFilter f
editTicketContentForm
:: TicketId
-> Ticket
-> WorkflowId
-> Form
( Ticket
, [ ( Maybe TicketParamTextId
, Maybe (WorkflowFieldId, Text)
)
]
, [ ( Maybe TicketParamEnumId
, Maybe (WorkflowFieldId, WorkflowFieldEnumCtorId)
)
]
)
editTicketContentForm tid t wid html = do
(tfs, efs) <-
lift $ runDB $
liftA2 (,)
( filter (editableField t . ttpField) <$>
getTicketTextParams tid wid
)
( filter (editableField t . tepField) <$>
getTicketEnumParams tid wid
)
flip renderDivs html $
liftA3 (,,)
(editTicketContentAForm t)
(traverse tEditField tfs)
(traverse eEditField efs)
assignTicketAForm :: PersonId -> ProjectId -> AForm Handler PersonId
assignTicketAForm pid jid =
areq (selectAssigneeFromProject pid jid) "Assignee*" Nothing
assignTicketForm :: PersonId -> ProjectId -> Form PersonId
assignTicketForm pid jid = renderDivs $ assignTicketAForm pid jid
claimRequestAForm :: AForm Handler Text
claimRequestAForm = unTextarea <$> areq textareaField "Message*" Nothing
claimRequestForm :: Form Text
claimRequestForm = renderDivs claimRequestAForm
ticketFilterAForm :: AForm Handler TicketFilter
ticketFilterAForm = TicketFilter
<$> aopt (selectField optionsEnum) "Status*" (Just Nothing)
ticketFilterForm :: Form TicketFilter
ticketFilterForm = renderDivs ticketFilterAForm
ticketDepAForm :: ProjectId -> TicketId -> AForm Handler TicketId
ticketDepAForm jid tid = areq (selectTicketDep jid tid) "Dependency" Nothing
ticketDepForm :: ProjectId -> TicketId -> Form TicketId
ticketDepForm jid tid = renderDivs $ ticketDepAForm jid tid
|