lib/TWiki/Form/ListFieldDefinition.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
     1 # See bottom of file for license and copyright details
     2 
     3 =pod
     4 
     5 ---++ package TWiki::Form::ListFieldDefinition
     6 Form field definitions that accept lists of values in the field definition.
     7 This is different to being multi-valued, which means the field type
     8 can *store* multiple values.
     9 
    10 =cut
    11 
    12 package TWiki::Form::ListFieldDefinition;
    13 use base 'TWiki::Form::FieldDefinition';
    14 
    15 use strict;
    16 use Assert;
    17 
    18 =begin twiki
    19 
    20 ---++ ObjectMethod finish()
    21 Break circular references.
    22 
    23 =cut
    24 
    25 # Note to developers; please undef *all* fields in the object explicitly,
    26 # whether they are references or not. That way this method is "golden
    27 # documentation" of the live fields in the object.
    28 sub finish {
    29     my $this = shift;
    30     $this->SUPER::finish();
    31     undef $this->{_options};
    32 }
    33 
    34 # PROTECTED - parse the {value} and extract a list of options.
    35 # Done lazily to avoid repeated topic reads.
    36 sub getOptions {
    37     # $web and $topic are where the form definition lives
    38     my $this = shift;
    39 
    40     return $this->{_options} if $this->{_options};
    41 
    42     my @vals = ();
    43 
    44     @vals = split( /,/, $this->{value} );
    45     if( !scalar( @vals )) {
    46         my $topic = $this->{definingTopic} || $this->{name};
    47         my $session = $this->{session};
    48         my( $fieldWeb, $fieldTopic ) =
    49           $session->normalizeWebTopicName( $this->{web}, $topic );
    50         my $store = $session->{store};
    51         if ( $store->topicExists( $fieldWeb, $fieldTopic ) ) {
    52             my( $meta, $text ) =
    53               $store->readTopic( $session->{user},
    54                                  $fieldWeb, $fieldTopic, undef );
    55             # Process SEARCHES for Lists
    56             $text = $this->{session}->handleCommonTags(
    57                 $text, $this->{web}, $topic, $meta);
    58             # SMELL: yet another table parser
    59             my $inBlock = 0;
    60             foreach( split( /\r?\n/, $text ) ) {
    61                 if( /^\s*\|\s*\*Name\*\s*\|/ ) {
    62                     $inBlock = 1;
    63                 } elsif( /^\s*\|\s*([^|]*?)\s*\|/ ) {
    64                     push( @vals, $1 ) if( $inBlock );
    65                 } else {
    66                     $inBlock = 0;
    67                 }
    68             }
    69         }
    70     }
    71     @vals = map { $_ =~ s/^\s*(.*)\s*$/$1/; $_; } @vals;
    72 
    73     $this->{_options} = \@vals;
    74 
    75     return $this->{_options};
    76 }
    77 
    78 1;
    79 __DATA__
    80 
    81 Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
    82 
    83 Copyright (C) 2001-2007 TWiki Contributors. All Rights Reserved.
    84 TWiki Contributors are listed in the AUTHORS file in the root of
    85 this distribution. NOTE: Please extend that file, not this notice.
    86 
    87 This program is free software; you can redistribute it and/or
    88 modify it under the terms of the GNU General Public License
    89 as published by the Free Software Foundation; either version 2
    90 of the License, or (at your option) any later version. For
    91 more details read LICENSE in the root of this distribution.
    92 
    93 This program is distributed in the hope that it will be useful,
    94 but WITHOUT ANY WARRANTY; without even the implied warranty of
    95 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    96 
    97 As per the GPL, removal of this notice is prohibited.
    98