3 # Copyright 2018 Paul Hänsch
5 # This is the forms helper, part of CGIlite.
7 # CGIlite is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # CGIlite is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with CGIlite. If not, see <http://www.gnu.org/licenses/>.
20 # ksh and zsh workaround
21 # set -o posix # ksh, not portable
22 setopt -o OCTAL_ZEROES 2>&-
25 # Produce a single radio button and an associated label
26 # Usage: form_radio Name Value Condition Label
27 # if "Condition" is the same as "Value", the button will be checked
33 id="rd_${name}_${value}"
35 [ "$value" = "$cond" ] && check='checked="checked"' || check=''
37 printf '<input type="radio" id="%s" name="%s" value="%s" %s /><label for="%s">%s</label>' \
38 "$id" "$name" "$value" "$check" "$id" "$label"
42 # Produce a checkbox and an associated label
43 # Usage: form_check Name Value Condition Label
44 # if "Condition" is the same as "Value", the Checkbox will be checked
50 id="rd_${name}_${value}"
52 [ "$value" = "$cond" ] && check='checked="checked"' || check=''
54 printf '<input type="checkbox" id="%s" name="%s" value="%s" %s /><label for="%s">%s</label>' \
55 "$id" "$name" "$value" "$check" "$id" "$label"
59 # Produce a group of radio buttons
60 # Buttons can be given in the form label/value either as parameters,
61 # or as lines on stdin
62 # if hte button value matches cond, this button will be selected
68 if [ -n "$*" ]; then for each in "$@"; do
69 form_radio "$name" "${each##*/}" "$cond" "${each%/*}"
70 done; else while read each; do
71 form_radio "$name" "${each##*/}" "$cond" "${each%/*}"