]> git.plutz.net Git - confetti/blob - actions/update_card.sh
simplified vcf parser, enabled field for health insurance
[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_post
21 cgi_refdata
22
23 filter="&filter=${_REF[filter]}"
24 filtertype="&filtertype=${_REF[filtertype]}"
25 order="&order=${_REF[order]}"
26
27 card="${_POST[card]}"
28 tempfile="$_DATA/temp/$card"
29 cardfile="$_DATA/vcard/$card"
30 attfile="$_DATA/mappings/attendance"
31
32 _POST[0N]="${_POST[0N]//;/,}"
33 _POST[1N]="${_POST[1N]//;/,}"
34 _POST[2N]="${_POST[2N]//;/,}"
35 _POST[3N]="${_POST[3N]//;/,}"
36 _POST[4N]="${_POST[4N]//;/,}"
37 [ -n "${_POST[hi_number]}" -o -n "${_POST[hi_company]}" ] \
38 && _POST[X-HEALTH-INSURANCE]="${_POST[hi_number]//;/,};${_POST[hi_company]//;/,}"
39
40 sed -r 's;$;\r;' >"$tempfile" <<EOF
41 BEGIN:VCARD
42 VERSION:4.0
43 N:${_POST[0N]};${_POST[1N]};${_POST[2N]};${_POST[3N]};${_POST[4N]}
44 UID:${_POST[UID]}
45 $(
46 for field in $VCF_FIELDS; do
47   for key in $field $field{0..100}; do
48     [ -z "$_POST[$key]" ] && break
49     value="$(printf %s "$_POST[$key]" |sed -r ':a;N;$!ba;s:\n:\\n:g;s:\r:\\r:g')"
50     printf '%s:%s\n' "${field}" "$value"
51   done
52 done
53 [ "${_POST[action]}" = addfield ] && printf '%s:\n' "${_POST[newfield]}"
54 )
55 END:VCARD
56 EOF
57
58 case "${_POST[action]}" in
59   addfield)
60     redirect "?p=cards${filter}${filtertype}${order}&edit=$card"
61     ;;
62   update)
63     attendance=()
64     for att in attendance attendance{0..100}; do
65       [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
66     done
67     sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
68       touch "$_DATA/ical/$course"
69     done
70     sed -i -r '/^(.+)\t'$card'$/d' "$attfile"
71     for each in $attendance; do
72       echo "$each\t$card"
73     done >>"$attfile"
74     sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
75       touch "$_DATA/ical/$course"
76     done
77
78     mv "$tempfile" "$cardfile"
79     redirect "?p=cards${filter}${filtertype}${order}#$card"
80     ;;
81   cancel)
82     rm "$tempfile"
83     [ -f "$cardfile" ] \
84     && redirect "?p=cards${filter}${filtertype}${order}#$card" \
85     || redirect "?p=cards${filter}${filtertype}${order}"
86     ;;
87   delete)
88     rm "$tempfile" "$cardfile"
89     redirect "?p=cards${filter}${filtertype}${order}"
90     ;;
91 esac