]> git.plutz.net Git - confetti/blob - actions/update_card.sh
apply tex escaping - typo
[confetti] / actions / update_card.sh
1 #!/bin/zsh
2
3 # Copyright 2014, 2016 Paul Hänsch
4 #
5 # This file is part of Confetti.
6
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.
11
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.
16
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/>. 
19
20 cgi_refdata
21
22 filter="&filter=${_REF[filter]}"
23 filtertype="&filtertype=${_REF[filtertype]}"
24 order="&order=${_REF[order]}"
25
26 card="${_POST[card]}"
27 tempfile="$_DATA/temp/$card"
28 cardfile="$_DATA/vcard/$card"
29 attfile="$_DATA/mappings/attendance"
30
31 vcf_escape(){
32   for each in "$@"; do
33     printf %s\\n "$each" \
34     | sed -r ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
35   done \
36   | sed -r ':X;$!{N;bX}; s;\n;\;;g'
37 }
38
39 [ "${_POST[hi_select]}" = "list" ] || _POST[hi_company]="${_POST[hi_other]}"
40 [ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
41 && _POST[X-HEALTH-INSURANCE]="$(vcf_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")"
42
43 sed -r 's;$;\r;' >"$tempfile" <<EOF
44 BEGIN:VCARD
45 VERSION:4.0
46 N:$(vcf_escape "${_POST[0N]}" "${_POST[1N]}" "${_POST[2N]}" "${_POST[3N]}" "${_POST[4N]}")
47 UID:${_POST[UID]}
48 $(
49   for field in $VCF_FIELDS; do for key in $field $field{0..100}; do
50     [ -z "${_POST[$key]+x}" ] && break
51     [ -z "${_POST[$key]}" ] && continue
52     case "$key" in
53       (TEL[0-9]*)
54         printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(vcf_escape "${_POST[$key]}")"
55         ;;
56       (X-HEALTH-INSURANCE)
57         printf '%s:%s\r\n' "${field}" "${_POST[$key]}"
58         ;;
59       (*)
60         printf '%s:%s\r\n' "${field}" "$(vcf_escape "${_POST[$key]}")"
61         ;;
62     esac
63   done; done
64   [ "${_POST[action]}" = addfield ] && printf '%s:\r\n' "${_POST[newfield]}"
65 )
66 END:VCARD
67 EOF
68
69 case "${_POST[action]}" in
70   addfield)
71     redirect "?p=cards${filter}${filtertype}${order}&edit=$card"
72     ;;
73   update)
74     attendance=()
75     for att in attendance attendance{0..100}; do
76       [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
77     done
78     sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
79       touch "$_DATA/ical/$course"
80     done
81     sed -i -r '/^(.+)\t'$card'$/d' "$attfile"
82     for each in $attendance; do
83       echo "$each\t$card"
84     done >>"$attfile"
85     sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
86       touch "$_DATA/ical/$course"
87     done
88
89     mv "$tempfile" "$cardfile"
90     redirect "?p=cards${filter}${filtertype}${order}#$card"
91     ;;
92   cancel)
93     rm "$tempfile"
94     [ -f "$cardfile" ] \
95     && redirect "?p=cards${filter}${filtertype}${order}#$card" \
96     || redirect "?p=cards${filter}${filtertype}${order}"
97     ;;
98   delete)
99     rm "$tempfile" "$cardfile"
100     redirect "?p=cards${filter}${filtertype}${order}"
101     ;;
102 esac