From 047067c2f45ea5ba54ad972a19d9682b36dfd347 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Fri, 5 Feb 2021 02:32:06 +0100 Subject: [PATCH] allow exporting list as csv --- cards/export_csv.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++ cards/filter_card.sh | 3 +++ cards/widgets.sh | 1 + l10n.sh | 1 + style.css | 2 ++ 5 files changed, 68 insertions(+) create mode 100755 cards/export_csv.sh diff --git a/cards/export_csv.sh b/cards/export_csv.sh new file mode 100755 index 0000000..a1004f7 --- /dev/null +++ b/cards/export_csv.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +. $_EXEC/pdiread.sh +. $_EXEC/cards/l10n.sh +. $_EXEC/cards/list.sh + +upcase=' y;abcdefghijklmnopqrstuvwxyzäöüé;ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜÉ;; ' + +filter="$(GET f)" +order="$(GET o)" + +[ "$order" ] || order=firstname + +list_attendance() { + grep -F " ${cardfile##*/}" "$_DATA/mappings/attendance" |while read each discard; do + { pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY || l10n "(unnamed course)"; } |unescape + done \ + | sed -E 's;";\\";g;' +} + +list_item() { + local item="$1" + local cnt="$(pdi_count "$card" "$item")" + local ret='' + + seq 1 $cnt |while read n; do case $item in + TEL) + tel="$(pdi_value "$card" "$item" "$n" |unescape)" + [ "$tel" ] && printf '%s: %s\n' "$(l10n "TYPE=$(pdi_attrib "$card" "$item" "$n" TYPE)")" "$tel" + ;; + GENDER) + gen="$(pdi_value "$card" "$item" "$n" |unescape)" + [ "$gen" ] && l10n "gender_$gen" + ;; + *) pdi_value "$card" "$item" "$n" |unescape + ;; + esac; done \ + | sed -E 's;";\\";g;' +} + +printf '%s\r\n' \ + 'Content-Type: text/csv; charset=utf-8' \ + 'Content-Disposition: inline; filename="confetti_export_'$(date +%F_%T)'.csv"' \ + '' + +printf '"%s";"%s";"%s";"%s";"%s";"%s";"%s";"%s";"%s"\n' \ + "$(l10n FN)" "$(l10n GENDER)" "$(l10n BDAY)" \ + "$(l10n TEL)" "$(l10n EMAIL)" "$(l10n ADR)" \ + "$(l10n NOTE)" "$(l10n courses)" "$(l10n CATEGORIES)" \ +| sed -E 's;­\;;;g;' + + +filter_cards \ +| order_cards \ +| while read cardfile; do + card="$(pdi_load "$cardfile")" + printf '"%s";"%s";"%s";"%s";"%s";"%s";"%s";"%s";"%s"\n' \ + "$(list_item FN)" "$(list_item GENDER)" "$(list_item BDAY)" \ + "$(list_item TEL)" "$(list_item EMAIL)" "$(list_item ADR)" \ + "$(list_item NOTE)" "$(list_attendance)" "$(list_item CATEGORIES)" +done diff --git a/cards/filter_card.sh b/cards/filter_card.sh index 9953a22..aacacbb 100755 --- a/cards/filter_card.sh +++ b/cards/filter_card.sh @@ -49,6 +49,9 @@ case $(POST choice) in new_filter) REDIRECT "/cards/?o=$(POST order)&f=${filter}&newfilter=yes" ;; + export_csv) + REDIRECT "/cards/export_csv.sh?o=$(POST order)&f=${filter}" + ;; *) REDIRECT '/cards/' ;; diff --git a/cards/widgets.sh b/cards/widgets.sh index 376199e..27cdf04 100755 --- a/cards/widgets.sh +++ b/cards/widgets.sh @@ -75,6 +75,7 @@ w_filter_diag(){ ] [button type="submit" name="choice" value="filter" $(l10n filter_apply)] [button type="submit" name="choice" value="del_filter" $(l10n filter_cancel)] + [button type="submit" name="choice" value="export_csv" $(l10n export_csv)] ] EOF } diff --git a/l10n.sh b/l10n.sh index c53cdd3..cab5bde 100755 --- a/l10n.sh +++ b/l10n.sh @@ -112,6 +112,7 @@ l10n_global() { filter_more) printf %s "+ mehr Filter";; filter_apply) printf %s "Filtern";; filter_cancel) printf %s "Filter löschen";; + export_csv) printf %s "Liste als CSV-Datei";; # UI Labels Special course_attendance) printf %s "Kurs­teil­nahme";; diff --git a/style.css b/style.css index fcc608d..06aacdd 100644 --- a/style.css +++ b/style.css @@ -173,6 +173,8 @@ form.filter button[type=submit] { margin-top: .5em; margin-bottom: .5em; } +form.filter button[value=export_csv] { margin-left: 1em; } + body.courses form .order { display: inline-block; margin-right: 2em;} body.cards form.newcard { display: flex; } -- 2.39.2