File: Queue.pm

package info (click to toggle)
otrs2 2.2.7-2lenny3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 13,444 kB
  • ctags: 5,808
  • sloc: perl: 129,825; xml: 16,139; sql: 11,400; sh: 1,198; makefile: 31; php: 16
file content (1067 lines) | stat: -rw-r--r-- 30,961 bytes parent folder | download
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# --
# Kernel/System/Queue.pm - lib for queue functions
# Copyright (C) 2001-2007 OTRS GmbH, http://otrs.org/
# --
# $Id: Queue.pm,v 1.74 2007/08/22 08:45:22 sb Exp $
# --
# 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 http://www.gnu.org/licenses/gpl.txt.
# --

package Kernel::System::Queue;

use strict;
use warnings;
use Kernel::System::StdResponse;
use Kernel::System::Group;
use Kernel::System::CustomerGroup;
use Kernel::System::Valid;

use vars qw($VERSION);
$VERSION = '$Revision: 1.74 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

=head1 NAME

Kernel::System::Queue - queue lib

=head1 SYNOPSIS

All queue functions. E. g. to add queue or other functions.

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

create a object

    use Kernel::Config;
    use Kernel::System::Log;
    use Kernel::System::Main;
    use Kernel::System::DB;
    use Kernel::System::Queue;

    my $ConfigObject = Kernel::Config->new();
    my $LogObject = Kernel::System::Log->new(
        ConfigObject => $ConfigObject,
    );
    my $MainObject = Kernel::System::Main->new(
        ConfigObject => $ConfigObject,
        LogObject => $LogObject,
    );
    my $DBObject = Kernel::System::DB->new(
        MainObject => $MainObject,
        ConfigObject => $ConfigObject,
        LogObject => $LogObject,
    );
    my $QueueObject = Kernel::System::Queue->new(
        ConfigObject => $ConfigObject,
        LogObject => $LogObject,
        DBObject => $DBObject,
    );

=cut

sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);

    $Self->{QueueID} = $Param{QueueID} || ''; #die "Got no QueueID!";

    # check needed objects
    foreach (qw(DBObject ConfigObject LogObject)) {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }
    $Self->{ValidObject} = Kernel::System::Valid->new(%Param);

    # lib object
    $Self->{StdResponseObject} = Kernel::System::StdResponse->new(%Param);
    if (!$Param{GroupObject}) {
        $Self->{GroupObject} = Kernel::System::Group->new(%Param);
    }
    else {
        $Self->{GroupObject} = $Param{GroupObject};
    }
    if (!$Param{CustomerGroupObject}) {
        $Self->{CustomerGroupObject} = Kernel::System::CustomerGroup->new(%Param);
    }
    else {
        $Self->{CustomerGroupObject} = $Param{CustomerGroupObject};
    }

    # --------------------------------------------------- #
    #  default queue  settings                            #
    #  these settings are used by the CLI version         #
    # --------------------------------------------------- #
    $Self->{QueueDefaults} = {
        UnlockTimeout => 0,
        FirstResponseTime => 0,
        UpdateTime => 0,
        SolutionTime => 0,
        FollowUpLock => 0,
        SystemAddressID => 1,
        SalutationID => 1,
        SignatureID => 1,
        FollowUpID => 1,
        FollowUpLock => 0,
        MoveNotify => 0,
        LockNotify => 0,
        StateNotify => 0,
    };

    return $Self;
}

=item GetSystemAddress()

get a queue system email address as hash (Email, RealName)

    my %Adresss = $QueueObject->GetSystemAddress(
        QueueID => 123,
    );

=cut

sub GetSystemAddress {
    my $Self = shift;
    my %Param = @_;
    my %Address;
    my $QueueID = $Param{QueueID} || $Self->{QueueID};
    my $SQL = "SELECT sa.value0, sa.value1 FROM system_address sa, queue sq ".
        " WHERE ".
        " sq.id = ".$Self->{DBObject}->Quote($QueueID, 'Integer')." ".
        " and ".
        " sa.id = sq.system_address_id";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $Address{Email} = $Row[0];
        $Address{RealName} = $Row[1];
    }
    # prepare realname quote
    if ($Address{RealName} =~ /(,|@|\(|\)|:)/ && $Address{RealName} !~ /^("|')/) {
        $Address{RealName} =~ s/"/\"/g;
        $Address{RealName} = '"'.$Address{RealName}.'"';
    }
    return %Address;
}

