X-Git-Url: https://git.plutz.net/?a=blobdiff_plain;f=cards%2Fupdate_card.sh;h=84d9c7e9b310662d403a9eb495dd8248a5796f54;hb=0aa4aa6d57b76d6ca32eb502e422b5f02bed3402;hp=ee9ac3eb295cb7e524714dc1550dab82f16b524d;hpb=a8d370f09eb54c50759fa6b92c2c50ffc4b4c604;p=lobster diff --git a/cards/update_card.sh b/cards/update_card.sh index ee9ac3e..84d9c7e 100755 --- a/cards/update_card.sh +++ b/cards/update_card.sh @@ -20,13 +20,25 @@ . "$_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 |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" REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}" @@ -37,40 +49,65 @@ elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then exit 0 fi -vcf_escape(){ - for each in "$@"; do - printf %s\\n "$each" \ - | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;' - done \ - | sed -E ':X;$!{N;bX}; s;\n;\;;g' -} - # [ "${_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]}")" +# && _POST[X-HEALTH-INSURANCE]="$(pdi_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")" -vcf="$(pdi_load "$cardfile")" +# vcf="$(pdi_load "$cardfile")" +vcf="$(pdi_load "$tempfile")" -vcf="$(pdi_update_value "$vcf" N 1 "$(vcf_escape "$(POST 1N)" "$(POST 2N)" "$(POST 3N)" "$(POST 4N)" "$(POST 5N)")")" +vcf="$(pdi_update_value "$vcf" N 1 "$(pdi_escape "$(POST 1N)" "$(POST 2N)" "$(POST 3N)" "$(POST 4N)" "$(POST 5N)")")" +vcf="$(pdi_update_value "$vcf" FN 1 "$(pdi_escape "$(POST 4N) $(POST 2N) $(POST 3N) $(POST 1N) $(POST 5N)" \ + | sed -E 's;^ +;;; s; +$;;; s; +; ;g;')" )" for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do for cnt in $(seq 1 $(POST_COUNT "$field")); do case "$field" in # (TEL) - # printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(vcf_escape "$(POST "$field" "$cnt")")" + # printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(pdi_escape "$(POST "$field" "$cnt")")" # ;; - (*) - vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(POST "$field" "$cnt")")" + X-HEALTH-INSURANCE) + hi_select="$(POST "$field" "$cnt")" + if [ "$hi_select" = list ]; then + vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "hi_company" "$cnt")" \ + "$(POST "hi_number" "$cnt")" \ + "$(POST "hi_status" "$cnt")" \ + )")" + elif [ "$hi_select" = other ]; then + vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "hi_other" "$cnt")" \ + "$(POST "hi_number" "$cnt")" \ + "$(POST "hi_status" "$cnt")" \ + )")" + fi + ;; + TEL) + vcf="$(pdi_update_attrib "$vcf" TEL $cnt TYPE="$(POST teltype $cnt |grep -Exm1 'HOME|WORK|CELL|FAX')")" + vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "$field" "$cnt")")")" + ;; + *) + vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(pdi_escape "$(POST "$field" "$cnt")")")" ;; esac done; done -[ "$(POST action)" = addfield ] && vcf="$(vcf_update_field "$vcf" "$(POST newfield)" $(($(pdi_count $(POST newfield)) + 1)) '')" -printf '%s' "$vcf" >"$tempfile" +# 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 +printf '%s' "$vcf" | sed -E '/^$/d; s/^([^:]+);:/\1:/;' >"$tempfile" -case "$(POST action)" in +case "$action" in addfield) - REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}" + REDIRECT "/cards/?o=${order}&f=${filter}&e=${card#/}" ;; update) # attendance=() @@ -90,12 +127,12 @@ case "$(POST action)" in cp "$tempfile" "$cardfile" RELEASE_SLOCK "$cardfile" - REDIRECT "/cards/?o=${order}&f=${filter}#${card}" + REDIRECT "/cards/?o=${order}&f=${filter}#${card#/}" ;; cancel) RELEASE_SLOCK "$cardfile" [ -f "$cardfile" ] \ - && REDIRECT "/cards/?o=${order}&f=${filter}#${card}" \ + && REDIRECT "/cards/?o=${order}&f=${filter}#${card#/}" \ || REDIRECT "/cards/?o=${order}&f=${filter}" ;; delete)