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