]> git.plutz.net Git - lobster/blob - cards/update_card.sh
fd0b2102ca0d352656e500837d65477a1e11838d
[lobster] / 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 unset filter order card action newfield
24 unset cardfile attfile tempfile
25 unset vcf field cnt delete_key
26
27 filter="$(REF f)"
28 order="$(REF o)"
29
30 card="$(POST card |PATH)"
31 cardfile="$_DATA/vcard/${card##*/}"
32 attfile="$_DATA/mappings/attendance"
33
34 action="$(POST action)"
35 newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
36
37 if printf '%s\n' "$action" |grep -qxE 'addfield [A-Z][A-Z0-9]*'; then
38   newfield="${action##* }"
39   action=addfield
40 fi
41
42 if ! tempfile=$(CHECK_SLOCK "$cardfile"); then
43   SET_COOKIE 0 message="NO VALID FILE LOCK"
44   REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
45   exit 0
46 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
47   SET_COOKIE 0 message="INVALID TRANSACTION ID"
48   REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
49   exit 0
50 fi
51
52 vcf_escape(){
53   for each in "$@"; do
54     printf %s\\n "$each" \
55     | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;'
56   done \
57   | sed -E ':X;$!{N;bX}; s;\n;\;;g'
58 }
59
60 # [ "${_POST[hi_select]}" = "list" ] || _POST[hi_company]="${_POST[hi_other]}"
61 # [ -n "${_POST[hi_company]}${_POST[hi_number]}${_POST[hi_status]}" ] \
62 # && _POST[X-HEALTH-INSURANCE]="$(vcf_escape "${_POST[hi_company]}" "${_POST[hi_number]}" "${_POST[hi_status]}")"
63
64 # vcf="$(pdi_load "$cardfile")"
65 vcf="$(pdi_load "$tempfile")"
66
67 vcf="$(pdi_update_value "$vcf" N 1 "$(vcf_escape "$(POST 1N)" "$(POST 2N)" "$(POST 3N)" "$(POST 4N)" "$(POST 5N)")")"
68 vcf="$(pdi_update_value "$vcf" FN 1 "$(vcf_escape "$(POST 4N) $(POST 2N) $(POST 3N) $(POST 1N) $(POST 5N)" \
69                                        | sed -E 's;^ +;;; s; +$;;; s; +; ;g;')" )"
70
71 for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do
72   for cnt in $(seq 1 $(POST_COUNT "$field")); do
73     case "$field" in
74       # (TEL)
75       #   printf '%s;TYPE=%s:%s\r\n' "${field}" "${_POST[phonetype${key#TEL}]}" "$(vcf_escape "$(POST "$field" "$cnt")")"
76       #   ;;
77       X-HEALTH-INSURANCE)
78         hi_select="$(POST "$field" "$cnt")"
79         if [ "$hi_select" = list ]; then
80           vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(vcf_escape "$(POST "hi_company" "$cnt")" \
81                                                                        "$(POST "hi_number" "$cnt")" \
82                                                                        "$(POST "hi_status" "$cnt")" \
83                                                           )")"
84         elif [ "$hi_select" = other ]; then
85           vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(vcf_escape "$(POST "hi_other" "$cnt")" \
86                                                                        "$(POST "hi_number" "$cnt")" \
87                                                                        "$(POST "hi_status" "$cnt")" \
88                                                           )")"
89         fi
90         ;;
91       TEL)
92          vcf="$(pdi_update_attrib "$vcf" TEL $cnt TYPE="$(POST teltype $cnt |grep -Exm1 'HOME|WORK|CELL|FAX')")"
93          vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
94          ;;
95       *)
96          vcf="$(pdi_update_value "$vcf" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")"
97         ;;
98     esac
99 done; done
100
101 # delete fields, first mark for deletion using delete_key
102 # this way the field enumeration is preserved during the process
103 # finally filter marked lines
104 delete_key="$(randomid)"
105 for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do
106   f="${delete%%_*}"; c="${delete##*_}";
107   [ "$(POST "$delete")" = "true" ] && vcf="$(pdi_update_value "$vcf" "$f" "$c" "delete=${delete_key}")"
108 done
109 vcf="$(printf '%s\n' "$vcf" |sed -E "/^[^:]+:delete=${delete_key}\$/d")"
110
111 if [ "$action" = addfield ]; then
112   vcf="$(pdi_update_value "$vcf" "$newfield" $(( $(pdi_count "$vcf" "$newfield") + 1 )) '')"
113 fi
114 printf '%s' "$vcf" | sed -E '/^$/d; s/^([^:]+);:/\1:/;' >"$tempfile"
115
116 case "$action" in
117   addfield)
118     REDIRECT "/cards/?o=${order}&f=${filter}&e=${card#/}"
119     ;;
120   update)
121     # attendance=()
122     # for att in attendance attendance{0..100}; do
123     #   [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}")
124     # done
125     # sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
126     #   touch "$_DATA/ical/$course"
127     # done
128     # sed -i -r '/^(.+)\t'$card'$/d' "$attfile"
129     # for each in $attendance; do
130     #   echo "$each\t$card"
131     # done >>"$attfile"
132     # sed -rn 's:^(.+)'$card'$:\1:p' "$attfile" |while read course; do
133     #   touch "$_DATA/ical/$course"
134     # done
135
136     cp "$tempfile" "$cardfile"
137     RELEASE_SLOCK "$cardfile"
138     REDIRECT "/cards/?o=${order}&f=${filter}#${card#/}"
139     ;;
140   cancel)
141     RELEASE_SLOCK "$cardfile"
142     [ -f "$cardfile" ] \
143     && REDIRECT "/cards/?o=${order}&f=${filter}#${card#/}" \
144     || REDIRECT "/cards/?o=${order}&f=${filter}"
145     ;;
146   delete)
147     rm "$cardfile"
148     RELEASE_SLOCK "$cardfile"
149     REDIRECT "/cards/?o=${order}&f=${filter}"
150     ;;
151 esac