]> git.plutz.net Git - confetti/blobdiff - cards/update_card.sh
drop pasted card info from course functions
[confetti] / cards / update_card.sh
index 1975e4ea2aa66afa8bf5851965d7680ebb402118..34db10cfb740bdf23493b1590b221c8167b3581a 100755 (executable)
 . "$_EXEC/pdiread.sh"
 . "$_EXEC/session_lock.sh"
 
+unset filter order card action newfield
+unset cardfile attfile tempfile
+unset vcf field cnt delete_key
+
 filter="$(REF f)"
 order="$(REF o)"
 
-card="$(POST card)"
-cardfile="$_DATA/vcard/$card"
+card="$(POST card |PATH)"
+cardfile="$_DATA/vcard/${card##*/}"
 attfile="$_DATA/mappings/attendance"
 
 action="$(POST action)"
-newfield="$(POST newfield)"
+newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
+
+if printf '%s\n' "$action" |grep -qxE 'addfield [A-Z][A-Z0-9]*'; then
+  newfield="${action##* }"
+  action=addfield
+fi
 
 if ! tempfile=$(CHECK_SLOCK "$cardfile"); then
   SET_COOKIE 0 message="NO VALID FILE LOCK"
@@ -72,6 +81,16 @@ for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
     esac
 done; done
 
+# delete fields, first mark for deletion using delete_key
+# this way the field enumeration is preserved during the process
+# finally filter marked lines
+delete_key="$(randomid)"
+for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
+  f="${delete%%_*}"; c="${delete##*_}";
+  [ "$(POST "$delete")" = "true" ] && vcf="$(pdi_update_value "$vcf" "$f" "$c" "delete=${delete_key}")"
+done
+vcf="$(printf '%s\n' "$vcf" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
+
 if [ "$action" = addfield ]; then
   vcf="$(pdi_update_value "$vcf" "$newfield" $(( $(pdi_count "$vcf" "$newfield") + 1 )) '')"
 fi