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