]> git.plutz.net Git - webpoll/blob - poll.sh
74fa3e11a36cb5850fd94b07e8cdb37932feba01
[webpoll] / poll.sh
1 #!/bin/sh
2
3 id="$(checkid "${PATH_INFO#/}")"
4 file="${_DATA}/${id}"
5
6 #cancel if poll is invalid
7 [ "$id" -a -f "$file" ] || REDIRECT "$_BASE/"
8
9 tkey() {
10   # convert time stamps for use in POST keys
11   local str="$1" out
12   while [ "$str" ]; do
13     case $str in
14       :*) out="${out}.";;
15       *) out="${out}${str%"${str#?}"}";;
16     esac
17     str="${str#?}"
18   done
19   printf %s "$out"
20 }
21
22 timelist() {
23   local dates todall splittimes
24   local date tod todsplit
25
26   if [ "$splittimes" = no -a "$dates" -a "$todall" ]; then
27     for date in $dates; do for tod in $todall; do
28       printf %s\\n "${date}_${tod%-}"
29     done ;done
30
31   elif [ "$splittimes" = no -a "$dates" ]; then
32     for date in $dates; do
33       printf %s\\n "${date}"
34     done
35
36   elif [ "$splittimes" = no -a "$todall" ]; then
37     for tod in $todall; do
38       printf %s\\n "${tod%-}"
39     done
40
41   elif [ "$splittimes" = yes ]; then
42     for date in $dates; do
43       todsplit="$(DBM "$file" get "tod_$date")"
44       [ "$todsplit" ] \
45       && for tod in $todsplit; do printf %s\\n "${date}_${tod%-}"; done \
46       || printf %s\\n "${date}"
47     done
48
49   else
50     return 1
51
52   fi
53 }
54
55 table_poll() {
56   local splittimes="$(DBM "$file" get splittimes || printf no)"
57   local dates="$(DBM "$file" get dates)"
58   local todall="$(DBM "$file" get todall)"
59   local timelist="$(timelist)"
60   local time date span name
61
62   [ "$timelist" ] || return 1
63
64   printf '[table .poll [thead\n'
65   # date header
66   if [ "$dates" ]; then
67     printf '[tr .dates [th]'
68     for date in $dates; do
69       span=0; for time in $timelist; do case $time in
70         ${date}*) span=$((span + 1));;
71       esac; done
72       date -d "$date" +"[th colspan=\"${span}\" . %A <br/> %B %_d, %Y]";
73     done
74     printf '[th]]\n'
75   fi
76   
77   # tod header
78   if [ "$splittimes" = yes -o "$todall" ]; then
79     printf '[tr .tod [th]'
80     for time in $timelist; do
81       [ "${time#*_}" = "${time}" ] && time="${time}_"
82       printf '[th . %s]' "${time#*_}"
83     done
84     printf '[th]]\n'
85   fi
86
87   printf '][tbody\n'
88
89   { DBM "$file" get participants; printf \\n; } |while read -r name; do
90       yes="$(DBM "$file" get "reply_yes_${name}")"
91        no="$(DBM "$file" get "reply_no_${name}")"
92     maybe="$(DBM "$file" get "reply_maybe_${name}")"
93
94     printf '[tr [th .name . %s]' "$(HTML "$name")"
95     for time in $timelist; do
96       printf %s   "$yes" |grep -qwF "$time" && printf '[td   .yes   Yes]' && continue
97       printf %s    "$no" |grep -qwF "$time" && printf '[td    .no    No]' && continue
98       printf %s "$maybe" |grep -qwF "$time" && printf '[td .maybe Maybe]' && continue
99       printf '[td .missing . ?]'
100     done
101     printf '[td]]'
102   done
103
104   # Submit line
105   printf '[tr .new [td [input name="name" value="" placeholder="Your Name" autocomplete=off]]'
106   for time in $timelist; do
107     time="$(tkey "$time")"
108     printf '[td  [radio "%s"   "yes"   #yes_%s][label   for="yes_%s"   Yes]
109                  [radio "%s"    "no"    #no_%s][label    for="no_%s"    No]
110                  [radio "%s" "maybe" #maybe_%s][label for="maybe_%s" Maybe]
111             ]' "${time}" "${time}" "${time}" \
112                "${time}" "${time}" "${time}" \
113                "${time}" "${time}" "${time}"
114   done
115   printf '[td [submit "new" "new" Submit]]]\n'
116
117   printf ']]'
118 }
119
120 if [ "$REQUEST_METHOD" = POST ]; then
121   local name="$(POST name |grep -m 1 -xE '.*[^  ].*')"
122   local splittimes="$(DBM "$file" get splittimes || printf no)"
123   local dates="$(DBM "$file" get dates)"
124   local todall="$(DBM "$file" get todall)"
125   local timelist="$(timelist)"
126   local time yes no maybe reply
127
128   if [ "$(POST new)" = new ]; then
129     if [ ! "$name" ]; then
130       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NONAME"
131     elif DBM "$file" get participants |grep -qxF "$name"; then
132       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMEEXISTS"
133     fi
134     DBM "$file" append participants "${BR}${name}" || DBM "$file" insert participants "${name}" \
135     || REDIRECT "${_BASE}${PATH_INFO}#ERROR_DBACCESS"
136
137     for time in $timelist; do reply="$(POST "$(tkey "$time")")"; case $reply in
138         yes)   yes="${yes}${yes:+ }${time}";;
139          no)    no="${no}${no:+ }${time}";;
140       maybe) maybe="${maybe}${maybe:+ }${time}";;
141     esac; done
142     DBM "$file" set "reply_yes_${name}" "$yes"
143     DBM "$file" set "reply_no_${name}" "$no"
144     DBM "$file" set "reply_maybe_${name}" "$maybe"
145     REDIRECT "${_BASE}${PATH_INFO}"
146   fi
147   
148 else
149   pagename="$(pagename "$id")"
150
151   yield_page "$pagename" poll <<-EOF
152         [form method=POST
153           [section .description
154             [h1 .title $(HTML "$pagename")]
155             $(DBM "$file" get description |markdown)
156           ]
157           $(table_poll || printf '[p Poll parameters are invalid]')
158         ]
159         EOF
160 fi