]> git.plutz.net Git - confetti/blobdiff - actions/update_card.sh
generate name field from available information
[confetti] / actions / update_card.sh
index 2250f1967abc5edb8a66c4b83ddc73527b27b437..f18c9ee3e659832c2da4e568a967385188220bf9 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.
 # 
@@ -29,32 +29,38 @@ 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:;: :')"
+_POST[0N]="${_POST[0N]//;/,}"
+_POST[1N]="${_POST[1N]//;/,}"
+_POST[2N]="${_POST[2N]//;/,}"
+_POST[3N]="${_POST[3N]//;/,}"
+_POST[4N]="${_POST[4N]//;/,}"
 
-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"
+[ "$_POST[hi_select]" = "list" ] && hi_company="${_POST[hi_company]}" || hi_company="${_POST[hi_other]}"
+[ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
+&& _POST[X-HEALTH-INSURANCE]="${hi_company//;/,};${_POST[hi_number]//;/,};${_POST[hi_status]//;/,}"
+
+sed -r 's;$;\r;' >"$tempfile" <<EOF
+BEGIN:VCARD
+VERSION:4.0
+N:${_POST[0N]};${_POST[1N]};${_POST[2N]};${_POST[3N]};${_POST[4N]}
+UID:${_POST[UID]}
+$(
 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))
+  for key in $field $field{0..100}; do
+    [ -z "${_POST[$key]+x}" ] && break
+    value="$(printf %s "$_POST[$key]" |sed -r ':a;N;$!ba;s:\n:\\n:g;s:\r:\\r:g')"
+    [ "${key%%[0-9]*}" = TEL ] && tag="TYPE=${_POST[phonetype${key#TEL}]}" || tag=''
+    [ -n "$value" ] && printf '%s:%s\n' "${field}${tag:+;$tag}" "$value"
   done
-done >>"$tempfile"
+done
+[ "${_POST[action]}" = addfield ] && printf '%s:\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 +78,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