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::Modules::AdminAppointmentImport;
10
11use strict;
12use warnings;
13
14use Kernel::Language qw(Translatable);
15use Kernel::System::VariableCheck qw(:all);
16
17our $ObjectManagerDisabled = 1;
18
19sub new {
20    my ( $Type, %Param ) = @_;
21
22    # allocate new hash for object
23    my $Self = {%Param};
24    bless( $Self, $Type );
25
26    return $Self;
27}
28
29sub Run {
30    my ( $Self, %Param ) = @_;
31
32    if ( $Self->{Subaction} eq 'Import' ) {
33
34        my $ParamObject = $Kernel::OM->Get('Kernel::System::Web::Request');
35
36        my $FormID = $ParamObject->GetParam( Param => 'FormID' ) || '';
37
38        my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
39
40        return $LayoutObject->FatalError() if !$FormID;
41
42        my %UploadStuff = $ParamObject->GetUploadAll(
43            Param => "FileUpload",
44        );
45
46        my $UploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
47
48        my $UpdateExisting = $ParamObject->GetParam( Param => 'UpdateExistingAppointments' ) || '';
49        my $CalendarID     = $ParamObject->GetParam( Param => 'CalendarID' )                 || '';
50
51        my %Errors;
52
53        if ( !$CalendarID ) {
54            $Errors{CalendarIDInvalid} = 'ServerError';
55        }
56
57        # save file in upload cache
58        if (%UploadStuff) {
59            my $Added = $UploadCacheObject->FormIDAddFile(
60                FormID => $FormID,
61                %UploadStuff,
62            );
63
64            # if file got not added to storage
65            # (e. g. because of 1 MB max_allowed_packet MySQL problem)
66            if ( !$Added ) {
67                return $LayoutObject->FatalError();
68            }
69        }
70
71        # get content from upload cache
72        else {
73
74            my @AttachmentData = $UploadCacheObject->FormIDGetAllFilesData(
75                FormID => $FormID,
76            );
77            if ( !@AttachmentData || ( $AttachmentData[0] && !%{ $AttachmentData[0] } ) ) {
78                $Errors{FileUploadInvalid} = 'ServerError';
79            }
80            else {
81                %UploadStuff = %{ $AttachmentData[0] };
82            }
83
84        }
85
86        # check if empty
87        if ( !$UploadStuff{Content} ) {
88            $Errors{FileUploadInvalid} = "ServerError";
89        }
90
91        if (%Errors) {
92            return $Self->_Overview(
93                %Errors,
94                CalendarID => $CalendarID,
95            );
96        }
97
98        my %Calendar;
99
100        my $CalendarObject = $Kernel::OM->Get('Kernel::System::Calendar');
101
102        if ($CalendarID) {
103            %Calendar = $CalendarObject->CalendarGet(
104                CalendarID => $CalendarID,
105                UserID     => $Self->{UserID},
106            );
107        }
108
109        # check calendar permissions
110        if ( !%Calendar ) {
111
112            # no permissions
113            return $LayoutObject->FatalError(
114                Message =>
115                    $LayoutObject->{LanguageObject}->Translate('No permissions'),
116            );
117        }
118
119        # permissions check
120        my $Permission = $CalendarObject->CalendarPermissionGet(
121            CalendarID => $Calendar{CalendarID},
122            UserID     => $Self->{UserID},
123        );
124
125        if ( $Permission ne 'create' && $Permission ne 'rw' ) {
126
127            # no permissions to import to the existing calendar
128            return $LayoutObject->FatalError(
129                Message =>
130                    $LayoutObject->{LanguageObject}->Translate('No permissions'),
131            );
132        }
133
134        my $Count = $Kernel::OM->Get('Kernel::System::Calendar::Import::ICal')->Import(
135            CalendarID     => $Calendar{CalendarID},
136            ICal           => $UploadStuff{Content},
137            UserID         => $Self->{UserID},
138            UpdateExisting => $UpdateExisting,
139        );
140
141        if ( !$Count ) {
142            return $LayoutObject->FatalError(
143                Message => Translatable('System was unable to import file!'),
144                Comment => Translatable('Please check the log for more information.'),
145            );
146        }
147
148        # Import ok
149        return $LayoutObject->Redirect(
150            OP => "Action=AdminAppointmentCalendarManage;ImportAppointmentsSuccess=1;Count=${Count};Name="
151                . $LayoutObject->LinkEncode( $Calendar{CalendarName} ),
152        );
153
154    }
155    else {
156        return $Self->_Overview();
157    }
158
159    return $Self->_Mask(%Param);
160}
161
162sub _Overview {
163    my ( $Self, %Param ) = @_;
164
165    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
166    my $GroupObject  = $Kernel::OM->Get('Kernel::System::Group');
167
168    # Get user's permissions to associated modules which are displayed as links.
169    for my $Module (qw(AdminAppointmentCalendarManage)) {
170        my $ModuleGroups = $Kernel::OM->Get('Kernel::Config')->Get('Frontend::Module')
171            ->{$Module}->{Group} // [];
172
173        if ( IsArrayRefWithData($ModuleGroups) ) {
174            MODULE_GROUP:
175            for my $ModuleGroup ( @{$ModuleGroups} ) {
176                my $HasPermission = $GroupObject->PermissionCheck(
177                    UserID    => $Self->{UserID},
178                    GroupName => $ModuleGroup,
179                    Type      => 'rw',
180                );
181                if ($HasPermission) {
182                    $Param{ModulePermissions}->{$Module} = 1;
183                    last MODULE_GROUP;
184                }
185            }
186        }
187
188        # Always allow links if no groups are specified.
189        else {
190            $Param{ModulePermissions}->{$Module} = 1;
191        }
192    }
193
194    $Param{Title} = $LayoutObject->{LanguageObject}->Translate("Import Appointments");
195    $Param{CalendarIDInvalid} //= '';
196
197    my @CalendarList = $Kernel::OM->Get('Kernel::System::Calendar')->CalendarList(
198        UserID     => $Self->{UserID},
199        Permission => 'create',
200        ValidID    => 1,
201    );
202
203    my @CalendarData = map {
204        {
205            Key   => $_->{CalendarID},
206            Value => $_->{CalendarName},
207        }
208    } sort { $a->{CalendarName} cmp $b->{CalendarName} } @CalendarList;
209
210    $Param{Calendar} = $LayoutObject->BuildSelection(
211        Data         => \@CalendarData,
212        Name         => 'CalendarID',
213        ID           => 'CalendarID',
214        Class        => 'Modernize Validate_Required ' . $Param{CalendarIDInvalid},
215        PossibleNone => 1,
216        Title        => $LayoutObject->{LanguageObject}->Translate("Calendar"),
217        SelectedID   => $Param{CalendarID} || '',
218    );
219
220    # get FormID from params
221    $Param{FormID} = $Kernel::OM->Get('Kernel::System::Web::Request')->GetParam( Param => 'FormID' ) || '';
222
223    # generate FormID if empty
224    $Param{FormID} = $Kernel::OM->Get('Kernel::System::Web::UploadCache')->FormIDCreate() if !$Param{FormID};
225
226    return $Self->_Mask(%Param);
227}
228
229sub _Mask {
230    my ( $Self, %Param ) = @_;
231
232    my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
233
234    # output page
235    my $Output = $LayoutObject->Header();
236    $Output .= $LayoutObject->NavigationBar();
237    $Output .= $LayoutObject->Output(
238        TemplateFile => 'AdminAppointmentImport',
239        Data         => {
240            %Param,
241        },
242    );
243    $Output .= $LayoutObject->Footer();
244    return $Output;
245}
246
2471;
248