=item GetSalutation()

get a queue salutation

    my $Salutation = $QueueObject->GetSalutation(QueueID => 123);

=cut

sub GetSalutation {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need QueueID!");
        return;
    }
    # db quote
    foreach (qw(QueueID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # sql
    my $String = '';
    my $SQL = "SELECT text FROM salutation sa, queue sq ".
        " WHERE ".
        " sq.id = $Param{QueueID} ".
        " and ".
        " sq.salutation_id = sa.id";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $String = $Row[0];
    }
    return $String;
}

=item GetSignature()

get a queue signature

    my $Signature = $QueueObject->GetSignature(QueueID => 123);

=cut

sub GetSignature {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need QueueID!");
        return;
    }
    # db quote
    foreach (qw(QueueID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # sql
    my $String = '';
    my $SQL = "SELECT text FROM signature si, queue sq ".
        " WHERE ".
        " sq.id = $Param{QueueID} ".
        " and ".
        " sq.signature_id = si.id";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $String = $Row[0];
    }
    return $String;
}

=item GetStdResponse()

get std response of a queue

    my $String = $QueueObject->GetStdResponse(ID => 123);

=cut

sub GetStdResponse {
    my $Self = shift;
    my %Param = @_;
    my $String = '';
    # check needed stuff
    if (!$Param{ID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need ID!");
        return;
    }
    # db quote
    foreach (qw(ID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # sql
    my $SQL = "SELECT text FROM standard_response".
        " WHERE ".
        " id = $Param{ID} ";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $String = $Row[0];
    }
    return $String;
}

# for comapt!
sub SetQueueStdResponse {
    my $Self = shift;
    my %Param = @_;
    unless  ($Param{ResponseID} && $Param{QueueID} && $Param{UserID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need ResponseID, QueueID and UserID!");
        return;
    }
    if ($Self->QueueHasStdResponse(%Param)) {
        return;
    }
    # db quote
    foreach (keys %Param) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_});
    }
    # sql
    my $SQL = sprintf(qq|INSERT INTO queue_standard_response (queue_id, standard_response_id, create_time, create_by, change_time, change_by)
    VALUES ( %s, %s, current_timestamp, %s, current_timestamp, %s)| , $Param{QueueID}, $Param{ResponseID}, $Param{UserID}, $Param{UserID});

    if ($Self->{DBObject}->Do(SQL => $SQL)) {
        return 1;
    }
    else {
        return 0;
    }
}

# for comapt!
sub QueueHasStdResponse {
    my $Self = shift;
    my %Param = @_;
    unless  ($Param{ResponseID} && $Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need ResponseID and QueueID!");
        return;
    }
    # db quote
    foreach (keys %Param) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_});
    }
    # sql
    my $SQL = sprintf("select count(*) from  queue_standard_response  where queue_id=%s and standard_response_id=%s" ,  $Param{QueueID}, $Param{ResponseID});
    #print "SQL was\n$SQL\n\n";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    my $Count;
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $Count = $Row[0];
    }
    return $Count;
}

=item GetStdResponses()

get std responses of a queue

    my %Responses = $QueueObject->GetStdResponses(QueueID => 123);

=cut

sub GetStdResponses {
    my $Self = shift;
    my %Param = @_;
    my %StdResponses;
    # check needed stuff
    if (!$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need QueueID!");
        return;
    }
    # check if this result is present
    if ($Self->{"StdResponses::$Param{QueueID}"}) {
        return %{$Self->{"StdResponses::$Param{QueueID}"}};
    }
    # get std. responses
    my $SQL = "SELECT sr.id, sr.name ".
        " FROM ".
        " standard_response sr, queue_standard_response qsr".
        " WHERE ".
        " qsr.queue_id IN (".$Self->{DBObject}->Quote($Param{QueueID}, 'Integer').")".
        " AND ".
        " qsr.standard_response_id = sr.id".
        " AND ".
        " sr.valid_id IN ( ${\(join ', ', $Self->{ValidObject}->ValidIDsGet())} )".
        " ORDER BY sr.name";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $StdResponses{$Row[0]} = $Row[1];
    }
    # store std responses
    $Self->{"StdResponses::$Param{QueueID}"} = \%StdResponses;
    # return responses
    return %StdResponses;
}

=item GetAllQueues()

get all system queues

    my %Queues = $QueueObject->GetAllQueues();

