1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::Output::HTML::TicketMenu::TicketWatcher;
10
11use parent 'Kernel::Output::HTML::Base';
12
13use strict;
14use warnings;
15
16use Kernel::Language qw(Translatable);
17
18our @ObjectDependencies = (
19    'Kernel::System::Log',
20    'Kernel::Config',
21    'Kernel::System::Group',
22    'Kernel::System::Ticket',
23    'Kernel::Output::HTML::Layout',
24);
25
26sub Run {
27    my ( $Self, %Param ) = @_;
28
29    # check needed stuff
30    if ( !$Param{Ticket} ) {
31        $Kernel::OM->Get('Kernel::System::Log')->Log(
32            Priority => 'error',
33            Message  => 'Need Ticket!'
34        );
35        return;
36    }
37
38    # get config object
39    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
40
41    # check if feature is active
42    return if !$ConfigObject->Get('Ticket::Watcher');
43
44    # check if frontend module registered, if not, do not show action
45    if ( $Param{Config}->{Action} ) {
46        my $Module = $ConfigObject->Get('Frontend::Module')->{ $Param{Config}->{Action} };
47        return if !$Module;
48    }
49
50    # check acl
51    my %ACLLookup = reverse( %{ $Param{ACL} || {} } );
52    return if ( !$ACLLookup{ $Param{Config}->{Action} } );
53
54    # check access
55    my @Groups;
56    if ( $ConfigObject->Get('Ticket::WatcherGroup') ) {
57        @Groups = @{ $ConfigObject->Get('Ticket::WatcherGroup') };
58    }
59
60    my $Access = 1;
61    if (@Groups) {
62        $Access = 0;
63        my $GroupObject = $Kernel::OM->Get('Kernel::System::Group');
64        GROUP:
65        for my $Group (@Groups) {
66
67            my $HasPermission = $GroupObject->PermissionCheck(
68                UserID    => $Self->{UserID},
69                GroupName => $Group,
70                Type      => 'rw',
71            );
72            if ($HasPermission) {
73                $Access = 1;
74                last GROUP;
75            }
76        }
77    }
78    return if !$Access;
79
80    # check if ticket get's watched right now
81    my %Watch = $Kernel::OM->Get('Kernel::System::Ticket')->TicketWatchGet(
82        TicketID => $Param{Ticket}->{TicketID},
83    );
84
85    # show subscribe action
86    if ( $Watch{ $Self->{UserID} } ) {
87        return {
88            %{ $Param{Config} },
89            %{ $Param{Ticket} },
90            %Param,
91            Name        => Translatable('Unwatch'),
92            Description => Translatable('Remove from list of watched tickets'),
93            Link =>
94                'Action=AgentTicketWatcher;Subaction=Unsubscribe;TicketID=[% Data.TicketID | uri %];[% Env("ChallengeTokenParam") | html %]',
95        };
96    }
97
98    # show unsubscribe action
99    return {
100        %{ $Param{Config} },
101        %{ $Param{Ticket} },
102        %Param,
103        Name        => Translatable('Watch'),
104        Description => Translatable('Add to list of watched tickets'),
105        Link =>
106            'Action=AgentTicketWatcher;Subaction=Subscribe;TicketID=[% Data.TicketID | uri %];[% Env("ChallengeTokenParam") | html %]',
107    };
108}
109
1101;
111