File: NotificationEvent.pm

package info (click to toggle)
otrs2 5.0.16-1%2Bdeb9u6
  • links: PTS
  • area: non-free
  • in suites: stretch
  • size: 141,108 kB
  • sloc: perl: 746,356; xml: 54,469; sql: 10,505; sh: 430; makefile: 64
file content (297 lines) | stat: -rw-r--r-- 9,604 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
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
# --
# Copyright (C) 2001-2017 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::Output::HTML::Preferences::NotificationEvent;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);
use Kernel::Language qw(Translatable);

our $ObjectManagerDisabled = 1;

sub new {
    my ( $Type, %Param ) = @_;

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

    # get all notification transports
    my $TransportConfigRaw = $Kernel::OM->Get('Kernel::Config')->Get('Notification::Transport') || {};

    # get main object
    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    # skip all non executable transports
    TRANSPORT:
    for my $TransportName ( sort keys %{$TransportConfigRaw} ) {

        # discard non available transports
        my $TransportLoaded = $MainObject->Require(
            $TransportConfigRaw->{$TransportName}->{Module},
            Silent => 1,
        );

        # skip transport
        next TRANSPORT if !$TransportLoaded;

        $Self->{TransportConfig}->{$TransportName} = $TransportConfigRaw->{$TransportName};
    }

    # get NotificationEvent object
    my $NotificationEventObject = $Kernel::OM->Get('Kernel::System::NotificationEvent');

    # get all notifications
    my %NotificationListRaw = $NotificationEventObject->NotificationList();

    # get valid object
    my $ValidObject = $Kernel::OM->Get('Kernel::System::Valid');

    NOTIFICATION:
    for my $NotificationID ( sort keys %NotificationListRaw ) {

        # get notification details
        my %Notification = $NotificationEventObject->NotificationGet(
            ID => $NotificationID,
        );

        # discard invalid notifications
        my @ValidIDs = $ValidObject->ValidIDsGet();
        next NOTIFICATION if !grep { $Notification{ValidID} == $_ } @ValidIDs;

        # discard all non customizable notifications
        next NOTIFICATION if !$Notification{Data}->{VisibleForAgent};
        next NOTIFICATION if !$Notification{Data}->{VisibleForAgent}->[0];

        # remember notification data
        $Self->{NotificationList}->{$NotificationID} = \%Notification;
    }

    return $Self;
}

sub Param {
    my ( $Self, %Param ) = @_;

    my @Params;

    # get layout object
    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');

    # print table header
    for my $Name (
        sort { $Self->{TransportConfig}->{$a}->{Prio} <=> $Self->{TransportConfig}->{$b}->{Prio} }
        keys %{ $Self->{TransportConfig} }
        )
    {

        my $TransportConfig = $Self->{TransportConfig}->{$Name};

        $LayoutObject->Block(
            Name => 'HeaderRow',
            Data => {
                TransportName => $TransportConfig->{Name},
                TransportIcon => $TransportConfig->{Icon},
            },
        );
    }

    # output table body
    if ( !IsHashRefWithData( $Self->{NotificationList} ) ) {
        $LayoutObject->Block(
            Name => 'NoDataFoundMsg',
            Data => {
                ColSpan => ( scalar keys %{ $Self->{TransportConfig} } ) + 1,
            },
        );
    }
    else {

        # get the user preferences (this needs to be done only once)
        my $UserNotificationTransport = $Kernel::OM->Get('Kernel::System::JSON')->Decode(
            Data => $Param{UserData}->{NotificationTransport},
        );

        NOTIFICATION:
        for my $NotificationID (
            sort { $Self->{NotificationList}->{$a}->{Name} cmp $Self->{NotificationList}->{$b}->{Name} }
            keys %{ $Self->{NotificationList} }
            )
        {

            next NOTIFICATION if !$Self->{NotificationList}->{$NotificationID};

            my $Notification = $Self->{NotificationList}->{$NotificationID};

            # fill notification column block
            $LayoutObject->Block(
                Name => 'BodyRow',
                Data => {
                    NotificationName  => $Notification->{Name},
                    NotificationTitle => $Notification->{Data}->{VisibleForAgentTooltip}->[0] || '',
                    VisibleForAgent   => $Notification->{Data}->{VisibleForAgent}->[0],
                },
            );

            TRANSPORT:
            for my $TransportName (
                sort { $Self->{TransportConfig}->{$a}->{Prio} <=> $Self->{TransportConfig}->{$b}->{Prio} }
                keys %{ $Self->{TransportConfig} }
                )
            {

                my $Identifier = "Notification-$NotificationID-$TransportName";

                my $TransportEnabled = grep { $_ eq $TransportName } @{ $Notification->{Data}->{Transports} };

                my $AgentEnabledByDefault
                    = grep { $_ eq $TransportName } @{ $Notification->{Data}->{AgentEnabledByDefault} };

                # get user preference for this transport and notification or fall back to
                #   notification default if there is no user preference
                my $Use = $UserNotificationTransport->{$Identifier} // $AgentEnabledByDefault;

                my $Checked     = 'checked="checked"';
                my $HiddenValue = 1;
                if ( !$Use ) {
                    $Checked     = '';
                    $HiddenValue = 0;
                }

                # fill each transport column
                $LayoutObject->Block(
                    Name => 'BodyTransportColumn',
                    Data => {},
                );
                if ($TransportEnabled) {
                    $LayoutObject->Block(
                        Name => 'BodyTransportColumnEnabled',
                        Data => {
                            NotificationName => $Notification->{Name},
                            TransportName    => $TransportName,
                            Identifier       => $Identifier,
                            Checked          => $Checked,
                            HiddenValue      => $HiddenValue,
                        },
                    );
                }
            }
        }
    }

    # generate HTML
    my $Output = $LayoutObject->Output(
        TemplateFile => 'PreferencesNotificationEvent',
        Data         => \%Param,
    );

    # generate return structure for generic "Option" block in AgentPreferences
    push(
        @Params,
        {
            %Param,
            Block => 'RawHTML',
            HTML  => $Output,
            Name  => 'NotificationEventTransport',
        },
    );

    return @Params;
}