get all system queues of a user with permission type (e. g. ro, move_into, rw, ...)

    my %Queues = $QueueObject->GetAllQueues(UserID => 123, Type => 'ro');

=cut

sub GetAllQueues {
    my $Self = shift;
    my %Param = @_;
    my $UserID = $Param{UserID} || '';
    my $Type = $Param{Type} || 'ro';
    # fetch all queues
    my %MoveQueues;
    if ($Param{UserID}) {
        if ($Self->{"QG::GetAllQueues::UserID::$Param{UserID}"}) {
            return %{$Self->{"QG::GetAllQueues::UserID::$Param{UserID}"}};
        }
        my @GroupIDs = $Self->{GroupObject}->GroupMemberList(
            UserID => $Param{UserID},
            Type => $Type,
            Result => 'ID',
            Cached => 1,
        );
        if (@GroupIDs) {
            my $SQL = "SELECT id, name FROM queue".
                " WHERE ".
                " group_id IN ( ${\(join ', ', @GroupIDs)} )".
                " AND ".
                " valid_id IN ( ${\(join ', ', $Self->{ValidObject}->ValidIDsGet())} )";
            $Self->{DBObject}->Prepare(SQL => $SQL);
        }
        else {
            return;
        }
    }
    elsif ($Param{CustomerUserID}) {
        if ($Self->{"QG::GetAllQueues::CustomerUserID::$Param{CustomerUserID}"}) {
            return %{$Self->{"QG::GetAllQueues::CustomerUserID::$Param{CustomerUserID}"}};
        }
        my @GroupIDs = $Self->{CustomerGroupObject}->GroupMemberList(
            UserID => $Param{CustomerUserID},
            Type => $Type,
            Result => 'ID',
            Cached => 1,
        );
        if (@GroupIDs) {
            my $SQL = "SELECT id, name FROM queue".
                " WHERE ".
                " group_id IN ( ${\(join ', ', @GroupIDs)} )".
                " AND ".
                " valid_id IN ( ${\(join ', ', $Self->{ValidObject}->ValidIDsGet())} )";
            $Self->{DBObject}->Prepare(SQL => $SQL);
        }
        else {
            return;
        }
    }
    else {
        if ($Self->{"QG::GetAllQueues"}) {
            return %{$Self->{"QG::GetAllQueues"}};
        }
        $Self->{DBObject}->Prepare(
            SQL => "SELECT id, name FROM queue WHERE valid_id IN ".
                "( ${\(join ', ', $Self->{ValidObject}->ValidIDsGet())} )",
        );
    }
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $MoveQueues{$Row[0]} = $Row[1];
    }

    if ($Param{UserID}) {
        $Self->{"QG::GetAllQueues::UserID::$Param{UserID}"} = \%MoveQueues;
    }
    elsif ($Param{CustomerUserID}) {
        $Self->{"QG::GetAllQueues::CustomerUserID::$Param{CustomerUserID}"} = \%MoveQueues;
    }
    else {
        $Self->{"QG::GetAllQueues"} = \%MoveQueues;
    }

    return %MoveQueues;
}

=item GetAllCustomQueues()

get all custom queues of one user

    my @Queues = $QueueObject->GetAllCustomQueues(UserID => 123);

=cut

sub GetAllCustomQueues {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{UserID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need UserID!");
        return;
    }
    # db quote
    foreach (qw(UserID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # fetch all queues
    my @QueueIDs;
    my $SQL = "SELECT queue_id FROM personal_queues WHERE user_id = $Param{UserID}";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        push(@QueueIDs, $Row[0]);
    }
    return @QueueIDs;
}

=item QueueLookup()

get id or name for queue

    my $Queue = $QueueObject->QueueLookup(QueueID => $QueueID);

    my $QueueID = $QueueObject->QueueLookup(Queue => $Queue);

=cut

sub QueueLookup {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{Queue} && !$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Got no Queue or QueueID!");
        return;
    }
    # check if we ask the same request (cache)?
    if ($Param{QueueID} && $Self->{"QL::Queue$Param{QueueID}"}) {
        return $Self->{"QL::Queue$Param{QueueID}"};
    }
    if ($Param{Queue} && $Self->{"QL::QueueID$Param{Queue}"}) {
        return $Self->{"QL::QueueID$Param{Queue}"};
    }
    # get data
    my $SQL = '';
    my $Suffix = '';
    if ($Param{Queue}) {
        $Param{What} = $Param{Queue};
        $Suffix = 'QueueID';
        $SQL = "SELECT id FROM queue WHERE name = '".$Self->{DBObject}->Quote($Param{Queue})."'";
    }
    else {
        $Param{What} = $Param{QueueID};
        $Suffix = 'Queue';
        $SQL = "SELECT name FROM queue WHERE id = ".$Self->{DBObject}->Quote($Param{QueueID}, 'Integer')."";
    }
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        # store result
        $Self->{"QL::$Suffix$Param{What}"} = $Row[0];
    }
    # check if data exists
    if (!exists $Self->{"QL::$Suffix$Param{What}"}) {
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Found no \$$Suffix for $Param{What}!",
        );
        return;
    }
    # return result
    return $Self->{"QL::$Suffix$Param{What}"};
}

