]> git.plutz.net Git - webpoll/blob - poll.sh
5624284347abf702012712528d37fb126fa39f12
[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 if expr match "$bookmarks" ".*${id}.*" >/dev/null; then
10   :
11 elif [ "$bookmarks" ]; then
12   SET_COOKIE +$((182 * 86400)) bookmarks="${bookmarks} ${id}" Path="${_BASE}/"
13 fi
14
15 admin="$(expr match "$bookmarks" ".*$id/\([a-zA-Z0-9:=]\{16\}\).*")"
16 admin="$(DBM "$file" get adminkey |grep -xF "$admin")"
17
18 tkey() {
19   # convert time stamps for use in POST keys
20   local str="$1" out
21   while [ "$str" ]; do
22     case $str in
23       :*) out="${out}.";;
24       *) out="${out}${str%"${str#?}"}";;
25     esac
26     str="${str#?}"
27   done
28   printf %s "$out"
29 }
30
31 timelist() {
32   local dates todall splittimes
33   local date tod todsplit
34
35   if [ "$splittimes" = no -a "$dates" -a "$todall" ]; then
36     for date in $dates; do for tod in $todall; do
37       printf %s\\n "${date}_${tod%-}"
38     done ;done
39
40   elif [ "$splittimes" = no -a "$dates" ]; then
41     for date in $dates; do
42       printf %s\\n "${date}"
43     done
44
45   elif [ "$splittimes" = no -a "$todall" ]; then
46     for tod in $todall; do
47       printf %s\\n "${tod%-}"
48     done
49
50   elif [ "$splittimes" = yes ]; then
51     for date in $dates; do
52       todsplit="$(DBM "$file" get "tod_$date")"
53       [ "$todsplit" ] \
54       && for tod in $todsplit; do printf %s\\n "${date}_${tod%-}"; done \
55       || printf %s\\n "${date}"
56     done
57
58   else
59     return 1
60
61   fi
62 }
63
64 table_poll() {
65   local splittimes="$(DBM "$file" get splittimes || printf no)"
66   local dates="$(DBM "$file" get dates)"
67   local todall="$(DBM "$file" get todall)"
68   local timelist="$(timelist)"
69   local edit="$(GET edit)"
70   local time date span name yes no maybe yc nc mc
71
72   [ "$timelist" ] || return 1
73
74   printf '[table .poll [thead\n'
75   # date header
76   if [ "$dates" ]; then
77     printf '[tr .dates [th]'
78     for date in $dates; do
79       span=0; for time in $timelist; do case $time in
80         ${date}*) span=$((span + 1));;
81       esac; done
82       date -d "$date" +"[th colspan=\"${span}\" . %A <br/> %B %_d, %Y]";
83     done
84     printf '[th]]\n'
85   fi
86   
87   # tod header
88   if [ "$splittimes" = yes -o "$todall" ]; then
89     printf '[tr .tod [th]'
90     for time in $timelist; do
91       [ "${time#*_}" = "${time}" ] && time="_${time}"
92       printf '[th . %s]' "${time#*_}"
93     done
94     printf '[th]]\n'
95   fi
96
97   printf '][tbody\n'
98
99   # Vote displays
100   { DBM "$file" get participants; printf \\n; } |while read -r name; do
101     [ "$name" = "$edit" ] && continue
102     maybe="$(DBM "$file" get "reply_maybe_${name}")"
103       yes="$(DBM "$file" get "reply_yes_${name}")"
104        no="$(DBM "$file" get "reply_no_${name}")"
105
106     printf '[tr [th .name . %s]' "$(HTML "$name")"
107     for time in $timelist; do
108       printf %s   "$yes" |grep -qwF "$time" && printf '[td   .yes   Yes]' && continue
109       printf %s    "$no" |grep -qwF "$time" && printf '[td    .no    No]' && continue
110       printf %s "$maybe" |grep -qwF "$time" && printf '[td .maybe Maybe]' && continue
111       printf '[td .missing . ?]'
112     done
113     printf '[td .edit [a href="?edit=%s" Edit]]]\n' "$(URL "$name")"
114   done
115
116   if [ "$edit" ]; then
117     maybe="$(DBM "$file" get "reply_maybe_${edit}")"
118       yes="$(DBM "$file" get "reply_yes_${edit}")"
119        no="$(DBM "$file" get "reply_no_${edit}")"
120
121     printf '[tr .new [th .name [submit "delete" "%s" -] %s]' "$(HTML "$edit")" "$(HTML "$edit")"
122     for time in $timelist; do
123       ktime="$(tkey "$time")"
124       printf '[td  [radio "%s"   "yes"   #yes_%s %s][label   for="yes_%s"   Yes]
125                    [radio "%s"    "no"    #no_%s %s][label    for="no_%s"    No]
126                    [radio "%s" "maybe" #maybe_%s %s][label for="maybe_%s" Maybe]
127               ]' "${ktime}" "${time}" "$(checked "$time"   $yes)" "${time}" \
128                  "${ktime}" "${time}" "$(checked "$time"    $no)" "${time}" \
129                  "${ktime}" "${time}" "$(checked "$time" $maybe)" "${time}"
130     done
131     printf '[td [submit "update" "%s" Update]]]\n' "$(HTML "$edit")"
132   else
133
134     # Vote counts
135     printf '[tr .votecount [td]'
136     for time in $timelist; do
137       yc=0 nc=0 mc=0
138       { DBM "$file" get participants; printf \\n\\n; } |while read -r name; do
139         [ ! "$name" ] && printf '[td %i (%i)]' "$yc" "$((yc + mc))" && break;
140           yes="$(DBM "$file" get "reply_yes_${name}")"
141            no="$(DBM "$file" get "reply_no_${name}")"
142         maybe="$(DBM "$file" get "reply_maybe_${name}")"
143
144         printf %s   "$yes" |grep -qwF "$time" && yc=$((yc + 1)) && continue
145         printf %s "$maybe" |grep -qwF "$time" && mc=$((mc + 1)) && continue
146       done
147     done
148     printf '[td]]\n'
149
150     # Submit line
151     printf '[tr .new [td [input name="name" value="" placeholder="Your Name" autocomplete=off]]'
152     for time in $timelist; do
153       time="$(tkey "$time")"
154       printf '[td  [radio "%s"   "yes"   #yes_%s][label   for="yes_%s"   Yes]
155                    [radio "%s"    "no"    #no_%s][label    for="no_%s"    No]
156                    [radio "%s" "maybe" #maybe_%s][label for="maybe_%s" Maybe]
157               ]' "${time}" "${time}" "${time}" \
158                  "${time}" "${time}" "${time}" \
159                  "${time}" "${time}" "${time}"
160     done
161     printf '[td [submit "new" "new" Submit]]]\n'
162   fi
163
164   printf ']]'
165 }
166
167 if [ "$REQUEST_METHOD" = POST ]; then
168   local update="$(POST update)" delete="$(POST delete)"
169   local name="$(POST name |grep -m 1 -xE '.*[^  ].*')"
170   local splittimes="$(DBM "$file" get splittimes || printf no)"
171   local dates="$(DBM "$file" get dates)"
172   local todall="$(DBM "$file" get todall)"
173   local timelist="$(timelist)"
174   local time yes no maybe reply
175
176   if [ "$(POST new)" = new -o "$update" ]; then
177     [ "$update" ] && name="$update"
178
179     if [ ! "$name" ]; then
180       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NONAME"
181     elif [ ! "$update" ] && DBM "$file" get participants |grep -qxF "$name"; then
182       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMEEXISTS"
183     elif [ "$update" ] && ! DBM "$file" get participants |grep -qxF "$name"; then
184       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
185     fi
186     if [ ! "$update" ]; then
187       DBM "$file" append participants "${BR}${name}" || DBM "$file" insert participants "${name}" \
188       || REDIRECT "${_BASE}${PATH_INFO}#ERROR_DBACCESS"
189     fi
190
191     for time in $timelist; do reply="$(POST "$(tkey "$time")")"; case $reply in
192         yes)   yes="${yes}${yes:+ }${time}";;
193          no)    no="${no}${no:+ }${time}";;
194       maybe) maybe="${maybe}${maybe:+ }${time}";;
195     esac; done
196     DBM "$file" set "reply_yes_${name}" "$yes"
197     DBM "$file" set "reply_no_${name}" "$no"
198     DBM "$file" set "reply_maybe_${name}" "$maybe"
199
200   elif [ "$delete" ]; then
201     if ! DBM "$file" get participants |grep -qxF "$delete"; then
202       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
203     fi
204     DBM "$file" set participants "$(DBM "$file" get participants |grep -vxF "$delete")"
205     DBM "$file" delete "reply_yes_${delete}"
206     DBM "$file" delete "reply_no_${delete}"
207     DBM "$file" delete "reply_maybe_${delete}"
208
209   elif [ "$(POST bookmark)" -a ! "$bookmarks" ]; then
210     SET_COOKIE +$((182 * 86400)) bookmarks="${id}" Path="${_BASE}/"
211
212   fi
213   REDIRECT "${_BASE}${PATH_INFO}"
214   
215 else
216   pagename="$(pagename "$id")"
217
218   yield_page "$pagename" poll <<-EOF
219         [form method=POST
220           [section .description
221             [h1 .title $(HTML "$pagename")]
222             $(DBM "$file" get description |markdown)
223           ]
224           $(table_poll || printf '[p Poll parameters are invalid]')
225           $(if [ "$admin" ]; then
226             printf '[section .bookmark You have bookmarked the admin page of this poll: [a href="./%s" modify poll]]' "$(URL ${id}/${admin})"
227           elif [ "$bookmarks" ]; then
228             printf '[section .bookmark This poll has been bookmarked and is accessible via a link on the front page.]'
229           else
230             printf '[section .bookmark Polls are accessible only via their URL. You can set a Cookie to bookmark all polls you visit. Bookmarked polls will be listed on the frontpage.<br/> [submit "bookmark" "add" Set Cookie]]'
231           fi)
232         ]
233         EOF
234 fi