-sub Create {
- my $self = shift;
- my %args = ( id => undef,
- TimeTaken => 0,
- Ticket => 0 ,
- Type => 'undefined',
- Data => '',
- Field => undef,
- OldValue => undef,
- NewValue => undef,
- MIMEObj => undef,
- ActivateScrips => 1,
- @_
- );
-
- #if we didn't specify a ticket, we need to bail
- unless ( $args{'Ticket'} ) {
- return(0, "RT::Transaction->Create couldn't, as you didn't specify a ticket id");
- }
-
- #lets create our transaction
- my $id = $self->SUPER::Create(Ticket => $args{'Ticket'},
- TimeTaken => $args{'TimeTaken'},
- Type => $args{'Type'},
- Data => $args{'Data'},
- Field => $args{'Field'},
- OldValue => $args{'OldValue'},
- NewValue => $args{'NewValue'},
- Created => $args{'Created'}
- );
- $self->Load($id);
- $self->_Attach($args{'MIMEObj'})
- if defined $args{'MIMEObj'};
-
- #Provide a way to turn off scrips if we need to
- if ($args{'ActivateScrips'}) {
-
- #We're really going to need a non-acled ticket for the scrips to work
- my $TicketAsSystem = RT::Ticket->new($RT::SystemUser);
- $TicketAsSystem->Load($args{'Ticket'}) ||
- $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}\n");
-
- my $TransAsSystem = RT::Transaction->new($RT::SystemUser);
- $TransAsSystem->Load($self->id) ||
- $RT::Logger->err("$self couldn't load a copy of itself as superuser\n");
-
- # {{{ Deal with Scrips
-
- #Load a scripscopes object
- use RT::Scrips;
- my $PossibleScrips = RT::Scrips->new($RT::SystemUser);
-
- $PossibleScrips->LimitToQueue($TicketAsSystem->QueueObj->Id); #Limit it to $Ticket->QueueObj->Id
- $PossibleScrips->LimitToGlobal(); # or to "global"
- my $ConditionsAlias = $PossibleScrips->NewAlias('ScripConditions');
-
- $PossibleScrips->Join(ALIAS1 => 'main', FIELD1 => 'ScripCondition',
- ALIAS2 => $ConditionsAlias, FIELD2=> 'id');
-
-
- #We only want things where the scrip applies to this sort of transaction
- $PossibleScrips->Limit(ALIAS=> $ConditionsAlias,
- FIELD=>'ApplicableTransTypes',
- OPERATOR => 'LIKE',
- VALUE => $args{'Type'},
- ENTRYAGGREGATOR => 'OR',
- );
-
- # Or where the scrip applies to any transaction
- $PossibleScrips->Limit(ALIAS=> $ConditionsAlias,
- FIELD=>'ApplicableTransTypes',
- OPERATOR => 'LIKE',
- VALUE => "Any",
- ENTRYAGGREGATOR => 'OR',
- );
-
- #Iterate through each script and check it's applicability.
-
- while (my $Scrip = $PossibleScrips->Next()) {
-
- #TODO: properly deal with errors raised in this scrip loop
-
- #$RT::Logger->debug("$self now dealing with ".$Scrip->Id. "\n");
- eval {
- local $SIG{__DIE__} = sub { $RT::Logger->error($_[0])};
-
-
- #Load the scrip's Condition object
- $Scrip->ConditionObj->LoadCondition(TicketObj => $TicketAsSystem,
- TransactionObj => $TransAsSystem);
-
-
- #If it's applicable, prepare and commit it
-
- $RT::Logger->debug ("$self: Checking condition ".$Scrip->ConditionObj->Name. "...\n");
-
- if ( $Scrip->IsApplicable() ) {
-
- $RT::Logger->debug ("$self: Matches condition ".$Scrip->ConditionObj->Name. "...\n");
- #TODO: handle some errors here
-
- $Scrip->ActionObj->LoadAction(TicketObj => $TicketAsSystem,
- TransactionObj => $TransAsSystem);
-
-
- if ($Scrip->Prepare()) {
- $RT::Logger->debug("$self: Prepared " .
- $Scrip->ActionObj->Name . "\n");
- if ($Scrip->Commit()) {
- $RT::Logger->debug("$self: Committed " .
- $Scrip->ActionObj->Name . "\n");
- }
- else {
- $RT::Logger->info("$self: Failed to commit ".
- $Scrip->ActionObj->Name . "\n");
- }
- }
- else {
- $RT::Logger->info("$self: Failed to prepare " .
- $Scrip->ActionObj->Name . "\n");
- }
-
- #We're done with it. lets clean up.
- #TODO: something else isn't letting these get garbage collected. check em out.
- $Scrip->ActionObj->DESTROY();
- $Scrip->ConditionObj->DESTROY;
- }
-
-
- else {
- $RT::Logger->debug ("$self: Doesn't match condition ".$Scrip->ConditionObj->Name. "...\n");
-
- # TODO: why doesn't this catch all the ScripObjs we create.
- # and why do we explictly need to destroy them?
- $Scrip->ConditionObj->DESTROY;
- }
- }
- }
-
- # }}}
-
- }
-
- return ($id, "Transaction Created");
-}