]> git.plutz.net Git - confetti/blob - cards/list.sh
process split fields as arrays
[confetti] / cards / list.sh
1 #!/bin/sh
2
3 . "${_EXEC}"/pdiread.sh
4
5 edit_card(){
6   local cardfile="$_DATA/vcard/$1" 
7   local tempfile card
8
9   . $_EXEC/session_lock.sh
10
11   if ! tempfile="$(CHECK_SLOCK "$cardfile")"; then
12     printf '[div .message %s]' "$(l10n "This card is not set up for editing within this session.")"
13   else
14     card="$(pdi_load "$tempfile")"
15     cat <<-EOF
16         [form .card #${cardfile##*/} action="${_BASE}/cards/update_card.sh" method="POST"
17           [input type="hidden" name="tid" value="$(transid ${tempfile})"]
18           [div .section .basic $(
19             edit_item "$card" N GENDER
20             [ "$(pdi_count "$card" NICKNAME)" -gt 0 ] \
21             && edit_item "$card" NICKNAME
22             edit_item "$card" BDAY
23             edit_item "$card" X-ZACK-JOINDATE
24             [ "$(pdi_count "$card" X-ZACK-LEAVEDATE)" -gt 0 ] \
25             && edit_item "$card" X-ZACK-LEAVEDATE
26             card_item "$card" SOUND PHOTO LOGO
27           )]
28           [div .section .phone   $(edit_item "$card" TEL)]
29           [div .section .message $(
30             edit_item "$card" EMAIL
31             [ $(pdi_count "$card" IMPP) -gt 0 ] && edit_item "$card" IMPP
32             [ $(pdi_count "$card" URL ) -gt 0 ] && edit_item "$card" URL
33           )]
34           [div .section .address $(
35             edit_item "$card" ADR
36             [ $(pdi_count "$card" X-IBAN) -gt 0 ] && edit_item "$card" X-IBAN
37           )]
38           [div .section .note    $(edit_item "$card" NOTE)]
39           [div .section .attendance
40             [h3 $(l10n course_attendance) ] [div .attendance $(
41             list_courses |while IFS=/ read course coursename; do
42               courseH="$(HTML "$course")"
43               printf '[input type="checkbox" id="cour%s" name="attendance" value="%s" %s][label for="cour%s" . %s]' \
44                      "$courseH" "$courseH" \
45                      "$(grep -qF "${course}     ${cardfile##*/}" "$_DATA/mappings/attendance" \
46                         && printf 'checked="checked"'
47                        )" \
48                      "$courseH" "$coursename"
49             done)]
50             [h3 $(l10n CATEGORIES) ] [div .categories $(
51             grep -xE '[^ ]+' "$_DATA"/mappings/categories |while read -r cat; do
52               catH="$(HTML "$cat")"
53               printf '[input type="checkbox" id="cat%s" name="CATEGORIES" value="%s" %s][label for="cat%s" . %s]' \
54                      "$catH" "$catH" \
55                      "$(seq 1 $(pdi_count "$card" CATEGORIES) |while read c; do
56                        pdi_value "$card" CATEGORIES $c |grep -qxF "$cat" \
57                        && printf 'checked="checked"' && break
58                      done)" \
59                      "$catH" "$catH"
60             done)]
61           ]
62           [div .control
63             [div .item .delete label="$(l10n edit_delete)"
64               [input type="checkbox" #delete]
65               [label for="delete" $(l10n edit_delete)]
66               [button type="submit" name="action" value="delete" $(l10n edit_delete)]
67             ]
68             [div .item .newfield
69               [select name="newfield"
70                 [option value="" disabled="disabled" selected="selected" $(l10n edit_addfieldtext)]
71                 $(for f in NICKNAME EMAIL TEL IMPP ADR URL NOTE X-ZACK-LEAVEDATE X-IBAN; do
72                   printf '[option value="%s" %s] ' "$f" "$(l10n "$f")"
73                 done)
74               ][button type="submit" name="action" value="addfield" $(l10n edit_addfield)]
75             ]
76             [button .item type="submit" name="action" value="update"   $(l10n edit_update)]
77             [button .item type="submit" name="action" value="cancel"   $(l10n edit_cancel)]
78           ]
79           [input type="hidden" name="UID" value="$(pdi_value "$card" UID |HTML)"]
80           [input type="hidden" name="card" value="${cardfile##*/}"]
81         ]
82         EOF
83   fi
84 }
85
86 print_card(){
87   local cardfile="$1"
88   local card="$(pdi_load "$cardfile")"
89   cat <<-EOF
90     [div .card #${cardfile##*/}
91       [div .section .basic . $(
92         card_item "$card" FN GENDER NICKNAME BDAY X-ZACK-JOINDATE X-ZACK-LEAVEDATE SOUND PHOTO LOGO
93       )]
94       [div .section .phone   . $(card_item "$card" TEL)]
95       [div .section .message . $(card_item "$card" EMAIL IMPP URL)]
96       [div .section .address . $(card_item "$card" ADR X-IBAN)]
97       [div .section .note    . $(card_item "$card" NOTE)]
98       [div .section .attendance [h3 $(l10n course_attendance) ] [ul
99         $(grep -F "     ${cardfile##*/}" "$_DATA/mappings/attendance" |while read each discard; do
100           printf '[li [a .item .attendance href="%s/courses#%s" . %s]]' \
101                  "${_BASE}" "$each" \
102                  "$(pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY || l10n "(unnamed course)" |unescape |HTML)"
103         done |sort -k7)]
104         $(card_item "$card" CATEGORIES)
105       ]
106       [div .control
107         [a .button .item href="${_BASE}/ledgers/account.sh?card=${cardfile##*/}" $(l10n ledger)]
108         [a .button .item href="${_BASE}/cards/edit_card.sh?card=${cardfile##*/}" $(l10n edit)]
109         [a .button .item href="${_BASE}/cards/export_card.sh?card=${cardfile##*/}" $(l10n vcf_export)]
110       ]
111     ]
112         EOF
113 }
114
115 print_cards(){
116   local cardfile cachefile date size name ldate=0 lsize lname
117
118   while read cardfile; do
119     cachefile="${_DATA}/cache/${cardfile##*/}.cache"
120     if [ -s "$cachefile" -a "$cachefile" -nt "$cardfile" ]; then
121       cat "$cachefile"
122     elif [ -s "$cardfile" ]; then
123       print_card "$cardfile" |tee "$cachefile"
124     fi
125   done
126 }
127
128 filter_attendance(){
129   fatt="$1"
130   attfile="$_DATA/mappings/attendance"
131
132   if [ ! "$fatt" ]; then
133     # debug 'list all'
134     printf '%s\n' "$_DATA/vcard"/*.vcf
135   elif [ "${fatt#* }" = "${fatt}" ]; then
136     # debug "list $fatt"
137     grep -xiE "(${fatt})        .+vcf" "$attfile" \
138     | while read vcf; do
139       printf '%s/vcard/%s\n' "$_DATA" "${vcf##* }"
140     done
141   else
142     # debug "filter ${fatt%% *}"
143     filter_attendance "${fatt#* }" \
144     | while read vcf; do
145       grep -xiE "(${fatt%% *})  ${vcf##*/}" "$attfile"
146     done \
147     | while read vcf; do
148       printf '%s/vcard/%s\n' "$_DATA" "${vcf##* }"
149     done
150   fi
151 }
152
153 filter_cards(){
154   local filter f fex='x;p;'
155   local upcase=' y;abcdefghijklmnopqrstuvwxyzäöüé;ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜÉ;; '
156
157   filter="$(printf %s "${filter}" \
158             | sed -E 's;[]\/\(\)\\\$\?\.\+\*\;\[\{\}];\\&;g;
159                       '"$upcase"
160            )^"
161
162   while [ "$filter" ]; do
163     f="${filter%%^*}" filter="${filter#*^}"
164     case $f in
165       '') break
166         ;;
167       COURSE:*) fatt="${fatt}${fatt:+ }${f#*:}"
168         ;;
169       ANY:*) fex="/\n.*(\;[^:]*)?:[^\n]*(${f#*:})[^\n]*\r?\n/{${fex}}"
170         ;;
171       NAME:*) fex="/\n(N|FN|NICKNAME)(\;[^:]*)?:[^\n]*(${f#*:})[^\n]*\r?\n/{${fex}}"
172         ;;
173       STREET:*|ZIP:*) fex="/\nADR(\;[^:]*)?:[^\n]*(${f#*:})[^\n]*\r?\n/{${fex}}"
174         ;;
175       *) fex="/\n${f%%:*}(\;[^:]*)?:[^\n]*(${f#*:})[^\n]*\r?\n/{${fex}}"
176         ;;
177     esac
178   done
179
180   # for cardfile in "${_DATA}"/vcard/*.vcf; do
181   filter_attendance "$fatt" |while read cardfile; do
182     printf '%s\n' "$cardfile"
183     cat "$cardfile"
184   done \
185   | sed -nE ':X; /\nEND\;?:VCARD\r?$/!{ N; bX; }; h; s;\n.*$;;; x; s;^[^\n]+\n;;;
186              '"$upcase""$fex"
187 }
188
189 order_cards() {
190   local cardfile card
191
192   while read cardfile; do
193     card="$(pdi_load "$cardfile")"
194
195     case $order in
196       firstname)
197         printf '%s      %s\n' "$(pdi_value "$card" FN)" "$cardfile"
198         ;;
199       lastname)
200         printf '%s      %s\n' "$(pdi_value "$card" N || pdi_value "$card" FN)" "$cardfile"
201         ;;
202       bdate)
203         printf '%s      %s\n' "$(pdi_value "$card" BDAY || printf 0000-00-00)" "$cardfile"
204         ;;
205     esac
206   done \
207   | sort \
208   | sed -E 's;^.*\t;;g'
209 }
210
211 list_cards(){
212   filter_cards \
213   | order_cards \
214   | grep -xvF "$edit" \
215   | print_cards
216 }