From debd0bd0d774c6151a8fbf6759778e2e0757c8bd Mon Sep 17 00:00:00 2001
From: paul <paul@plutz.net>
Date: Sun, 1 Dec 2013 04:29:07 +0000
Subject: [PATCH] keep filter and order settings when updating a card

svn path=/trunk/; revision=28
---
 actions/edit_attendee.sh   | 11 +++++++++++
 actions/new_attendee.sh    |  7 ++++++-
 actions/update_attendee.sh | 15 ++++++++++-----
 cgi.sh                     |  9 +++++++++
 templates/view_attendee.sh |  2 +-
 5 files changed, 37 insertions(+), 7 deletions(-)
 create mode 100644 actions/edit_attendee.sh

diff --git a/actions/edit_attendee.sh b/actions/edit_attendee.sh
new file mode 100644
index 0000000..9a5bcb4
--- /dev/null
+++ b/actions/edit_attendee.sh
@@ -0,0 +1,11 @@
+#!/bin/zsh
+
+cgi_refdata
+
+card="$_GET[\"card\"]"
+debug card: $card
+filter="&filter=$_REF[\"filter\"]"
+filtertype="&filtertype=$_REF[\"filtertype\"]"
+order="&order=$_REF[\"order\"]"
+
+echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}&edit=$card\n\n"
diff --git a/actions/new_attendee.sh b/actions/new_attendee.sh
index b7242bf..d1eb77a 100644
--- a/actions/new_attendee.sh
+++ b/actions/new_attendee.sh
@@ -1,6 +1,11 @@
 #!/bin/zsh
 
 cgi_post
+cgi_refdata
+
+filter="&filter=$_REF[\"filter\"]"
+filtertype="&filtertype=$_REF[\"filtertype\"]"
+order="&order=$_REF[\"order\"]"
 
 uid=$(dbus-uuidgen)
 card="${uid}.vcf"
@@ -20,4 +25,4 @@ UID:$uid
 END:VCARD
 EOF
 
-echo -n "Location: http://$HTTP_HOST/?page=attendees&edit=$card\n\n"
+echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}&edit=$card\n\n"
diff --git a/actions/update_attendee.sh b/actions/update_attendee.sh
index 6bfbe88..9657d05 100644
--- a/actions/update_attendee.sh
+++ b/actions/update_attendee.sh
@@ -1,6 +1,11 @@
 #!/bin/zsh
 
 cgi_post
+cgi_refdata
+
+filter="&filter=$_REF[\"filter\"]"
+filtertype="&filtertype=$_REF[\"filtertype\"]"
+order="&order=$_REF[\"order\"]"
 
 card="$_POST[\"card\"]"
 tempfile="temp/$card"
@@ -24,21 +29,21 @@ case "$_POST[\"action\"]" in
   addfield)
     echo "$_POST[\"newfield\"]:\r" >>"$tempfile"
     echo "END:VCARD\r" >>"$tempfile"
-    echo -n "Location: http://$HTTP_HOST/?page=attendees&edit=$card\n\n"
+    echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}&edit=$card\n\n"
     ;;
   update)
     echo "END:VCARD\r" >>"$tempfile"
     mv "$tempfile" "$cardfile"
-    echo -n "Location: http://$HTTP_HOST/?page=attendees#$card\n\n"
+    echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}#$card\n\n"
     ;;
   cancel)
     rm "$tempfile"
     [ -f "$cardfile" ] \
-      && echo -n "Location: http://$HTTP_HOST/?page=attendees#$card\n\n" \
-      || echo -n "Location: http://$HTTP_HOST/?page=attendees\n\n"
+      && echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}#$card\n\n" \
+      || echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}\n\n"
     ;;
   delete)
     rm "$tempfile" "$cardfile"
-    echo -n "Location: http://$HTTP_HOST/?page=attendees\n\n"
+    echo -n "Location: http://$HTTP_HOST/?page=attendees${filter}${filtertype}${order}\n\n"
     ;;
 esac
diff --git a/cgi.sh b/cgi.sh
index bde75d6..39172ba 100755
--- a/cgi.sh
+++ b/cgi.sh
@@ -2,6 +2,7 @@
 
 declare -A _GET
 declare -A _POST
+declare -A _REF
 
 cgi_get() {  # parse HTTP GET string
   echo "$QUERY_STRING" |tr '&' '\n' |while read query; do
@@ -25,3 +26,11 @@ cgi_post() {  # parse HTTP POST string
     fi
   done
 }
+
+cgi_refdata() { # Parse GET data from referer
+  echo "$HTTP_REFERER" |cut -d'?' -f2- |tr '&' '\n' |while read query; do
+    key="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')"
+    val="$(echo "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')"
+    _REF["$key"]="$(echo -e "$(echo "$val" |sed 's:+: :g;s:%:\\x:g')")"
+  done
+}
diff --git a/templates/view_attendee.sh b/templates/view_attendee.sh
index 1bfccee..53c508c 100755
--- a/templates/view_attendee.sh
+++ b/templates/view_attendee.sh
@@ -59,7 +59,7 @@ done
 echo '  </div>'
 echo ''
 echo '  <div class="section control">'
-echo '    <a class="item control" href="?page=attendees&edit='${id}'">'$(l10n edit)'</a>'
+echo '    <a class="item control" href="?action=edit_attendee&card='${id}'">'$(l10n edit)'</a>'
 echo '    <a class="item control" href="?export=vcard&card='${id}'">'$(l10n vcf_export)'</a>'
 echo '  </div>'
 echo '</div>'
-- 
2.39.5