File: AgentLookup.pm

package info (click to toggle)
otrs2 2.0.4p01-18
  • links: PTS
  • area: main
  • in suites: etch
  • size: 7,900 kB
  • ctags: 4,437
  • sloc: perl: 81,607; xml: 8,135; sql: 8,013; sh: 1,113; makefile: 22; php: 16
file content (160 lines) | stat: -rw-r--r-- 5,406 bytes parent folder | download | duplicates (2)
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
# --
# Kernel/Modules/AgentLookup.pm - a generic lookup module
# Copyright (C) 2001-2005 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: AgentLookup.pm,v 1.6 2005/03/27 11:52:22 martin 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::Modules::AgentLookup;

use strict;

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

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

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

    # get common opjects
    foreach (keys %Param) {
        $Self->{$_} = $Param{$_};
    }

    # check all needed objects
    foreach (qw(TicketObject ParamObject DBObject LayoutObject ConfigObject LogObject)) {
        if (!$Self->{$_}) {
            $Self->{LayoutObject}->FatalError(Message => "Got no $_!");
        }
    }

    return $Self;
}
# --
sub Run {
    my $Self = shift;
    my %Param = @_;
    my $Output;
    # check needed params
    foreach (qw(What Source)) {
        $Param{$_} = $Self->{ParamObject}->GetParam(Param => $_);
        if (!$Param{$_}) {
            # error page
            return $Self->{LayoutObject}->ErrorScreen(
                Message => "Need Param '$_'!",
            );
        }
    }
    # max shown entries in a search list
    $Self->{Map} = $Self->{ConfigObject}->Get('DataLookup');
    if (!$Self->{Map}->{$Param{Source}}) {
        # error page
        return $Self->{LayoutObject}->ErrorScreen(
            Message => "Need '$Param{Source}' as DataLookup config option!",
        );
    }
    # config options
    $Self->{Limit} = $Self->{Map}->{$Param{Source}}->{ResultLimit} || 250;
    $Self->{Table} = $Self->{Map}->{$Param{Source}}->{Params}->{Table} || die "Need DataLookup->$Param{Source}->Params->Table in Kernel/Config.pm!";
    $Self->{KeyList} = $Self->{Map}->{$Param{Source}}->{KeyList} || die "Need DataLookup->$Param{Source}->KeyList in Kernel/Config.pm!";
    $Self->{ValueList} = $Self->{Map}->{$Param{Source}}->{ValueList} || die "Need DataLookup->$Param{Source}->ValueList in Kernel/Config.pm!";
    $Self->{SearchPrefix} = $Self->{Map}->{$Param{Source}}->{SearchPrefix};
    if (!defined($Self->{SearchPrefix})) {
        $Self->{SearchPrefix} = '';
    }
    $Self->{SearchSuffix} = $Self->{Map}->{$Param{Source}}->{SearchSuffix};
    if (!defined($Self->{SearchSuffix})) {
        $Self->{SearchSuffix} = '*';
    }
    # create new db connect if DSN is given
    if ($Self->{Map}->{$Param{Source}}->{Params}->{DSN}) {
        $Self->{DBObject} = Kernel::System::DB->new(
            LogObject => $Self->{LogObject},
            ConfigObject => $Self->{ConfigObject},
            DatabaseDSN => $Self->{Map}->{$Param{Source}}->{Params}->{DSN},
            DatabaseUser => $Self->{Map}->{$Param{Source}}->{Params}->{User},
            DatabasePw => $Self->{Map}->{$Param{Source}}->{Params}->{Password},
        ) || die $DBI::errstr;
        # remember that we have the DBObject not from parent call
        $Self->{NotParentDBObject} = 1;
    }
    # get data list
    my $Search = $Self->{ParamObject}->GetParam(Param => 'Search');
    my %Result = ();
    if ($Search) {
        my $SearchDB = $Self->{SearchPrefix}.$Search.$Self->{SearchSuffix};
        $SearchDB =~ s/\*/%/g;
        $SearchDB =~ s/%%/%/g;
        # build SQL string
        my $SQL = "SELECT ";
        my $What = '';
        foreach my $Entry (@{$Self->{KeyList}}) {
            if ($What) {
                $What .= ', ';
            }
            $What .= $Entry;
        }
        $SQL .= $What;
        $SQL .= " FROM $Self->{Table} WHERE ";
        my $Where = '';
        foreach my $Entry (@{$Self->{KeyList}}) {
            if ($Where) {
                $Where .= ' OR ';
            }
            $Where .= " $Entry LIKE '".$Self->{DBObject}->Quote($SearchDB)."'";
        }
        $SQL .= $Where;
        $Self->{DBObject}->Prepare(SQL => $SQL, Limit => 100);
        while (my @Row = $Self->{DBObject}->FetchrowArray()) {
            my $KeyValue = '';
            foreach (0..8) {
                if ($Row[$_]) {
                    $KeyValue .= $Row[$_].';';
                }
            }
            $Result{$KeyValue} = $KeyValue;
        }
    }
    # start with page ...
    $Output .= $Self->{LayoutObject}->Header(Area => 'Ticket', Title => 'Lookup', Type => 'Small');
    $Output .= $Self->_Mask(
        List => \%Result,
        Search => $Search,
        %Param,
    );
    $Output .= $Self->{LayoutObject}->Footer(Type => 'Small');
    return $Output;
}
# --
sub _Mask {
    my $Self = shift;
    my %Param = @_;
    # do html quoteing
    my %List = %{$Param{List}};
    foreach (sort {$List{$a} cmp $List{$b}} keys %List) {
        my $QuoteData = $Self->{LayoutObject}->Ascii2Html(Text => $List{$_});
        $Param{Table} .= '<tr><td><a href="" onclick="updateMessage(\''.$QuoteData.'\');">'.$QuoteData."</a></td></tr>";
    }
    # create & return output
    return $Self->{LayoutObject}->Output(TemplateFile => 'AgentLookup', Data => \%Param);
}
# --
sub DESTROY {
    my $Self = shift;
    # disconnect if it's not a parent DBObject
    if ($Self->{NotParentDBObject}) {
        $Self->{DBObject}->Disconnect();
    }
}
# --
1;