/usr/share/otrs/Kernel/System/CommunicationChannel.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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 | # --
# 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::CommunicationChannel;
use strict;
use warnings;
use Kernel::System::VariableCheck qw(DataIsDifferent IsHashRefWithData);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Cache',
'Kernel::System::DB',
'Kernel::System::Main',
'Kernel::System::Log',
'Kernel::System::Ticket::Article',
'Kernel::System::Valid',
'Kernel::System::XML',
'Kernel::System::YAML',
);
=head1 NAME
Kernel::System::CommunicationChannel - Functions to manage communication channels.
=head1 DESCRIPTION
All functions to manage communication channels.
=head1 PUBLIC INTERFACE
=head2 new()
Don't use the constructor directly, use the ObjectManager instead:
my $CommunicationChannelObject = $Kernel::OM->Get('Kernel::System::CommunicationChannel');
=cut
sub new {
my ( $Type, %Param ) = @_;
my $Self = {};
bless( $Self, $Type );
$Self->{CacheTTL} = 24 * 3600 * 30; # 1 month
return $Self;
}
=head2 ChannelAdd()
Add new communication channel.
my $ChannelID = $CommunicationChannelObject->ChannelAdd(
ChannelName => 'Email', # (required) Communication channel name
Module => 'Kernel::System::CommunicationChannel::Internal', # (required) Module
PackageName => 'Framework', # (required) Package name
ChannelData => {...}, # (optional) Additional channel data (can be array, hash, scalar).
ValidID => 1, # (optional) Default 1.
UserID => 1, # (required) UserID
);
Returns:
$ChannelID = 1;
=cut
sub ChannelAdd {
my ( $Self, %Param ) = @_;
for my $Needed (qw(ChannelName UserID Module PackageName)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}
$Param{ValidID} //= $Kernel::OM->Get('Kernel::System::Valid')->ValidLookup(
Valid => 'valid',
);
$Param{ChannelData} //= '';
my $ChannelDataYAML = $Kernel::OM->Get('Kernel::System::YAML')->Dump(
Data => $Param{ChannelData},
);
my $SQL = '
INSERT INTO communication_channel
(name, module, package_name, channel_data, valid_id, create_time, create_by, change_time, change_by)
VALUES (?, ?, ?, ?, ?, current_timestamp, ?, current_timestamp, ?)
';
return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => $SQL,
Bind => [
\$Param{ChannelName},
\$Param{Module},
\$Param{PackageName},
\$ChannelDataYAML,
\$Param{ValidID},
\$Param{UserID},
\$Param{UserID},
],
);
my %Channel = $Self->ChannelGet(
ChannelName => $Param{ChannelName},
);
$Self->_ChannelListCacheCleanup();
return $Channel{ChannelID};
}
=head2 ChannelGet()
Get a communication channel.
my %CommunicationChannel = $CommunicationChannelObject->ChannelGet(
ChannelID => '1', # (optional) Communication channel id
# or
ChannelName => 'Email', # (optional) Communication channel name
);
Returns:
%CommunicationChannel = (
ChannelID => 1,
ChannelName => 'Email',
Module => 'Kernel::System::CommunicationChannel::Email',
PackageName => 'Framework',
ChannelData => {...}, # Additional channel data (can be array, hash, scalar).
DisplayName => 'Email', # Configurable
DisplayIcon => 'fa-envelope', # Configurable
ValidID => 1,
CreateTime => '2017-01-01 00:00:00',
CreateBy => 1,
ChangeTime => '2017-01-01 00:00:00',
ChangeBy => 1,
);
=cut
sub ChannelGet {
my ( $Self, %Param ) = @_;
# Check needed stuff.
if ( !$Param{ChannelName} && !$Param{ChannelID} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need ChannelID or ChannelName!',
);
return;
}
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
my $CacheType = 'CommunicationChannel';
my $CacheKey = 'ChannelGet::';
if ( $Param{ChannelName} ) {
$CacheKey .= "ChannelName::$Param{ChannelName}";
}
else {
$CacheKey .= "ChannelID::$Param{ChannelID}";
}
my $Cache = $CacheObject->Get(
Type => $CacheType,
Key => $CacheKey,
);
if ( ref $Cache eq 'HASH' ) {
my $Config = $ConfigObject->Get('CommunicationChannel')->{ $Cache->{ChannelName} } || {};
# Add some runtime values from config.
$Cache->{DisplayName} = $Config->{Name} || $Cache->{ChannelName};
$Cache->{DisplayIcon} = $Config->{Icon} || 'fa-exchange';
return %{$Cache};
}
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $SQL = '
SELECT id, name, module, package_name, channel_data, valid_id, create_time, create_by, change_time, change_by
FROM communication_channel
WHERE
';
my @Bind;
if ( $Param{ChannelName} ) {
$SQL .= "name = ? ";
push @Bind, \$Param{ChannelName},
}
else {
$SQL .= "id = ? ";
push @Bind, \$Param{ChannelID},
}
return if !$DBObject->Prepare(
SQL => $SQL,
Bind => \@Bind,
);
my $YAMLObject = $Kernel::OM->Get('Kernel::System::YAML');
my %Result;
while ( my @Row = $DBObject->FetchrowArray() ) {
$Result{ChannelID} = $Row[0];
$Result{ChannelName} = $Row[1];
$Result{Module} = $Row[2];
$Result{PackageName} = $Row[3];
$Result{ChannelData} = $YAMLObject->Load( Data => $Row[4] );
$Result{ValidID} = $Row[5];
$Result{CreateTime} = $Row[6];
$Result{CreateBy} = $Row[7];
$Result{ChangeTime} = $Row[8];
$Result{ChangeBy} = $Row[9];
}
return if !%Result;
$CacheObject->Set(
Type => $CacheType,
Key => $CacheKey,
Value => \%Result,
TTL => $Self->{CacheTTL},
);
my $Config = $ConfigObject->Get('CommunicationChannel')->{ $Result{ChannelName} } || {};
# Add some runtime values from config (do not cache).
$Result{DisplayName} = $Config->{Name} || $Result{ChannelName};
$Result{DisplayIcon} = $Config->{Icon} || 'fa-exchange';
return %Result;
}
=head2 ChannelUpdate()
Update communication channel.
my $Success = $CommunicationChannelObject->ChannelUpdate(
ChannelID => '1', # (required) ChannelID that will be updated
ChannelName => 'Email', # (required) New channel name
Module => 'Kernel::System::CommunicationChannel::Internal', # (optional) Module
PackageName => 'Framework', # (optional) Package name
ChannelData => {...}, # (optional) Additional channel data (can be array, hash, scalar)
ValidID => 1, # (optional) ValidID
UserID => 1, # (required) UserID
);
Returns:
$Success = 1;
=cut
sub ChannelUpdate {
my ( $Self, %Param ) = @_;
# Check needed stuff.
for my $Needed (qw(ChannelID ChannelName UserID)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);
return;
}
}
my %Channel = $Self->ChannelGet(
ChannelID => $Param{ChannelID},
);
return if !%Channel;
# Update ValidID only if it's needed.
if ( defined $Param{ValidID} ) {
$Channel{ValidID} = $Param{ValidID};
}
if ( defined $Param{ChannelData} ) {
$Channel{ChannelData} = $Param{ChannelData};
}
if ( defined $Param{Module} ) {
$Channel{Module} = $Param{Module};
}
if ( defined $Param{PackageName} ) {
$Channel{PackageName} = $Param{PackageName};
}
my $ChannelDataYAML = $Kernel::OM->Get('Kernel::System::YAML')->Dump(
Data => $Channel{ChannelData},
);
my $SQL = '
UPDATE communication_channel
SET
name = ?,
module = ?,
package_name = ?,
channel_data = ?,
valid_id = ?,
change_time = current_timestamp,
change_by = ?
WHERE id = ?
';
return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => $SQL,
Bind => [
\$Param{ChannelName},
\$Channel{Module},
\$Channel{PackageName},
\$ChannelDataYAML,
\$Channel{ValidID},
\$Param{UserID},
\$Param{ChannelID},
],
);
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
# Delete cache.
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => "ChannelGet::ChannelName::$Channel{ChannelName}",
);
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => "ChannelGet::ChannelID::$Param{ChannelID}",
);
$Self->_ChannelListCacheCleanup();
return 1;
}
=head2 ChannelList()
Get a list of all available communication channels.
my @CommunicationChannels = $CommunicationChannelObject->ChannelList(
ValidID => 1, # (optional) filter by ValidID
);
Returns:
@CommunicationChannels = (
{
ChannelID => 1,
ChannelName => 'Email',
Module => 'Kernel::System::CommunicationChannel::Email',
PackageName => 'Framework',
ChannelData => {...},
DisplayName => 'Email',
DisplayIcon => 'fa-envelope',
ValidID => 1,
CreateTime => '2017-01-01 00:00:00',
CreateBy => 1,
ChangeTime => '2017-01-01 00:00:00',
ChangeBy => 1,
},
{
ChannelID => 2,
ChannelName => 'Phone',
Module => 'Kernel::System::CommunicationChannel::Phone',
PackageName => 'Framework',
ChannelData => {...},
DisplayName => 'Phone',
DisplayIcon => 'fa-phone',
ValidID => 1,
CreateTime => '2017-01-01 00:00:00',
CreateBy => 1,
ChangeTime => '2017-01-01 00:00:00',
ChangeBy => 1,
},
...
);
=cut
sub ChannelList {
my ( $Self, %Param ) = @_;
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
my $CacheType = 'CommunicationChannel';
my $CacheKey = 'ChannelList';
my @Bind;
if ( defined $Param{ValidID} ) {
$CacheKey .= '::ValidID::' . $Param{ValidID};
push @Bind, \$Param{ValidID};
}
my $Cache = $CacheObject->Get(
Type => $CacheType,
Key => $CacheKey,
);
if ( ref $Cache eq 'ARRAY' ) {
for my $Channel ( @{$Cache} ) {
my $Config = $ConfigObject->Get('CommunicationChannel')->{ $Channel->{ChannelName} } || {};
# Add some runtime values from config.
$Channel->{DisplayName} = $Config->{Name} || $Channel->{ChannelName};
$Channel->{DisplayIcon} = $Config->{Icon} || 'fa-exchange';
}
return @{$Cache};
}
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $SQL = '
SELECT id, name, module, package_name, channel_data, valid_id, create_time, create_by, change_time, change_by
FROM communication_channel
';
if ( defined $Param{ValidID} ) {
$SQL .= " WHERE valid_id = ? ";
}
$SQL .= ' ORDER BY name ASC ';
return if !$DBObject->Prepare(
SQL => $SQL,
Bind => \@Bind,
);
my @Result;
my $YAMLObject = $Kernel::OM->Get('Kernel::System::YAML');
while ( my @Row = $DBObject->FetchrowArray() ) {
push @Result, {
ChannelID => $Row[0],
ChannelName => $Row[1],
Module => $Row[2],
PackageName => $Row[3],
ChannelData => $YAMLObject->Load( Data => $Row[4] ),
ValidID => $Row[5],
CreateTime => $Row[6],
CreateBy => $Row[7],
ChangeTime => $Row[8],
ChangeBy => $Row[9],
};
}
$CacheObject->Set(
Type => $CacheType,
Key => $CacheKey,
Value => \@Result,
TTL => $Self->{CacheTTL},
);
for my $Channel (@Result) {
my $Config = $ConfigObject->Get('CommunicationChannel')->{ $Channel->{ChannelName} } || {};
# Add some runtime values from config (do not cache).
$Channel->{DisplayName} = $Config->{Name} || $Channel->{ChannelName};
$Channel->{DisplayIcon} = $Config->{Icon} || 'fa-exchange';
}
return @Result;
}
=head2 ChannelSync()
Synchronize communication channels in the database with channel registration in configuration.
my $Result = $CommunicationChannelObject->ChannelSync(
UserID => 1,
);
Returns:
$Result = {
ChannelsUpdated => [ 'Email', 'Phone' ],
ChannelsAdded => [ 'Chat' ],
ChannelsInvalid => [ 'Internal' ],
};
=cut
sub ChannelSync {
my ( $Self, %Param ) = @_;
if ( !$Param{UserID} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need UserID!',
);
return;
}
# Get known channels and transform the result into a hash for easier lookup.
my %CommunicationChannels = map { $_->{ChannelName} => 1 } $Self->ChannelList();
# Get channel registration data.
my $ChannelRegistration = $Kernel::OM->Get('Kernel::Config')->Get('CommunicationChannel');
my %ChannelsRegistered = map { $_ => 1 } keys %{ $ChannelRegistration // {} };
# Merge the already known and registered channels.
%CommunicationChannels = (
%CommunicationChannels,
%ChannelsRegistered,
);
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
my %Result;
CHANNEL:
for my $Channel ( sort keys %CommunicationChannels ) {
# Get channel data if it is already known.
my %CommunicationChannel = $Self->ChannelGet(
ChannelName => $Channel,
);
# Check if channel registration is not present.
if ( !IsHashRefWithData( $ChannelRegistration->{$Channel} ) ) {
# Drop the channel, but only if there is no article data associated to it.
if ( $CommunicationChannel{ChannelID} ) {
$Self->ChannelDrop( ChannelID => $CommunicationChannel{ChannelID} );
push @{ $Result{ChannelsInvalid} }, $Channel;
}
next CHANNEL;
}
my $Module = $ChannelRegistration->{$Channel}->{Module};
if ( !$Module ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Missing Module in registration (CommunicationChannel###$Channel)!",
);
next CHANNEL;
}
my $Loaded = $MainObject->Require(
$Module,
Silent => 1,
);
if ( !$Loaded ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "System was unable to require module '$Module'!",
);
next CHANNEL;
}
my $Object = $Module->new();
my $PackageName = $Object->PackageNameGet();
if ( !$PackageName ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "System was unable to retrieve package name ($Module)!",
);
next CHANNEL;
}
my %ChannelData = (
ArticleDataTables => [ $Object->ArticleDataTables() ],
ArticleDataArticleIDField => $Object->ArticleDataArticleIDField(),
);
# Update existing communication channel.
if (%CommunicationChannel) {
if (
$ChannelRegistration->{$Channel}->{Module} ne $CommunicationChannel{Module}
|| $PackageName ne $CommunicationChannel{PackageName}
|| DataIsDifferent(
Data1 => \%ChannelData,
Data2 => $CommunicationChannel{ChannelData}
)
)
{
my $Success = $Self->ChannelUpdate(
ChannelID => $CommunicationChannel{ChannelID},
ChannelName => $Channel,
Module => $ChannelRegistration->{$Channel}->{Module},
PackageName => $PackageName,
ChannelData => \%ChannelData,
ValidID => 1,
UserID => $Param{UserID},
);
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "System was not able to update communication channel ($Channel)!",
);
next CHANNEL;
}
push @{ $Result{ChannelsUpdated} }, $Channel;
}
}
# Create communication channel if it's missing.
else {
my $ChannelID = $Self->ChannelAdd(
ChannelName => $Channel,
ChannelData => \%ChannelData,
Module => $Module,
PackageName => $PackageName,
UserID => $Param{UserID},
);
if ( !$ChannelID ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "System was not able to add communication channel ($Channel)!",
);
next CHANNEL;
}
push @{ $Result{ChannelsAdded} }, $Channel;
}
}
return %Result;
}
=head2 ChannelObjectGet()
Returns instance of the Channel object.
my $Object = $CommunicationChannelObject->ChannelObjectGet(
ChannelName => 'Email', # ChannelName or ChannelID required
);
my $Object = $CommunicationChannelObject->ChannelObjectGet(
ChannelID => 2, # ChannelName or ChannelID required
);
B<Warning>: this function returns no object in case a channel is no longer available in the system,
so make sure to check its return value.
=cut
sub ChannelObjectGet {
my ( $Self, %Param ) = @_;
if ( !$Param{ChannelName} && !$Param{ChannelID} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need ChannelName or ChannelID!'
);
return;
}
my %Channel = $Self->ChannelGet(%Param);
return if !%Channel;
# Return nothing if channel is missing.
my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
$Channel{Module},
Silent => 1,
);
return if !$Loaded;
return $Kernel::OM->Get( $Channel{Module} );
}
=head2 ChannelDrop()
Drop an invalid communication channel (that only exists in the database, but not in the configuration).
By default, this will only drop channels that have no associated article data; use C<DropArticleData> to
force article data removal as well. Channels provided by the OTRS framework can never be dropped.
my $Success = $CommunicationChannelObject->ChannelDrop(
ChannelID => 1, # (required) Delete by channel ID
# or
ChannelName => 'SomeChannel', # (required) Delete by channel name
DropArticleData => 1, # (optional) Also drop article data if possible, default: 0
);
Returns:
$Success = 1;
=cut
sub ChannelDrop {
my ( $Self, %Param ) = @_;
if ( !$Param{ChannelID} && !$Param{ChannelName} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need ChannelID or ChannelName!',
);
return;
}
my %Channel = $Self->ChannelGet(%Param);
return if !%Channel;
# Avoid dropping channel if it's still registered.
my $ChannelRegistration = $Kernel::OM->Get('Kernel::Config')->Get('CommunicationChannel');
return if IsHashRefWithData( $ChannelRegistration->{ $Channel{ChannelName} } );
# Prevent dropping of channels provided by the framework (fail-safe).
return if $Channel{PackageName} eq 'Framework';
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
# Find all existing articles that belongs to the channel.
return if !$DBObject->Prepare(
SQL => '
SELECT id, ticket_id
FROM article
WHERE communication_channel_id = ?
ORDER BY id ASC
',
Bind => [ \$Channel{ChannelID} ],
);
# Create article to ticket ID lookup map.
my %TicketLookup;
while ( my @Row = $DBObject->FetchrowArray() ) {
$TicketLookup{ $Row[0] } = $Row[1];
}
# If some data was found and is not supposed to be dropped, bail out early!
return if %TicketLookup && !$Param{DropArticleData};
if (%TicketLookup) {
# If specified channel backend is not in the system anymore, following method will, in fact, return invalid
# channel backend instead, which should still be able to delete any articles.
my $ArticleBackendObject = $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForChannel(%Channel);
# Remove articles and associated data.
for my $ArticleID ( sort keys %TicketLookup ) {
$ArticleBackendObject->ArticleDelete(
TicketID => $TicketLookup{$ArticleID},
ArticleID => $ArticleID,
UserID => 1,
);
}
}
# Get a list of all tables in database.
my @Tables = $DBObject->ListTables();
# Check for table existence.
my @TablesToDrop;
for my $ArticleDataTable ( @{ $Channel{Data}->{ArticleDataTables} // [] } ) {
if ( grep { $_ eq $ArticleDataTable } @Tables ) {
push @TablesToDrop, $ArticleDataTable;
}
}
# Drop article storage tables.
if (@TablesToDrop) {
my $TableList = join ', ', @TablesToDrop;
my $DBType = $DBObject->{'DB::Type'};
if ( $DBType eq 'mysql' ) {
# Drop all article tables in same statement.
$DBObject->Do( SQL => "DROP TABLE $TableList" );
}
elsif ( $DBType eq 'postgresql' ) {
# Drop all article tables in same statement.
$DBObject->Do( SQL => "DROP TABLE $TableList" );
}
elsif ( $DBType eq 'oracle' ) {
# Drop every article table in a separate statement.
for my $Table (@TablesToDrop) {
$DBObject->Do( SQL => "DROP TABLE $Table CASCADE CONSTRAINTS" );
}
}
}
return $Self->ChannelDelete(%Param);
}
=head2 ChannelDelete()
Delete communication channel record from the database.
my $Success = $CommunicationChannelObject->ChannelDelete(
ChannelID => 1, # (optional) Delete by ChannelID
# or
ChannelName => 'Email', # (optional) Delete by Channel name
);
Returns:
$Success = 1;
=cut
sub ChannelDelete {
my ( $Self, %Param ) = @_;
if ( !$Param{ChannelID} && !$Param{ChannelName} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => 'Need ChannelID or ChannelName!',
);
return;
}
my %Channel = $Self->ChannelGet(%Param);
if ( !%Channel ) {
my $Parameter = $Param{ChannelID} || $Param{ChannelName};
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Communication channel not found ($Parameter)!"
);
return;
}
my $SQL = '
DELETE FROM communication_channel
WHERE
';
my @Bind;
if ( $Param{ChannelID} ) {
$SQL .= "id = ?";
push @Bind, \$Param{ChannelID};
}
else {
$SQL .= "name = ?";
push @Bind, \$Param{ChannelName};
}
return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => $SQL,
Bind => \@Bind,
);
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
# Delete cache.
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => "ChannelGet::ChannelName::$Channel{ChannelName}",
);
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => "ChannelGet::ChannelID::$Channel{ChannelID}",
);
$Self->_ChannelListCacheCleanup();
return 1;
}
=head1 PRIVATE FUNCTIONS
=head2 _ChannelListCacheCleanup()
Delete communication channel cache.
my $Success = $CommunicationChannelObject->_ChannelListCacheCleanup();
Returns:
$Success = 1;
=cut
sub _ChannelListCacheCleanup {
my ( $Self, %Param ) = @_;
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
# Delete cache.
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => 'ChannelList',
);
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => 'ChannelList::ValidID::0',
);
$CacheObject->Delete(
Type => 'CommunicationChannel',
Key => 'ChannelList::ValidID::1',
);
return 1;
}
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
|