]> git.plutz.net Git - confetti/blob - cards/update_card.sh
basic support for telephone type
[confetti] / cards / update_card.sh
1 #!/bin/zsh
2
3 # Copyright 2014, 2016, 2019 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 . "$_EXEC/pdiread.sh"
21 . "$_EXEC/session_lock.sh"
22
23 filter="$(REF f)"
24 order="$(REF o)"
25
26 card="$(POST card)"
27 cardfile="$_DATA/vcard/$card"
28 attfile="$_DATA/mappings/attendance"
29
30 if ! tempfile=$(CHECK_SLOCK "$cardfile"); then
31   SET_COOKIE 0 message="NO VALID FILE LOCK"
32   REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
33   exit 0
34 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
35   SET_COOKIE 0 message="INVALID TRANSACTION ID"
36   REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
37   exit 0
38 fi
39
40 vcf_escape(){
41   for each in "$@"; do
42     printf %s\\n "$each" \
43     | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
44   done \
45   | sed -E ':X;$!{N;bX}; s;\n;\;;g'
46 }
47
48 # [ "${_POST[hi_select]}" = "list" ] || _POST[hi_company]="${_POST[hi_other]}"
49 # [ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
50 # && _POST[X-HEALTH-INSURANCE]="$(vcf_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")"
51
52 vcf="$(pdi_load "$cardfile")"
53
54 vcf="$(pdi_update_value "$vcf" N 1 "$(vcf_escape "$(POST 1N)" "$(POST 2N)" "$(POST 3N)" "$(POST 4N)" "$(POST 5N)")")"
55
56 for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
57   for cnt in $(seq 1 $(POST_COUNT "$field")); do
58     case "$field" in
59       # (TEL)
60       #   printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(vcf_escape "$(POST "$field" "$cnt")")"
61       #   ;;
62       TEL)
63          vcf="$(pdi_update_attrib "$vcf" TEL $cnt TYPE="$(POST teltype $cnt |grep -Exm1 'HOME|WORK|CELL|FAX')")"
64          vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(POST "$field" "$cnt")")"
65          ;;
66       *)
67          vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(POST "$field" "$cnt")")"
68         ;;
69     esac
70 done; done
71 [ "$(POST action)" = addfield ] && vcf="$(vcf_update_field "$vcf" "$(POST newfield)" $(($(pdi_count $(POST newfield)) + 1)) '')"
72
73 printf '%s' "$vcf" >"$tempfile"
74
75 case "$(POST action)" in
76   addfield)
77     REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
78     ;;
79   update)
80     # attendance=()
81     # for att in attendance attendance{0..100}; do
82     #   [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
83     # done
84     # sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
85     #   touch "$_DATA/ical/$course"
86     # done
87     # sed -i -r '/^(.+)\t'$card'$/d' "$attfile"
88     # for each in $attendance; do
89     #   echo "$each\t$card"
90     # done >>"$attfile"
91     # sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
92     #   touch "$_DATA/ical/$course"
93     # done
94
95     cp "$tempfile" "$cardfile"
96     RELEASE_SLOCK "$cardfile"
97     REDIRECT "/cards/?o=${order}&f=${filter}#${card}"
98     ;;
99   cancel)
100     RELEASE_SLOCK "$cardfile"
101     [ -f "$cardfile" ] \
102     && REDIRECT "/cards/?o=${order}&f=${filter}#${card}" \
103     || REDIRECT "/cards/?o=${order}&f=${filter}"
104     ;;
105   delete)
106     rm "$cardfile"
107     RELEASE_SLOCK "$cardfile"
108     REDIRECT "/cards/?o=${order}&f=${filter}"
109     ;;
110 esac