init III
This commit is contained in:
189
Perl OTRS/Kernel/System/ITSMChange/Template/CAB.pm
Normal file
189
Perl OTRS/Kernel/System/ITSMChange/Template/CAB.pm
Normal file
@@ -0,0 +1,189 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::ITSMChange::Template::CAB;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
## nofilter(TidyAll::Plugin::OTRS::Perl::Dumper)
|
||||
use Data::Dumper;
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::ITSMChange',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Main',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::ITSMChange::Template::CAB - all template functions for CAB
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
create an object
|
||||
|
||||
use Kernel::System::ObjectManager;
|
||||
local $Kernel::OM = Kernel::System::ObjectManager->new();
|
||||
my $TemplateObject = $Kernel::OM->Get('Kernel::System::ITSMChange::Template::CAB');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# allocate new hash for object
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
# set the debug flag
|
||||
$Self->{Debug} = $Param{Debug} || 0;
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Serialize()
|
||||
|
||||
Serialize the CAB of a change. This is done with Data::Dumper. It returns
|
||||
a serialized string of the data structure. The CAB actions
|
||||
are "wrapped" within a hash reference...
|
||||
|
||||
my $TemplateString = $TemplateObject->Serialize(
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
Return => 'HASH', # (optional) HASH|STRING default 'STRING'
|
||||
);
|
||||
|
||||
returns
|
||||
|
||||
'{CABAdd => { CABCustomers => [ 'mm@localhost' ], ... }}'
|
||||
|
||||
If parameter C<Return> is set to C<HASH>, the Perl data structure
|
||||
is returned
|
||||
|
||||
{
|
||||
CABAdd => { ... },
|
||||
Children => [ ... ],
|
||||
}
|
||||
|
||||
=cut
|
||||
|
||||
sub Serialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# set default value for 'Return'
|
||||
$Param{Return} ||= 'STRING';
|
||||
|
||||
# get CAB of the change
|
||||
my $Change = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeGet(
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
return if !$Change;
|
||||
|
||||
# templates have to be an array reference;
|
||||
my $OriginalData = {
|
||||
CABAdd => {
|
||||
CABCustomers => $Change->{CABCustomers},
|
||||
CABAgents => $Change->{CABAgents},
|
||||
},
|
||||
};
|
||||
|
||||
if ( $Param{Return} eq 'HASH' ) {
|
||||
return $OriginalData;
|
||||
}
|
||||
|
||||
# no indentation (saves space)
|
||||
local $Data::Dumper::Indent = 0;
|
||||
|
||||
# do not use cross-referencing
|
||||
local $Data::Dumper::Deepcopy = 1;
|
||||
|
||||
# serialize the data (do not use $VAR1, but $TemplateData for Dumper output)
|
||||
my $SerializedData = $Kernel::OM->Get('Kernel::System::Main')->Dump( $OriginalData, 'binary' );
|
||||
|
||||
return $SerializedData;
|
||||
}
|
||||
|
||||
=head2 DeSerialize()
|
||||
|
||||
Updates the CAB of a change based on the given CAB template. It
|
||||
returns the change id the cab is for.
|
||||
|
||||
my $ChangeID = $TemplateObject->DeSerialize(
|
||||
Data => {
|
||||
CABCustomers => [ 'mm@localhost' ],
|
||||
CABAgents => [ 1, 2 ],
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub DeSerialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# get current CAB of change
|
||||
my $Change = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeGet(
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
# a CAB add is actually a CAB update on a change
|
||||
return if !$Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeCABUpdate(
|
||||
ChangeID => $Param{ChangeID},
|
||||
CABCustomers => [ @{ $Param{Data}->{CABCustomers} }, @{ $Change->{CABCustomers} } ],
|
||||
CABAgents => [ @{ $Param{Data}->{CABAgents} }, @{ $Change->{CABAgents} } ],
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
ID => $Param{ChangeID},
|
||||
ChangeID => $Param{ChangeID},
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
626
Perl OTRS/Kernel/System/ITSMChange/Template/ITSMChange.pm
Normal file
626
Perl OTRS/Kernel/System/ITSMChange/Template/ITSMChange.pm
Normal file
@@ -0,0 +1,626 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::ITSMChange::Template::ITSMChange;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
## nofilter(TidyAll::Plugin::OTRS::Perl::Dumper)
|
||||
use Data::Dumper;
|
||||
use Kernel::System::VariableCheck qw(:all);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::ITSMChange',
|
||||
'Kernel::System::ITSMChange::ITSMCondition',
|
||||
'Kernel::System::ITSMChange::ITSMStateMachine',
|
||||
'Kernel::System::ITSMChange::Template::ITSMCondition',
|
||||
'Kernel::System::ITSMChange::Template::ITSMWorkOrder',
|
||||
'Kernel::System::LinkObject',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Main',
|
||||
'Kernel::System::User',
|
||||
'Kernel::System::CustomerUser',
|
||||
'Kernel::System::Valid',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::ITSMChange::Template::ITSMChange - all template functions for changes
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
All functions for change templates in ITSMChangeManagement.
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
create an object
|
||||
|
||||
use Kernel::System::ObjectManager;
|
||||
local $Kernel::OM = Kernel::System::ObjectManager->new();
|
||||
my $TemplateObject = $Kernel::OM->Get('Kernel::System::ITSMChange::Template::ITSMChange');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# allocate new hash for object
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
# set the debug flag
|
||||
$Self->{Debug} = $Param{Debug} || 0;
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Serialize()
|
||||
|
||||
Serialize a change. This is done with Data::Dumper. It returns
|
||||
a serialized string of the data structure. The change actions
|
||||
are "wrapped" within an array reference...
|
||||
|
||||
my $TemplateString = $TemplateObject->Serialize(
|
||||
ChangeID => 1,
|
||||
StateReset => 1, # (optional) reset to default state
|
||||
UserID => 1,
|
||||
Return => 'HASH', # (optional) HASH|STRING default 'STRING'
|
||||
);
|
||||
|
||||
returns
|
||||
|
||||
'{ChangeAdd => {Title => 'title', ...}}, {WorkOrderAdd => { ChangeID => 123, ... }}'
|
||||
|
||||
If parameter C<Return> is set to C<HASH>, the Perl data structure
|
||||
is returned
|
||||
|
||||
{
|
||||
ChangeAdd => { ... },
|
||||
Children => [
|
||||
{
|
||||
WorkOrderAdd => { ... },
|
||||
Children => [ ... ],
|
||||
},
|
||||
{
|
||||
WorkOrderAdd => { ... },
|
||||
Children => [ ... ],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
=cut
|
||||
|
||||
sub Serialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# set default value for 'Return'
|
||||
$Param{Return} ||= 'STRING';
|
||||
|
||||
# get change
|
||||
my $Change = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeGet(
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
return if !$Change;
|
||||
|
||||
# keep only wanted attributes
|
||||
my $CleanChange;
|
||||
for my $Attribute (
|
||||
qw(
|
||||
ChangeID ChangeNumber ChangeStateID ChangeTitle Description
|
||||
Justification ChangeManagerID ChangeBuilderID
|
||||
CategoryID ImpactID PriorityID CABAgents CABCustomers RequestedTime
|
||||
CreateTime CreateBy ChangeTime ChangeBy PlannedStartTime PlannedEndTime)
|
||||
)
|
||||
{
|
||||
$CleanChange->{$Attribute} = $Change->{$Attribute};
|
||||
}
|
||||
|
||||
# reset change state to default if requested
|
||||
if ( $Param{StateReset} ) {
|
||||
|
||||
# get initial change state id
|
||||
my $NextStateIDs = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMStateMachine')->StateTransitionGet(
|
||||
StateID => 0,
|
||||
Class => 'ITSM::ChangeManagement::Change::State',
|
||||
);
|
||||
|
||||
$CleanChange->{ChangeStateID} = $NextStateIDs->[0];
|
||||
}
|
||||
|
||||
# add change dynamic fields to list of wanted attributes
|
||||
ATTRIBUTE:
|
||||
for my $Attribute ( sort keys %{$Change} ) {
|
||||
|
||||
# find the change dynamic field attributes
|
||||
if ( $Attribute =~ m{ \A DynamicField_.* \z }xms ) {
|
||||
|
||||
$CleanChange->{$Attribute} = $Change->{$Attribute};
|
||||
}
|
||||
}
|
||||
|
||||
my $OriginalData = { ChangeAdd => $CleanChange };
|
||||
|
||||
# get attachments
|
||||
my @ChangeAttachments = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeAttachmentList(
|
||||
ChangeID => $Change->{ChangeID},
|
||||
);
|
||||
for my $Filename (@ChangeAttachments) {
|
||||
|
||||
# save attachments to this template
|
||||
push @{ $OriginalData->{Children} }, { AttachmentAdd => { Filename => $Filename } };
|
||||
}
|
||||
|
||||
# get workorders
|
||||
WORKORDERID:
|
||||
for my $WorkOrderID ( @{ $Change->{WorkOrderIDs} } ) {
|
||||
my $WorkOrder = $Kernel::OM->Get('Kernel::System::ITSMChange::Template::ITSMWorkOrder')->Serialize(
|
||||
WorkOrderID => $WorkOrderID,
|
||||
StateReset => $Param{StateReset} || 0,
|
||||
UserID => $Param{UserID},
|
||||
Return => 'HASH',
|
||||
);
|
||||
|
||||
next WORKORDERID if !$WorkOrder;
|
||||
|
||||
push @{ $OriginalData->{Children} }, $WorkOrder;
|
||||
}
|
||||
|
||||
# get condition list for the change
|
||||
my $ConditionList = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ConditionList(
|
||||
ChangeID => $Param{ChangeID},
|
||||
Valid => 0,
|
||||
UserID => $Param{UserID},
|
||||
) || [];
|
||||
|
||||
# get each condition
|
||||
CONDITIONID:
|
||||
for my $ConditionID ( @{$ConditionList} ) {
|
||||
my $Condition = $Kernel::OM->Get('Kernel::System::ITSMChange::Template::ITSMCondition')->Serialize(
|
||||
ConditionID => $ConditionID,
|
||||
UserID => $Param{UserID},
|
||||
Return => 'HASH',
|
||||
);
|
||||
|
||||
next CONDITIONID if !$Condition;
|
||||
|
||||
push @{ $OriginalData->{Children} }, $Condition;
|
||||
}
|
||||
|
||||
# get links to other object
|
||||
my $LinkListWithData = $Kernel::OM->Get('Kernel::System::LinkObject')->LinkListWithData(
|
||||
Object => 'ITSMChange',
|
||||
Key => $Change->{ChangeID},
|
||||
State => 'Valid',
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
for my $TargetObject ( sort keys %{$LinkListWithData} ) {
|
||||
for my $Type ( sort keys %{ $LinkListWithData->{$TargetObject} } ) {
|
||||
for my $Key ( sort keys %{ $LinkListWithData->{$TargetObject}->{$Type} } ) {
|
||||
for my $TargetID (
|
||||
sort keys %{ $LinkListWithData->{$TargetObject}->{$Type}->{$Key} }
|
||||
)
|
||||
{
|
||||
my $LinkInfo = {
|
||||
SourceObject => 'ITSMChange',
|
||||
SourceKey => $Change->{ChangeID},
|
||||
TargetObject => $TargetObject,
|
||||
TargetKey => $TargetID,
|
||||
Type => $Type,
|
||||
State => 'Valid',
|
||||
UserID => $Param{UserID},
|
||||
};
|
||||
push @{ $OriginalData->{Children} }, { LinkAdd => $LinkInfo };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $Param{Return} eq 'HASH' ) {
|
||||
return $OriginalData;
|
||||
}
|
||||
|
||||
# no indentation (saves space)
|
||||
local $Data::Dumper::Indent = 0;
|
||||
|
||||
# do not use cross-referencing
|
||||
local $Data::Dumper::Deepcopy = 1;
|
||||
|
||||
# serialize the data (do not use $VAR1, but $TemplateData for Dumper output)
|
||||
my $SerializedData = $Kernel::OM->Get('Kernel::System::Main')->Dump( $OriginalData, 'binary' );
|
||||
|
||||
return $SerializedData;
|
||||
}
|
||||
|
||||
=head2 DeSerialize()
|
||||
|
||||
DeSerialize() is a wrapper for all the _XXXAdd methods.
|
||||
|
||||
my %Info = $TemplateObject->DeSerialize(
|
||||
Data => {
|
||||
# ... Params for ChangeAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
Method => 'ChangeAdd',
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub DeSerialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID Method Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# dispatch table
|
||||
my %Method2Sub = (
|
||||
ChangeAdd => '_ChangeAdd',
|
||||
AttachmentAdd => '_AttachmentAdd',
|
||||
LinkAdd => '_LinkAdd',
|
||||
);
|
||||
|
||||
my $Sub = $Method2Sub{ $Param{Method} };
|
||||
|
||||
if ( !$Sub ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => 'Invalid Methodname!',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return $Self->$Sub(%Param);
|
||||
}
|
||||
|
||||
=head1 PRIVATE INTERFACE
|
||||
|
||||
=head2 _ChangeAdd()
|
||||
|
||||
Creates a new change based on a template. It returns a hash with additional
|
||||
info like ChangeID.
|
||||
|
||||
my %Return = $TemplateObject->_ChangeAdd(
|
||||
Data => {
|
||||
ChangeTitle => 'test',
|
||||
},
|
||||
# other change attributes
|
||||
ChangeID => 0,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _ChangeAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
my $LogObject = $Kernel::OM->Get('Kernel::System::Log');
|
||||
my $UserObject = $Kernel::OM->Get('Kernel::System::User');
|
||||
my $CustomerUserObject = $Kernel::OM->Get('Kernel::System::CustomerUser');
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$LogObject->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# make a local copy
|
||||
my %Data = %{ $Param{Data} };
|
||||
|
||||
# we need the old change id for expressions
|
||||
my $OldChangeID = $Data{ChangeID};
|
||||
|
||||
# these attributes are generated automatically, so don't pass them to ChangeAdd()
|
||||
delete @Data{qw(ChangeID ChangeNumber CreateTime CreateBy ChangeTime ChangeBy)};
|
||||
delete @Data{qw(DescriptionPlain JustificationPlain)};
|
||||
|
||||
# if user set a new time, calculate difference
|
||||
my $Difference;
|
||||
if ( $Param{NewTimeInEpoche} ) {
|
||||
my $OldTime = $Data{ $Param{MoveTimeType} };
|
||||
|
||||
if ($OldTime) {
|
||||
$Difference = $Self->_GetTimeDifference(
|
||||
CurrentTime => $OldTime,
|
||||
NewTimeInEpoche => $Param{NewTimeInEpoche},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
# PlannedXXXTime was saved just for "move time" purposes
|
||||
delete $Data{PlannedEndTime};
|
||||
delete $Data{PlannedStartTime};
|
||||
|
||||
# RequestedTime should not be set
|
||||
delete $Data{RequestedTime};
|
||||
|
||||
# delete all parameters whose values are 'undef'
|
||||
# _CheckChangeParams throws an error otherwise
|
||||
for my $Parameter ( sort keys %Data ) {
|
||||
delete $Data{$Parameter} if !defined $Data{$Parameter};
|
||||
}
|
||||
|
||||
# replace the ChangeBuilderID from the saved template with the current user id
|
||||
$Data{ChangeBuilderID} = $Param{UserID};
|
||||
|
||||
if ( $Data{ChangeManagerID} ) {
|
||||
|
||||
# Check if the change manager is still valid, leave empty if not
|
||||
my %UserData = $UserObject->GetUserData(
|
||||
UserID => $Data{ChangeManagerID},
|
||||
Valid => 1,
|
||||
);
|
||||
|
||||
if ( !$UserData{UserID} ) {
|
||||
delete $Data{ChangeManagerID};
|
||||
}
|
||||
}
|
||||
|
||||
# Check if CAB agents are valid agents, otherwise remove them.
|
||||
if ( IsArrayRefWithData( $Data{CABAgents} ) ) {
|
||||
|
||||
my @NewCABAgents;
|
||||
|
||||
# check users
|
||||
USERID:
|
||||
for my $UserID ( @{ $Data{CABAgents} } ) {
|
||||
|
||||
# get user data
|
||||
my %UserData = $UserObject->GetUserData(
|
||||
UserID => $UserID,
|
||||
Valid => 1,
|
||||
);
|
||||
|
||||
next USERID if !$UserData{UserID};
|
||||
|
||||
push @NewCABAgents, $UserID;
|
||||
}
|
||||
|
||||
$Data{CABAgents} = \@NewCABAgents;
|
||||
}
|
||||
|
||||
# Check if CAB customers are valid customers, otherwise remove them.
|
||||
if ( IsArrayRefWithData( $Data{CABCustomers} ) ) {
|
||||
|
||||
my @NewCABCustomers;
|
||||
|
||||
# get the valid id for "valid"
|
||||
my $ValidID = $Kernel::OM->Get('Kernel::System::Valid')->ValidLookup(
|
||||
Valid => 'valid',
|
||||
);
|
||||
|
||||
# check customer users
|
||||
CUSTOMERUSER:
|
||||
for my $CustomerUser ( @{ $Data{CABCustomers} } ) {
|
||||
|
||||
# get customer user data
|
||||
my %CustomerUserData = $CustomerUserObject->CustomerUserDataGet(
|
||||
User => $CustomerUser,
|
||||
Valid => 1,
|
||||
);
|
||||
|
||||
next CUSTOMERUSER if $CustomerUserData{ValidID} ne $ValidID;
|
||||
|
||||
push @NewCABCustomers, $CustomerUser;
|
||||
}
|
||||
|
||||
$Data{CABCustomers} = \@NewCABCustomers;
|
||||
}
|
||||
|
||||
# add the change
|
||||
my $ChangeID = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeAdd(
|
||||
%Data,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
# error handling
|
||||
if ( !$ChangeID ) {
|
||||
$LogObject->Log(
|
||||
Priority => 'error',
|
||||
Message => "Could not create change!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my %Info = (
|
||||
ID => $ChangeID,
|
||||
ChangeID => $ChangeID,
|
||||
TimeDifference => $Difference,
|
||||
OldChangeID => $OldChangeID,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
=head2 _GetTimeDifference()
|
||||
|
||||
If a new planned start/end time was given, the difference is needed
|
||||
to move all time values
|
||||
|
||||
my $DiffInSeconds = $TemplateObject->_GetTimeDifference(
|
||||
CurrentTime => '2010-01-12 00:00:00',
|
||||
NewTimeInEpoche => 1234567890,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _GetTimeDifference {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(CurrentTime NewTimeInEpoche)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# get current time as timestamp
|
||||
my $CurrentSystemTime = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $Param{CurrentTime},
|
||||
}
|
||||
)->ToEpoch();
|
||||
|
||||
my $DiffSeconds = $Param{NewTimeInEpoche} - $CurrentSystemTime;
|
||||
|
||||
return $DiffSeconds;
|
||||
}
|
||||
|
||||
=head2 _AttachmentAdd()
|
||||
|
||||
Creates new attachments for a change or a C<workorder> based on the given template.
|
||||
It returns a hash of information (with just one key - "Success")
|
||||
|
||||
my %Info = $TemplateObject->_AttachmentAdd(
|
||||
Data => {
|
||||
# ... Params for AttachmentAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _AttachmentAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
my $Attachment = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeAttachmentGet(
|
||||
ChangeID => $Param{OldChangeID},
|
||||
Filename => $Param{Data}->{Filename},
|
||||
);
|
||||
|
||||
my $Success = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeAttachmentAdd(
|
||||
%{$Attachment},
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
Success => $Success,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
=head2 _LinkAdd()
|
||||
|
||||
Creates new links for a change or a C<workorder> based on the given template. It
|
||||
returns a hash of information (with just one key - "Success")
|
||||
|
||||
my %Info = $TemplateObject->_LinkAdd(
|
||||
Data => {
|
||||
# ... Params for LinkAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
WorkOrderID => 123, # optional
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _LinkAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
my $SourceKey;
|
||||
|
||||
if ( $Param{Data}->{SourceObject} eq 'ITSMChange' ) {
|
||||
$SourceKey = $Param{ChangeID};
|
||||
}
|
||||
elsif ( $Param{Data}->{SourceObject} eq 'ITSMWorkOrder' ) {
|
||||
$SourceKey = $Param{WorkOrderID};
|
||||
}
|
||||
|
||||
if ( !$SourceKey ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => 'Need WorkOrderID or ChangeID!',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my $Success = $Kernel::OM->Get('Kernel::System::LinkObject')->LinkAdd(
|
||||
%{ $Param{Data} },
|
||||
SourceKey => $SourceKey,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
Success => $Success,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
402
Perl OTRS/Kernel/System/ITSMChange/Template/ITSMCondition.pm
Normal file
402
Perl OTRS/Kernel/System/ITSMChange/Template/ITSMCondition.pm
Normal file
@@ -0,0 +1,402 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::ITSMChange::Template::ITSMCondition;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
## nofilter(TidyAll::Plugin::OTRS::Perl::Dumper)
|
||||
use Data::Dumper;
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::ITSMChange::ITSMCondition',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Main',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::ITSMChange::Template::ITSMCondition - all template functions for conditions
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
Create an object.
|
||||
|
||||
use Kernel::System::ObjectManager;
|
||||
local $Kernel::OM = Kernel::System::ObjectManager->new();
|
||||
my $TemplateObject = $Kernel::OM->Get('Kernel::System::ITSMChange::Template::ITSMCondition');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# allocate new hash for object
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
# set the debug flag
|
||||
$Self->{Debug} = $Param{Debug} || 0;
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Serialize()
|
||||
|
||||
Serialize a condition. This is done with Data::Dumper. It returns
|
||||
a serialized string of the data structure. The condition actions
|
||||
are "wrapped" within a hash reference...
|
||||
|
||||
my $TemplateString = $TemplateObject->Serialize(
|
||||
ConditionID => 1,
|
||||
UserID => 1,
|
||||
Return => 'HASH', # (optional) HASH|STRING default 'STRING'
|
||||
);
|
||||
|
||||
returns
|
||||
|
||||
'{ConditionAdd => { ... }}'
|
||||
|
||||
If parameter C<Return> is set to C<HASH>, the Perl data structure
|
||||
is returned
|
||||
|
||||
{
|
||||
ConditionAdd => { ... },
|
||||
Children => [ ... ],
|
||||
}
|
||||
|
||||
=cut
|
||||
|
||||
sub Serialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ConditionID)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# set default value for 'Return'
|
||||
$Param{Return} ||= 'STRING';
|
||||
|
||||
# get condition
|
||||
my $Condition = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ConditionGet(
|
||||
ConditionID => $Param{ConditionID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
return if !$Condition;
|
||||
|
||||
# templates have to be an array reference;
|
||||
my $OriginalData = { ConditionAdd => $Condition };
|
||||
|
||||
# get expressions
|
||||
my $Expressions = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ExpressionList(
|
||||
ConditionID => $Param{ConditionID},
|
||||
UserID => $Param{UserID},
|
||||
) || [];
|
||||
|
||||
# add each expression to condition data
|
||||
for my $ExpressionID ( @{$Expressions} ) {
|
||||
my $Expression = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ExpressionGet(
|
||||
ExpressionID => $ExpressionID,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
push @{ $OriginalData->{Children} }, { ExpressionAdd => $Expression };
|
||||
}
|
||||
|
||||
# get actions
|
||||
my $Actions = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ActionList(
|
||||
ConditionID => $Param{ConditionID},
|
||||
UserID => $Param{UserID},
|
||||
) || [];
|
||||
|
||||
# add each action to condition data
|
||||
for my $ActionID ( @{$Actions} ) {
|
||||
my $Action = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ActionGet(
|
||||
ActionID => $ActionID,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
push @{ $OriginalData->{Children} }, { ActionAdd => $Action };
|
||||
}
|
||||
|
||||
if ( $Param{Return} eq 'HASH' ) {
|
||||
return $OriginalData;
|
||||
}
|
||||
|
||||
# no indentation (saves space)
|
||||
local $Data::Dumper::Indent = 0;
|
||||
|
||||
# do not use cross-referencing
|
||||
local $Data::Dumper::Deepcopy = 1;
|
||||
|
||||
# serialize the data (do not use $VAR1, but $TemplateData for Dumper output)
|
||||
my $SerializedData = $Kernel::OM->Get('Kernel::System::Main')->Dump( $OriginalData, 'binary' );
|
||||
|
||||
return $SerializedData;
|
||||
}
|
||||
|
||||
=head2 DeSerialize()
|
||||
|
||||
DeSerialize() is a wrapper for all the _XXXAdd methods.
|
||||
|
||||
my %Info = $TemplateObject->DeSerialize(
|
||||
Data => {
|
||||
# ... Params for ConditionAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
Method => 'ConditionAdd',
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub DeSerialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID Method Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# dispatch table
|
||||
my %Method2Sub = (
|
||||
ConditionAdd => '_ConditionAdd',
|
||||
ExpressionAdd => '_ExpressionAdd',
|
||||
ActionAdd => '_ActionAdd',
|
||||
);
|
||||
|
||||
my $Sub = $Method2Sub{ $Param{Method} };
|
||||
|
||||
if ( !$Sub ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => 'Invalid Methodname!',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return $Self->$Sub(%Param);
|
||||
}
|
||||
|
||||
=head1 PRIVATE INTERFACE
|
||||
|
||||
=head2 _ConditionAdd()
|
||||
|
||||
Creates new conditions for a change based on the given template. It
|
||||
returns a hash of information (change id it was created for, id is
|
||||
the condition id)
|
||||
|
||||
my %Info = $TemplateObject->_ConditionAdd(
|
||||
Data => {
|
||||
# ... Params for ConditionAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _ConditionAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
my %Data = %{ $Param{Data} };
|
||||
|
||||
# delete attributes
|
||||
delete $Data{ConditionID};
|
||||
|
||||
# add condition
|
||||
my $ConditionID = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ConditionAdd(
|
||||
%Data,
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
ID => $ConditionID,
|
||||
ChangeID => $Param{ChangeID},
|
||||
ConditionID => $ConditionID,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
=head2 _ExpressionAdd()
|
||||
|
||||
Creates new expressions for a condition based on the given template. It
|
||||
returns a hash of information (change id it was created for, id is
|
||||
the expression id)
|
||||
|
||||
my %Info = $TemplateObject->_ExpressionAdd(
|
||||
Data => {
|
||||
# ... Params for ExpressionAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _ExpressionAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data ConditionID)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
my %Data = %{ $Param{Data} };
|
||||
|
||||
# delete attributes that are not needed
|
||||
delete $Data{ExpressionID};
|
||||
|
||||
# replace old ids with new ids
|
||||
$Data{ConditionID} = $Param{ConditionID};
|
||||
|
||||
# replace old id only if it is an ID
|
||||
if ( $Data{Selector} =~ m{ \A \d+ \z }xms ) {
|
||||
my $Object = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ObjectGet(
|
||||
ObjectID => $Data{ObjectID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
if ( $Object->{Name} eq 'ITSMChange' ) {
|
||||
$Data{Selector} = $Param{ChangeID};
|
||||
}
|
||||
elsif ( $Object->{Name} eq 'ITSMWorkOrder' ) {
|
||||
$Data{Selector} = $Param{OldWorkOrderIDs}->{ $Data{Selector} };
|
||||
}
|
||||
}
|
||||
|
||||
# add expression
|
||||
my $ExpressionID = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ExpressionAdd(
|
||||
%Data,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
ID => $ExpressionID,
|
||||
ChangeID => $Param{ChangeID},
|
||||
ExpressionID => $ExpressionID,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
=head2 _ActionAdd()
|
||||
|
||||
Creates new actions for a condition based on the given template. It
|
||||
returns a hash of information (change id it was created for, id is
|
||||
the action id)
|
||||
|
||||
my %Info = $TemplateObject->_ActionAdd(
|
||||
Data => {
|
||||
# ... Params for ActionAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _ActionAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data ConditionID)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
my %Data = %{ $Param{Data} };
|
||||
|
||||
# delete attributes that are not needed
|
||||
delete $Data{ActionID};
|
||||
|
||||
# replace old ids with new ids
|
||||
$Data{ConditionID} = $Param{ConditionID};
|
||||
|
||||
# replace old id only if it is an ID
|
||||
if ( $Data{Selector} =~ m{ \A \d+ \z }xms ) {
|
||||
my $Object = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ObjectGet(
|
||||
ObjectID => $Data{ObjectID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
if ( $Object->{Name} eq 'ITSMChange' ) {
|
||||
$Data{Selector} = $Param{ChangeID};
|
||||
}
|
||||
elsif ( $Object->{Name} eq 'ITSMWorkOrder' ) {
|
||||
$Data{Selector} = $Param{OldWorkOrderIDs}->{ $Data{Selector} };
|
||||
}
|
||||
}
|
||||
|
||||
# add action
|
||||
my $ActionID = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMCondition')->ActionAdd(
|
||||
%Data,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
ID => $ActionID,
|
||||
ChangeID => $Param{ChangeID},
|
||||
ActionID => $ActionID,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
625
Perl OTRS/Kernel/System/ITSMChange/Template/ITSMWorkOrder.pm
Normal file
625
Perl OTRS/Kernel/System/ITSMChange/Template/ITSMWorkOrder.pm
Normal file
@@ -0,0 +1,625 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::ITSMChange::Template::ITSMWorkOrder;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
## nofilter(TidyAll::Plugin::OTRS::Perl::Dumper)
|
||||
use Data::Dumper;
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::ITSMChange::ITSMStateMachine',
|
||||
'Kernel::System::ITSMChange::ITSMWorkOrder',
|
||||
'Kernel::System::LinkObject',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Main',
|
||||
'Kernel::System::User',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::ITSMChange::Template::ITSMWorkOrder - all template functions for workorders
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
All functions for work order templates in ITSMChangeManagement.
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=cut
|
||||
|
||||
=head2 new()
|
||||
|
||||
create an object
|
||||
|
||||
use Kernel::System::ObjectManager;
|
||||
local $Kernel::OM = Kernel::System::ObjectManager->new();
|
||||
my $TemplateObject = $Kernel::OM->Get('Kernel::System::ITSMChange::Template::ITSMWorkOrder');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# allocate new hash for object
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
# set the debug flag
|
||||
$Self->{Debug} = $Param{Debug} || 0;
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Serialize()
|
||||
|
||||
Serialize a C<workorder>. This is done with Data::Dumper. It returns
|
||||
a serialized string of the data structure. The C<workorder> actions
|
||||
are "wrapped" within a hash reference...
|
||||
|
||||
my $TemplateString = $TemplateObject->Serialize(
|
||||
WorkOrderID => 1,
|
||||
StateReset => 1, # (optional) reset to default state
|
||||
UserID => 1,
|
||||
Return => 'HASH', # (optional) HASH|STRING default 'STRING'
|
||||
);
|
||||
|
||||
returns
|
||||
|
||||
'{WorkOrderAdd => { ChangeID => 123, ... }}'
|
||||
|
||||
If parameter C<Return> is set to C<HASH>, the Perl data structure
|
||||
is returned
|
||||
|
||||
{
|
||||
WorkOrderAdd => { ... },
|
||||
Children => [ ... ],
|
||||
}
|
||||
|
||||
=cut
|
||||
|
||||
sub Serialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID WorkOrderID)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# set default value for 'Return'
|
||||
$Param{Return} ||= 'STRING';
|
||||
|
||||
# get workorder
|
||||
my $WorkOrder = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMWorkOrder')->WorkOrderGet(
|
||||
WorkOrderID => $Param{WorkOrderID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
return if !$WorkOrder;
|
||||
|
||||
# keep just wanted attributes
|
||||
my $CleanWorkOrder;
|
||||
for my $Attribute (
|
||||
qw(
|
||||
WorkOrderID ChangeID WorkOrderNumber WorkOrderTitle Instruction
|
||||
Report WorkOrderStateID WorkOrderTypeID WorkOrderAgentID
|
||||
PlannedStartTime PlannedEndTime ActualStartTime ActualEndTime AccountedTime PlannedEffort
|
||||
CreateTime CreateBy ChangeTime ChangeBy)
|
||||
)
|
||||
{
|
||||
$CleanWorkOrder->{$Attribute} = $WorkOrder->{$Attribute};
|
||||
}
|
||||
|
||||
# reset workorder state to default if requested
|
||||
if ( $Param{StateReset} ) {
|
||||
|
||||
# get initial workorder state id
|
||||
my $NextStateIDs = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMStateMachine')->StateTransitionGet(
|
||||
StateID => 0,
|
||||
Class => 'ITSM::ChangeManagement::WorkOrder::State',
|
||||
);
|
||||
$CleanWorkOrder->{WorkOrderStateID} = $NextStateIDs->[0];
|
||||
|
||||
# reset actual start and end time
|
||||
$CleanWorkOrder->{ActualStartTime} = undef;
|
||||
$CleanWorkOrder->{ActualEndTime} = undef;
|
||||
}
|
||||
|
||||
# add workorder fields to list of wanted attribute
|
||||
ATTRIBUTE:
|
||||
for my $Attribute ( sort keys %{$WorkOrder} ) {
|
||||
|
||||
# find the workorder dynamic field attributes
|
||||
if ( $Attribute =~ m{ \A DynamicField_.* \z }xms ) {
|
||||
|
||||
$CleanWorkOrder->{$Attribute} = $WorkOrder->{$Attribute};
|
||||
}
|
||||
}
|
||||
|
||||
# templates have to be an array reference;
|
||||
my $OriginalData = { WorkOrderAdd => $CleanWorkOrder };
|
||||
|
||||
# get attachments
|
||||
my @WorkOrderAttachments = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMWorkOrder')->WorkOrderAttachmentList(
|
||||
WorkOrderID => $WorkOrder->{WorkOrderID},
|
||||
);
|
||||
|
||||
for my $Filename (@WorkOrderAttachments) {
|
||||
|
||||
# save attachments to this template
|
||||
push @{ $OriginalData->{Children} }, { AttachmentAdd => { Filename => $Filename } };
|
||||
}
|
||||
|
||||
# get links to other object
|
||||
my $LinkListWithData = $Kernel::OM->Get('Kernel::System::LinkObject')->LinkListWithData(
|
||||
Object => 'ITSMWorkOrder',
|
||||
Key => $WorkOrder->{WorkOrderID},
|
||||
State => 'Valid',
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
for my $TargetObject ( sort keys %{$LinkListWithData} ) {
|
||||
for my $Type ( sort keys %{ $LinkListWithData->{$TargetObject} } ) {
|
||||
for my $Key ( sort keys %{ $LinkListWithData->{$TargetObject}->{$Type} } ) {
|
||||
for my $TargetID (
|
||||
sort keys %{ $LinkListWithData->{$TargetObject}->{$Type}->{$Key} }
|
||||
)
|
||||
{
|
||||
my $LinkInfo = {
|
||||
SourceObject => 'ITSMWorkOrder',
|
||||
SourceKey => $WorkOrder->{WorkOrderID},
|
||||
TargetObject => $TargetObject,
|
||||
TargetKey => $TargetID,
|
||||
Type => $Type,
|
||||
State => 'Valid',
|
||||
UserID => $Param{UserID},
|
||||
};
|
||||
push @{ $OriginalData->{Children} }, { LinkAdd => $LinkInfo };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $Param{Return} eq 'HASH' ) {
|
||||
return $OriginalData;
|
||||
}
|
||||
|
||||
# no indentation (saves space)
|
||||
local $Data::Dumper::Indent = 0;
|
||||
|
||||
# do not use cross-referencing
|
||||
local $Data::Dumper::Deepcopy = 1;
|
||||
|
||||
# serialize the data (do not use $VAR1, but $TemplateData for Dumper output)
|
||||
my $SerializedData = $Kernel::OM->Get('Kernel::System::Main')->Dump( $OriginalData, 'binary' );
|
||||
|
||||
return $SerializedData;
|
||||
}
|
||||
|
||||
=head2 DeSerialize()
|
||||
|
||||
DeSerialize() is a wrapper for all the _XXXAdd methods.
|
||||
|
||||
my %Info = $TemplateObject->DeSerialize(
|
||||
Data => {
|
||||
# ... Params for WorkOrderAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
Method => 'WorkOrderAdd',
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub DeSerialize {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID Method Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# dispatch table
|
||||
my %Method2Sub = (
|
||||
WorkOrderAdd => '_WorkOrderAdd',
|
||||
AttachmentAdd => '_AttachmentAdd',
|
||||
LinkAdd => '_LinkAdd',
|
||||
);
|
||||
|
||||
my $Sub = $Method2Sub{ $Param{Method} };
|
||||
|
||||
if ( !$Sub ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => 'Invalid Methodname!',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return $Self->$Sub(%Param);
|
||||
}
|
||||
|
||||
=head2 _WorkOrderAdd()
|
||||
|
||||
Creates a new C<workorder> based on a template. It returns the
|
||||
change id it was created for and the new C<workorder> id.
|
||||
|
||||
my ( $ChangeID, $WorkOrderID ) = $TemplateObject->_WorkOrderAdd(
|
||||
Data => {
|
||||
WorkOrderTitle => 'test',
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _WorkOrderAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# make a local copy
|
||||
my %Data = %{ $Param{Data} };
|
||||
|
||||
# we need the old change id for expressions
|
||||
my $OldWorkOrderID = $Data{WorkOrderID};
|
||||
|
||||
# these attributes are generated automatically, so don't pass them to WorkOrderAdd()
|
||||
delete @Data{qw(WorkOrderID CreateTime CreateBy ChangeTime ChangeBy)};
|
||||
delete @Data{qw(InstructionPlain ReportPlain)};
|
||||
|
||||
# delete all parameters whose values are 'undef'
|
||||
# _CheckWorkOrderParams throws an error otherwise
|
||||
for my $Parameter ( sort keys %Data ) {
|
||||
delete $Data{$Parameter} if !defined $Data{$Parameter};
|
||||
}
|
||||
|
||||
# xxx(?:Start|End)Times are empty strings on WorkOrderGet when
|
||||
# no time value is set. This confuses _CheckTimestamps. Thus
|
||||
# delete these parameters.
|
||||
for my $Prefix (qw(Actual Planned)) {
|
||||
for my $Suffix (qw(Start End)) {
|
||||
if ( !$Data{"$Prefix${Suffix}Time"} ) {
|
||||
delete $Data{"$Prefix${Suffix}Time"};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# move time slot for workorder if neccessary
|
||||
my $Difference = $Param{TimeDifference};
|
||||
if ( $Difference || $Param{NewTimeInEpoche} ) {
|
||||
|
||||
# calc new values for start and end time
|
||||
for my $Suffix (qw(Start End)) {
|
||||
|
||||
if ( $Data{"Planned${Suffix}Time"} ) {
|
||||
|
||||
# get difference if not already calculated (allow zero difference)
|
||||
if ( !defined $Difference && $Param{NewTimeInEpoche} ) {
|
||||
|
||||
# time needs to be corrected if the move time type is the planned end time
|
||||
my $WorkOrderLengthInSeconds = 0;
|
||||
if ( $Param{MoveTimeType} eq 'PlannedEndTime' ) {
|
||||
|
||||
# calculate the old planned start time into epoch seconds
|
||||
my $OldPlannedStartTimeInSeconds = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $Data{PlannedStartTime},
|
||||
},
|
||||
)->ToEpoch();
|
||||
|
||||
# calculate the old planned end time into epoch seconds
|
||||
my $OldPlannedEndTimeInSeconds = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $Data{PlannedEndTime},
|
||||
},
|
||||
)->ToEpoch();
|
||||
|
||||
# the time length of the workorder in seconds
|
||||
$WorkOrderLengthInSeconds = $OldPlannedEndTimeInSeconds - $OldPlannedStartTimeInSeconds;
|
||||
}
|
||||
|
||||
# calculate the time difference
|
||||
$Difference = $Self->_GetTimeDifference(
|
||||
CurrentTime => $Data{"Planned${Suffix}Time"},
|
||||
NewTimeInEpoche => $Param{NewTimeInEpoche} - $WorkOrderLengthInSeconds,
|
||||
);
|
||||
}
|
||||
|
||||
# get new value
|
||||
$Data{"Planned${Suffix}Time"} = $Self->_MoveTime(
|
||||
CurrentTime => $Data{"Planned${Suffix}Time"},
|
||||
Difference => $Difference,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $Data{WorkOrderAgentID} ) {
|
||||
|
||||
# Check if the workorder agent is still valid, leave empty if not
|
||||
my %UserData = $Kernel::OM->Get('Kernel::System::User')->GetUserData(
|
||||
UserID => $Data{WorkOrderAgentID},
|
||||
Valid => 1,
|
||||
);
|
||||
|
||||
if ( !$UserData{UserID} ) {
|
||||
delete $Data{WorkOrderAgentID};
|
||||
}
|
||||
}
|
||||
|
||||
# override the change id from the template
|
||||
my $WorkOrderID = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMWorkOrder')->WorkOrderAdd(
|
||||
%Data,
|
||||
NoNumberCalc => $Param{NoNumberCalc},
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
if ( !$WorkOrderID ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Could not create Workorder from Template!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
# we need a mapping "old id" to "new id" for the conditions
|
||||
my $OldIDs2NewIDs = {
|
||||
%{ $Param{OldWorkOrderIDs} || {} },
|
||||
$OldWorkOrderID => $WorkOrderID,
|
||||
};
|
||||
|
||||
my %Info = (
|
||||
ID => $WorkOrderID,
|
||||
WorkOrderID => $WorkOrderID,
|
||||
ChangeID => $Param{ChangeID},
|
||||
OldWorkOrderIDs => $OldIDs2NewIDs,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
=head1 PRIVATE INTERFACE
|
||||
|
||||
=head2 _GetTimeDifference()
|
||||
|
||||
If a new planned start/end time was given, the difference is needed
|
||||
to move all time values
|
||||
|
||||
my $DiffInSeconds = $TemplateObject->_GetTimeDifference(
|
||||
CurrentTime => '2010-01-12 00:00:00',
|
||||
NewTimeInEpoche => 1234567890,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _GetTimeDifference {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(CurrentTime NewTimeInEpoche)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# get current time as timestamp
|
||||
my $CurrentSystemTime = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $Param{CurrentTime},
|
||||
}
|
||||
)->ToEpoch();
|
||||
|
||||
my $DiffSeconds = $Param{NewTimeInEpoche} - $CurrentSystemTime;
|
||||
|
||||
return $DiffSeconds;
|
||||
}
|
||||
|
||||
=head2 _MoveTime()
|
||||
|
||||
This method returns the new value for a time column based on the
|
||||
difference.
|
||||
|
||||
my $TimeValue = $TemplateObject->_MoveTime(
|
||||
CurrentTime => '2010-01-12 00:00:00',
|
||||
Difference => 135, # in seconds
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _MoveTime {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
# need to check for defined, because 0 is allowed for Difference
|
||||
for my $Argument (qw(CurrentTime Difference)) {
|
||||
if ( !defined $Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# get current time as timestamp
|
||||
my $CurrentSystemTime = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $Param{CurrentTime},
|
||||
},
|
||||
)->ToEpoch();
|
||||
|
||||
# get planned time as timestamp
|
||||
my $NewTime = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
Epoch => $CurrentSystemTime + $Param{Difference},
|
||||
},
|
||||
)->ToString();
|
||||
|
||||
return $NewTime;
|
||||
}
|
||||
|
||||
=head2 _AttachmentAdd()
|
||||
|
||||
Creates new attachments for a change or a C<workorder> based on the given template.
|
||||
It returns a hash of information (with just one key - "Success")
|
||||
|
||||
my %Info = $TemplateObject->_AttachmentAdd(
|
||||
Data => {
|
||||
# ... Params for AttachmentAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _AttachmentAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID ChangeID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# build a lookup hash from new workorder id to old workorder id
|
||||
my %NewWorkOrderID2OldWorkOrderID = reverse %{ $Param{OldWorkOrderIDs} };
|
||||
|
||||
my $OldWorkOrderID = $NewWorkOrderID2OldWorkOrderID{ $Param{WorkOrderID} };
|
||||
|
||||
my $Attachment = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMWorkOrder')->WorkOrderAttachmentGet(
|
||||
WorkOrderID => $OldWorkOrderID,
|
||||
Filename => $Param{Data}->{Filename},
|
||||
);
|
||||
|
||||
my $Success = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMWorkOrder')->WorkOrderAttachmentAdd(
|
||||
%{$Attachment},
|
||||
ChangeID => $Param{ChangeID},
|
||||
WorkOrderID => $Param{WorkOrderID},
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
Success => $Success,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
=head2 _LinkAdd()
|
||||
|
||||
Creates new links for a change or a C<workorder> based on the given template. It
|
||||
returns a hash of information (with just one key - "Success")
|
||||
|
||||
my %Info = $TemplateObject->_LinkAdd(
|
||||
Data => {
|
||||
# ... Params for LinkAdd
|
||||
},
|
||||
ChangeID => 1,
|
||||
WorkOrderID => 123, # optional
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _LinkAdd {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Argument (qw(UserID Data)) {
|
||||
if ( !$Param{$Argument} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Argument!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
my $SourceKey;
|
||||
|
||||
if ( $Param{Data}->{SourceObject} eq 'ITSMChange' ) {
|
||||
$SourceKey = $Param{ChangeID};
|
||||
}
|
||||
elsif ( $Param{Data}->{SourceObject} eq 'ITSMWorkOrder' ) {
|
||||
$SourceKey = $Param{WorkOrderID};
|
||||
}
|
||||
|
||||
if ( !$SourceKey ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => 'Need WorkOrderID or ChangeID!',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my $Success = $Kernel::OM->Get('Kernel::System::LinkObject')->LinkAdd(
|
||||
%{ $Param{Data} },
|
||||
SourceKey => $SourceKey,
|
||||
UserID => $Param{UserID},
|
||||
);
|
||||
|
||||
my %Info = (
|
||||
Success => $Success,
|
||||
);
|
||||
|
||||
return %Info;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
Reference in New Issue
Block a user