Skip to content

Commit

Permalink
Item8392: Complete sort="$fieldDefinition" feature but not using form…
Browse files Browse the repository at this point in the history
… topic :/

.txt changes

git-svn-id: http://svn.foswiki.org/trunk/FormFieldListPlugin@8418 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
PaulHarvey authored and PaulHarvey committed Aug 4, 2010
1 parent a1fe91d commit 3057fdb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
20 changes: 11 additions & 9 deletions data/System/FormFieldListPlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ This plugin uses Foswiki:Extensions/TopicDataHelperPlugin
---+++ Sort options
%TABLE{columnwidths="20%,30%,15%,40%"}%
| *Parameter* | *Comment* | *Default value* | *Example* |
| =sort= | Sorts the retrieved form fields on: %BR%\
name (=$name=) %BR%\
value (=$value=) %BR%\
containing topic (=$topicName=) %BR%\
date (=$fieldDate=) (when field has changed, this is checked against a cache) %BR%\
topic date (=$topicDate=) %BR%\
user (=$topicUser=) %BR%\
The sort order can be specified with parameter =sortorder=. | =$topicName= | =sort="$name"= |
| =sort= | Sorts the retrieved form fields on: <ul>\
<li>name (=$name=)</li>\
<li>value (=$value=)</li>\
<li>containing topic (=$topicName=)</li>\
<li>date (=$fieldDate=) (when field has changed, this is checked against a cache)</li>\
<li>position in form definition (=$fieldDefinition=)</li>\
<li>topic date (=$topicDate=)</li>\
<li>user (=$topicUser=)</li>\
</ul>The sort order can be specified with parameter =sortorder=. | =$topicName= | =sort="$name"= |
| =sortorder= | Lists sorted attachments: ascending or descending. The default sort order depends on the =sort= type: %BR%\
=descending=: default for =$date= (latest file first) %BR%\
=ascending=: default for =$fieldName=, =$fieldValue= and =$topicName= (alphabetically) | see left | =sortorder="ascending"= |
Expand Down Expand Up @@ -171,8 +172,9 @@ Use the notation =NAMEOFPLUGIN_NAMEOFSETTING=, for example: =FORMFIELDLISTPLUGIN

