lib/TWiki/Form/ListFieldDefinition.pm
changeset 0 414e01d06fd5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/Form/ListFieldDefinition.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +# See bottom of file for license and copyright details
     1.5 +
     1.6 +=pod
     1.7 +
     1.8 +---++ package TWiki::Form::ListFieldDefinition
     1.9 +Form field definitions that accept lists of values in the field definition.
    1.10 +This is different to being multi-valued, which means the field type
    1.11 +can *store* multiple values.
    1.12 +
    1.13 +=cut
    1.14 +
    1.15 +package TWiki::Form::ListFieldDefinition;
    1.16 +use base 'TWiki::Form::FieldDefinition';
    1.17 +
    1.18 +use strict;
    1.19 +use Assert;
    1.20 +
    1.21 +=begin twiki
    1.22 +
    1.23 +---++ ObjectMethod finish()
    1.24 +Break circular references.
    1.25 +
    1.26 +=cut
    1.27 +
    1.28 +# Note to developers; please undef *all* fields in the object explicitly,
    1.29 +# whether they are references or not. That way this method is "golden
    1.30 +# documentation" of the live fields in the object.
    1.31 +sub finish {
    1.32 +    my $this = shift;
    1.33 +    $this->SUPER::finish();
    1.34 +    undef $this->{_options};
    1.35 +}
    1.36 +
    1.37 +# PROTECTED - parse the {value} and extract a list of options.
    1.38 +# Done lazily to avoid repeated topic reads.
    1.39 +sub getOptions {
    1.40 +    # $web and $topic are where the form definition lives
    1.41 +    my $this = shift;
    1.42 +
    1.43 +    return $this->{_options} if $this->{_options};
    1.44 +
    1.45 +    my @vals = ();
    1.46 +
    1.47 +    @vals = split( /,/, $this->{value} );
    1.48 +    if( !scalar( @vals )) {
    1.49 +        my $topic = $this->{definingTopic} || $this->{name};
    1.50 +        my $session = $this->{session};
    1.51 +        my( $fieldWeb, $fieldTopic ) =
    1.52 +          $session->normalizeWebTopicName( $this->{web}, $topic );
    1.53 +        my $store = $session->{store};
    1.54 +        if ( $store->topicExists( $fieldWeb, $fieldTopic ) ) {
    1.55 +            my( $meta, $text ) =
    1.56 +              $store->readTopic( $session->{user},
    1.57 +                                 $fieldWeb, $fieldTopic, undef );
    1.58 +            # Process SEARCHES for Lists
    1.59 +            $text = $this->{session}->handleCommonTags(
    1.60 +                $text, $this->{web}, $topic, $meta);
    1.61 +            # SMELL: yet another table parser
    1.62 +            my $inBlock = 0;
    1.63 +            foreach( split( /\r?\n/, $text ) ) {
    1.64 +                if( /^\s*\|\s*\*Name\*\s*\|/ ) {
    1.65 +                    $inBlock = 1;
    1.66 +                } elsif( /^\s*\|\s*([^|]*?)\s*\|/ ) {
    1.67 +                    push( @vals, $1 ) if( $inBlock );
    1.68 +                } else {
    1.69 +                    $inBlock = 0;
    1.70 +                }
    1.71 +            }
    1.72 +        }
    1.73 +    }
    1.74 +    @vals = map { $_ =~ s/^\s*(.*)\s*$/$1/; $_; } @vals;
    1.75 +
    1.76 +    $this->{_options} = \@vals;
    1.77 +
    1.78 +    return $this->{_options};
    1.79 +}
    1.80 +
    1.81 +1;
    1.82 +__DATA__
    1.83 +
    1.84 +Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
    1.85 +
    1.86 +Copyright (C) 2001-2007 TWiki Contributors. All Rights Reserved.
    1.87 +TWiki Contributors are listed in the AUTHORS file in the root of
    1.88 +this distribution. NOTE: Please extend that file, not this notice.
    1.89 +
    1.90 +This program is free software; you can redistribute it and/or
    1.91 +modify it under the terms of the GNU General Public License
    1.92 +as published by the Free Software Foundation; either version 2
    1.93 +of the License, or (at your option) any later version. For
    1.94 +more details read LICENSE in the root of this distribution.
    1.95 +
    1.96 +This program is distributed in the hope that it will be useful,
    1.97 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.98 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.99 +
   1.100 +As per the GPL, removal of this notice is prohibited.
   1.101 +