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