]> git.plutz.net Git - webpoll/blob - newdate.sh
non-bold calendar headings
[webpoll] / newdate.sh
1 #!/bin/sh
2
3 fs_timeofday() {
4   local todall="$(DBM "$file" get todall)" time c=0
5   cat <<-EOF
6         [fieldset .timeofday
7           [label .todstart Start Time (optional):
8           ]
9           [label .todend   End Time (optional):
10           ]
11           $(for time in ${todall:--}; do
12             c=$((c + 1))
13             printf '
14               <input name="todstart" value="%s" placeholder="HH:MM" list="dlist_timeofday"
15                      pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
16               <input name="todend"   value="%s" placeholder="HH:MM"   list="dlist_timeofday"
17                      pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
18               [submit "todremove" "%i" -]
19             ' "${time%-*}" "${time#*-}" "${c}"
20           done)
21           [submit "addtime" "global" + Add Time Option]
22           [checkbox "none" "none" .splittimes disabled=disabled] [submit "splittimes" "yes" Separate Time Options per Day]
23         ]
24         EOF
25 }
26
27 fs_splittimes() {
28   local day times time c
29   cat <<-EOF
30         [fieldset .splittimes
31           [checkbox "none" "none" .splittimes checked disabled=disabled] [submit "splittimes" "no" Separate Time Options per Day]
32           $(for day in $(DBM "$file" get dates); do 
33             date -d $day +"[h2 . %A - %B %_d, %Y]"
34             times=$(DBM "$file" get "tod_$day")
35             for time in ${times:--}; do
36               c=$((c + 1))
37               printf '
38                 <input name="todstart_%s" value="%s" placeholder="HH:MM" list="dlist_timeofday"
39                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
40                 <input name="todend_%s"   value="%s" placeholder="HH:MM"   list="dlist_timeofday"
41                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
42                 [submit "todremove" "%i" -]
43               ' "$day" "${time%-*}" "$day" "${time#*-}" "${c}"
44             done
45             printf '[submit "addtime" "%s" + Add Time Option]' "$day"
46           done)
47         ]
48         EOF
49 }
50
51 if [ "$REQUEST_METHOD" = POST ]; then
52   id="${PATH_INFO%/newdate}"; id="${id#/}"
53   file="$_DATA/$id"
54   month="$(POST month |grep -m 1 -xE '[0-9]{4}-(0[1-9]|1[012])')"
55   todremove="$(POST todremove |grep -m 1 -xE '[0-9]+')"
56   splittimes="$(POST splittimes |grep -m 1 -xE 'yes|no')"
57   addtime="$(POST addtime)"
58
59   if [ "$splittimes" = yes ]; then
60     DBM "$file" set splittimes "$splittimes"
61     splittimes="no"   # receive remainder of todall form
62   elif [ "$splittimes" = no ]; then
63     DBM "$file" set splittimes "$splittimes"
64     splittimes="yes"  # receive remainder of splittimes form
65   else
66     splittimes="$(DBM "$file" get splittimes || printf no)"
67   fi
68
69   DBM "$file" set title "$(POST title)"
70   DBM "$file" set description "$(POST description)"
71   # Store common time options "todall"
72   [ "$splittimes" = no ] && DBM "$file" set todall "$(
73     for todcount in $(seq 1 $(POST_COUNT todstart)); do
74       [ "$todremove" -eq "$todcount" ] 2>&- && continue;
75       todstart="$(POST todstart "$todcount")"
76       todend="$(POST todend "$todcount")"
77       [ "${todstart%:??}" -lt "${todend%:??}" -o "${todstart%:??}" -eq "${todend%:??}" -a "${todstart#*:}" -lt "${todend#*:}" ] \
78       2>&- \
79       && { printf '%02i:%02i-%02i:%02i\n' "${todstart%:??}" "${todstart#*:}" "${todend%:??}" "${todend#*:}"; }\
80       || { [ "${todstart%:??}" -ge 0 -a  "${todstart#*:}" -ge 0 ] 2>&- && printf '%02i:%02i-\n' "${todstart%:??}" "${todstart#*:}"; }
81     done |grep -xE '^([01][0-9]|2[0-3]):([0-5][0-9])-(([01][0-9]|2[0-3]):([0-5][0-9]))?$' |sort -u
82   )"
83   [ "$addtime" = global ] && DBM "$file" append todall "${BR}-"
84
85   # Store per-date time options "tod_YYYY-mm-dd"
86   [ "$splittimes" = yes ] && for date in $(DBM "$file" get dates); do
87     todremove="$(POST todremove_$date |grep -m 1 -xE '[0-9]+')"
88     DBM "$file" set "tod_$date" "$(
89       for todcount in $(seq 1 $(POST_COUNT "todstart_${date}")); do
90         [ "$todremove" -eq "$todcount" ] 2>&- && continue;
91         todstart="$(POST "todstart_${date}" "$todcount")"
92         todend="$(POST "todend_${date}" "$todcount")"
93         [ "${todstart%:??}" -lt "${todend%:??}" -o "${todstart%:??}" -eq "${todend%:??}" -a "${todstart#*:}" -lt "${todend#*:}" ] \
94         2>&- \
95         && { printf '%02i:%02i-%02i:%02i\n' "${todstart%:??}" "${todstart#*:}" "${todend%:??}" "${todend#*:}"; }\
96         || { [ "${todstart%:??}" -ge 0 -a  "${todstart#*:}" -ge 0 ] 2>&- && printf '%02i:%02i-\n' "${todstart%:??}" "${todstart#*:}"; }
97       done |grep -xE '^([01][0-9]|2[0-3]):([0-5][0-9])-(([01][0-9]|2[0-3]):([0-5][0-9]))?$' |sort -u
98     )"
99     [ "$addtime" = "$date" ] && DBM "$file" append "tod_${date}" "${BR}-"
100   done
101
102   DBM "$file" set dates "$(
103     for date in $(seq 0 $(POST_COUNT date)); do
104       [ "$date" -eq 0 ] \
105       && POST date_add \
106       || POST date "$date"
107       printf \\n
108     done \
109     | grep -vxF "$(POST date_remove)" \
110     | grep -xE '^[0-9]{4}-((01|03|05|07|08|10|12)-([012][0-9]|3[01])|(04|06|09|11)-([012][0-9]|30)|02-[012][0-9])$' \
111     | sort -u
112   )"
113
114   if [ "$(POST cancel)" = cancel ]; then
115     rm -- "$file"
116     REDIRECT "$_BASE/"
117   else
118     REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}"
119   fi
120 else
121   id="${PATH_INFO%/newdate}"; id="${id#/}"
122   file="$_DATA/$id"
123   month="$(GET month |grep -m1 -xE '[0-9]{4}-(0[1-9]|1[012])' || date +%Y-%m)"
124   Y="${month%-*}"; m="${month#*-}"; Y=${Y#0}; m=${m#0};
125   [ "$m" = 1 ] && prev=$(printf '%04i-%02i' $((Y - 1)) 12) || prev=$(printf '%04i-%02i' $Y $((m - 1)))
126   [ "$m" = 12 ] && next=$(printf '%04i-%02i' $((Y + 1)) 01) || next=$(printf '%04i-%02i' $Y $((m + 1)))
127   dates="$(DBM "$file" get dates)"
128   days="$(printf %s "$dates" |sed -E "/^${month}-/!d; s;^.*-([0-9]{2})$;\1;g")"
129   additional="$(printf %s "$dates" |sed -E "/^${month}-/d;")"
130   splittimes="$(DBM "$file" get splittimes || printf no)"
131
132   yield_page "$(pagename "$id")" "newdate" <<-EOF
133         $(dlist_timeofday)
134         [form method=post
135           [input name=title value="$(DBM "$file" get title |HTML)" placeholder="Title"]
136           [textarea name=description placeholder="Description" . $(DBM "$file" get description |HTML)]
137           [fieldset .date
138             $(printf '[hidden "date" "%s"]' $additional)
139             [submit "month" "$prev" Previous Month]
140             $([ "$splittimes" = yes ] && w_month submit date "$month" $days || w_month multiple date "$month" $days)
141             [submit "month" "$next" Next Month]
142             [hidden "month" "$month"]
143           ]
144           $([ "$splittimes" = "yes" ] && fs_splittimes || fs_timeofday )
145           [submit "cancel" "cancel" Cancel]
146           [submit "post" "post" Post Event]
147         ]
148         EOF
149 fi