]> git.plutz.net Git - webpoll/blob - poll.sh
clear deleted polls from bookmarks
[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       case $time in
92        *-*-*_*:*) time="${time#*_}";;
93        *-*-*) time="";;
94        *:*);;  # time="${time}"
95       esac
96       printf '[th . %s]' "${time}"
97     done
98     printf '[th]]\n'
99   fi
100
101   printf '][tbody\n'
102
103   # Vote displays
104   { DBM "$file" get participants; printf \\n; } |while read -r name; do
105     [ "$name" = "$edit" ] && continue
106     maybe="$(DBM "$file" get "reply_maybe_${name}")"
107       yes="$(DBM "$file" get "reply_yes_${name}")"
108        no="$(DBM "$file" get "reply_no_${name}")"
109
110     printf '[tr [th .name . %s]' "$(HTML "$name")"
111     for time in $timelist; do
112       printf %s   "$yes" |grep -qwF "$time" && printf '[td   .yes   Yes]' && continue
113       printf %s    "$no" |grep -qwF "$time" && printf '[td    .no    No]' && continue
114       printf %s "$maybe" |grep -qwF "$time" && printf '[td .maybe Maybe]' && continue
115       printf '[td .missing . ?]'
116     done
117     printf '[td .edit [a href="?edit=%s" Edit]]]\n' "$(URL "$name")"
118   done
119
120   if [ "$edit" ]; then
121     maybe="$(DBM "$file" get "reply_maybe_${edit}")"
122       yes="$(DBM "$file" get "reply_yes_${edit}")"
123        no="$(DBM "$file" get "reply_no_${edit}")"
124
125     printf '[tr .new [th .name [submit "delete" "%s" -] %s]' "$(HTML "$edit")" "$(HTML "$edit")"
126     for time in $timelist; do
127       ktime="$(tkey "$time")"
128       printf '[td  [radio "%s"   "yes"   #yes_%s %s][label   for="yes_%s"   Yes]
129                    [radio "%s"    "no"    #no_%s %s][label    for="no_%s"    No]
130                    [radio "%s" "maybe" #maybe_%s %s][label for="maybe_%s" Maybe]
131               ]' "${ktime}" "${time}" "$(checked "$time"   $yes)" "${time}" \
132                  "${ktime}" "${time}" "$(checked "$time"    $no)" "${time}" \
133                  "${ktime}" "${time}" "$(checked "$time" $maybe)" "${time}"
134     done
135     printf '[td [submit "update" "%s" Update]]]\n' "$(HTML "$edit")"
136   else
137
138     # Vote counts
139     printf '[tr .votecount [td]'
140     for time in $timelist; do
141       yc=0 nc=0 mc=0
142       { DBM "$file" get participants; printf \\n\\n; } |while read -r name; do
143         [ ! "$name" ] && printf '[td %i (%i)]' "$yc" "$((yc + mc))" && break;
144           yes="$(DBM "$file" get "reply_yes_${name}")"
145            no="$(DBM "$file" get "reply_no_${name}")"
146         maybe="$(DBM "$file" get "reply_maybe_${name}")"
147
148         printf %s   "$yes" |grep -qwF "$time" && yc=$((yc + 1)) && continue
149         printf %s "$maybe" |grep -qwF "$time" && mc=$((mc + 1)) && continue
150       done
151     done
152     printf '[td]]\n'
153
154     # Submit line
155     printf '[tr .new [td [input name="name" value="" placeholder="Your Name" autocomplete=off]]'
156     for time in $timelist; do
157       time="$(tkey "$time")"
158       printf '[td  [radio "%s"   "yes"   #yes_%s][label   for="yes_%s"   Yes]
159                    [radio "%s"    "no"    #no_%s][label    for="no_%s"    No]
160                    [radio "%s" "maybe" #maybe_%s][label for="maybe_%s" Maybe]
161               ]' "${time}" "${time}" "${time}" \
162                  "${time}" "${time}" "${time}" \
163                  "${time}" "${time}" "${time}"
164     done
165     printf '[td [submit "new" "new" Submit]]]\n'
166   fi
167
168   printf ']]'
169 }
170
171 if [ "$REQUEST_METHOD" = POST ]; then
172   local update="$(POST update)" delete="$(POST delete)"
173   local name="$(POST name |grep -m 1 -xE '.*[^  ].*')"
174   local splittimes="$(DBM "$file" get splittimes || printf no)"
175   local dates="$(DBM "$file" get dates)"
176   local todall="$(DBM "$file" get todall)"
177   local timelist="$(timelist)"
178   local time yes no maybe reply
179
180   if [ "$(POST new)" = new -o "$update" ]; then
181     [ "$update" ] && name="$update"
182
183     if [ ! "$name" ]; then
184       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NONAME"
185     elif [ ! "$update" ] && DBM "$file" get participants |grep -qxF "$name"; then
186       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMEEXISTS"
187     elif [ "$update" ] && ! DBM "$file" get participants |grep -qxF "$name"; then
188       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
189     fi
190     if [ ! "$update" ]; then
191       DBM "$file" append participants "${BR}${name}" || DBM "$file" insert participants "${name}" \
192       || REDIRECT "${_BASE}${PATH_INFO}#ERROR_DBACCESS"
193     fi
194
195     for time in $timelist; do reply="$(POST "$(tkey "$time")")"; case $reply in
196         yes)   yes="${yes}${yes:+ }${time}";;
197          no)    no="${no}${no:+ }${time}";;
198       maybe) maybe="${maybe}${maybe:+ }${time}";;
199     esac; done
200     DBM "$file" set "reply_yes_${name}" "$yes"
201     DBM "$file" set "reply_no_${name}" "$no"
202     DBM "$file" set "reply_maybe_${name}" "$maybe"
203
204   elif [ "$delete" ]; then
205     if ! DBM "$file" get participants |grep -qxF "$delete"; then
206       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
207     fi
208     DBM "$file" set participants "$(DBM "$file" get participants |grep -vxF "$delete")"
209     DBM "$file" delete "reply_yes_${delete}"
210     DBM "$file" delete "reply_no_${delete}"
211     DBM "$file" delete "reply_maybe_${delete}"
212
213   elif [ "$(POST bookmark)" -a ! "$bookmarks" ]; then
214     SET_COOKIE +$((182 * 86400)) bookmarks="${id}" Path="${_BASE}/"
215
216   fi
217   REDIRECT "${_BASE}${PATH_INFO}"
218   
219 else
220   pagename="$(pagename "$id")"
221
222   yield_page "$pagename" poll <<-EOF
223         [form method=POST
224           [section .description
225             [h1 .title $(HTML "$pagename")]
226             $(DBM "$file" get description |markdown)
227           ]
228           $(table_poll || printf '[p Poll parameters are invalid]')
229           $(if [ "$admin" ]; then
230             printf '[section .bookmark You have bookmarked the admin page of this poll: [a href="./%s" modify poll]]' "$(URL ${id}/${admin})"
231           elif [ "$bookmarks" ]; then
232             printf '[section .bookmark This poll has been bookmarked and is accessible via a link on the front page.]'
233           else
234             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]]'
235           fi)
236         ]
237         EOF
238 fi