From c0302919c54331fc9fe3785f7d2a44e639e1bba0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 6 Jan 2021 12:05:10 +0100 Subject: [PATCH] implement course edit and updates --- courses/edit_course.sh | 2 +- courses/list.sh | 7 +- courses/update_course.sh | 210 +++++++++++++++++++++------------------ courses/widgets.sh | 122 ++++++++++++++++------- 4 files changed, 205 insertions(+), 136 deletions(-) diff --git a/courses/edit_course.sh b/courses/edit_course.sh index 5235344..ffe5351 100755 --- a/courses/edit_course.sh +++ b/courses/edit_course.sh @@ -21,7 +21,7 @@ locktimeout=900 . "$_EXEC"/session_lock.sh course="$(GET course |PATH)" -coursefile="$_DATA/courses/${course##*/}" +coursefile="$_DATA/ical/${course##*/}" if tempfile="$(SLOCK "$coursefile" "$locktimeout")"; then REDIRECT "/courses/?e=${course}" diff --git a/courses/list.sh b/courses/list.sh index 4a23af7..93ed174 100755 --- a/courses/list.sh +++ b/courses/list.sh @@ -3,7 +3,7 @@ . "${_EXEC}"/pdiread.sh edit_course(){ - local coursefile="$_DATA/courses/$1" + local coursefile="$_DATA/ical/$1" local tempfile course . $_EXEC/session_lock.sh @@ -13,7 +13,8 @@ edit_course(){ else course="$(pdi_load "$tempfile")" cat <<-EOF - [form .course #${coursefile##*/} action="/cards/update_course.sh" method="POST" + [form .course #${coursefile##*/} action="/courses/update_course.sh" method="POST" + [input type="hidden" name="course" value="${coursefile##*/}"] [input type="hidden" name="tid" value="$(transid ${tempfile})"] [div .section .basic . $( edit_item "$course" SUMMARY COMMENT @@ -107,7 +108,7 @@ order_courses() { } list_courses(){ - printf '%s\n' ${_DATA}/courses/*.ics \ + printf '%s\n' ${_DATA}/ical/*.ics \ | order_courses \ | print_courses } diff --git a/courses/update_course.sh b/courses/update_course.sh index a5ba8ce..3ddcc73 100755 --- a/courses/update_course.sh +++ b/courses/update_course.sh @@ -1,6 +1,6 @@ #!/bin/zsh -# Copyright 2014, 2015 Paul Hänsch +# Copyright 2014, 2015, 2020 Paul Hänsch # # This file is part of Confetti. # @@ -17,114 +17,134 @@ # You should have received a copy of the GNU Affero General Public License # along with Confetti. If not, see . -cgi_post +. "$_EXEC/pdiread.sh" +. "$_EXEC/session_lock.sh" +. "$_EXEC/cgilite/storage.sh" -course="${_POST[course]}" -tempfile="temp/$course" -coursefile="ical/$course" +unset coursefile attfile tempfile + +course="$(POST course |PATH)"; course="${course##*/}" +coursefile="$_DATA/ical/$course" attfile="$_DATA/mappings/attendance" -# DURATION: -uid="${_POST[UID]}" +action="$(POST action)" +newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')" + +if printf '%s\n' "$action" |grep -qxE 'addfield [A-Z][A-Z0-9]*'; then + newfield="${action##* }" + action=addfield +fi + +if ! tempfile="$(CHECK_SLOCK "$coursefile")"; then + SET_COOKIE 0 message="NO VALID FILE LOCK" + REDIRECT "/courses/?e=${course}" + exit 0 +elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then + SET_COOKIE 0 message="INVALID TRANSACTION ID" + REDIRECT "/courses/?e=${course}" + exit 0 +fi + +vcf_escape(){ + for each in "$@"; do + printf %s\\n "$each" \ + | sed -E ':X;$!{N;bX}; s;\r\n;\n;g; s;([;,\\]);\\\1;g; s;\n;\\n;g;' + done \ + | sed -E ':X;$!{N;bX}; s;\n;\;;g' +} + +course="$(pdi_load "$coursefile")" tzid=$(cat /etc/timezone) -tstamp=$(TZ="$tzid" date +%Y%m%dT%H%M%S) - -dts_year="${_POST[DTSYEAR]}" -dts_month="${_POST[DTSMONTH]}" -dts_day="${_POST[DTSDAY]}" -[ -n "${_POST[DTSDAY0]}" ] && dts_day="${_POST[DTSDAY0]}" -dts_hour="${_POST[DTSHOUR]}" -dts_minute="${_POST[DTSMINUTE]}" - -[ -z $dts_year ] && dts_year=$(date +%Y) -[ -z $dts_month ] && dts_month=$(date +%m) -[ -z $dts_day ] && dts_day=$(date +%d) -date -d ${dts_year}-${dts_month}-${dts_day} >/dev/null 2>/dev/null || dts_day="01" -[ -z $dts_hour ] && dts_hour=$(date +%H) -[ -z $dts_minute ] && dts_minute=$(date +%M) - -dtstart="TZID=${tzid}:${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00" - -rr_int="${_POST[RRULE_INTERVAL]}" -rr_freq="${_POST[RRULE_FREQ]}" -rr_limit="${_POST[RRULE_LIMIT]}" -case "$rr_limit" in - ETERN) - rrule="FREQ=$rr_freq;INTERVAL=$rr_int" - ;; - COUNT) - t="${_POST[RRULE_COUNT]}" - rrule="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$t" - ;; - UNTIL) - uy="${_POST[RRULE_UYEAR]}" - um="${_POST[RRULE_UMONTH]}" - ud="${_POST[RRULE_UDAY]}" - rrule="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${uy}${um}${ud}T000000Z" - ;; + +course="$(pdi_update_attrib "$course" DTSTAMP 1 "TZID=${tzid}")" +course="$(pdi_update_value "$course" DTSTAMP 1 "$(TZ="$tzid" date +%Y%m%dT%H%M%S)")" + +dts_year="$( POST DTS_YEAR |grep -m1 -xE '[0-9]{4}' || date +%Y)" +dts_month="$( POST DTS_MONTH |grep -m1 -xE '0[1-9]|1[012]' || date +%m)" +dts_day="$( POST DTS_DAY |grep -m1 -xE '0[1-9]|[12][0-9]|3[01]' || date +%d)" +dts_hour="$( POST DTS_HOUR |grep -m1 -xE '[0-9]|1[0-9]|2[0-3]' || date +%H)" +dts_minute="$(POST DTS_MINUTE |grep -m1 -xE '[0-9]|[1-5][0-9]' || date +%M)" +[ ${#dts_hour} -eq 1 ] && dts_minute="0$dts_hour" +[ ${#dts_minute} -eq 1 ] && dts_minute="0$dts_minute" +DTSTART="${dts_year}${dts_month}${dts_day}T${dts_hour}${dts_minute}00" + +course="$(pdi_update_attrib "$course" DTSTART 1 "TZID=${tzid}")" +course="$(pdi_update_value "$course" DTSTART 1 "$DTSTART")" + +rr_int=$( POST RRULE_INTERVAL |grep -m1 -xE '[0-9]+' || printf 1) +rr_count=$(POST RRULE_COUNT |grep -m1 -xE '[0-9]+' || printf 1) +rr_freq=$( POST RRULE_FREQ |grep -m1 -xE 'DAILY|WEEKLY|MONTHLY|YEARLY' || printf MONTHLY) +rr_uy=$( POST RRULE_UYEAR |grep -m1 -xE '[0-9]{4}' || date +%Y) +rr_um=$( POST RRULE_UMONTH |grep -m1 -xE '[1-9]|1[012]' || date +%m) +rr_ud=$( POST RRULE_UDAY |grep -m1 -xE '[1-9]|[12][0-9]|3[01]' || date +%d) +[ ${#rr_um} -eq 1 ] && rr_um="0$rr_um" +[ ${#rr_ud} -eq 1 ] && rr_ud="0$rr_ud" + +case $(POST RRULE_LIMIT) in + COUNT) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;COUNT=$rr_count";; + UNTIL) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int;UNTIL=${rr_uy}${rr_um}${rr_ud}T000000Z";; + ETERN|*) RRULE="FREQ=$rr_freq;INTERVAL=$rr_int";; esac -echo "BEGIN:VCALENDAR\r" >"$tempfile" -echo "VERSION:2.0\r" >>"$tempfile" -echo "PRODID:Berlin RAW Confetti\r" >>"$tempfile" -echo "BEGIN:VEVENT\r" >>"$tempfile" -echo "UID:$uid\r" >>"$tempfile" -echo "DTSTAMP:TZID=${tzid}:${tstamp}\r" >>"$tempfile" -echo "DTSTART:${dtstart}\r" >>"$tempfile" -echo "RRULE:${rrule}\r" >>"$tempfile" -for field in SUMMARY COMMENT; do - value="${_POST[$field]}" - n=0 - while [ -n "$value" ]; do - value="$(echo "$value" |sed -r ':a;N;$!ba;s:\n:\\\\n:g;s:\r:\\\\r:g')" - echo "${field}:${value}\r" - value="${_POST[$field$n]}" - n=$(($n + 1)) - done -done >>"$tempfile" - -case "${_POST[action]}" in +course="$(pdi_update_value "$course" RRULE 1 "$RRULE")" + +for field in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*'); do + for cnt in $(seq 1 $(POST_COUNT "$field")); do + case "$field" in + *) + course="$(pdi_update_value "$course" "$field" "$cnt" "$(vcf_escape "$(POST "$field" "$cnt")")")" + ;; + esac +done; done + +# delete fields, first mark for deletion using delete_key +# this way the field enumeration is preserved during the process +# finally filter marked lines +delete_key="$(randomid)" +for delete in $(POST_KEYS |grep -xE '[A-Z][A-Z0-9-]*_delete_[0-9]+'); do + f="${delete%%_*}"; c="${delete##*_}"; + [ "$(POST "$delete")" = "true" ] && course="$(pdi_update_value "$course" "$f" "$c" "delete=${delete_key}")" +done +course="$(printf '%s\n' "$course" |sed -E "/^[^:]+:delete=${delete_key}\$/d")" + +case "$(POST action)" in addfield) - echo "${_POST[newfield]}:\r" >>"$tempfile" - echo "END:VEVENT\r" >>"$tempfile" - echo "END:VCALENDAR\r" >>"$tempfile" - echo -n "Location: ?p=courses&edit=$course\n\n" + course="$(pdi_update_value "$course" "$newfield" $(( $(pdi_count "$course" "$newfield") + 1 )) '')" + printf '%s' "$course" |grep -vx '' >"$tempfile" + REDIRECT "/courses/?e=${course}" ;; update) - attendance=() - for att in attendance attendance{0..100}; do - [ -n "${_POST[$att]}" ] && attendance+=("${_POST[$att]}") - done - sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do - touch "$_DATA/vcard/$card" - done - sed -i -r '/^'$course'\t(.+)$/d' "$attfile" - for each in $attendance; do - echo "$course\t$each" - done >>"$attfile" - sed -rn 's:^'$course'\t(.+)$:\1:p' "$attfile" |while read card; do - touch "$_DATA/vcard/$card" - done - - echo "END:VEVENT\r" >>"$tempfile" - echo "END:VCALENDAR\r" >>"$tempfile" - mv "$tempfile" "$coursefile" - echo -n "Location: ?p=courses#$course\n\n" + if LOCK "$attfile"; then + grep -F "${course} " "$attfile" |while read junk card; do + touch "$_DATA/vcard/${card}" + done + sed -i -r "/^${course} .+\$/d" "$attfile" + seq 1 $(POST_COUNT attendance) |while read n; do + printf '%s %s\n' "$course" "$(POST attendance $n)" + done >>"$attfile" + grep -F "${course} " "$attfile" |while read junk card; do + touch "$_DATA/vcard/${card}" + done + RELEASE "$attfile" + else + SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS" + fi + + printf '%s' "$course" |grep -vx '' >"${tempfile}.cp" + mv "${tempfile}.cp" "$coursefile" + RELEASE_SLOCK "$coursefile" + REDIRECT "/courses/#${course}" ;; cancel) - rm "$tempfile" + RELEASE_SLOCK "$coursefile" [ -f "$coursefile" ] \ - && echo -n "Location: ?p=courses#$course\n\n" \ - || echo -n "Location: ?p=courses\n\n" + && REDIRECT "/courses/#${course}" \ + || REDIRECT "/courses/" ;; delete) - rm "$tempfile" "$coursefile" - echo -n "Location: ?p=courses\n\n" - ;; - *) - echo "END:VEVENT\r" >>"$tempfile" - echo "END:VCALENDAR\r" >>"$tempfile" - echo -n "Location: ?p=courses&edit=$course\n\n" + rm "$coursefile" + RELEASE_SLOCK "$coursefile" + REDIRECT "/courses/" ;; esac diff --git a/courses/widgets.sh b/courses/widgets.sh index 9e4fdd0..d4ce22a 100755 --- a/courses/widgets.sh +++ b/courses/widgets.sh @@ -1,4 +1,4 @@ -# Copyright 2014, 2019 Paul Hänsch +# Copyright 2014, 2019, 2020 Paul Hänsch # # This file is part of Confetti. # @@ -122,44 +122,75 @@ edit_item(){ local mstart="${dtstart#*-}"; mstart="${mstart%%-*}" local dstart="${dtstart##*-}"; dstart="${dstart%% *}" local hhstart="${dtstart##* }"; hhstart="${hhstart%%:*}" - local mmstart="${dtstart##*:}"; + local mmstart="${dtstart##* }"; mmstart="${mmstart#*:}"; mmstart="${mmstart%:*}" local m mn cdow d - printf '[div .section .DTSTART][h3 %s]' "$(l10n DTSTART)" - printf '[select .DTSYEAR name="DTSYEAR" onchange="this.form.submit();"\n' - seq $((ystart - 50)) $((ystart + 50)) |while read y; do - printf '[option value="%i" %s %i]\n' $y "$([ $y -eq $ystart ] && printf selected)" $y - done - printf ']' - printf '[select .DTSMONTH name="DTSMONTH" onchange="this.form.submit();"\n' - m=1; for mn in $(l10n January February March April May June July August September October November December); do - printf '[option value="%i" %s . %s]\n' $m "$(selected $m $mstart)" "$mn" - m=$((m+1)) - done - printf '][submit "DTS" "update" .DTS %s]\n' "$(l10n edit_dtscal)" - printf '[table .dtscalt [tr' - printf '[th . %s]' $(l10n Mon Tue Wed Thu Fri Sat Sun) - printf ']\n[tr ' - cdow="$(date -d ${ystart}-${mstart}-1 +%u)" - seq 2 $cdow |xargs -n1 printf '[td .padding .%s]' - d=1; while [ "$d" -lt 29 ] || [ "$(date -d ${ystart}-${mstart}-${d} +%m)" -eq "$mstart" ]; do - [ $cdow -eq 1 -a $d -ne 1 ] && printf ']\n[tr ' - printf '[td [input .DTSCAL type="radio" name="DTSDAY" #DTSCAL_%i value="%i" %s][label .DTSCAL for="DTSCAL_%i" %i]]' \ - $d $d "$(checked $d $dstart)" $d $d - d=$((d + 1)); cdow=$(((cdow + 1) % 7)) - done 2>/dev/null - printf ']]\n' - printf '[span .DTSTIME %s:][select .DTSTIME name="DTSHOUR"' "$(l10n time)" - seq 00 23 |while read h; do - printf '[option value="%i" %s %02i]' $h "$(selected "$h" "$hhstart")" $h - done - printf ']:[select .DTSTIME name="DTSMINUTE"' - seq 00 5 55 |while read m; do - printf '[option value="%i" %s %02i]' $m "$(selected "$m" "$mmstart")" $m - done - printf ']' + cat <<-EOF + [h3 . $(l10n DTSTART)] + [input type="number" name="DTS_YEAR" value="${ystart}" placeholder="$(l10n YYYY)"] + [select name="DTS_MONTH" onchange="this.form.submit();" + $(m=1; for mn in $(l10n January February March April May June July August September October November December); do + printf ' [option value="%02i" %s . %s]\n' $m "$(selected $m $mstart)" "$mn" + m=$((m+1)) + done) + ][submit "DTS" "update" . $(l10n edit_dtscal)] + [table .dtscalt + [tr $(printf '[th . %s]' $(l10n Mon Tue Wed Thu Fri Sat Sun))] + [tr $( + local cdow d + cdow="$(date -d ${ystart}-${mstart}-1 +%u)" + seq 2 $cdow |xargs -n1 printf '[td .padding .%s]' + d=1; while [ "$d" -lt 29 ] || [ "$(date -d ${ystart}-${mstart}-${d} +%m)" -eq "$mstart" ]; do + [ $cdow -eq 1 -a $d -ne 1 ] && printf ']\n [tr ' + printf '[td [input type="radio" name="DTS_DAY" #DTSCAL_%i value="%02i" %s][label for="DTSCAL_%i" %i]]' \ + $d $d "$(checked $d $dstart)" $d $d + d=$((d + 1)); cdow=$(((cdow + 1) % 7)) + done 2>/dev/null + )] + ] + [span .DTSTIME $(l10n time):] + [input type="number" name="DTS_HOUR" value="$hhstart" min="0" max="23"]:[input type="number" name="DTS_MINUTE" value="$mmstart" min="0" max="59"] + EOF ;; RRULE) + local dtstart="$(pdi_value "$course" DTSTART |cal_date)" + local ystart="${dtstart%%-*}"; ystart="${ystart##* }" + local mstart="${dtstart#*-}"; mstart="${mstart%%-*}" + local dstart="${dtstart##*-}"; dstart="${dstart%% *}" + + local rrule="$(pdi_value "$course" RRULE)" + local rr_int="$(printf %s "$rrule" |sed -rn 's;^(.*\;[ ]*)?INTERVAL=([0-9]+)(\;.*)?$;\2;p')" + local rr_count="$(printf %s "$rrule" |sed -rn 's;^(.*\;[ ]*)?COUNT=([0-9]+)(\;.*)?$;\2;p')" + local rr_freq="$(printf %s "$rrule" |sed -rn 's;^(.*\;[ ]*)?FREQ=(DAILY|WEEKLY|MONTHLY|YEARLY)(\;.*)?$;\2;p')" + local rr_until="$(printf %s "$rrule" |sed -rn 's;^(.*\;[ ]*)?UNTIL=([0-9]{8}T[0-9]{6}Z)(\;.*)?$;\2;p')" + local rr_uyear="${rr_until%????T??????Z}" + local rr_umonth=${rr_until#????}; rr_umonth="${rr_umonth%??T??????Z}" + local rr_uday=${rr_until#??????}; rr_uday="${rr_uday%T??????Z}" + local rr_limit="ETERN" + [ "$rr_count" ] && [ "$rr_count" -ge 0 ] && rr_limit="COUNT" + [ "$rr_uyear" ] && [ "$rr_uyear" -ge 0 ] && rr_limit="UNTIL" + + cat <<-EOF + [h3 . $(l10n "$item")] + [span .item . $(l10n t_every) + [input type="number" .RRULE .INTERVAL name="RRULE_INTERVAL" placeholder="#N" value="${rr_int:-1}" min="1"] + [select .RRULE .FREQ name="RRULE_FREQ" + $(for f in DAILY WEEKLY MONTHLY YEARLY; do + printf ' [option value="%s" %s . %s]\n' "$f" "$(selected $f "$rr_freq")" "$(l10n $f)" + done) + ]] + [span .item [input type="radio" name="RRULE_LIMIT" value="ETERN" $(checked "$rr_limit" ETERN) . $(l10n t_eternal)][br]] + [span .item + [input type="radio" name="RRULE_LIMIT" value="COUNT" $(checked "$rr_limit" COUNT)] + [input type="number" .RRULE .COUNT name="RRULE_COUNT" placeholder="#N" value="${rr_count:-1}" min="1"] $(l10n t_times) + ] + [span .item + [input type="radio" name="RRULE_LIMIT" value="UNTIL" $(checked "$rr_limit" UNTIL) . $(l10n t_until)] + [input type="number" .RRULE .UYEAR name="RRULE_UYEAR" placeholder="$(l10n YYYY)" value="${rr_uyear:-$ystart}" min="$ystart"] + [input type="number" .RRULE .UMONTH name="RRULE_UMONTH" placeholder="$(l10n MM)" value="${rr_umonth:-$mstart}" min="1" max="12"] + [input type="number" .RRULE .UDAY name="RRULE_UDAY" placeholder="$(l10n DD)" value="${rr_uday:-$dstart}" min="1" max="31"] + ] + EOF ;; COMMENT) printf '[h3 %s]' "$(l10n "$item")" @@ -171,8 +202,25 @@ edit_item(){ done printf '[button type="submit" name="action" value="addfield %s" %s ]' "$item" "$(l10n edit_addfield)" ;; - attendance);; - SUMMARY|*)printf '[h3 %s]' "$(l10n "$item")" + attendance) + printf '[h3 %s]' "$(l10n course_attendance)" + for vcf in "$_DATA"/vcard/*.vcf; do + fn="$(pdi_value "$(pdi_load "$vcf")" FN)" + printf '%s/%s\n' "${vcf##*/}" "$fn" + done \ + | sort -t/ -k2 \ + | while IFS=/ read -r vcf fn; do + printf '[label [input type="checkbox" .item name="attendance" value="%s" %s] %s]' \ + "$vcf" "$(grep -qxF "${coursefile##*/} $vcf" "$_DATA/mappings/attendance" && printf 'checked="checked"')" "$fn" + done + ;; + SUMMARY) + printf '[h3 %s]' "$(l10n "$item")" + printf '[input .item .%s name="%s" value="%s" placeholder="%s"]' \ + "$item" "$item" "$(pdi_value "$course" "$item" |unescape |HTML)" "$(l10n "$item")" + ;; + *) + 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)" -- 2.39.2