=item GetFollowUpOption()

...

    ...

=cut

sub GetFollowUpOption {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need QueueID!");
        return;
    }
    # db quote
    foreach (qw(QueueID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # fetch queues data
    my $Return = '';
    my $SQL = "SELECT sf.name ".
        " FROM ".
        " follow_up_possible sf, queue sq ".
        " WHERE ".
        " sq.follow_up_id = sf.id ".
        " AND ".
        " sq.id = $Param{QueueID}";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $Return = $Row[0];
    }
    return $Return;
}

=item GetFollowUpLockOption()

...

    ...

=cut

sub GetFollowUpLockOption {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need QueueID!");
        return;
    }
    # db quote
    foreach (qw(QueueID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # fetch queues data
    my $Return = 0;
    my $SQL = "SELECT sq.follow_up_lock ".
        " FROM ".
        " queue sq ".
        " WHERE ".
        " sq.id = $Param{QueueID}";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $Return = $Row[0];
    }
    return $Return;
}

=item GetQueueGroupID()

...

    ...

=cut

sub GetQueueGroupID {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{QueueID}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need QueueID!");
        return;
    }
    if ($Self->{"QG::GetQueueGroupID::$Param{QueueID}"}) {
        return $Self->{"QG::GetQueueGroupID::$Param{QueueID}"};
    }
    # db quote
    foreach (qw(QueueID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # sql
    my $GID = '';
    my $SQL = "SELECT group_id ".
        " FROM ".
        " queue ".
        " WHERE ".
        " id = $Param{QueueID}";
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Row = $Self->{DBObject}->FetchrowArray()) {
        $GID = $Row[0];
    }
    $Self->{"QG::GetQueueGroupID::$Param{QueueID}"} = $GID;
    return $GID;
}

=item QueueAdd()

add queue with attributes

    $QueueObject->QueueAdd(
        Name => 'Some::Queue',
        ValidID => 1,
        GroupID => 1,
        SystemAddressID => 1,
        SalutationID => 1,
        SignatureID => 1,
        UserID => 123,
        MoveNotify => 0,
        StateNotify => 0,
        LockNotify => 0,
        OwnerNotify => 0,
    );

=cut

