Permalink
Browse files

Item13587: SetFormValuesPlugin enhanced with new conditional rule

  • Loading branch information...
1 parent b91a4fe commit e705ed539182f3c981ecb736f2f533432c3ebd34 @KennethLavrsen KennethLavrsen committed Jul 30, 2015
Showing with 23 additions and 12 deletions.
  1. +9 −5 data/System/SetFormValuesPlugin.txt
  2. +14 −7 lib/Foswiki/Plugins/SetFormValuesPlugin.pm
@@ -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>
@@ -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>
@@ -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% |
@@ -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.
@@ -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:
@@ -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 =~ /(.+)\.(.+)/ ) {
@@ -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;
}
@@ -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 );
@@ -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');

0 comments on commit e705ed5

Please sign in to comment.