]> git.plutz.net Git - webpoll/blobdiff - poll.sh
reset table padding from updated cgilite style
[webpoll] / poll.sh
diff --git a/poll.sh b/poll.sh
index 74fa3e11a36cb5850fd94b07e8cdb37932feba01..8e55600ed85ecabf7b766e88f6e930cbd415c64a 100644 (file)
--- a/poll.sh
+++ b/poll.sh
@@ -1,10 +1,25 @@
 #!/bin/sh
 
+. "${_EXEC}/comments.sh"
+. "${_EXEC}/languages.sh"
+
 id="$(checkid "${PATH_INFO#/}")"
 file="${_DATA}/${id}"
 
 #cancel if poll is invalid
-[ "$id" -a -f "$file" ] || REDIRECT "$_BASE/"
+if [ ! "$id" -o ! -f "$file" ]; then
+  page_home;
+  return 0
+fi
+
+if expr match "$bookmarks" ".*${id}.*" >/dev/null; then
+  :
+elif [ "$bookmarks" ]; then
+  SET_COOKIE +$((182 * 86400)) bookmarks="${bookmarks} ${id}" Path="${_BASE}/"
+fi
+
+admin="$(expr match "$bookmarks" ".*$id/\([a-zA-Z0-9:=]\{16\}\).*")"
+admin="$(DBM "$file" get adminkey |grep -xF "$admin")"
 
 tkey() {
   # convert time stamps for use in POST keys
@@ -57,7 +72,10 @@ table_poll() {
   local dates="$(DBM "$file" get dates)"
   local todall="$(DBM "$file" get todall)"
   local timelist="$(timelist)"
-  local time date span name
+  local edit="$(GET edit)"
+  local time date span name yes no maybe yc nc mc
+
+  table_date="${table_date:-"%A <br/> %B %_d, %Y"}"
 
   [ "$timelist" ] || return 1
 
@@ -69,7 +87,7 @@ table_poll() {
       span=0; for time in $timelist; do case $time in
         ${date}*) span=$((span + 1));;
       esac; done
-      date -d "$date" +"[th colspan=\"${span}\" . %A <br/> %B %_d, %Y]";
+      date -d "$date" +"[th colspan=\"${span}\" . ${table_date}]";
     done
     printf '[th]]\n'
   fi
@@ -78,18 +96,24 @@ table_poll() {
   if [ "$splittimes" = yes -o "$todall" ]; then
     printf '[tr .tod [th]'
     for time in $timelist; do
-      [ "${time#*_}" = "${time}" ] && time="${time}_"
-      printf '[th . %s]' "${time#*_}"
+      case $time in
+       *-*-*_*:*) time="${time#*_}";;
+       *-*-*) time="";;
+       *:*);;  # time="${time}"
+      esac
+      printf '[th . %s]' "${time}"
     done
     printf '[th]]\n'
   fi
 
   printf '][tbody\n'
 
+  # Vote displays
   { DBM "$file" get participants; printf \\n; } |while read -r name; do
+    [ "$name" = "$edit" ] && continue
+    maybe="$(DBM "$file" get "reply_maybe_${name}")"
       yes="$(DBM "$file" get "reply_yes_${name}")"
        no="$(DBM "$file" get "reply_no_${name}")"
-    maybe="$(DBM "$file" get "reply_maybe_${name}")"
 
     printf '[tr [th .name . %s]' "$(HTML "$name")"
     for time in $timelist; do
@@ -98,26 +122,62 @@ table_poll() {
       printf %s "$maybe" |grep -qwF "$time" && printf '[td .maybe Maybe]' && continue
       printf '[td .missing . ?]'
     done
-    printf '[td]]'
+    printf '[td .edit [a href="?edit=%s" Edit]]]\n' "$(URL "$name")"
   done
 
-  # Submit line
-  printf '[tr .new [td [input name="name" value="" placeholder="Your Name" autocomplete=off]]'
-  for time in $timelist; do
-    time="$(tkey "$time")"
-    printf '[td  [radio "%s"   "yes"   #yes_%s][label   for="yes_%s"   Yes]
-                 [radio "%s"    "no"    #no_%s][label    for="no_%s"    No]
-                 [radio "%s" "maybe" #maybe_%s][label for="maybe_%s" Maybe]
-            ]' "${time}" "${time}" "${time}" \
-               "${time}" "${time}" "${time}" \
-               "${time}" "${time}" "${time}"
-  done
-  printf '[td [submit "new" "new" Submit]]]\n'
+  if [ "$edit" ]; then
+    maybe="$(DBM "$file" get "reply_maybe_${edit}")"
+      yes="$(DBM "$file" get "reply_yes_${edit}")"
+       no="$(DBM "$file" get "reply_no_${edit}")"
+
+    printf '[tr .new [th .name [submit "delete" "%s" -] %s]' "$(HTML "$edit")" "$(HTML "$edit")"
+    for time in $timelist; do
+      ktime="$(tkey "$time")"
+      printf '[td  [radio "%s"   "yes"   #yes_%s %s][label   for="yes_%s"   Yes]
+                   [radio "%s"    "no"    #no_%s %s][label    for="no_%s"    No]
+                   [radio "%s" "maybe" #maybe_%s %s][label for="maybe_%s" Maybe]
+              ]' "${ktime}" "${time}" "$(checked "$time"   $yes)" "${time}" \
+                 "${ktime}" "${time}" "$(checked "$time"    $no)" "${time}" \
+                 "${ktime}" "${time}" "$(checked "$time" $maybe)" "${time}"
+    done
+    printf '[td [submit "update" "%s" Update]]]\n' "$(HTML "$edit")"
+  else
+
+    # Vote counts
+    printf '[tr .votecount [td]'
+    for time in $timelist; do
+      yc=0 nc=0 mc=0
+      { DBM "$file" get participants; printf \\n\\n; } |while read -r name; do
+        [ ! "$name" ] && printf '[td %i (%i)]' "$yc" "$((yc + mc))" && break;
+          yes="$(DBM "$file" get "reply_yes_${name}")"
+           no="$(DBM "$file" get "reply_no_${name}")"
+        maybe="$(DBM "$file" get "reply_maybe_${name}")"
+
+        printf %s   "$yes" |grep -qwF "$time" && yc=$((yc + 1)) && continue
+        printf %s "$maybe" |grep -qwF "$time" && mc=$((mc + 1)) && continue
+      done
+    done
+    printf '[td]]\n'
+
+    # Submit line
+    printf '[tr .new [td [input name="name" value="" placeholder="Your Name" autocomplete=off]]'
+    for time in $timelist; do
+      time="$(tkey "$time")"
+      printf '[td  [radio "%s"   "yes"   #yes_%s][label   for="yes_%s"   Yes]
+                   [radio "%s"    "no"    #no_%s][label    for="no_%s"    No]
+                   [radio "%s" "maybe" #maybe_%s][label for="maybe_%s" Maybe]
+              ]' "${time}" "${time}" "${time}" \
+                 "${time}" "${time}" "${time}" \
+                 "${time}" "${time}" "${time}"
+    done
+    printf '[td [submit "new" "new" Submit]]]\n'
+  fi
 
   printf ']]'
 }
 
 if [ "$REQUEST_METHOD" = POST ]; then
+  local update="$(POST update)" delete="$(POST delete)"
   local name="$(POST name |grep -m 1 -xE '.*[^         ].*')"
   local splittimes="$(DBM "$file" get splittimes || printf no)"
   local dates="$(DBM "$file" get dates)"
@@ -125,14 +185,20 @@ if [ "$REQUEST_METHOD" = POST ]; then
   local timelist="$(timelist)"
   local time yes no maybe reply
 
-  if [ "$(POST new)" = new ]; then
+  if [ "$(POST new)" = new -o "$update" ]; then
+    [ "$update" ] && name="$update"
+
     if [ ! "$name" ]; then
       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NONAME"
-    elif DBM "$file" get participants |grep -qxF "$name"; then
+    elif [ ! "$update" ] && DBM "$file" get participants |grep -qxF "$name"; then
       REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMEEXISTS"
+    elif [ "$update" ] && ! DBM "$file" get participants |grep -qxF "$name"; then
+      REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
+    fi
+    if [ ! "$update" ]; then
+      DBM "$file" append participants "${BR}${name}" || DBM "$file" insert participants "${name}" \
+      || REDIRECT "${_BASE}${PATH_INFO}#ERROR_DBACCESS"
     fi
-    DBM "$file" append participants "${BR}${name}" || DBM "$file" insert participants "${name}" \
-    || REDIRECT "${_BASE}${PATH_INFO}#ERROR_DBACCESS"
 
     for time in $timelist; do reply="$(POST "$(tkey "$time")")"; case $reply in
         yes)   yes="${yes}${yes:+ }${time}";;
@@ -142,19 +208,44 @@ if [ "$REQUEST_METHOD" = POST ]; then
     DBM "$file" set "reply_yes_${name}" "$yes"
     DBM "$file" set "reply_no_${name}" "$no"
     DBM "$file" set "reply_maybe_${name}" "$maybe"
-    REDIRECT "${_BASE}${PATH_INFO}"
+
+  elif [ "$delete" ]; then
+    if ! DBM "$file" get participants |grep -qxF "$delete"; then
+      REDIRECT "${_BASE}${PATH_INFO}#ERROR_NAMENONEXIST"
+    fi
+    DBM "$file" set participants "$(DBM "$file" get participants |grep -vxF "$delete")"
+    DBM "$file" delete "reply_yes_${delete}"
+    DBM "$file" delete "reply_no_${delete}"
+    DBM "$file" delete "reply_maybe_${delete}"
+
+  elif [ "$(POST bookmark)" -a ! "$bookmarks" ]; then
+    SET_COOKIE +$((182 * 86400)) bookmarks="${id}" Path="${_BASE}/"
+
   fi
+  REDIRECT "${_BASE}${PATH_INFO}"
   
 else
   pagename="$(pagename "$id")"
 
   yield_page "$pagename" poll <<-EOF
-       [form method=POST
+       [main
          [section .description
            [h1 .title $(HTML "$pagename")]
            $(DBM "$file" get description |markdown)
          ]
-         $(table_poll || printf '[p Poll parameters are invalid]')
+         [form method=POST
+           $(table_poll || printf '[p Poll parameters are invalid]')
+
+            $(if [ "$admin" ]; then
+              printf '[section .bookmark You have bookmarked the admin page of this poll: [a href="./%s" modify poll]]' "$(URL ${id}/${admin})"
+            elif [ "$bookmarks" ]; then
+              printf '[section .bookmark This poll has been bookmarked and is accessible via a link on the front page.]'
+            else
+              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]]'
+            fi)
+         ]
+
+          $(w_comments)
        ]
        EOF
 fi