sub QueueAdd {
    my $Self=shift;
    my %Param = @_;
    # Add Queue to the Database

    # Requires
    # Params{GroupID}   : ID of the group responsible for this quese
    # Param{QueueName}  : Duh! Name of the Queue
    # Param{ValidID}    : Is the queue invalid, valid, suspended etc
    # Param{UserID}     : ID of the person creating the Queue

    # Returns
    # new Queue ID on success
    # null/false on failure

    # ' and , are for modems. Line noise
    # A less noisy way to defining @Params is
    my @Params = qw(
        Name
        GroupID
        UnlockTimeout
        SystemAddressID
        Calendar
        DefaultSignKey
        SalutationID
        SignatureID
        FollowUpID
        FollowUpLock
        FirstResponseTime
        UpdateTime
        SolutionTime
        MoveNotify
        StateNotify
        Comment
        ValidID
    );

    for (qw(UnlockTimeout FirstResponseTime UpdateTime SolutionTime
        FollowUpLock SystemAddressID SalutationID SignatureID
        FollowUpID FollowUpLock MoveNotify StateNotify LockNotify OwnerNotify DefaultSignKey Calendar)) {
        # these are coming from Config.pm
        # I added default values in the Load Routine
        $Param{$_} = $Self->{QueueDefaults}{$_} || 0  unless ($Param{$_});
    };

    # check needed stuff
    foreach (qw(Name GroupID SystemAddressID SalutationID SignatureID MoveNotify StateNotify
        LockNotify OwnerNotify ValidID UserID)) {
        if (!defined($Param{$_})) {
            $Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
            return;
        }
    }
    foreach (qw(Name GroupID SystemAddressID SalutationID SignatureID ValidID)) {
        if (!$Param{$_}) {
            $Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
            return;
        }
    }
    # cleanup queue name
    $Param{Name} =~ s/(\n|\r)//g;
    $Param{Name} =~ s/\s$//g;
    # check queue name
    if ($Param{Name} =~ /::$/i) {
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Invalid Queue name '$Param{Name}'!",
        );
        return;
    }
    # quote db
    foreach (qw(Name DefaultSignKey Calendar Comment)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}) || '';
    };
    foreach (qw(GroupID UnlockTimeout SystemAddressID SalutationID SignatureID FollowUpID FollowUpLock
        FirstResponseTime UpdateTime SolutionTime
        MoveNotify StateNotify ValidID)) {
        $Param{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    my $SQL = "INSERT INTO queue ".
        "(name, ".
        " group_id, ".
        " unlock_timeout, ".
        " system_address_id, ".
        " calendar_name, ".
        " default_sign_key, ".
        " salutation_id, ".
        " signature_id, ".
        " first_response_time, ".
        " update_time, ".
        " solution_time, ".
        " follow_up_id, ".
        " follow_up_lock, ".
        " state_notify, ".
        " move_notify, ".
        " lock_notify, ".
        " owner_notify, ".
        " valid_id, ".
        " comments, ".
        " create_time, ".
        " create_by, ".
        " change_time, ".
        " change_by)".
        " VALUES ".
        " ('$Param{Name}', ".
        " $Param{GroupID}, ".
        " $Param{UnlockTimeout}, ".
        " $Param{SystemAddressID}, ".
        " '$Param{Calendar}', ".
        " '$Param{DefaultSignKey}', ".
        " $Param{SalutationID}, ".
        " $Param{SignatureID}, ".
        " $Param{FirstResponseTime}, ".
        " $Param{UpdateTime}, ".
        " $Param{SolutionTime}, ".
        " $Param{FollowUpID}, ".
        " $Param{FollowUpLock}, ".
        " $Param{StateNotify}, ".
        " $Param{MoveNotify}, ".
        " $Param{LockNotify}, ".
        " $Param{OwnerNotify}, ".
        " $Param{ValidID}, ".
        " '$Param{Comment}', ".
        " current_timestamp, ".
        " $Param{UserID}, ".
        " current_timestamp, ".
        " $Param{UserID})";

    if ($Self->{DBObject}->Do(SQL => $SQL)) {
        # get new queue id
        $SQL = "SELECT id ".
            " FROM ".
            " queue ".
            " WHERE ".
            " name = '$Param{Name}'";
        my $QueueID = '';
        $Self->{DBObject}->Prepare(SQL => $SQL);
        while (my @Row = $Self->{DBObject}->FetchrowArray()) {
            $QueueID = $Row[0];
        }
        # add default responses (if needed), add response by name
        if ($Self->{ConfigObject}->Get('StdResponse2QueueByCreating')) {
            foreach (@{$Self->{ConfigObject}->{StdResponse2QueueByCreating}}) {
                my $StdResponseID = $Self->{StdResponseObject}->StdResponseLookup(StdResponse => $_);
                if ($StdResponseID) {
                    $Self->SetQueueStdResponse(
                        QueueID => $QueueID,
                        ResponseID => $StdResponseID,
                        UserID => $Param{UserID},
                    );
                }
            }
        }
        # add response by id
        if ($Self->{ConfigObject}->Get('StdResponseID2QueueByCreating')) {
            foreach (@{$Self->{ConfigObject}->{StdResponseID2QueueByCreating}}) {
                $Self->SetQueueStdResponse(
                    QueueID => $QueueID,
                    ResponseID => $_,
                    UserID => $Param{UserID},
                );
            }
        }
        return $QueueID;
    }
    else {
        return;
    }
}

=item QueueGet()

get queue attributes

    my %Queue = $QueueObject->QueueGet(
        ID => 123,
        Cache => 1, # optional
    );

    my %Queue = $QueueObject->QueueGet(
        Name => 'Some::Queue',
        Cache => 1, # optional
    );

=cut

sub QueueGet {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    if (!$Param{ID} && !$Param{Name}) {
        $Self->{LogObject}->Log(Priority => 'error', Message => "Need ID or Name!");
        return;
    }
    # check if we ask the same request (cache)?
    if ($Param{Cache} && $Param{ID} && $Self->{"QG::ID$Param{ID}"}) {
        return %{$Self->{"QG::ID$Param{ID}"}};
    }
    if ($Param{Cache} && $Param{Queue} && $Self->{"QL::Name$Param{Name}"}) {
        return %{$Self->{"QG::Name$Param{Name}"}};
    }
    # sql
    my $SQL = "SELECT q.name, q.group_id, q.unlock_timeout, ".
        " q.system_address_id, q.salutation_id, q.signature_id, q.comments, q.valid_id, ".
        " q.first_response_time, q.update_time, q.solution_time, ".
        " q.follow_up_id, q.follow_up_lock, sa.value0, sa.value1, q.id, ".
        " q.move_notify, q.state_notify, q.lock_notify, q.owner_notify, q.default_sign_key, ".
        " q.calendar_name ".
        " FROM ".
        " queue q, system_address sa".
        " WHERE ".
        " q.system_address_id = sa.id ".
        " AND ";
    my $Suffix = '';
    if ($Param{ID}) {
        $Param{What} = $Param{ID};
        $Suffix = 'ID';
        $SQL .= " q.id = ".$Self->{DBObject}->Quote($Param{ID}, 'Integer')."";
    }
    else {
        $Param{What} = $Param{Name};
        $Suffix = 'Name';
        $SQL .= " q.name = '".$Self->{DBObject}->Quote($Param{Name})."'";
    }
    my %QueueData = ();
    $Self->{DBObject}->Prepare(SQL => $SQL);
    while (my @Data = $Self->{DBObject}->FetchrowArray()) {
        %QueueData = (
            QueueID => $Data[15],
            Name => $Data[0],
            GroupID => $Data[1],
            UnlockTimeout => $Data[2],
            FirstResponseTime => $Data[8],
            UpdateTime => $Data[9],
            SolutionTime => $Data[10],
            FollowUpID => $Data[11],
            FollowUpLock => $Data[12],
            SystemAddressID => $Data[3],
            SalutationID => $Data[4],
            SignatureID => $Data[5],
            Comment => $Data[6],
            ValidID => $Data[7],
            Email => $Data[13],
            RealName => $Data[14],
            StateNotify => $Data[17],
            MoveNotify => $Data[16],
            LockNotify => $Data[18],
            OwnerNotify => $Data[19],
            DefaultSignKey => $Data[20],
            Calendar => $Data[21],
        );
        $Self->{"QG::$Suffix$Param{What}"} = \%QueueData;
    }
    # check if data exists
    if (!exists $Self->{"QG::$Suffix$Param{What}"}) {
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Found no \$$Suffix for $Param{What}!",
        );
        return;
    }
    # return result
    return %{$Self->{"QG::$Suffix$Param{What}"}};
}

