]> git.plutz.net Git - webpoll/blob - newdate.sh
UI improvements
[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 days day times time c
29   days="$(DBM "$file" get dates)"
30   cat <<-EOF
31         [fieldset .splittimes
32           [checkbox "none" "none" .splittimes checked disabled=disabled] [submit "splittimes" "no" Separate Time Options per Day]
33           $([ ! "$days" ] && printf '[p You have not selected any days yet.]\n')
34           $(for day in $days; do 
35             date -d $day +"[h2 . %A - %B %_d, %Y]"
36             times=$(DBM "$file" get "tod_$day")
37             for time in ${times:--}; do
38               c=$((c + 1))
39               printf '
40                 <input name="todstart_%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                 <input name="todend_%s"   value="%s" placeholder="HH:MM"   list="dlist_timeofday"
43                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
44                 [submit "todremove" "%i" -]
45               ' "$day" "${time%-*}" "$day" "${time#*-}" "${c}"
46             done
47             printf '[submit "addtime" "%s" + Add Time Option]' "$day"
48           done)
49         ]
50         EOF
51 }
52
53 id="${PATH_INFO%/*}"; id="${id#/}"
54 admin=${PATH_INFO##*/}
55 file="$_DATA/$id"
56
57 if [ "$(DBM "$file" get adminkey)" != "$admin" ]; then
58   REDIRECT "$_BASE/#ERRO_INVALIDKEY"
59 elif expr match "$bookmarks" ".*${id}/${admin}.*" >/dev/null; then
60   :
61 elif expr match "$bookmarks" ".*${id}.*" >/dev/null; then
62   bookmarks="$(printf %s "$bookmarks" |tr \  \\n |grep -vxF "${id}")"
63   bookmarks="$(printf %s "$bookmarks" |tr \\n \ )"
64   SET_COOKIE +$((182 * 86400)) bookmarks="${bookmarks}${bookmarks:+ }${id}/${admin}" Path="${_BASE}/"
65 elif [ "$bookmarks" ]; then
66   SET_COOKIE +$((182 * 86400)) bookmarks="${bookmarks} ${id}/${admin}" Path="${_BASE}/"
67 fi
68
69 if [ "$REQUEST_METHOD" = POST ]; then
70   month="$(POST month |grep -m 1 -xE '[0-9]{4}-(0[1-9]|1[012])')"
71   todremove="$(POST todremove |grep -m 1 -xE '[0-9]+')"
72   splittimes="$(POST splittimes |grep -m 1 -xE 'yes|no')"
73   addtime="$(POST addtime)"
74
75   if [ "$splittimes" = yes ]; then
76     DBM "$file" set splittimes "$splittimes"
77     splittimes="no"   # receive remainder of todall form
78   elif [ "$splittimes" = no ]; then
79     DBM "$file" set splittimes "$splittimes"
80     splittimes="yes"  # receive remainder of splittimes form
81   else
82     splittimes="$(DBM "$file" get splittimes || printf no)"
83   fi
84
85   DBM "$file" set title "$(POST title)"
86   DBM "$file" set description "$(POST description)"
87   # Store common time options "todall"
88   [ "$splittimes" = no ] && DBM "$file" set todall "$(
89     for todcount in $(seq 1 $(POST_COUNT todstart)); do
90       [ "$todremove" -eq "$todcount" ] 2>&- && continue;
91       todstart="$(POST todstart "$todcount")"
92       todend="$(POST todend "$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" = global ] && DBM "$file" append todall "${BR}-"
100
101   # Store per-date time options "tod_YYYY-mm-dd"
102   [ "$splittimes" = yes ] && for date in $(DBM "$file" get dates); do
103     todremove="$(POST todremove_$date |grep -m 1 -xE '[0-9]+')"
104     DBM "$file" set "tod_$date" "$(
105       for todcount in $(seq 1 $(POST_COUNT "todstart_${date}")); do
106         [ "$todremove" -eq "$todcount" ] 2>&- && continue;
107         todstart="$(POST "todstart_${date}" "$todcount")"
108         todend="$(POST "todend_${date}" "$todcount")"
109         [ "${todstart%:??}" -lt "${todend%:??}" -o "${todstart%:??}" -eq "${todend%:??}" -a "${todstart#*:}" -lt "${todend#*:}" ] \
110         2>&- \
111         && { printf '%02i:%02i-%02i:%02i\n' "${todstart%:??}" "${todstart#*:}" "${todend%:??}" "${todend#*:}"; }\
112         || { [ "${todstart%:??}" -ge 0 -a  "${todstart#*:}" -ge 0 ] 2>&- && printf '%02i:%02i-\n' "${todstart%:??}" "${todstart#*:}"; }
113       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
114     )"
115     [ "$addtime" = "$date" ] && DBM "$file" append "tod_${date}" "${BR}-"
116   done
117
118   DBM "$file" set dates "$(
119     for date in $(seq 0 $(POST_COUNT date)); do
120       [ "$date" -eq 0 ] \
121       && POST date_add \
122       || POST date "$date"
123       printf \\n
124     done \
125     | grep -vxF "$(POST date_remove)" \
126     | 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])$' \
127     | sort -u
128   )"
129
130   if [ "$(POST delete)" = delete ]; then
131     if [ "$(POST delconfirm)" -o ! "$(DBM "$file" get participants)" ]; then
132       rm -- "$file"
133       REDIRECT "$_BASE/"
134     else
135       REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}#ERROR_NEEDCONFIRM"
136     fi
137   elif [ "$(POST post)" = post ]; then
138     REDIRECT "$_BASE${PATH_INFO%/*}"
139   elif [ "$(POST bookmark)" -a ! "$bookmarks" ]; then
140     SET_COOKIE +$((182 * 86400)) bookmarks="${id}/${admin}" Path="${_BASE}/"
141     REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}"
142   else
143     REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}"
144   fi
145 else
146   month="$(GET month |grep -m1 -xE '[0-9]{4}-(0[1-9]|1[012])' || date +%Y-%m)"
147   Y="${month%-*}"; m="${month#*-}"; Y=${Y#0}; m=${m#0};
148   [ "$m" = 1 ] && prev=$(printf '%04i-%02i' $((Y - 1)) 12) || prev=$(printf '%04i-%02i' $Y $((m - 1)))
149   [ "$m" = 12 ] && next=$(printf '%04i-%02i' $((Y + 1)) 01) || next=$(printf '%04i-%02i' $Y $((m + 1)))
150   dates="$(DBM "$file" get dates)"
151   days="$(printf %s "$dates" |sed -E "/^${month}-/!d; s;^.*-([0-9]{2})$;\1;g")"
152   additional="$(printf %s "$dates" |sed -E "/^${month}-/d;")"
153   splittimes="$(DBM "$file" get splittimes || printf no)"
154
155   yield_page "$(pagename "$id")" "newdate" <<-EOF
156         $(dlist_timeofday)
157         [form method=post
158           [input name=title value="$(DBM "$file" get title |HTML)" placeholder="Title"]
159           [textarea name=description placeholder="Description" . $(DBM "$file" get description |HTML)]
160           [fieldset .date
161             $(printf '[hidden "date" "%s"]' $additional)
162             [submit "month" "$prev" Previous Month]
163             $([ "$splittimes" = yes ] && w_month submit date "$month" $days || w_month multiple date "$month" $days)
164             [submit "month" "$next" Next Month]
165             [hidden "month" "$month"]
166           ]
167           $([ "$splittimes" = "yes" ] && fs_splittimes || fs_timeofday )
168           $(if [ "$(DBM "$file" get participants)" ]; then
169             printf '[checkbox "delconfirm" "confirm" id="delconfirm"][label for=delconfirm Delete Poll]
170                     [submit "delete" "delete" Delete Poll] [submit "post" "post" Poll page]'
171           else
172             printf '[submit "delete" "delete" Cancel] [submit "post" "post" Post Event]'
173           fi)
174           $(if [ "$bookmarks" ]; then
175             printf '[section .bookmark This admin page is accessible via the link [a href="%s" %s]. You must copy this link and keep it safe, so you can modify this poll later! The poll has also been bookmarked and will be listed on the front page.' \
176             "$(URL "//$(HEADER Host)/${_BASE}/${id}/${admin}")" \
177             "$(HTML "${HTTPS:+https:}${HTTPS:-http:}//$(HEADER Host)$(PATH "/${_BASE}/${id}/${admin}")")"
178           else
179             printf '[section .bookmark This admin page is accessible via the link [a href="%s" %s]. You must copy this link and keep it safe, so you can modify this poll later! You can also set a Cookie to bookmark all polls you visit, including this admin page. Bookmarked polls will be listed on the frontpage.<br/> [submit "bookmark" "add" Set Cookie]]' \
180             "$(URL "//$(HEADER Host)/${_BASE}/${id}/${admin}")" \
181             "$(HTML "${HTTPS:+https:}${HTTPS:-http:}//$(HEADER Host)$(PATH "/${_BASE}/${id}/${admin}")")"
182           fi)
183         ]
184         EOF
185 fi