3 # Copyright 2014, 2016, 2019, 2020, 2021 Paul Hänsch
5 # This file is part of Confetti.
7 # Confetti is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # Confetti is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Confetti. If not, see <http://www.gnu.org/licenses/>.
21 . "$_EXEC/session_lock.sh"
22 . "$_EXEC/cgilite/storage.sh"
24 unset filter order card action newfield
25 unset cardfile attfile tempfile
26 unset vcf field cnt delete_key
31 card="$(POST card |PATH)"; card="${card##*/}"
32 cardfile="$_DATA/vcard/${card}"
33 attfile="$_DATA/mappings/attendance"
35 action="$(POST action)"
36 newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
38 if printf '%s\n' "$action" |grep -qxE 'addfield [A-Z][A-Z0-9-]*'; then
39 newfield="${action##* }"
43 if ! tempfile=$(CHECK_SLOCK "$cardfile"); then
44 SET_COOKIE 0 message="NO VALID FILE LOCK"
45 REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
47 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
48 SET_COOKIE 0 message="INVALID TRANSACTION ID"
49 REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
55 printf %s\\n "$each" \
56 | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
58 | sed -E ':X;$!{N;bX}; s;\n;\;;g'
61 # [ "${_POST[hi_select]}" = "list" ] || _POST[hi_company]="${_POST[hi_other]}"
62 # [ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
63 # && _POST[X-HEALTH-INSURANCE]="$(vcf_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")"
65 vcf="$(pdi_load "$tempfile")"
67 n1="$(POST 1N)" n2="$(POST 2N)" n3="$(POST 3N)" n4="$(POST 4N)" n5="$(POST 5N)"
68 # 3N (Middle Names) is not actually used
71 vcf="$(pdi_update_value "$vcf" N 1 "$(vcf_escape "$n1" "${n2%% *}" "${n3# }" "$n4" "$n5")")"
72 vcf="$(pdi_update_value "$vcf" FN 1 "$(vcf_escape "$n4 $n2 $n1 $n5" |sed -E 's;(^ +| +$);;g; s; +; ;g;')")"
73 vcf="$(printf '%s\n' "$vcf" |sed -E "/^CATEGORIES;[^:]*:.*$/d")"
75 for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
76 for cnt in $(seq 1 $(POST_COUNT "$field")); do
79 # printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(vcf_escape "$(POST "$field" "$cnt")")"
82 vcf="$(pdi_update_attrib "$vcf" TEL $cnt TYPE="$(POST teltype $cnt |grep -Exm1 'HOME|WORK|CELL|FAX')")"
83 vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
86 vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
91 # delete fields, first mark for deletion using delete_key
92 # this way the field enumeration is preserved during the process
93 # finally filter marked lines
94 delete_key="$(randomid)"
95 for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
96 f="${delete%%_*}"; c="${delete##*_}";
97 [ "$(POST "$delete")" = "true" ] && vcf="$(pdi_update_value "$vcf" "$f" "$c" "delete=${delete_key}")"
99 vcf="$(printf '%s\n' "$vcf" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
101 if [ "$action" = addfield ]; then
102 vcf="$(pdi_update_value "$vcf" "$newfield" $(( $(pdi_count "$vcf" "$newfield") + 1 )) '')"
104 printf '%s' "$vcf" |grep -vx '' >"$tempfile"
108 REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
111 if LOCK "$attfile"; then
112 grep -F " ${card}" "$attfile" |while read course junk; do
113 touch "$_DATA/ical/${course}"
115 sed -i -E "/^.+ ${card}\$/d" "$attfile"
116 seq 1 $(POST_COUNT attendance) |while read n; do
117 printf '%s %s\n' "$(POST attendance $n)" "$card"
119 grep -F " ${card}" "$attfile" |while read course junk; do
120 touch "$_DATA/ical/${course}"
124 SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS"
127 cp "$tempfile" "$cardfile"
128 RELEASE_SLOCK "$cardfile"
129 REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}#${card}"
132 RELEASE_SLOCK "$cardfile"
134 && REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}#${card}" \
135 || REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}"
139 RELEASE_SLOCK "$cardfile"
140 if LOCK "$attfile"; then
141 grep -F " ${card}" "$attfile" |while read course junk; do
142 touch "$_DATA/ical/${course}"
144 sed -i -E "/^.+ ${card}\$/d" "$attfile"
147 SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS"
149 REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}"