=item QueueUpdate()

update queue attributes

    $QueueObject->QueueUpdate(
        QueueID => 123,
        Name => 'Some::Queue',
        ValidID => 1,
        GroupID => 1,
        SystemAddressID => 1,
        SalutationID => 1,
        SignatureID => 1,
        UserID => 123,
        MoveNotify => 0,
        StateNotify => 0,
        LockNotify => 0,
        OwnerNotify => 0,
    );

=cut

sub QueueUpdate {
    my $Self = shift;
    my %Param = @_;
    # check needed stuff
    foreach (qw(QueueID Name ValidID GroupID SystemAddressID SalutationID SignatureID FollowUpID UserID
        MoveNotify StateNotify LockNotify OwnerNotify)) {
        if (!defined ($Param{$_})) {
            $Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
            return;
        }
    }
    foreach (qw(QueueID Name ValidID GroupID SystemAddressID SalutationID SignatureID UserID)) {
        if (!$Param{$_}) {
            $Self->{LogObject}->Log(Priority => 'error', Message => "Need $_!");
            return;
        }
    }
    # db quote
    my %DB = ();
    # check !!!
    foreach (qw(UnlockTimeout FollowUpLock MoveNotify StateNotify LockNotify OwnerNotify
        FirstResponseTime UpdateTime SolutionTime
        FollowUpID FollowUpLock DefaultSignKey Calendar)) {
        $Param{$_} = 0 if (!$Param{$_});
    }
    foreach (qw(Name DefaultSignKey Calendar Comment)) {
        $DB{$_} = $Self->{DBObject}->Quote($Param{$_}) || '';
    };
    foreach (qw(QueueID GroupID UnlockTimeout SystemAddressID SalutationID SignatureID FollowUpID
        FirstResponseTime UpdateTime SolutionTime
        FollowUpLock StateNotify LockNotify OwnerNotify MoveNotify StateNotify ValidID UserID)) {
        $DB{$_} = $Self->{DBObject}->Quote($Param{$_}, 'Integer');
    }
    # cleanup queue name
    $Param{Name} =~ s/(\n|\r)//g;
    $Param{Name} =~ s/\s$//g;
    # check queue name
    if ($Param{Name} =~ /::$/i) {
        $Self->{LogObject}->Log(
            Priority => 'error',
            Message => "Invalid Queue name '$Param{Name}'!",
        );
        return;
    }
    # check if queue name exists
    my %AllQueue = $Self->{DBObject}->GetTableData(
        Table => 'queue',
        What => 'id, name',
    );
    my %OldQueue = $Self->QueueGet(ID => $Param{QueueID});
    foreach (keys %AllQueue) {
        if ($AllQueue{$_} =~ /^$Param{Name}$/i && $_ != $Param{QueueID}) {
            $Self->{LogObject}->Log(
                Priority => 'error',
                Message => "Queue '$Param{Name}' exists! Can't updated queue '$OldQueue{Name}'.",
            );
            return;
        }
    }
    # sql
    my $SQL = "UPDATE queue SET name = '$DB{Name}', " .
        " comments = '$DB{Comment}', " .
        " group_id = $DB{GroupID}, " .
        " unlock_timeout = $DB{UnlockTimeout}, " .
        " first_response_time = $DB{FirstResponseTime}, " .
        " update_time = $DB{UpdateTime}, " .
        " solution_time = $DB{SolutionTime}, " .
        " follow_up_id = $DB{FollowUpID}, " .
        " follow_up_lock = $DB{FollowUpLock}, " .
        " system_address_id = $DB{SystemAddressID}, " .
        " calendar_name = '$DB{Calendar}', " .
        " default_sign_key = '$DB{DefaultSignKey}', " .
        " salutation_id = $DB{SalutationID}, " .
        " signature_id = $DB{SignatureID}, " .
        " move_notify = $DB{MoveNotify}, " .
        " state_notify = $DB{StateNotify}, " .
        " lock_notify = $DB{LockNotify}, " .
        " owner_notify = $DB{OwnerNotify}, " .
        " valid_id = $DB{ValidID}, " .
        " change_time = current_timestamp, " .
        " change_by = $DB{UserID} " .
        " WHERE id = $DB{QueueID}";
    if ($Self->{DBObject}->Do(SQL => $SQL)) {
        # updated all sub queue names
        my @ParentQueue = split(/::/, $OldQueue{Name});
        my %AllQueue = $Self->{DBObject}->GetTableData(
            Table => 'queue',
            What => 'id, name',
        );
        foreach (keys %AllQueue) {
            my @SubQueue = split(/::/, $AllQueue{$_});
            if ($#SubQueue > $#ParentQueue) {
                if ($AllQueue{$_} =~ /^\Q$OldQueue{Name}::\E/i) {
                    my $NewQueueName = $AllQueue{$_};
                    $NewQueueName =~ s/\Q$OldQueue{Name}\E/$Param{Name}/;
                    $NewQueueName = $Self->{DBObject}->Quote($NewQueueName);
                    my $SQL = "UPDATE queue SET ".
                        " name = '$NewQueueName', ".
                        " change_time = current_timestamp, " .
                        " change_by = $DB{UserID} " .
                        " WHERE id = $_";
                    $Self->{DBObject}->Do(SQL => $SQL);
                }
            }
        }
        return 1;
    }
    else {
        return;
    }
}

1;

=back

=head1 TERMS AND CONDITIONS

This Software is part of the OTRS project (http://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 http://www.gnu.org/licenses/gpl.txt.

=cut

=head1 VERSION

$Revision: 1.74 $ $Date: 2007/08/22 08:45:22 $

=cut