/
Form.pm
613 lines (479 loc) · 18.2 KB
/
Form.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# See bottom of file for license and copyright details
=pod
---+ package TWiki::Form
Object representing a single form definition.
Form definitions are mainly used to control rendering of a form for
editing, though there is some application login there that handles
transferring values between edits and saves.
A form definition consists of a TWiki::Form object, which has a list
of field definitions. Each field definition is an object of a type
derived from TWiki::Form::FieldDefinition. These objects are responsible
for the actual syntax and semantics of the field type. Form definitions
are parsed from TWiki tables, and the types are mapped by name to a
class declared in TWiki::Form::* - for example, the =text= type is mapped
to =TWiki::Form::Text= and the =checkbox= type to =TWiki::Form::Checkbox=.
The =TWiki::Form::FieldDefinition= class declares default behaviours for
types that accept a single value in their definitions. The
=TWiki::Form::ListFieldDefinition= extends this for types that have lists
of possible values.
=cut
# The bulk of this object is a parser for form definitions. All the
# intelligence is in the individual field types.
package TWiki::Form;
use strict;
use Assert;
use Error qw( :try );
# The following are reserved as URL parameters to scripts and may not be
# used as field names in forms.
my %reservedFieldNames = map { $_ => 1 }
qw( action breaklock contenttype cover dontnotify editaction
forcenewrevision formtemplate onlynewtopic onlywikiname
originalrev skin templatetopic text topic topicparent user );
=pod
---++ ClassMethod new ( $session, $web, $form, \@def )
Looks up a form in the session object or, if it hasn't been read yet,
reads it frm the form definition topic on disc.
* =$web= - default web to recover form from, if =$form= doesn't
specify a web
* =$form= - name of the form
* =\@def= - optional. A reference to a list of field definitions.
If present, these definitions will be used, rather than any read from
the form definition topic. Note that this array should not be modified
again after being passed into this constructor (it is not copied).
May throw TWiki::OopsException
=cut
sub new {
my ( $class, $session, $web, $form, $def ) = @_;
( $web, $form ) = $session->normalizeWebTopicName( $web, $form );
my $this = $session->{forms}->{"$web.$form"};
unless ($this) {
$this = bless(
{
session => $session,
web => $web,
topic => $form,
},
$class
);
$session->{forms}->{"$web.$form"} = $this;
unless ($def) {
my $store = $session->{store};
# Read topic that defines the form
if ( $store->topicExists( $web, $form ) ) {
my ( $meta, $text ) =
$store->readTopic( $session->{user}, $web, $form, undef );
$this->{fields} = _parseFormDefinition( $this, $meta, $text );
}
else {
delete $session->{forms}->{"$web.$form"};
return undef;
}
}
elsif ( ref($def) eq 'ARRAY' ) {
$this->{fields} = $def;
}
else {
# TWiki::Meta object
$this->{fields} = $this->_extractPseudoFieldDefs($def);
}
}
return $this;
}
=begin twiki
---++ ObjectMethod finish()
Break circular references.
=cut
# Note to developers; please undef *all* fields in the object explicitly,
# whether they are references or not. That way this method is "golden
# documentation" of the live fields in the object.
sub finish {
my $this = shift;
undef $this->{web};
undef $this->{topic};
foreach ( @{ $this->{fields} } ) {
$_->finish();
}
undef $this->{fields};
undef $this->{session};
}
=pod
---++ StaticMethod fieldTitle2FieldName($title) -> $name
Chop out all except A-Za-z0-9_. from a field name to create a
valid "name" for storing in meta-data
=cut
sub fieldTitle2FieldName {
my ($text) = @_;
return '' unless defined($text);
$text =~ s/<nop>//g; # support <nop> character in title
$text =~ s/[^A-Za-z0-9_\.]//g;
return $text;
}
# Get definition from supplied topic text
# Returns array of arrays
# 1st - list fields
# 2nd - name, title, type, size, vals, tooltip, attributes
# Possible attributes are "M" (mandatory field)
sub _parseFormDefinition {
my ( $this, $meta, $text ) = @_;
my $store = $this->{session}->{store};
my @fields = ();
my $inBlock = 0;
$text =~ s/\r//g;
$text =~ s/\\\n//g; # remove trailing '\' and join continuation lines
# | *Name:* | *Type:* | *Size:* | *Value:* | *Tooltip message:* | *Attributes:* |
# Tooltip and attributes are optional
foreach my $line ( split( /\n/, $text ) ) {
if ( $line =~ /^\s*\|.*Name[^|]*\|.*Type[^|]*\|.*Size[^|]*\|/ ) {
$inBlock = 1;
next;
}
# Only insist on first field being present FIXME - use oops page instead?
if ( $inBlock && $line =~ s/^\s*\|\s*// ) {
$line =~ s/\\\|/\007/g; # protect \| from split
my ( $title, $type, $size, $vals, $tooltip, $attributes ) =
map { s/\007/|/g; $_ } split( /\s*\|\s*/, $line );
$title ||= '';
$type ||= '';
$type = lc $type;
$type =~ s/^\s*//go;
$type =~ s/\s*$//go;
$type = 'text' if ( !$type );
$size ||= '';
$vals ||= '';
$vals =
$this->{session}
->handleCommonTags( $vals, $this->{web}, $this->{topic}, $meta );
$vals =~ s/<\/?(nop|noautolink)\/?>//go;
$vals =~ s/^\s+//g;
$vals =~ s/\s+$//g;
$tooltip ||= '';
$attributes ||= '';
$attributes =~ s/\s*//go;
$attributes = '' if ( !$attributes );
my $definingTopic = "";
if ( $title =~ /\[\[(.+)\]\[(.+)\]\]/ ) {
# use common defining topics with different field titles
$definingTopic = fieldTitle2FieldName($1);
$title = $2;
}
my $name = fieldTitle2FieldName($title);
# Rename fields with reserved names
if ( $reservedFieldNames{$name} ) {
$name .= '_';
}
my $fieldDef = $this->createField(
$type,
name => $name,
title => $title,
size => $size,
value => $vals,
tooltip => $tooltip,
attributes => $attributes,
definingTopic => $definingTopic,
web => $this->{web},
topic => $this->{topic}
);
push( @fields, $fieldDef );
$this->{mandatoryFieldsPresent} ||= $fieldDef->isMandatory();
}
else {
$inBlock = 0;
}
}
return \@fields;
}
# PROTECTED
# Create a field object. Done like this so that this method can be
# overridden by subclasses to extend the range of field types.
sub createField {
my $this = shift;
my $type = shift;
my $class = $type;
$class =~ /^(\w*)/; # cut off +buttons etc
#The following is a workaround for a bug in Perl 5.8.4 that was ultimately fixed on Perl 5.8.7-8
# see http://bugs.debian.org/303308
# using $class=TWiki::Sandbox::untaintUnchecked($class) also works but is one more method call.
my $workaround = $1;
$class = 'TWiki::Form::' . ucfirst($workaround);
eval 'require ' . $class;
if ($@) {
# Type not available; use base type
require TWiki::Form::FieldDefinition;
$class = 'TWiki::Form::FieldDefinition';
}
return $class->new( session => $this->{session}, type => $type, @_ );
}
# Generate a link to the given topic, so we can bring up details in a
# separate window.
sub _link {
my ( $this, $meta, $string, $tooltip, $topic ) = @_;
$string =~ s/[\[\]]//go;
$topic ||= $string;
my $defaultToolTip =
$this->{session}->i18n->maketext('Details in separate window');
$tooltip ||= $defaultToolTip;
my $web;
( $web, $topic ) =
$this->{session}->normalizeWebTopicName( $this->{web}, $topic );
my $link;
my $store = $this->{session}->{store};
if ( $store->topicExists( $web, $topic ) ) {
$link = CGI::a(
{
target => $topic,
onclick => 'return launchWindow("'
. $web . '","'
. $topic . '")',
title => $tooltip,
href =>
$this->{session}->getScriptUrl( 0, 'view', $web, $topic ),
rel => 'nofollow'
},
$string
);
}
else {
my $expanded =
$this->{session}->handleCommonTags( $string, $web, $topic, $meta );
if ( $tooltip ne $defaultToolTip ) {
$link = CGI::span( { title => $tooltip }, $expanded );
}
else {
$link = $expanded;
}
}
return $link;
}
=pod
---++ ObjectMethod renderForEdit( $web, $topic, $meta ) -> $html
* =$web= the web of the topic being rendered
* =$topic= the topic being rendered
* =$meta= the meta data for the form
Render the form fields for entry during an edit session, using data values
from $meta
=cut
sub renderForEdit {
my ( $this, $web, $topic, $meta ) = @_;
ASSERT( $meta->isa('TWiki::Meta') ) if DEBUG;
require CGI;
my $session = $this->{session};
if ( $this->{mandatoryFieldsPresent} ) {
$session->enterContext('mandatoryfields');
}
my $tmpl = $session->templates->readTemplate("form");
$tmpl = $session->handleCommonTags( $tmpl, $web, $topic, $meta );
# Note: if WEBFORMS preference is not set, can only delete form.
$tmpl =~ s/%FORMTITLE%/_link(
$this, $meta, $this->{web}.'.'.$this->{topic})/ge;
my ( $text, $repeatTitledText, $repeatUntitledText, $afterText ) =
split( /%REPEAT%/, $tmpl );
foreach my $fieldDef ( @{ $this->{fields} } ) {
my $value;
my $tooltip = $fieldDef->{tooltip};
my $definingTopic = $fieldDef->{definingTopic};
my $title = $fieldDef->{title};
my $tmp = '';
if ( !$title && !$fieldDef->isEditable() ) {
# Special handling for untitled labels.
# SMELL: Assumes that uneditable fields are not multi-valued
$tmp = $repeatUntitledText;
$value = $session->{renderer}->getRenderedVersion(
$session->handleCommonTags(
$fieldDef->{value},
$web, $topic, $meta
)
);
}
else {
$tmp = $repeatTitledText;
if ( defined( $fieldDef->{name} ) ) {
my $field = $meta->get( 'FIELD', $fieldDef->{name} );
$value = $field->{value};
}
my $extra = ''; # extras on col 0
unless ( defined($value) ) {
my $dv = $fieldDef->getDefaultValue($value);
if ( defined($dv) ) {
$dv =
$this->{session}
->handleCommonTags( $dv, $web, $topic, $meta );
$value = TWiki::expandStandardEscapes($dv); # Item2837
}
}
# Give plugin field types a chance first (but no chance to add to
# col 0 :-(
# SMELL: assumes that the field value is a string
my $output = $session->{plugins}->dispatch(
'renderFormFieldForEditHandler', $fieldDef->{name},
$fieldDef->{type}, $fieldDef->{size},
$value, $fieldDef->{attributes},
$fieldDef->{value}
);
if ($output) {
$value = $output;
}
else {
( $extra, $value ) =
$fieldDef->renderForEdit( $web, $topic, $value );
}
if ( $fieldDef->isMandatory() ) {
$extra .= CGI::span( { class => 'twikiAlert' }, ' *' );
}
$tmp =~ s/%ROWTITLE%/_link(
$this, $meta, $title, $tooltip, $definingTopic )/ge;
$tmp =~ s/%ROWEXTRA%/$extra/g;
}
$tmp =~ s/%ROWVALUE%/$value/g;
$text .= $tmp;
}
$text .= $afterText;
return $text;
}
=pod
---++ ObjectMethod renderHidden( $meta ) -> $html
Render form fields found in the meta as hidden inputs, so they pass
through edits untouched.
=cut
sub renderHidden {
my ( $this, $meta ) = @_;
ASSERT( $meta->isa('TWiki::Meta') ) if DEBUG;
my $text = '';
foreach my $field ( @{ $this->{fields} } ) {
$text .= $field->renderHidden($meta);
}
return $text;
}
=pod
---++ ObjectMethod getFieldValuesFromQuery($query, $metaObject) -> ( $seen, \@missing )
Extract new values for form fields from a query.
* =$query= - the query
* =$metaObject= - the meta object that is storing the form values
For each field, if there is a value in the query, use it.
Otherwise if there is already entry for the field in the meta, keep it.
Returns the number of fields which had values provided by the query,
and a references to an array of the names of mandatory fields that were
missing from the query.
=cut
sub getFieldValuesFromQuery {
my ( $this, $query, $meta ) = @_;
ASSERT( $meta->isa('TWiki::Meta') ) if DEBUG;
my @missing;
my $seen = 0;
# Remove the old defs so we apply the
# order in the form definition, and not the
# order in the previous meta object. See Item1982.
my @old = $meta->find('FIELD');
$meta->remove('FIELD');
foreach my $fieldDef ( @{ $this->{fields} } ) {
my ( $set, $present ) =
$fieldDef->populateMetaFromQueryData( $query, $meta, \@old );
if ($present) {
$seen++;
}
if ( !$set && $fieldDef->isMandatory() ) {
# Remember missing mandatory fields
push( @missing, $fieldDef->{title} || "unnamed field" );
}
}
return ( $seen, \@missing );
}
=pod
---++ ObjectMethod isTextMergeable( $name ) -> $boolean
* =$name= - name of a form field (value of the =name= attribute)
Returns true if the type of the named field allows it to be text-merged.
If the form does not define the field, it is assumed to be mergeable.
=cut
sub isTextMergeable {
my ( $this, $name ) = @_;
my $fieldDef = $this->getField($name);
if ($fieldDef) {
return $fieldDef->isTextMergeable();
}
# Field not found - assume it is mergeable
return 1;
}
=pod
---++ ObjectMethod getField( $name ) -> $fieldDefinition
* =$name= - name of a form field (value of the =name= attribute)
Returns a =TWiki::Form::FieldDefinition=, or undef if the form does not
define the field.
=cut
sub getField {
my ( $this, $name ) = @_;
foreach my $fieldDef ( @{ $this->{fields} } ) {
return $fieldDef if ( $fieldDef->{name} && $fieldDef->{name} eq $name );
}
return undef;
}
=pod
---++ ObjectMethod getFields() -> \@fields
Return a list containing references to field name/value pairs.
Each entry in the list has a {name} field and a {value} field. It may
have other fields as well, which caller should ignore. The
returned list should be treated as *read only* (must not be written to).
=cut
sub getFields {
my $this = shift;
return $this->{fields};
}
sub renderForDisplay {
my ( $this, $meta ) = @_;
my $templates = $this->{session}->templates;
$templates->readTemplate('formtables');
my $text = $templates->expandTemplate('FORM:display:header');
my $rowTemplate = $templates->expandTemplate('FORM:display:row');
foreach my $fieldDef ( @{ $this->{fields} } ) {
my $fm = $meta->get( 'FIELD', $fieldDef->{name} );
next unless $fm;
my $fa = $fm->{attributes} || '';
unless ( $fa =~ /H/ ) {
my $row = $rowTemplate;
# Legacy; was %A_TITLE% before it was $title
$row =~ s/%A_TITLE%/\$title/g;
$row =~ s/%A_VALUE%/\$value/g; # Legacy
$text .= $fieldDef->renderForDisplay( $row, $fm->{value} );
}
}
$text .= $templates->expandTemplate('FORM:display:footer');
$text =~ s/%A_TITLE%/$this->{web}.$this->{topic}][$this->{topic}/g;
return $text;
}
# extractPseudoFieldDefs( $meta ) -> $fieldDefs
# Examine the FIELDs in $meta and reverse-engineer a set of field
# definitions that can be used to construct a new "pseudo-form". This
# fake form can be used to support editing of topics that have an attached
# form that has no definition topic.
sub _extractPseudoFieldDefs {
my ( $this, $meta ) = @_;
my @fields = $meta->find('FIELD');
my @fieldDefs;
require TWiki::Form::FieldDefinition;
foreach my $field (@fields) {
# Fields are name, value, title, but there is no other type
# information so we have to treat them all as "text" :-(
my $fieldDef = new TWiki::Form::FieldDefinition(
session => $this->{session},
name => $field->{name},
title => $field->{title} || $field->{name},
attributes => $field->{attributes} || ''
);
push( @fieldDefs, $fieldDef );
}
return \@fieldDefs;
}
1;
__DATA__
Module of Foswiki - The Free Open Source Wiki, http://foswiki.org/, http://TWiki.org/
Copyright (C) 2001-2007 Peter Thoeny, peter@thoeny.org
and 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.