]> git.plutz.net Git - webpoll/blob - poll.sh
put home page into separate file
[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 edit="$(GET edit)"
61   local time date span name yes no maybe yc nc mc
62
63   [ "$timelist" ] || return 1
64
65   printf '[table .poll [thead\n'
66   # date header
67   if [ "$dates" ]; then
68     printf '[tr .dates [th]'
69     for date in $dates; do
70       span=0; for time in $timelist; do case $time in
71         ${date}*) span=$((span + 1));;
72       esac; done
73       date -d "$date" +"[th colspan=\"${span}\" . %A <br/> %B %_d, %Y]";
74     done
75     printf '[th]]\n'
76   fi
77   
78   # tod header
79   if [ "$splittimes" = yes -o "$todall" ]; then
80     printf '[tr .tod [th]'
81     for time in $timelist; do
82       [ "${time#*_}" = "${time}" ] && time="${time}_"
83       printf '[th . %s]' "${time#*_}"
84     done
85     printf '[th]]\n'
86   fi
87
88   printf '][tbody\n'
89
90   # Vote displays
91   { DBM "$file" get participants; printf \\n; } |while read -r name; do
92     [ "$name" = "$edit" ] && continue
93     maybe="$(DBM "$file" get "reply_maybe_${name}")"
94       yes="$(DBM "$file" get "reply_yes_${name}")"
95        no="$(DBM "$file" get "reply_no_${name}")"
96
97     printf '[tr [th .name . %s]' "$(HTML "$name")"
98     for time in $timelist; do
99       printf %s   "$yes" |grep -qwF "$time" && printf '[td   .yes   Yes]' && continue
100       printf %s    "$no" |grep -qwF "$time" && printf '[td    .no    No]' && continue
101       printf %s "$maybe" |grep -qwF "$time" && printf '[td .maybe Maybe]' && continue
102       printf '[td .missing . ?]'
103     done
104     printf '[td .edit [a href="?edit=%s" Edit]]]\n' "$(URL "$name")"
105   done
106
107   if [ "$edit" ]; then
108     maybe="$(DBM "$file" get "reply_maybe_${edit}")"
109       yes="$(DBM "$file" get "reply_yes_${edit}")"
110        no="$(DBM "$file" get "reply_no_${edit}")"
111
112     printf '[tr .new [th .name [submit "delete" "%s" -] %s]' "$(HTML "$edit")" "$(HTML "$edit")"
113     for time in $timelist; do
114       ktime="$(tkey "$time")"
115       printf '[td  [radio "%s"   "yes"   #yes_%s %s][label   for="yes_%s"   Yes]
116                    [radio "%s"    "no"    #no_%s %s][label    for="no_%s"    No]
117                    [radio "%s" "maybe" #maybe_%s %s][label for="maybe_%s" Maybe]
118               ]' "${ktime}" "${time}" "$(checked "$time"   $yes)" "${time}" \
119                  "${ktime}" "${time}" "$(checked "$time"    $no)" "${time}" \
120                  "${ktime}" "${time}" "$(checked "$time" $maybe)" "${time}"
121     done
122     printf '[td [submit "update" "%s" Update]]]\n' "$(HTML "$edit")"
123   else
124
125     # Vote counts
126     printf '[tr .votecount [td]'
127     for time in $timelist; do
128       yc=0 nc=0 mc=0
129       { DBM "$file" get participants; printf \\n\\n; } |while read -r name; do
130         [ ! "$name" ] && printf '[td %i (%i)]' "$yc" "$((yc + mc))" && break;
131           yes="$(DBM "$file" get "reply_yes_${name}")"
132            no="$(DBM "$file" get "reply_no_${name}")"
133         maybe="$(DBM "$file" get "reply_maybe_${name}")"
134
135         printf %s   "$yes" |grep -qwF "$time" && yc=$((yc + 1)) && continue
136         printf %s "$maybe" |grep -qwF "$time" && mc=$((mc + 1)) && continue
137       done
138     done
139     printf '[td]]\n'
140
141     # Submit line
142     printf '[tr .new [td [input name="name" value="" placeholder="Your Name" autocomplete=off]]'
143     for time in $timelist; do
144       time="$(tkey "$time")"
145       printf '[td  [radio "%s"   "yes"   #yes_%s][label   for="yes_%s"   Yes]
146                    [radio "%s"    "no"    #no_%s][label    for="no_%s"    No]
147                    [radio "%s" "maybe" #maybe_%s][label for="maybe_%s" Maybe]
148               ]' "${time}" "${time}" "${time}" \
149                  "${time}" "${time}" "${time}" \
150                  "${time}" "${time}" "${time}"
151     done
152     printf '[td [submit "new" "new" Submit]]]\n'
153   fi
154
155   printf ']]'
156 }
157
158 if [ "$REQUEST_METHOD" = POST ]; then
159   local update="$(POST update)" delete="$(POST delete)"
160   local name="$(POST name |grep -m 1 -xE '.*[^  ].*')"
161   local splittimes="$(DBM "$file" get splittimes || printf no)"
162   local dates="$(DBM "$file" get dates)"
163   local todall="$(DBM "$file" get todall)"
164   local timelist="$(timelist)"
165   local time yes no maybe reply
166
167   if [ "$(POST new)" = new -o "$update" ]; then
168     [ "$update" ] && name="$update"
169
170     if [ ! "$name" ]; then
171       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NONAME"
172     elif [ ! "$update" ] && DBM "$file" get participants |grep -qxF "$name"; then
173       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMEEXISTS"
174     elif [ "$update" ] && ! DBM "$file" get participants |grep -qxF "$name"; then
175       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
176     fi
177     if [ ! "$update" ]; then
178       DBM "$file" append participants "${BR}${name}" || DBM "$file" insert participants "${name}" \
179       || REDIRECT "${_BASE}${PATH_INFO}#ERROR_DBACCESS"
180     fi
181
182     for time in $timelist; do reply="$(POST "$(tkey "$time")")"; case $reply in
183         yes)   yes="${yes}${yes:+ }${time}";;
184          no)    no="${no}${no:+ }${time}";;
185       maybe) maybe="${maybe}${maybe:+ }${time}";;
186     esac; done
187     DBM "$file" set "reply_yes_${name}" "$yes"
188     DBM "$file" set "reply_no_${name}" "$no"
189     DBM "$file" set "reply_maybe_${name}" "$maybe"
190
191   elif [ "$delete" ]; then
192     if ! DBM "$file" get participants |grep -qxF "$delete"; then
193       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
194     fi
195     DBM "$file" set participants "$(DBM "$file" get participants |grep -vxF "$delete")"
196     DBM "$file" delete "reply_yes_${delete}"
197     DBM "$file" delete "reply_no_${delete}"
198     DBM "$file" delete "reply_maybe_${delete}"
199   fi
200   REDIRECT "${_BASE}${PATH_INFO}"
201   
202 else
203   pagename="$(pagename "$id")"
204
205   yield_page "$pagename" poll <<-EOF
206         [form method=POST
207           [section .description
208             [h1 .title $(HTML "$pagename")]
209             $(DBM "$file" get description |markdown)
210           ]
211           $(table_poll || printf '[p Poll parameters are invalid]')
212         ]
213         EOF
214 fi