/
Checkbox.pm
182 lines (150 loc) · 5.33 KB
/
Checkbox.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# See bottom of file for license and copyright information
package Foswiki::Form::Checkbox;
use strict;
use warnings;
use Assert;
use Foswiki::Form::ListFieldDefinition ();
our @ISA = ('Foswiki::Form::ListFieldDefinition');
sub new {
my ( $class, @args ) = @_;
my $this = $class->SUPER::new(@args);
$this->{size} ||= 0;
$this->{size} =~ s/\D//g;
$this->{size} ||= 0;
$this->{size} = 4 if ( $this->{size} < 1 );
return $this;
}
sub finish {
my $this = shift;
$this->SUPER::finish();
undef $this->{valueMap};
}
sub getOptions {
my $this = shift;
return $this->{_options} if $this->{_options};
my $vals = $this->SUPER::getOptions(@_);
if ( $this->isValueMapped() ) {
# create a values map
$this->{valueMap} = ();
$this->{_options} = ();
my $str;
foreach my $val (@$vals) {
if ( $val =~ /^(.*?[^\\])=(.*)$/ ) {
$str = TAINT($1);
my $descr = $this->{_descriptions}{$val};
$val = $2;
$this->{_descriptions}{$val} = $descr;
$str =~ s/\\=/=/g;
}
else {
$str = $val;
}
$this->{valueMap}{$val} = Foswiki::urlDecode($str);
push @{ $this->{_options} }, $val;
}
}
return $vals;
}
# Checkboxes can't provide a default from the form spec
sub getDefaultValue { return; }
# Checkbox store multiple values
sub isMultiValued { return 1; }
sub isValueMapped { return shift->{type} =~ /\+values/; }
sub renderForDisplay {
my ( $this, $format, $value, $attrs ) = @_;
$this->getOptions();
if ( $this->isValueMapped() ) {
my @vals = ();
foreach my $val ( split( /\s*,\s*/, $value ) ) {
if ( defined( $this->{valueMap}{$val} ) ) {
push @vals, $this->{valueMap}{$val};
}
else {
push @vals, $val;
}
}
$value = join( ", ", @vals );
}
return $this->SUPER::renderForDisplay( $format, $value, $attrs );
}
sub renderForEdit {
my ( $this, $topicObject, $value ) = @_;
my $session = $this->{session};
my $extra = '';
if ( $this->{type} =~ m/\+buttons/ ) {
my $boxes = scalar( @{ $this->getOptions() } );
$extra = CGI::br();
$extra .= CGI::button(
-class => 'foswikiCheckbox',
-value => $session->i18n->maketext('Set all'),
-onClick => 'checkAll(this,2,' . $boxes . ',true)'
);
$extra .= ' ';
$extra .= CGI::button(
-class => 'foswikiCheckbox',
-value => $session->i18n->maketext('Clear all'),
-onClick => 'checkAll(this,1,' . $boxes . ',false)'
);
}
$value = '' unless defined($value) && length($value);
my %isSelected = map { $_ => 1 } split( /\s*,\s*/, $value );
my %attrs;
my @defaults;
foreach my $item ( @{ $this->getOptions() } ) {
my $title = $item;
$title = $this->{_descriptions}{$item}
if $this->{_descriptions}{$item};
# NOTE: Does not expand $item in title
$attrs{$item} = {
class => $this->cssClasses('foswikiCheckbox'),
title => $topicObject->expandMacros($title),
};
if ( $isSelected{$item} ) {
# One or the other; not both, or CGI generates two checked="checked"
if ( $this->isValueMapped() ) {
$attrs{$item}{checked} = 'checked';
}
else {
push( @defaults, $item );
}
}
}
my %params = (
-name => $this->{name},
-values => $this->getOptions(),
-defaults => \@defaults,
-columns => $this->{size},
-attributes => \%attrs
);
if ( defined $this->{valueMap} ) {
$params{-labels} = $this->{valueMap};
}
$value = CGI::checkbox_group(%params);
# Item2410: We need a dummy control to detect the case where
# all checkboxes have been deliberately unchecked
# Item3061:
# Don't use CGI, it will insert the sticky value from the query
# once again and we need an empty field here.
$value .= '<input type="hidden" name="' . $this->{name} . '" value="" />';
return ( $extra, $value );
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
TWiki Contributors are listed in the AUTHORS file in the root
of this distribution. NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.