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