# --
# 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::Console::Command::Admin::ITSM::Change::Delete;
use strict;
use warnings;
use parent qw(Kernel::System::Console::BaseCommand);
our @ObjectDependencies = (
'Kernel::System::ITSMChange',
);
sub Configure {
my ( $Self, %Param ) = @_;
$Self->Description('Delete changes (all or by number).');
$Self->AddOption(
Name => 'all',
Description => "Delete all changes.",
Required => 0,
HasValue => 0,
);
$Self->AddArgument(
Name => 'accept',
Description => "Accept delete all or cancel.",
Required => 0,
ValueRegex => qr/(y|n)/smx,
);
$Self->AddOption(
Name => 'change-number',
Description => "Delete listed changes.",
Required => 0,
HasValue => 1,
ValueRegex => qr/\d+/smx,
Multiple => 1,
);
return;
}
sub PreRun {
my ( $Self, %Param ) = @_;
my @ChangeNumbers = @{ $Self->GetOption('change-number') // [] };
if ( !$Self->GetOption('all') && !@ChangeNumbers ) {
die "Please provide option --all or --change-number. For more details use --help\n";
}
return;
}
sub Run {
my ( $Self, %Param ) = @_;
$Self->Print("Deleting changes...\n");
$Self->Print( "" . ( '=' x 69 ) . "\n" );
# get change object
my $ChangeObject = $Kernel::OM->Get('Kernel::System::ITSMChange');
# get change numbers
my @ChangeNumbers = @{ $Self->GetOption('change-number') // [] };
# delete all changes
if ( $Self->GetOption('all') ) {
# get all change ids
my @ChangesIDs = @{ $ChangeObject->ChangeList( UserID => 1 ) };
# get number of changes
my $ChangeCount = scalar @ChangesIDs;
# if there are any changes to delete
if ($ChangeCount) {
$Self->Print("Are you sure that you want to delete ALL $ChangeCount changes?\n");
$Self->Print("This is irrevocable. [y/n] \n");
my $Confirmation = $Self->GetArgument('accept');
chomp( $Confirmation = lc ) if !defined $Confirmation;
# if the user confirms the deletion
if ( $Confirmation eq 'y' ) {
# delete changes
$Self->Print("Deleting all changes...\n");
$Self->DeleteChanges( ChangesIDs => \@ChangesIDs );
}
else {
$Self->Print("Command delete was canceled!\n");
return $Self->ExitCodeOk();
}
}
else {
$Self->Print("There are NO changes to delete.\n");
}
}
# delete listed changes
elsif (@ChangeNumbers) {
my @ChangesIDs;
for my $ChangeNumber (@ChangeNumbers) {
# checks the validity of the change id
my $ID = $ChangeObject->ChangeLookup(
ChangeNumber => $ChangeNumber,
);
if ($ID) {
push @ChangesIDs, $ID;
}
else {
$Self->PrintError("Unable to find change $ChangeNumber.");
}
}
# delete changes (if any valid number was given)
if (@ChangesIDs) {
$Self->Print("Deleting specified changes...\n");
$Self->DeleteChanges( ChangesIDs => \@ChangesIDs );
}
}
else {
$Self->PrintError("No change for delete.");
}
$Self->Print( "" . ( '=' x 69 ) . "\n" );
$Self->Print("Done.\n");
return $Self->ExitCodeOk();
}
sub DeleteChanges {
my ( $Self, %Param ) = @_;
my $DeletedChanges = 0;
# delete specified changes
for my $ChangeID ( @{ $Param{ChangesIDs} } ) {
my $True = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeDelete(
ChangeID => $ChangeID,
UserID => 1,
);
if ( !$True ) {
$Self->PrintError("Unable to delete change with id $ChangeID\n");
}
else {
$DeletedChanges++;
}
}
$Self->Print("Deleted $DeletedChanges change(s).\n\n");
return 1;
}
1;
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L).
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.
=cut