Skip to content

Commit

Permalink
Item13587: SetFormValuesPlugin enhanced with new conditional rule
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethLavrsen committed Jul 30, 2015
1 parent b91a4fe commit e705ed5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
14 changes: 9 additions & 5 deletions data/System/SetFormValuesPlugin.txt
Expand Up @@ -31,10 +31,12 @@ An example of a rules topic is

* Set NOAUTOLINK = on

| *Form* | *Match field* | *Match value* | *Target Field* | *Target Value* |
| SubmissionRecordForm | State | New | DateCreated | $date |
| SubmissionRecordForm | State | Assigned | DateAssigned | $date |
| SubmissionRecordForm | State | Completed | DateCompleted | $date |
| *Form* | *Match field* | *Match value* | *Target Field* | *Target Value* | *Condition* |
| SubmissionRecordForm | State | New | DateCreated | $date | |
| SubmissionRecordForm | State | Assigned | DateAssigned | $date | |
| SubmissionRecordForm | State | Performed | DatePerformed | $date | |
| SubmissionRecordForm | State | Completed | DateCompleted | $date | |
| SubmissionRecordForm | State | Completed | DatePerformed | $date | EMPTY |

</verbatim>

Expand All @@ -45,6 +47,7 @@ The columns are
* The value to which this field must change. Simple regex are allowed incl .* (match any change)
* The name of the target field
* The value of the target field. You can use $date and $time to define date and time for the field. Date follows the format for the Foswiki site
* Optional condition. Only valid value is to leave blank or the exact string EMPTY. The EMPTY condition means that the value is only set if the field is empty. This prevents overwriting an existing value. In the example above it is used to set the field "DatePerformed" when the State changes to Completed if the State has never been set to Performed.
* The NOAUTOLINK is only to prevent the table from showing field names as broken wiki links in case no topic is defined for the field name
</verbatim>

Expand All @@ -59,12 +62,13 @@ One line description, required for extensions repository catalog.
---++ Plugin Info

| Author: | KennethLavrsen |
| Copyright: | 2013-2014, KennethLavrsen, All Rights Reserved |
| Copyright: | 2013-2015, KennethLavrsen, All Rights Reserved |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Dependencies: | %$DEPENDENCIES% |
| Version: | %$VERSION% |
| Release: | %$RELEASE% |
| Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; |
| 1.1 (30 Jul 2015): | Added the Condition attribute. |
| 1.0 (30 Jul 2014): | Initial version |
| Home: | http://foswiki.org/Extensions/%TOPIC% |
| Support: | http://foswiki.org/Support/%TOPIC% |
21 changes: 14 additions & 7 deletions lib/Foswiki/Plugins/SetFormValuesPlugin.pm
Expand Up @@ -97,7 +97,7 @@ use Foswiki::Time ();
#
# These statements MUST be on the same line. See "perldoc version" for more
# information on version strings.
our $VERSION = '1.0';
our $VERSION = '1.1';

# $RELEASE is used in the "Find More Extensions" automation in configure.
# It is a manually maintained string used to identify functionality steps.
Expand All @@ -111,7 +111,7 @@ our $VERSION = '1.0';
# topic - if you use %$RELEASE% with BuildContrib this is done automatically.
# It is preferred to keep this compatible with $VERSION. At some future
# date, Foswiki will deprecate RELEASE and use the VERSION string.
our $RELEASE = '1.0';
our $RELEASE = '1.1';

# Short description of this plugin
# One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
Expand Down Expand Up @@ -248,7 +248,7 @@ sub beforeSaveHandler {
return 1 unless ( $rulesTopic = Foswiki::Func::getPreferencesValue('SETFORMVALUESPLUGIN_RULES') );
$rulesTopic =~ s/\s+$//;

my ( $matchForm, $matchField, $matchValue, $targetField, $targetValue );
my ( $matchForm, $matchField, $matchValue, $targetField, $targetValue, $targetRule );

my $rulesWeb = $web;
if ( $rulesTopic =~ /(.+)\.(.+)/ ) {
Expand All @@ -259,9 +259,9 @@ sub beforeSaveHandler {
foreach (
split( /\n/, Foswiki::Func::readTopicText( $rulesWeb, $rulesTopic, undef, 1 ) ) ) {

# form, match field, match value, target field, target value
if ( m/\|\s*([^\s|*]+)\s*\|\s*([^\|]+?)\s*\|\s*([^\|]*?)\s*\|\s*([^\|]+?)\s*\|\s*([^\|]*?)\s*\|\s*$/ ) {
( $matchForm, $matchField, $matchValue, $targetField, $targetValue ) = ($1, $2, $3, $4, $5 );
# form, match field, match value, target field, target value, condition (optional for backwards compatibility)
if ( m/\|\s*([^\s|*]+)\s*\|\s*([^\|]+?)\s*\|\s*([^\|]*?)\s*\|\s*([^\|]+?)\s*\|\s*([^\|]*?)\s*\|(?:\s*([^\|]*?)\s*\|)\s*$/ ) {
( $matchForm, $matchField, $matchValue, $targetField, $targetValue, $targetRule ) = ($1, $2, $3, $4, $5, $6 );
} else {
next;
}
Expand All @@ -270,7 +270,6 @@ sub beforeSaveHandler {
next unless ( $matchForm && $matchField && $targetField );

if ( $meta->getFormName eq $matchForm ) {
#my $fieldName = "State";

my ( $oldmeta, $oldtext ) = Foswiki::Func::readTopic( $web, $topic );

Expand All @@ -280,12 +279,20 @@ sub beforeSaveHandler {
my $oldValue = $oldfieldhashref ?
$oldmeta->get( 'FIELD', $matchField )->{'value'} :
'';

my $newfieldhashref = $meta->get( 'FIELD', $matchField );
next unless $newfieldhashref;

my $newValue = $newfieldhashref ?
$meta->get( 'FIELD', $matchField )->{'value'} :
'';

my $oldTargetFieldhashref = $oldmeta->get( 'FIELD', $targetField );
my $oldTargetValue = $oldTargetFieldhashref ?
$oldmeta->get( 'FIELD', $targetField )->{'value'} :
'';

next if ( $targetRule eq 'EMPTY' && $oldTargetValue ne '' );

my $currentDate = Foswiki::Time::formatTime( time(), $Foswiki::cfg{DefaultDateFormat} );
my $currentTime = Foswiki::Time::formatTime( time(), '$hour:$min');
Expand Down

0 comments on commit e705ed5

Please sign in to comment.