sub Run {
    my ( $Self, %Param ) = @_;

    my @IdentifierList;
    my @MandatoryNotificationIDs;

    NOTIFICATION:
    for my $NotificationID ( sort keys %{ $Self->{NotificationList} } ) {

        next NOTIFICATION if !$Self->{NotificationList}->{$NotificationID};

        my $Notification          = $Self->{NotificationList}->{$NotificationID};
        my $NotificationMandatory = 0;
        if ( $Notification->{Data}->{VisibleForAgent} && $Notification->{Data}->{VisibleForAgent}->[0] == 2 ) {
            $NotificationMandatory = 1;
            push @MandatoryNotificationIDs, $NotificationID;
        }

        TRANSPORT:
        for my $TransportName ( sort keys %{ $Self->{TransportConfig} } ) {
            next TRANSPORT if !grep { $_ eq $TransportName } @{ $Notification->{Data}->{Transports} };
            push @IdentifierList, {
                Name           => "Notification-$NotificationID-$TransportName",
                NotificationID => $NotificationID,
                IsMandatory    => $NotificationMandatory,
            };
        }
    }

    # get needed objects
    my $ParamObject  = $Kernel::OM->Get('Kernel::System::Web::Request');
    my $UserObject   = $Kernel::OM->Get('Kernel::System::User');
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    my %UserNotificationTransport;
    my %MandatoryFulfilled;
    for my $Identifier (@IdentifierList) {
        $UserNotificationTransport{ $Identifier->{Name} } = $ParamObject->GetParam( Param => $Identifier->{Name} ) || 0;

        # check if this is a mandatory notification and this transport is selected
        if ( $UserNotificationTransport{ $Identifier->{Name} } == 1 ) {
            $MandatoryFulfilled{ $Identifier->{NotificationID} } = 1;
        }
    }

    # get layout object
    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');

    # now check if there are notifications for which no transport has been selected
    for my $NotificationID (@MandatoryNotificationIDs) {
        if ( $MandatoryFulfilled{$NotificationID} != 1 ) {
            $Self->{Error} = Translatable(
                "Please make sure you've chosen at least one transport method for mandatory notifications."
            );
            return;
        }
    }

    my $Value = $Kernel::OM->Get('Kernel::System::JSON')->Encode(
        Data => \%UserNotificationTransport,
    );

    # update user preferences
    if ( !$ConfigObject->Get('DemoSystem') ) {
        $UserObject->SetPreferences(
            UserID => $Param{UserData}->{UserID},
            Key    => 'NotificationTransport',
            Value  => $Value // '',
        );
    }

    $Self->{Message} = Translatable('Preferences updated successfully!');

    return 1;
}

sub Error {
    my ( $Self, %Param ) = @_;

    return $Self->{Error} || '';
}

sub Message {
    my ( $Self, %Param ) = @_;

    return $Self->{Message} || '';
}

1;