/usr/share/otrs/Kernel/System/EventHandler.pm is in otrs2 6.0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | # --
# Copyright (C) 2001-2018 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::System::EventHandler;
## nofilter(TidyAll::Plugin::OTRS::Perl::Pod::FunctionPod)
use strict;
use warnings;
use Kernel::System::VariableCheck qw(IsArrayRefWithData);
our $ObjectManagerDisabled = 1;
=head1 NAME
Kernel::System::EventHandler - event handler interface
=head1 DESCRIPTION
Inherit from this class if you want to use events there.
use parent qw(Kernel::System::EventHandler);
In your class, have to call L</EventHandlerInit()> first.
Then, to register events as they occur, use the L</EventHandler()>
method. It will call the event handler modules which are registered
for the given event, or queue them for later execution (so-called
'Transaction' events).
In the destructor, you should add a call to L</EventHandlerTransaction()>
to make sure that also C<Transaction> events will be executed correctly.
This is only necessary if you use C<Transaction> events in your class.
=head1 PUBLIC INTERFACE
=head2 EventHandlerInit()
Call this to initialize the event handling mechanisms to work
correctly with your object.
$Self->EventHandlerInit(
# name of configured event modules
Config => 'Example::EventModule',
);
Example 1:
$Self->EventHandlerInit(
Config => 'Ticket::EventModulePost',
);
Example 1 XML config:
<ConfigItem Name="Example::EventModule###99-EscalationIndex" Required="0" Valid="1">
<Description Translatable="1">Example event module updates the example escalation index.</Description>
<Group>Example</Group>
<SubGroup>Core::Example</SubGroup>
<Setting>
<Hash>
<Item Key="Module">Kernel::System::Example::Event::ExampleEscalationIndex</Item>
<Item Key="Event">(ExampleSLAUpdate|ExampleQueueUpdate|ExampleStateUpdate|ExampleCreate)</Item>
<Item Key="SomeOption">Some Option accessable via $Param{Config}->{SomeOption} in Run() of event module.</Item>
<Item Key="Transaction">(0|1)</Item>
</Hash>
</Setting>
</ConfigItem>
Example 2:
$Self->EventHandlerInit(
Config => 'ITSM::EventModule',
);
Example 2 XML config:
<ConfigItem Name="ITSM::EventModule###01-HistoryAdd" Required="0" Valid="1">
<Description Translatable="1">ITSM event module updates the history for Change and WorkOrder objects..</Description>
<Group>ITSM Change Management</Group>
<SubGroup>Core::ITSMEvent</SubGroup>
<Setting>
<Hash>
<Item Key="Module">Kernel::System::ITSMChange::Event::HistoryAdd</Item>
<Item Key="Event">(ChangeUpdate|WorkOrderUpdate|ChangeAdd|WorkOrderAdd)</Item>
<Item Key="SomeOption">Some Option accessable via $Param{Config}->{SomeOption} in Run() of event module.</Item>
<Item Key="Transaction">(0|1)</Item>
</Hash>
</Setting>
</ConfigItem>
<ConfigItem Name="ITSM::EventModule###02-HistoryAdd" Required="0" Valid="1">
<Description Translatable="1">ITSM event module updates the ConfigItem History.</Description>
<Group>ITSM Configuration Management</Group>
<SubGroup>Core::ITSMEvent</SubGroup>
<Setting>
<Hash>
<Item Key="Module">Kernel::System::ITSMConfigurationManagement::Event::HistoryAdd</Item>
<Item Key="Event">(ConfigItemUpdate|ConfigItemAdd)</Item>
<Item Key="SomeOption">Some Option accessable via $Param{Config}->{SomeOption} in Run() of event module.</Item>
<Item Key="Transaction">(0|1)</Item>
</Hash>
</Setting>
</ConfigItem>
=cut
sub EventHandlerInit {
my ( $Self, %Param ) = @_;
$Self->{EventHandlerInit} = \%Param;
$Kernel::OM->ObjectRegisterEventHandler( EventHandler => $Self );
return 1;
}
=head2 EventHandler()
call event handler, returns true if it was executed successfully.
Example 1:
my $Success = $EventHandler->EventHandler(
Event => 'TicketStateUpdate', # event classification, passed to the configured event handlers
Data => { # data payload for the event, passed to the configured event handlers
TicketID => 123,
},
UserID => 123,
Transaction => 1, # optional, 0 or 1
);
In 'Transaction' mode, all events will be collected and executed together,
usually in the destructor of your object.
Example 2:
my $Success = $EventHandler->EventHandler(
Event => 'ChangeUpdate',
Data => {
ChangeID => 123,
},
UserID => 123,
);
=cut
sub EventHandler {
my ( $Self, %Param ) = @_;
# check needed stuff
for (qw(Data Event UserID)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $_!"
);
return;
}
}
# get configured modules
my $Modules = $Kernel::OM->Get('Kernel::Config')->Get( $Self->{EventHandlerInit}->{Config} );
# return if there is no one
return 1 if !$Modules;
# remember events only on normal mode
if ( !$Self->{EventHandlerTransaction} ) {
push @{ $Self->{EventHandlerPipe} }, \%Param;
}
# get main object
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
# load modules and execute
MODULE:
for my $Module ( sort keys %{$Modules} ) {
# If the module has an event configuration, determine if it should be executed for this event,
# and store the result in a small cache to avoid repetition on jobs involving many tickets.
if ( !defined $Self->{ExecuteModuleOnEvent}->{$Module}->{ $Param{Event} } ) {
if ( !$Modules->{$Module}->{Event} ) {
$Self->{ExecuteModuleOnEvent}->{$Module}->{ $Param{Event} } = 1;
}
else {
$Self->{ExecuteModuleOnEvent}->{$Module}->{ $Param{Event} } =
$Param{Event} =~ /$Modules->{$Module}->{Event}/;
}
}
if ( $Self->{ExecuteModuleOnEvent}->{$Module}->{ $Param{Event} } ) {
if ( $Self->{EventHandlerTransaction} && !$Param{Transaction} ) {
# This is a special case. A new event was fired during processing of
# the queued events in transaction mode. This event must be immediately
# processed.
}
else {
# This is the regular case. A new event was fired in regular mode, or
# we are processing a queued event in transaction mode. Only execute
# this if the transaction settings of event and listener are the same.
# skip if we are not in transaction mode, but module is in transaction
next MODULE if !$Param{Transaction} && $Modules->{$Module}->{Transaction};
# skip if we are in transaction mode, but module is not in transaction
next MODULE if $Param{Transaction} && !$Modules->{$Module}->{Transaction};
}
# load event module
next MODULE if !$MainObject->Require( $Modules->{$Module}->{Module} );
# execute event backend
my $Generic = $Modules->{$Module}->{Module}->new();
$Generic->Run(
%Param,
Config => $Modules->{$Module},
);
}
}
return 1;
}
=head2 EventHandlerTransaction()
handle all queued 'Transaction' events which were collected up to this point.
$EventHandler->EventHandlerTransaction();
Call this method in the destructor of your object which inherits from
Kernel::System::EventHandler, like this:
sub DESTROY {
my $Self = shift;
# execute all transaction events
$Self->EventHandlerTransaction();
return 1;
}
=cut
sub EventHandlerTransaction {
my ( $Self, %Param ) = @_;
# remember, we are in destroy mode, do not execute new events
$Self->{EventHandlerTransaction} = 1;
# execute events on end of transaction
if ( $Self->{EventHandlerPipe} ) {
for my $Params ( @{ $Self->{EventHandlerPipe} } ) {
$Self->EventHandler(
%Param,
%{$Params},
Transaction => 1,
);
}
# delete event pipe
$Self->{EventHandlerPipe} = undef;
}
# reset transaction mode
$Self->{EventHandlerTransaction} = 0;
return 1;
}
=head2 EventHandlerHasQueuedTransactions()
Return a true value if there are queued transactions, which
C<EventHandlerTransaction> handles, when called.
=cut
sub EventHandlerHasQueuedTransactions {
my ( $Self, %Param ) = @_;
return IsArrayRefWithData( $Self->{EventHandlerPipe} );
}
1;
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<http://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=cut
|