From 5e967d9b823fd384c8e32eab742e9ce423d65ec9 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Sat, 25 Oct 2008 05:38:08 +0000 Subject: [PATCH] added: DataForm can now trigger workflow when adding an entry --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.6.1-7.6.2.pl | 18 +++++ lib/WebGUI/Asset/Wobject/DataForm.pm | 50 ++++++++++++- lib/WebGUI/AssetCollateral/DataForm/Entry.pm | 79 +++++++++++++++++++- lib/WebGUI/i18n/English/Asset_DataForm.pm | 12 +++ 5 files changed, 156 insertions(+), 4 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index d2980dcb0..84f162e36 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -32,6 +32,7 @@ - added a perltidyrc to the docs folder. we'll be using this to clean up code. - fixed: Site Nav navigation template can now be used more than once per page - added: TextArea now supports "maxlength" attribute + - added: DataForm can now run a workflow when an entry is added 7.6.1 diff --git a/docs/upgrades/upgrade_7.6.1-7.6.2.pl b/docs/upgrades/upgrade_7.6.1-7.6.2.pl index d5cda5ae8..e51205fda 100644 --- a/docs/upgrades/upgrade_7.6.1-7.6.2.pl +++ b/docs/upgrades/upgrade_7.6.1-7.6.2.pl @@ -36,6 +36,7 @@ changeDefaultPaginationInSearch($session); upgradeToYui26($session); addUsersOnlineMacro($session); addProfileExtrasField($session); +addWorkflowToDataform( $session ); finish($session); # this line required #---------------------------------------------------------------------------- @@ -107,6 +108,23 @@ sub addProfileExtrasField { print "DONE!\n" unless $quiet; } +#---------------------------------------------------------------------------- +# Add the workflow property to DataForm +sub addWorkflowToDataform { + my $session = shift; + print "\tAdding Workflow to DataForm... " unless $quiet; + + my $sth = $session->db->read('DESCRIBE `DataForm`'); + while (my ($col) = $sth->array) { + if ( $col eq 'workflowIdAddEntry' ) { + print "Already done, skipping.\n" unless $quiet; + return; + } + } + + $session->db->write( "ALTER TABLE DataForm ADD COLUMN workflowIdAddEntry CHAR(22) BINARY" ); + print "DONE!\n" unless $quiet; +} # -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Asset/Wobject/DataForm.pm b/lib/WebGUI/Asset/Wobject/DataForm.pm index 2fe3038a1..e5c780448 100644 --- a/lib/WebGUI/Asset/Wobject/DataForm.pm +++ b/lib/WebGUI/Asset/Wobject/DataForm.pm @@ -314,6 +314,15 @@ sub definition { label => $i18n->get('editForm useCaptcha label'), hoverHelp => $i18n->get('editForm useCaptcha description'), }, + workflowIdAddEntry => { + tab => "properties", + fieldType => "workflow", + defaultValue => undef, + type => "WebGUI::AssetCollateral::DataForm::Entry", + none => 1, + label => $i18n->get('editForm workflowIdAddEntry label'), + hoverHelp => $i18n->get('editForm workflowIdAddEntry description'), + }, fieldConfiguration => { fieldType => 'hidden', }, @@ -1164,12 +1173,16 @@ sub www_editFieldSave { my $fieldName = $form->process('fieldName'); my $newName = $self->session->url->urlize($form->process('newName') || $form->process('label')); $newName =~ tr{-/}{}; + + # Make sure we don't rename special fields if ($fieldName) { my $field = $self->getFieldConfig($fieldName); if ($field->{isMailField}) { $newName = $fieldName; } } + + # Make sure our field name is unique if (!$fieldName || $fieldName ne $newName) { my $i = ''; while ($self->getFieldConfig($newName . $i)) { @@ -1178,6 +1191,7 @@ sub www_editFieldSave { } $newName .= $i; } + my %field = ( width => $form->process("width", 'integer'), label => $form->process("label"), @@ -1191,6 +1205,7 @@ sub www_editFieldSave { vertical => $form->process("vertical", 'yesNo'), extras => $form->process("extras"), ); + my $newSelf = $self->addRevision; if ($fieldName) { if ($fieldName ne $newName) { @@ -1231,6 +1246,8 @@ sub setField { my $self = shift; my $fieldName = shift; my $field = shift; + + $field->{ name } = $fieldName; my $fieldConfig = $self->getFieldConfig; if (!$fieldConfig->{$fieldName}) { @@ -1528,18 +1545,47 @@ sub www_process { }; } - $var->{error_loop} = \@errors; + # Prepare template variables $var = $self->getRecordTemplateVars($var, $entry); + + # If errors, show error page if (@errors) { - $self->prepareFormView; + $var->{error_loop} = \@errors; + $self->prepareViewForm; return $self->processStyle($self->viewForm($var, $entry)); } + + # Send email if ($self->get("mailData") && !$entryId) { $self->sendEmail($var, $entry); } + + # Save entry to database if ($self->get('storeData')) { $entry->save; } + + # Run the workflow + if ( $self->get("workflowIdAddEntry") ) { + my $instanceVar = { + workflowId => $self->get( "workflowIdAddEntry" ), + className => "WebGUI::AssetCollateral::DataForm::Entry", + }; + + # If we've saved the entry, we only need the ID + if ( $self->get( 'storeData' ) ) { + $instanceVar->{ methodName } = "new"; + $instanceVar->{ parameters } = $entry->getId; + } + # We haven't saved the entry, we need the whole thing + else { + $instanceVar->{ methodName } = "newFromHash"; + $instanceVar->{ parameters } = [ $self->getId, $entry->getHash ]; + } + + WebGUI::Workflow::Instance->create( $self->session, $instanceVar )->start; + } + return $self->processStyle($self->processTemplate($var,$self->get("acknowlegementTemplateId"))) if $self->defaultViewForm; return ''; diff --git a/lib/WebGUI/AssetCollateral/DataForm/Entry.pm b/lib/WebGUI/AssetCollateral/DataForm/Entry.pm index 4a7873789..b850c16d0 100644 --- a/lib/WebGUI/AssetCollateral/DataForm/Entry.pm +++ b/lib/WebGUI/AssetCollateral/DataForm/Entry.pm @@ -1,6 +1,5 @@ package WebGUI::AssetCollateral::DataForm::Entry; use strict; -use warnings; our $VERSION = '0.0.1'; @@ -60,6 +59,41 @@ sub fields { return { %{ $entryData } }; } +#---------------------------------------------------------------------------- + +=head2 getHash ( ) + +Gets a hash reference of data for this entry, which looks like this: + + { + DataForm_entryId => "entryId", + userId => "userId", + username => "username", + ipAddress => "0.0.0.0", + assetId => "assetId", + submissionDate => "2008-00-00 00:00:00", # in UTC + entryData => { name => value, name => value, ... }, + } + +=cut + +sub getHash { + my $self = shift; + my $id = id $self; + + my $var = { + DataForm_entryId => $entryId{$id}, + userId => $userId{$id}, + username => $username{$id}, + ipAddress => $ipAddress{$id}, + assetId => $assetId{$id}, + submissionDate => $submissionDate{$id}->toDatabase, + entryData => $entryData{$id}, + }; + + return $var; +} + #------------------------------------------------------------------- sub getCount { my $class = shift; @@ -102,6 +136,16 @@ sub iterateAll { } #------------------------------------------------------------------- + +=head2 new ( asset [, entryId ] ) + +=head2 new ( session, entryId ) + +Instantiate an object. If C is defined, will pull the correct entry +from the database. If C is not defined, will create a new entry. + +=cut + sub new { my ($class, $asset, $entryId) = @_; my $self = register($class); @@ -124,7 +168,6 @@ sub new { if (! defined $properties->{'DataForm_entryId'}) { WebGUI::Error::ObjectNotFound->throw(error => 'no such DataForm_entryId', id => $entryId); } - $session = $session{$id} = $asset->session; if (! $assetId{$id}) { $assetId{$id} = $properties->{assetId}; $asset{$id} = WebGUI::Asset::Wobject::DataForm->new($session, $properties->{assetId}); @@ -140,6 +183,38 @@ sub new { return $self; } +#---------------------------------------------------------------------------- + +=head2 newFromHash ( asset, properties ) + +=head2 newFromHash ( session, assetId, properties ) + +Create a new DataForm entry from the given properties. + +=cut + +sub newFromHash { + my $class = shift; + my $asset = shift; + my $self; + + if ( defined $asset && ref $asset && $asset->isa( 'WebGUI::Asset::Wobject::DataForm' ) ) { + my $properties = shift; + $self = $class->new( $asset ); + $self->setFromHash( $properties ); + } + elsif ( defined $asset && ref $asset && $asset->isa( 'WebGUI::Session' ) ) { + my $session = $asset; + my $assetId = shift; + my $properties = shift; + $asset = WebGUI::Asset->newByDynamicClass( $session, $assetId ); + $self = $class->new( $asset ); + $self->setFromHash( $properties ); + } + + return $self; +} + #------------------------------------------------------------------- sub purgeAssetEntries { my $class = shift; diff --git a/lib/WebGUI/i18n/English/Asset_DataForm.pm b/lib/WebGUI/i18n/English/Asset_DataForm.pm index 6c4a596e5..398ab0d2b 100644 --- a/lib/WebGUI/i18n/English/Asset_DataForm.pm +++ b/lib/WebGUI/i18n/English/Asset_DataForm.pm @@ -1001,6 +1001,18 @@ be useful, others may not.|, lastUpdated => 0, context => q{Description of template variable}, }, + + 'editForm workflowIdAddEntry label' => { + message => q{Add Entry Workflow}, + lastUpdated => 0, + context => q{Label for asset property}, + }, + + 'editForm workflowIdAddEntry description' => { + message => q{Workflow to be run when an entry is added to the DataForm}, + lastUpdated => 0, + context => q{Description of asset property}, + }, }; 1;