| Authors: | TWiki:Main/BerndRaichle (2004), Foswiki:Main/ArthurClemens (2008, 2009) |
| Copyright &copy;: | TWiki:Main/BerndRaichle (2004), Foswiki:Main/ArthurClemens (2008, 2009) |
| Plugin Version: | 20 Jun 2009 (v2.1) |
| Plugin Version: | 03 Aug 2009 (%$RELEASE%) |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 03 Aug 2010: | v.2.2 [[Foswiki:Main.PaulHarvey][Paul Harvey]]: [[Foswikitask:8392][Item8392]] added =$fieldDefinition= sort attribute to sort fields by their order of appearance in the [[DataForms][DataForm]] definition |
| 20 Jun 2009: | v.2.1 Arthur Clemens: ported to Foswiki. Changed =FORMAT= setting to =DEFAULT_FORMAT=. |
| 25 Oct 2008: | v.2.010 Arthur Clemens: Added param =includemissingfields= to explicitly generate field substitutes. |
| 24 Oct 2008: | v.2.000 Arthur Clemens: Major code refactoring with the addition of numerous parameters. Created unit tests. |
Expand Down
38 changes: 33 additions & 5 deletions lib/Foswiki/Plugins/FormFieldListPlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ sub _handleFormFieldList {
my $properties = {};
$properties->{includeMissingFields} = $inParams->{'doIncludeMissingFields'},
$properties->{formFields} = $formFieldsHash;
$properties->{sortParam} = $inParams->{'sort'};

Foswiki::Plugins::TopicDataHelperPlugin::insertObjectData( $topicData,
\&_createFormFieldData, $properties );
Expand All @@ -144,6 +145,29 @@ sub _handleFormFieldList {
return $formatted;
}

sub _populateOrderingFromFormDefinition {
my ( $web, $topic, $existing ) = @_;

my $topicObject = new Foswiki::Meta($Foswiki::Plugins::SESSION, $web, $topic);
my $startPosition = 1;
my %orderedFields = %$existing;
$topicObject->reload();
while ( my ($field, $order) = each %$existing ) {
if ($order >= $startPosition) {
$startPosition = $order + 1;
}
}
my @fields = $topicObject->find('FIELD');
my $index = $startPosition;
foreach my $fieldref (@fields) {
my %field = %$fieldref;
$orderedFields{$field{name}} = $index;
$index = $index + 1;
}

return \%orderedFields;
}

=pod
Called from Foswiki::Plugins::TopicDataHelperPlugin::insertObjectData.
Expand All @@ -166,6 +190,11 @@ sub _createFormFieldData {
my $formFieldsHash = $inProperties->{formFields};
my $includeMissingFields = $inProperties->{includeMissingFields};

if ( defined $inProperties->{sortParam} and ($inProperties->{sortParam} eq '$fieldDefinition' ) ) {
$formFieldsHash =
_populateOrderingFromFormDefinition($inWeb, $inTopic, $formFieldsHash);
}

# define value for topic key only if topic
# has META:FIELD data
my ( $fields, $meta ) = _getFormFieldsInTopic( $inWeb, $inTopic );
Expand Down Expand Up @@ -460,9 +489,9 @@ sub _sortFields {

# translate input to sort parameters
my $sortOrderParam = $inParams->{'sortorder'} || 'none';
my $sortOrder = $sortInputTable{$sortOrderParam}
|| $Foswiki::Plugins::TopicDataHelperPlugin::sortDirections{'NONE'};

my $sortOrder = $sortInputTable{$sortOrderParam} ||
$Foswiki::Plugins::TopicDataHelperPlugin::sortDirections{'NONE'};
# set default sort order for sort modes
if ( $sortOrder ==
$Foswiki::Plugins::TopicDataHelperPlugin::sortDirections{'NONE'} )
Expand All @@ -474,8 +503,7 @@ sub _sortFields {
# exception for dates: newest on top
$sortOrder = $Foswiki::Plugins::TopicDataHelperPlugin::sortDirections{
'DESCENDING'};
}
else {
} else {

# otherwise sort by default ascending
$sortOrder = $Foswiki::Plugins::TopicDataHelperPlugin::sortDirections{
Expand Down
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/FormFieldListPlugin/FormFieldData.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use strict;
use overload ( '""' => \&as_string );

my %sortKeys = (
'$fieldDefinition' => [ 'fieldDefinition', 'string' ],
'$name' => [ 'name', 'string' ],
'$value' => [ 'value', 'string' ],
'$fieldDate' => [ 'fieldDate', 'integer' ],
Expand Down
13 changes: 9 additions & 4 deletions test/unit/FormFieldListPlugin/FormFieldListPluginTests.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package FormFieldListPluginTests;

use strict;
use warnings;

package FormFieldListPluginTests;
use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );

Expand Down Expand Up @@ -108,14 +110,17 @@ my $allTopics =
"$testForms{topic1}{name}, $testForms{topic2}{name}, $testForms{topic3}{name}, $testForms{topic4}{name}, $testForms{topic5}{name}";

sub new {
my $self = shift()->SUPER::new( 'FormFieldListPluginTests', @_ );
my $self = shift()->SUPER::new( @_ );
return $self;
}

sub set_up {
my $this = shift;
my ($this) = @_;

$this->SUPER::set_up();
$Foswiki::cfg{Plugins}{FormFieldListPlugin}{Enabled} = 1;
$this->{session}->finish();
$this->{session} = Foswiki::new('Foswiki');
$this->_createForms();
}

Expand Down Expand Up @@ -1770,7 +1775,7 @@ sub _simulate_view {
sub _makeDelay {
my ($inDelaySeconds) = @_;

sleep($inDelaySeconds);
#sleep($inDelaySeconds);
}

=pod
Expand Down

0 comments on commit 3057fdb

Please sign in to comment.