# 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 .
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 course="$1"
local item cnt c
shift 1
for item in $@; do
cnt="$(pdi_count "$course" "$item")"
[ $cnt -lt 1 ] && cnt=1
case $item in
DTSTART)
dtstart="$(pdi_value "$course" DTSTART |cal_date)"
ystart="${dtstart%%-*}"; ystart="${ystart##* }"
mstart="${dtstart#*-}"; mstart="${mstart%%-*}"
dstart="${dtstart##*-}"; dstart="${dstart%% *}"
hhstart="${dtstart##* }"; hhstart="${hhstart%%:*}"
mmstart="${dtstart##*:}";
printf '[div .section .DTSTART][h3 %s]' "$(l10n DTSTART)"
printf '[select .DTSYEAR name="DTSYEAR" onchange="this.form.submit();"'
seq $((ystart - 50)) $((ystart + 50)) |while read y; do
printf '[option value="%i" %s %i]' $y "$([ $y -eq $ystart ] && printf selected)" $y
done
printf ']'
printf '[select .DTSMONTH name="DTSMONTH" onchange="this.form.submit();"'
seq 1 12 |while read m; do
printf '[option value="%i" %s %s]' $m "$([ $m -eq $mstart ] && printf selected)" "$(date -d "2000-${m}-01" +%B)"
done
printf '][submit "DTS" "update" .DTS %s]' "$(l10n edit_dtscal)"
printf 'mon tue wed thu fri sat sun' |xargs -n1 date -d '{}' +"[span .DTSCALHEAD %a]"
seq 2 "$(date -d ${ystart}-${mstart}-1 +%u)" |xargs -n1 printf '[span .DTSCAL]'
cnt=$(date -d ${dts_year}-${dts_month}-1 +%s)
dn=1; while [ "$(date ${ystart}-${mstart}-${dn} +%m)" -eq "$mstart" ]; do
printf '[input .DTSCAL type="radio" name="DTSDAY" #DTSCAL_%i value="%i" %s][label .DTSCAL for="DTSCAL_%i" %i]' \
$dn $n "$([ $dn -eq "$dstart" ] && printf checked)" $dn $dn
dn=$((dn + 1))
done 2>/dev/null
printf '[span .DTSTIME %s:][select .DTSTIME name="DTSHOUR"' "$(l10n time)"
seq 0 23 |while read h; do
printf '[option value="%i" %s %i]' $h "$([ $h -eq $hhstart ] && printf checked)" $h
done
printf ']:[select .DTSTIME name="DTSMINUTE"'
seq 0 5 55 |while read m; do
printf '[option value="%i" %s %i]' $m "$([ $m -eq $mmstart ] && printf checked)" $m
done
printf ']'
;;
RRULE)
;;
COMMENT)
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 '' \
"$item" "$item" "$(pdi_value "$course" "$item" $c |unescape |HTML)"
done
printf '[button type="submit" name="action" value="addfield %s" %s ]' "$item" "$(l10n edit_addfield)"
;;
attendance);;
SUMMARY|*)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 "$course" "$item" $c |unescape |HTML)" "$(l10n "$item")"
done
printf '[button type="submit" name="action" value="addfield %s" %s ]' "$item" "$(l10n edit_addfield)"
;;
esac
done
}