Changeset 412


Ignore:
Timestamp:
11/03/08 13:48:21 (3 years ago)
Author:
CrawfordCurrie
Message:

Item67: a missing form definition would make a topic uneditable

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/UnitTestContrib/test/unit/FormDefTests.pm

    r14 r412  
    176176} 
    177177 
     178sub test_makeFromMeta { 
     179    my $this = shift; 
     180    $this->{twiki}->{store}->saveTopic( 
     181        $this->{twiki}->{user}, $this->{test_web}, 'SplodgeOne', <<FORM); 
     182%META:FORM{name="NonExistantForm"}% 
     183%META:FIELD{name="Ecks" attributes="" title="X" value="Blah"}% 
     184FORM 
     185    my ($meta, $text) = 
     186      $this->{twiki}->{store}->readTopic( 
     187          undef, $this->{test_web}, 'SplodgeOne'); 
     188    my $form = new TWiki::Form( 
     189        $this->{twiki}, $this->{test_web}, 'NonExistantForm', $meta); 
     190    my $f = $form->getField('Ecks'); 
     191    $this->assert_str_equals('', $f->getDefaultValue()); 
     192    $this->assert_str_equals('Ecks', $f->{name}); 
     193    $this->assert_str_equals('X', $f->{title}); 
     194    $this->assert_str_equals('', $f->{size}); 
     195} 
     196 
    1781971; 
  • trunk/core/lib/TWiki/Form.pm

    r411 r412  
    4444=pod 
    4545 
    46 ---++ ClassMethod new ( $session, $web, $form, $def ) 
     46---++ ClassMethod new ( $session, $web, $form, \@def ) 
    4747 
    4848Looks up a form in the session object or, if it hasn't been read yet, 
    4949reads it frm the form definition topic on disc. 
    50    * $web - default web to recover form from, if $form doesn't specify a web 
    51    * =$form= - topic name to read form definition from 
    52    * =$def= - optional. a reference to a list of field definitions. if present, 
    53               these definitions will be used, rather than those in =$form=. 
     50   * =$web= - default web to recover form from, if =$form= doesn't 
     51     specify a web 
     52   * =$form= - name of the form 
     53   * =\@def= - optional. A reference to a list of field definitions. 
     54     If present, these definitions will be used, rather than any read from 
     55     the form definition topic. Note that this array should not be modified 
     56     again after being passed into this constructor (it is not copied). 
    5457 
    5558May throw TWiki::OopsException 
     
    8184 
    8285            # Read topic that defines the form 
    83             unless ( $store->topicExists( $web, $form ) ) { 
     86            if ( $store->topicExists( $web, $form ) ) { 
     87                my ( $meta, $text ) = 
     88                  $store->readTopic( $session->{user}, $web, $form, undef ); 
     89 
     90                $this->{fields} = _parseFormDefinition( $this, $meta, $text ); 
     91            } 
     92            else { 
     93                delete $session->{forms}->{"$web.$form"}; 
    8494                return undef; 
    8595            } 
    86             my ( $meta, $text ) = 
    87               $store->readTopic( $session->{user}, $web, $form, undef ); 
    88  
    89             $this->{fields} = _parseFormDefinition( $this, $meta, $text ); 
    90  
     96        } 
     97        elsif ( ref($def) eq 'ARRAY' ) { 
     98            $this->{fields} = $def; 
    9199        } 
    92100        else { 
    93101 
    94             $this->{fields} = $def; 
    95  
     102            # TWiki::Meta object 
     103            $this->{fields} = $this->_extractPseudoFieldDefs($def); 
    96104        } 
    97105    } 
     
    182190            $vals =~ s/^\s+//g; 
    183191            $vals =~ s/\s+$//g; 
    184  
    185             # SMELL: This expansion of $users is undocumented, AFAICT not 
    186             # used, and downright *dangerous* (it won't work with a non-TWiki 
    187             # user mapping for example) so in the interests of good hygiene, 
    188             # I have removed it (CC, 30 Jun 07). 
    189             #if( $vals eq '$users' ) { 
    190             #    $vals = $TWiki::cfg{UsersWebName} . '.' . 
    191             #      join( ", ${TWiki::cfg{UsersWebName}}.", 
    192             #        ( $store->getTopicNames( $TWiki::cfg{UsersWebName} ))); 
    193             #} 
    194192 
    195193            $tooltip ||= ''; 
     
    566564} 
    567565 
     566# extractPseudoFieldDefs( $meta ) -> $fieldDefs 
     567# Examine the FIELDs in $meta and reverse-engineer a set of field 
     568# definitions that can be used to construct a new "pseudo-form". This 
     569# fake form can be used to support editing of topics that have an attached 
     570# form that has no definition topic. 
     571sub _extractPseudoFieldDefs { 
     572    my ( $this, $meta ) = @_; 
     573    my @fields = $meta->find('FIELD'); 
     574    my @fieldDefs; 
     575    require TWiki::Form::FieldDefinition; 
     576    foreach my $field (@fields) { 
     577 
     578        # Fields are name, value, title, but there is no other type 
     579        # information so we have to treat them all as "text" :-( 
     580        my $fieldDef = new TWiki::Form::FieldDefinition( 
     581            session    => $this->{session}, 
     582            name       => $field->{name}, 
     583            title      => $field->{title} || $field->{name}, 
     584            attributes => $field->{attributes} || '' 
     585        ); 
     586        push( @fieldDefs, $fieldDef ); 
     587    } 
     588    return \@fieldDefs; 
     589} 
     590 
    5685911; 
    569592 
  • trunk/core/lib/TWiki/Form/FieldDefinition.pm

    r411 r412  
    3636    $attrs{attributes} ||= ''; 
    3737    $attrs{type}       ||= '';    # default 
     38    $attrs{size}       ||= ''; 
    3839    $attrs{size} =~ s/^\s*//; 
    3940    $attrs{size} =~ s/\s*$//; 
  • trunk/core/lib/TWiki/Meta.pm

    r411 r412  
    628628    } 
    629629    else { 
    630         return CGI::span( { class => 'twikiAlert' }, 
    631             "Form definition '$fname' not found" ); 
     630 
     631        # Make pseudo-form from field data 
     632        $form = 
     633          new TWiki::Form( $this->{_session}, $this->{_web}, $fname, $this ); 
     634        return CGI::span( 
     635            { class => 'twikiAlert' }, 
     636            "%MAKETEXT{\"Form definition '[_1]' not found\" args=\"$fname\"}%" 
     637        ) . $form->renderForDisplay($this); 
    632638    } 
    633639} 
  • trunk/core/lib/TWiki/UI/Edit.pm

    r411 r412  
    351351        require TWiki::Form; 
    352352        my $formDef = new TWiki::Form( $session, $templateWeb, $form ); 
    353         unless ($formDef) { 
    354             throw TWiki::OopsException( 
    355                 'attention', 
    356                 def    => 'no_form_def', 
    357                 web    => $session->{webName}, 
    358                 topic  => $session->{topicName}, 
    359                 params => [ $templateWeb, $form ] 
    360             ); 
    361         } 
     353        if ( !$formDef ) { 
     354 
     355            # Reverse-engineer a form definition from the meta-data. 
     356            $formDef = new TWiki::Form( $session, $templateWeb, $form, $meta ); 
     357        } 
     358 
     359        # Update with field values from the query 
    362360        $formDef->getFieldValuesFromQuery( $session->{request}, $meta ); 
    363361 
  • trunk/core/lib/TWiki/UI/Save.pm

    r411 r412  
    237237        $formDef = new TWiki::Form( $session, $webName, $formName ); 
    238238        unless ($formDef) { 
    239             throw TWiki::OopsException( 
    240                 'attention', 
    241                 def    => 'no_form_def', 
    242                 web    => $session->{webName}, 
    243                 topic  => $session->{topicName}, 
    244                 params => [ $webName, $formName ] 
    245             ); 
     239            unless ($prevMeta) { 
     240                throw TWiki::OopsException( 
     241                    'attention', 
     242                    def    => 'no_form_def', 
     243                    web    => $session->{webName}, 
     244                    topic  => $session->{topicName}, 
     245                    params => [ $webName, $formName ] 
     246                ); 
     247            } 
     248 
     249            # Recreate the form fields from the previous rev of the topic. 
     250            $formDef = 
     251              new TWiki::Form( $session, $webName, $formName, $prevMeta ); 
    246252        } 
    247253        $newMeta->put( 'FORM', { name => $formName } ); 
Note: See TracChangeset for help on using the changeset viewer.