]> git.plutz.net Git - webpoll/blob - newdate.sh
reset table padding from updated cgilite style
[webpoll] / newdate.sh
1 #!/bin/sh
2
3 id="${PATH_INFO%/*}"; id="${id#/}"
4 admin=${PATH_INFO##*/}
5 file="$_DATA/$id"
6
7 if [ "$(DBM "$file" get adminkey)" != "$admin" ]; then
8   page_home;
9   return 0
10   # REDIRECT "$_BASE/#ERRO_INVALIDKEY"
11 elif expr match "$bookmarks" ".*${id}/${admin}.*" >/dev/null; then
12   :
13 elif expr match "$bookmarks" ".*${id}.*" >/dev/null; then
14   bookmarks="$(printf %s "$bookmarks" |tr \  \\n |grep -vxF "${id}")"
15   bookmarks="$(printf %s "$bookmarks" |tr \\n \ )"
16   SET_COOKIE +$((182 * 86400)) bookmarks="${bookmarks}${bookmarks:+ }${id}/${admin}" Path="${_BASE}/"
17 elif [ "$bookmarks" ]; then
18   SET_COOKIE +$((182 * 86400)) bookmarks="${bookmarks} ${id}/${admin}" Path="${_BASE}/"
19 fi
20
21 fs_timeofday() {
22   local todall="$(DBM "$file" get todall)" time c=0
23   cat <<-EOF
24         [fieldset .timeofday
25           [label .todstart Start Time (optional):
26           ]
27           [label .todend   End Time (optional):
28           ]
29           $(for time in ${todall:--}; do
30             c=$((c + 1))
31             printf '
32               <input name="todstart" value="%s" placeholder="HH:MM" list="dlist_timeofday"
33                      pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
34               <input name="todend"   value="%s" placeholder="HH:MM"   list="dlist_timeofday"
35                      pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
36               [submit "todremove" "%i" -]
37             ' "${time%-*}" "${time#*-}" "${c}"
38           done)
39           [submit "addtime" "global" + Add Time Option]
40           [checkbox "none" "none" .splittimes disabled=disabled] [submit "splittimes" "yes" Separate Time Options per Day]
41         ]
42         EOF
43 }
44
45 fs_splittimes() {
46   local days day times time c
47   days="$(DBM "$file" get dates)"
48   cat <<-EOF
49         [fieldset .splittimes
50           [checkbox "none" "none" .splittimes checked disabled=disabled] [submit "splittimes" "no" Separate Time Options per Day]
51           $([ ! "$days" ] && printf '[p You have not selected any days yet.]\n')
52           $(for day in $days; do 
53             date -d $day +"[h2 . %A - %B %_d, %Y]"
54             times=$(DBM "$file" get "tod_$day")
55             for time in ${times:--}; do
56               c=$((c + 1))
57               printf '
58                 <input name="todstart_%s" value="%s" placeholder="HH:MM" list="dlist_timeofday"
59                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
60                 <input name="todend_%s"   value="%s" placeholder="HH:MM"   list="dlist_timeofday"
61                        pattern="^(0?\[0-9\]|1\[0-9\]|2\[0-3\]):(\[0-5\]\[0-9\])$"/>
62                 [submit "todremove_%s" "%i" -]
63               ' "$day" "${time%-*}" "$day" "${time#*-}" "$day" "${c}"
64             done
65             printf '[submit "addtime" "%s" + Add Time Option]' "$day"
66           done)
67         ]
68         EOF
69 }
70
71 if [ "$REQUEST_METHOD" = POST ]; then
72   month="$(POST month |grep -m 1 -xE '[0-9]{4}-(0[1-9]|1[012])')"
73   todremove="$(POST todremove |grep -m 1 -xE '[0-9]+')"
74   splittimes="$(POST splittimes |grep -m 1 -xE 'yes|no')"
75   addtime="$(POST addtime)"
76
77   if [ "$splittimes" = yes ]; then
78     DBM "$file" set splittimes "$splittimes"
79     splittimes="no"   # receive remainder of todall form
80   elif [ "$splittimes" = no ]; then
81     DBM "$file" set splittimes "$splittimes"
82     splittimes="yes"  # receive remainder of splittimes form
83   else
84     splittimes="$(DBM "$file" get splittimes || printf no)"
85   fi
86
87   DBM "$file" set title "$(POST title)"
88   DBM "$file" set description "$(POST description)"
89   # Store common time options "todall"
90   [ "$splittimes" = no ] && DBM "$file" set todall "$(
91     for todcount in $(seq 1 $(POST_COUNT todstart)); do
92       [ "$todremove" -eq "$todcount" ] 2>&- && continue;
93       todstart="$(POST todstart "$todcount")"
94       todend="$(POST todend "$todcount")"
95       [ "${todstart%:??}" -lt "${todend%:??}" -o "${todstart%:??}" -eq "${todend%:??}" -a "${todstart#*:}" -lt "${todend#*:}" ] \
96       2>&- \
97       && { printf '%02i:%02i-%02i:%02i\n' "${todstart%:??}" "${todstart#*:}" "${todend%:??}" "${todend#*:}"; }\
98       || { [ "${todstart%:??}" -ge 0 -a  "${todstart#*:}" -ge 0 ] 2>&- && printf '%02i:%02i-\n' "${todstart%:??}" "${todstart#*:}"; }
99     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
100   )"
101   [ "$addtime" = global ] && DBM "$file" append todall "${BR}-"
102
103   # Store per-date time options "tod_YYYY-mm-dd"
104   [ "$splittimes" = yes ] && for date in $(DBM "$file" get dates); do
105     todremove="$(POST todremove_$date |grep -m 1 -xE '[0-9]+')"
106     DBM "$file" set "tod_$date" "$(
107       for todcount in $(seq 1 $(POST_COUNT "todstart_${date}")); do
108         [ "$todremove" -eq "$todcount" ] 2>&- && continue;
109         todstart="$(POST "todstart_${date}" "$todcount")"
110         todend="$(POST "todend_${date}" "$todcount")"
111         [ "${todstart%:??}" -lt "${todend%:??}" -o "${todstart%:??}" -eq "${todend%:??}" -a "${todstart#*:}" -lt "${todend#*:}" ] \
112         2>&- \
113         && { printf '%02i:%02i-%02i:%02i\n' "${todstart%:??}" "${todstart#*:}" "${todend%:??}" "${todend#*:}"; }\
114         || { [ "${todstart%:??}" -ge 0 -a  "${todstart#*:}" -ge 0 ] 2>&- && printf '%02i:%02i-\n' $(dec ${todstart%:??} ${todstart#*:}); }
115       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
116     )"
117     [ "$addtime" = "$date" ] && DBM "$file" append "tod_${date}" "${BR}-"
118   done
119
120   DBM "$file" set dates "$(
121     for date in $(seq 0 $(POST_COUNT date)); do
122       [ "$date" -eq 0 ] \
123       && POST date_add \
124       || POST date "$date"
125       printf \\n
126     done \
127     | grep -vxF "$(POST date_remove)" \
128     | 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])$' \
129     | sort -u
130   )"
131
132   if [ "$(POST delete)" = delete ]; then
133     if [ "$(POST delconfirm)" -o ! "$(DBM "$file" get participants)" ]; then
134       rm -- "$file"
135       REDIRECT "$_BASE/"
136     else
137       REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}#ERROR_NEEDCONFIRM"
138     fi
139   elif [ "$(POST post)" = post ]; then
140     REDIRECT "$_BASE${PATH_INFO%/*}"
141   elif [ "$(POST bookmark)" -a ! "$bookmarks" ]; then
142     SET_COOKIE +$((182 * 86400)) bookmarks="${id}/${admin}" Path="${_BASE}/"
143     REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}"
144   else
145     REDIRECT "$_BASE$PATH_INFO${month:+?month=}${month}"
146   fi
147 else
148   month="$(GET month |grep -m1 -xE '[0-9]{4}-(0[1-9]|1[012])' || date +%Y-%m)"
149   Y="${month%-*}"; m="${month#*-}"; Y=${Y#0}; m=${m#0};
150   [ "$m" = 1 ] && prev=$(printf '%04i-%02i' $((Y - 1)) 12) || prev=$(printf '%04i-%02i' $Y $((m - 1)))
151   [ "$m" = 12 ] && next=$(printf '%04i-%02i' $((Y + 1)) 01) || next=$(printf '%04i-%02i' $Y $((m + 1)))
152   dates="$(DBM "$file" get dates)"
153   days="$(printf %s "$dates" |sed -E "/^${month}-/!d; s;^.*-([0-9]{2})$;\1;g")"
154   additional="$(printf %s "$dates" |sed -E "/^${month}-/d;")"
155   splittimes="$(DBM "$file" get splittimes || printf no)"
156
157   yield_page "$(pagename "$id")" "newdate" <<-EOF
158         $(dlist_timeofday)
159         [main [form method=post
160           [input name=title value="$(DBM "$file" get title |HTML)" placeholder="Title" autocomplete=off]
161           [textarea name=description placeholder="Description" . $(DBM "$file" get description |HTML)]
162           $(if [ "$bookmarks" ]; then
163             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.]' \
164             "$(URL "//$(HEADER Host)/${_BASE}/${id}/${admin}")" \
165             "$(HTML "${HTTPS:+https:}${HTTPS:-http:}//$(HEADER Host)$(PATH "/${_BASE}/${id}/${admin}")")"
166           else
167             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]]' \
168             "$(URL "//$(HEADER Host)/${_BASE}/${id}/${admin}")" \
169             "$(HTML "${HTTPS:+https:}${HTTPS:-http:}//$(HEADER Host)$(PATH "/${_BASE}/${id}/${admin}")")"
170           fi)
171           [fieldset .date
172             $(printf '[hidden "date" "%s"]' $additional)
173             [submit "month" "$prev" Previous Month]
174             $([ "$splittimes" = yes ] && w_month submit date "$month" $days || w_month multiple date "$month" $days)
175             [submit "month" "$next" Next Month]
176             [hidden "month" "$month"]
177           ]
178           $([ "$splittimes" = "yes" ] && fs_splittimes || fs_timeofday )
179           $(if [ "$(DBM "$file" get participants)" ]; then
180             printf '[checkbox "delconfirm" "confirm" id="delconfirm"][label for=delconfirm Delete Poll]
181                     [submit "delete" "delete" Delete Poll] [submit "post" "post" Poll page]'
182           else
183             printf '[submit "delete" "delete" Cancel] [submit "post" "post" Post Event]'
184           fi)
185         ]]
186         EOF
187 fi