+# Copyright 2014, 2019 Paul Hänsch
+#
+# This file is part of Confetti.
+#
+# Confetti is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Confetti is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Confetti. If not, see <http://www.gnu.org/licenses/>.
+
+check(){
+ [ "$1" = "$2" ] && printf 'checked="checked"'
+}
+
+edit="$(GET e)"
+order="$(GET o |grep -m1 -xE 'DOW|TOD')"
+
+w_sort_courses(){
+ cat <<-EOF
+ [form .sort .search action="?" method="GET"
+ [span .label $(l10n sort_order):]
+ [radio "order" "DOW" $(check $order DOW) $(l10n order_DOW)]
+ [radio "order" "TOD" $(check $order TOD) $(l10n order_TOD)]
+ [submit "" "" $(l10n order_apply)]
+ ]
+ EOF
+}
+
+cal_date(){
+ { [ $# -eq 0 ] && cat || printf %s "$*"; } |sed -rnE '
+ 2q
+ s/^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z$/\1-\2-\3 \4:\5:\6 UTC/p;t
+ s/^TZID=(.+)\:([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})$/TZ="\1" \2-\3-\4 \5:\6:\7/p;t
+ s/^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})$/\1-\2-\3 \4:\5:\6/p;t
+ '
+}
+
+cal_item(){
+ local course="$1"
+ local item cnt c
+ shift 1
+
+ for item in $@; do
+ cnt="$(pdi_count "$course" "$item")"
+
+ case $item in
+ SUMMARY)
+ printf '[h2 ­%s]' "$(pdi_value "$course" SUMMARY)"
+ ;;
+ DTSTART)
+ printf '[span .text .DTSTART %s %s ]' \
+ "$(date -d "$(pdi_value "$course" DTSTART |cal_date)" '+%A %B %d, %Y - %H:%M')" \
+ "$(l10n t_oclock)"
+ ;;
+ RRULE)
+ dts_date="$(pdi_value "$course" DTSTART |cal_date)"
+ rrule=" $(pdi_value "$course" RRULE)"
+ rr_int="${rrule##*INTERVAL=}"; rr_int="${rr_int%%;*}"
+ rr_count="${rrule##*COUNT=}"; rr_count="${rr_count%%;*}"
+ rr_freq="${rrule##*FREQ=}"; rr_freq="${rr_freq%%;*}"
+ rr_until="${rrule##*UNTIL=}"; rr_until="${rr_until%%;*}"
+ rr_until="$(cal_date "${rr_until}")"
+
+ [ "$rr_int" -eq 1 ] \
+ && printf '[span .text .RRULE %s]' "$(l10n "s$rr_freq")" \
+ || printf '[span .text .RRULE %s %s %s]' "$(l10n t_every)" "${rr_int}" "$(l10n $rr_freq)"
+ case "$rrule $rr_freq" in
+ *COUNT*DAILY*)
+ printf '[span .text %s %s]' "$(l10n t_until)" "$(date -d "$dts_date + $((rr_int * rr_count)) day" "+%A %B %d, %Y - %H:%M")"
+ ;;
+ *COUNT*WEEKLY*)
+ printf '[span .text %s %s]' "$(l10n t_until)" "$(date -d "$dts_date + $((rr_int * rr_count)) week" "+%A %B %d, %Y - %H:%M")"
+ ;;
+ *COUNT*MONTHLY*)
+ printf '[span .text %s %s]' "$(l10n t_until)" "$(date -d "$dts_date + $((rr_int * rr_count)) month" "+%A %B %d, %Y - %H:%M")"
+ ;;
+ *COUNT*YEARLY*)
+ printf '[span .text %s %s]' "$(l10n t_until)" "$(date -d "$dts_date + $((rr_int * rr_count)) year" "+%A %B %d, %Y - %H:%M")"
+ ;;
+ *UNTIL*)
+ printf '[span .text %s %s]' "$(l10n t_until)" "$(date -d "$rr_until" "+%A %B %d, %Y - %H:%M")"
+ ;;
+ esac
+ ;;
+ attendance);;
+ COMMENT)[ $cnt -gt 0 ] && printf '[h3 %s]' "$(l10n "$item")"
+ seq 1 $cnt |while read c; do
+ printf '[p .item .%s ­%s]' "$item" \
+ "$(pdi_value "$course" "$item" $c |unescape |HTML)"
+ done
+ ;;
+ *)[ $cnt -gt 0 ] && printf '[h3 %s]' "$(l10n "$item")"
+ seq 1 $cnt |while read c; do
+ printf '[span .item .%s ­%s]' "$item" \
+ "$(pdi_value "$course" "$item" $c |unescape |HTML)"
+ done
+ ;;
+ esac
+ done
+}
+
+edit_item(){
+ local card="$1"
+ local item cnt c
+ shift 1
+
+ for item in $@; do
+ cnt="$(pdi_count "$card" "$item")"
+ [ $cnt -lt 1 ] && cnt=1
+
+ case $item in
+ N)N="$(pdi_value "$card" N)"
+ if [ "$N" ]; then
+ IFS=\; read n1 n2 n3 n4 n5 <<-EOF
+ $N
+ EOF
+ else
+ N="$(pdi_value "$card" FN |unescape)"
+ n1="${N%%[a-z]*}" n1="${N#$n1}"
+ [ "$n1" ] || n1="${N##* }"
+ n2="${N%$n1}"
+ fi
+ printf '
+ [h3 %s]
+ [input .item .N name="4N" placeholder="%s" value="%s"]
+ [input .item .N name="2N" placeholder="%s" value="%s"]
+ [input .item .N name="1N" placeholder="%s" value="%s"]
+ [input .item .N name="5N" placeholder="%s" value="%s"]
+ ' "$(l10n "$item")" \
+ "$(l10n n_pre)" "$(HTML "$n4")" \
+ "$(l10n n_first)" "$(HTML "$n2" "$n3")" \
+ "$(l10n n_last)" "$(HTML "$n1")" \
+ "$(l10n n_post)" "$(HTML "$n5")"
+ ;;
+ GENDER)
+ gender="$(pdi_value "$card" GENDER)"
+ printf '
+ [select .item .GENDER name="GENDER"
+ [option value="" disabled="disabled" %s %s]
+ [option value="female" %s %s]
+ [option value="male" %s %s]
+ [option value="other" %s %s]
+ [option value="none" %s %s]
+ ]\n' \
+ "$([ "$gender" = '' ] && printf 'selected="selected"')" "$(l10n GENDER)" \
+ "$([ "$gender" = 'female' ] && printf 'selected="selected"')" "$(l10n gender_female)" \
+ "$([ "$gender" = 'male' ] && printf 'selected="selected"')" "$(l10n gender_male)" \
+ "$([ "$gender" = 'other' ] && printf 'selected="selected"')" "$(l10n gender_other)" \
+ "$([ "$gender" = 'none' ] && printf 'selected="selected"')" "$(l10n gender_none)"
+ ;;
+ BDAY|X-ZACK-JOINDATE|X-ZACK-LEAVEDATE)
+ printf '[h3 %s]
+ [input .item .%s name="%s" value="%s" placeholder="YYYY-MM-DD"]' \
+ "$(l10n "$item")" "$item" "$item" "$(pdi_value "$card" "$item" |grep -xE '[0-9-]+')"
+ ;;
+ ADR|NOTE)
+ printf '[h3 %s]' "$(l10n "$item")"
+ seq 1 $cnt |while read c; do
+ printf '[checkbox "%s_delete_%i" "true" .delete #%s_delete_%i][label for="%s_delete_%i" %s]' \
+ "$item" $c "$item" $c "$item" $c "$(l10n delete)"
+ printf '<textarea class="item %s" name="%s">%s</textarea>' \
+ "$item" "$item" "$(pdi_value "$card" "$item" $c |unescape |HTML)"
+ done
+ printf '[button type="submit" name="action" value="addfield %s" %s ]' "$item" "$(l10n edit_addfield)"
+ ;;
+ TEL) printf '[h3 %s]' "$(l10n "$item")"
+ seq 1 $cnt |while read c; do
+ printf '[checkbox "%s_delete_%i" "true" .delete #%s_delete_%i][label for="%s_delete_%i" %s]' \
+ "$item" $c "$item" $c "$item" $c "$(l10n delete)"
+ teltype="$(pdi_attrib "$card" TEL $c TYPE)"
+ printf '[select .item .teltype name="teltype"
+ [option value="" disabled="disabled" %s %s]
+ [option value="HOME" %s %s]
+ [option value="WORK" %s %s]
+ [option value="CELL" %s %s]
+ [option value="FAX" %s %s]
+ ]\n' \
+ "$([ "$teltype" = '' ] && printf 'selected="selected"')" "$(l10n teltype)" \
+ "$([ "$teltype" = 'HOME' ] && printf 'selected="selected"')" "$(l10n TYPE=HOME)" \
+ "$([ "$teltype" = 'WORK' ] && printf 'selected="selected"')" "$(l10n TYPE=WORK)" \
+ "$([ "$teltype" = 'CELL' ] && printf 'selected="selected"')" "$(l10n TYPE=CELL)" \
+ "$([ "$teltype" = 'FAX' ] && printf 'selected="selected"')" "$(l10n TYPE=FAX)"
+
+ printf '[input .item .%s name="%s" value="%s" placeholder="%s"]' \
+ "$item" "$item" "$(pdi_value "$card" "$item" $c |unescape |HTML)" "$(l10n "$item")"
+ done
+ printf '[button type="submit" name="action" value="addfield %s" %s ]' "$item" "$(l10n edit_addfield)"
+ ;;
+ *)printf '[h3 %s]' "$(l10n "$item")"
+ seq 1 $cnt |while read c; do
+ printf '[checkbox "%s_delete_%i" "true" .delete #%s_delete_%i][label for="%s_delete_%i" %s]' \
+ "$item" $c "$item" $c "$item" $c "$(l10n delete)"
+ printf '[input .item .%s name="%s" value="%s" placeholder="%s"]' \
+ "$item" "$item" "$(pdi_value "$card" "$item" $c |unescape |HTML)" "$(l10n "$item")"
+ done
+ printf '[button type="submit" name="action" value="addfield %s" %s ]' "$item" "$(l10n edit_addfield)"
+ ;;
+ esac
+ done
+}