]> git.plutz.net Git - confetti/blobdiff - actions/update_card.sh
unify client and attendee editing templates
[confetti] / actions / update_card.sh
index 2250f1967abc5edb8a66c4b83ddc73527b27b437..a1143c3d72057359ef7b42a07c919c3d21ef33f4 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/zsh
 
-# Copyright 2014 Paul Hänsch
+# Copyright 2014, 2016 Paul Hänsch
 #
 # This file is part of Confetti.
 # 
@@ -17,7 +17,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with Confetti.  If not, see <http://www.gnu.org/licenses/>. 
 
-cgi_post
 cgi_refdata
 
 filter="&filter=${_REF[filter]}"
@@ -29,32 +28,47 @@ tempfile="$_DATA/temp/$card"
 cardfile="$_DATA/vcard/$card"
 attfile="$_DATA/mappings/attendance"
 
-_POST[0N]="$(echo "${_POST[0N]}" |sed 's:;: :')"
-_POST[1N]="$(echo "${_POST[1N]}" |sed 's:;: :')"
-_POST[2N]="$(echo "${_POST[2N]}" |sed 's:;: :')"
-_POST[3N]="$(echo "${_POST[3N]}" |sed 's:;: :')"
-_POST[4N]="$(echo "${_POST[4N]}" |sed 's:;: :')"
+vcf_escape(){
+  for each in "$@"; do
+    printf %s\\n "$each" \
+    | sed -r ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
+  done \
+  | sed -r ':X;$!{N;bX}; s;\n;\;;g'
+}
 
-echo "BEGIN:VCARD\r" >"$tempfile"
-echo "VERSION:4.0\r" >>"$tempfile"
-echo "N:${_POST[0N]};${_POST[1N]};${_POST[2N]};${_POST[3N]};${_POST[4N]}\r" >>"$tempfile"
-echo "UID:${_POST[UID]}\r" >>"$tempfile"
-for field in $VCF_FIELDS; do
-  value="${_POST[$field]}"
-  n=0
-  while [ -n "$value" ]; do
-    value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')"
-    echo "${field}:${value}\r"
-    value="${_POST[$field$n]}"
-    n=$(($n + 1))
-  done
-done >>"$tempfile"
+[ "${_POST[hi_select]}" = "list" ] || _POST[hi_company]="${_POST[hi_other]}"
+[ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
+&& _POST[X-HEALTH-INSURANCE]="$(vcf_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")"
+
+sed -r 's;$;\r;' >"$tempfile" <<EOF
+BEGIN:VCARD
+VERSION:4.0
+N:$(vcf_escape "${_POST[0N]}" "${_POST[1N]}" "${_POST[2N]}" "${_POST[3N]}" "${_POST[4N]}")
+UID:${_POST[UID]}
+$(
+  for field in $VCF_FIELDS; do for key in $field $field{0..100}; do
+    [ -z "${_POST[$key]+x}" ] && break
+    [ -z "${_POST[$key]}" ] && continue
+    case "$key" in
+      (TEL[0-9]*)
+        printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(vcf_escape "${_POST[$key]}")"
+        ;;
+      (X-HEALTH-INSURANCE)
+        printf '%s:%s\r\n' "${field}" "${_POST[$key]}"
+        ;;
+      (*)
+        printf '%s:%s\r\n' "${field}" "$(vcf_escape "${_POST[$key]}")"
+        ;;
+    esac
+  done; done
+  [ "${_POST[action]}" = addfield ] && printf '%s:\r\n' "${_POST[newfield]}"
+)
+END:VCARD
+EOF
 
 case "${_POST[action]}" in
   addfield)
-    echo "${_POST[newfield]}:\r" >>"$tempfile"
-    echo "END:VCARD\r" >>"$tempfile"
-    echo -n "Location: ?p=cards${filter}${filtertype}${order}&edit=$card\n\n"
+    redirect "?p=cards${filter}${filtertype}${order}&edit=$card"
     ;;
   update)
     attendance=()
@@ -72,18 +86,17 @@ case "${_POST[action]}" in
       touch "$_DATA/ical/$course"
     done
 
-    echo "END:VCARD\r" >>"$tempfile"
     mv "$tempfile" "$cardfile"
-    echo -n "Location: ?p=cards${filter}${filtertype}${order}#$card\n\n"
+    redirect "?p=cards${filter}${filtertype}${order}#$card"
     ;;
   cancel)
     rm "$tempfile"
     [ -f "$cardfile" ] \
-      && echo -n "Location: ?p=cards${filter}${filtertype}${order}#$card\n\n" \
-      || echo -n "Location: ?p=cards${filter}${filtertype}${order}\n\n"
+    && redirect "?p=cards${filter}${filtertype}${order}#$card" \
+    || redirect "?p=cards${filter}${filtertype}${order}"
     ;;
   delete)
     rm "$tempfile" "$cardfile"
-    echo -n "Location: ?p=cards${filter}${filtertype}${order}\n\n"
+    redirect "?p=cards${filter}${filtertype}${order}"
     ;;
 esac