]> git.plutz.net Git - shcgi/blob - forms.sh
experimental radio list
[shcgi] / forms.sh
1 #!/bin/sh
2
3 # Copyright 2018 Paul Hänsch
4 #
5 # This is the forms helper, part of CGIlite.
6
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.
11
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.
16
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/>. 
19
20 # ksh and zsh workaround
21 # set -o posix # ksh, not portable
22 setopt -o OCTAL_ZEROES 2>&-
23
24 form_radio(){
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
28
29   name="$1"
30   value="$2"
31   cond="$3"
32   label="$4"
33   id="rd_${name}_${value}"
34
35   [ "$value" = "$cond" ] && check='checked="checked"' || check=''
36
37   printf '<input type="radio" id="%s" name="%s" value="%s" %s /><label for="%s">%s</label>' \
38          "$id" "$name" "$value" "$check" "$id" "$label"
39 }
40
41 form_check(){
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
45
46   name="$1"
47   value="$2"
48   cond="$3"
49   label="$4"
50   id="rd_${name}_${value}"
51
52   [ "$value" = "$cond" ] && check='checked="checked"' || check=''
53
54   printf '<input type="checkbox" id="%s" name="%s" value="%s" %s /><label for="%s">%s</label>' \
55          "$id" "$name" "$value" "$check" "$id" "$label"
56 }
57
58 form_radio_list(){
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
63
64   name="$1"
65   cond="$2"
66   shift 2
67
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%/*}"
72   done; fi
73 }