File: AppointmentCalendar.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (519 lines) | stat: -rw-r--r-- 16,248 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
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# 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 https://www.gnu.org/licenses/gpl-3.0.txt.
# --

package Kernel::Output::HTML::Dashboard::AppointmentCalendar;

use strict;
use warnings;

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

our $ObjectManagerDisabled = 1;

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

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

    # get needed parameters
    for my $Needed (qw(Config Name UserID)) {
        die "Got no $Needed!" if ( !$Self->{$Needed} );
    }

    # get param object
    my $ParamObject = $Kernel::OM->Get('Kernel::System::Web::Request');

    # get current filter
    my $Name           = $ParamObject->GetParam( Param => 'Name' ) || '';
    my $PreferencesKey = 'DashboardCalendarAppointmentFilter' . $Self->{Name};
    if ( $Self->{Name} eq $Name ) {
        $Self->{Filter} = $ParamObject->GetParam( Param => 'Filter' ) || '';
    }

    # get config object
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # remember filter
    if ( $Self->{Filter} ) {

        # update session
        $Kernel::OM->Get('Kernel::System::AuthSession')->UpdateSessionID(
            SessionID => $Self->{SessionID},
            Key       => $PreferencesKey,
            Value     => $Self->{Filter},
        );

        # update preferences
        if ( !$ConfigObject->Get('DemoSystem') ) {
            $Kernel::OM->Get('Kernel::System::User')->SetPreferences(
                UserID => $Self->{UserID},
                Key    => $PreferencesKey,
                Value  => $Self->{Filter},
            );
        }
    }

    # set default filter if not set yet
    if ( !$Self->{Filter} ) {
        $Self->{Filter} = $Self->{$PreferencesKey} || $Self->{Config}->{Filter} || 'Today';
    }

    # setup the prefrences keys
    $Self->{PrefKeyShown}   = 'AppointmentDashboardPref' . $Self->{Name} . '-Shown';
    $Self->{PrefKeyRefresh} = 'AppointmentDashboardPref' . $Self->{Name} . '-Refresh';

    $Self->{PageShown} = $Kernel::OM->Get('Kernel::Output::HTML::Layout')->{ $Self->{PrefKeyShown} }
        || $Self->{Config}->{Limit} || 10;

    $Self->{PageRefresh} = $Kernel::OM->Get('Kernel::Output::HTML::Layout')->{ $Self->{PrefKeyRefresh} }
        || 1;

    $Self->{StartHit} = int( $ParamObject->GetParam( Param => 'StartHit' ) || 1 );

    $Self->{CacheKey} = $Self->{Name} . '::';

    # get configuration for the full name order for user names
    # and append it to the cache key to make sure, that the
    # correct data will be displayed every time
    my $FirstnameLastNameOrder = $ConfigObject->Get('FirstnameLastnameOrder') || 0;
    $Self->{CacheKey} .= '::' . $FirstnameLastNameOrder;

    return $Self;
}

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

    my @Params = (
        {
            Desc  => Translatable('Shown'),
            Name  => $Self->{PrefKeyShown},
            Block => 'Option',
            Data  => {
                5  => ' 5',
                10 => '10',
                15 => '15',
                20 => '20',
                25 => '25',
            },
            SelectedID  => $Self->{PageShown},
            Translation => 0,
        },
        {
            Desc  => Translatable('Refresh (minutes)'),
            Name  => $Self->{PrefKeyRefresh},
            Block => 'Option',
            Data  => {
                0  => Translatable('off'),
                1  => '1',
                2  => '2',
                5  => '5',
                7  => '7',
                10 => '10',
                15 => '15',
            },
            SelectedID  => $Self->{PageRefresh},
            Translation => 1,
        },
    );

    return @Params;
}

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

    return (
        %{ $Self->{Config} },

        CanRefresh => 1,

        # remember, do not allow to use page cache
        # (it's not working because of internal filter)
        CacheKey => undef,
        CacheTTL => undef,
    );
}

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

    # get config settings
    my $IdleMinutes = $Self->{Config}->{IdleMinutes} || 60;
    my $SortBy      = $Self->{Config}->{SortBy}      || 'UserFullname';

    # get a list of at least readable calendars
    my @CalendarList = $Kernel::OM->Get('Kernel::System::Calendar')->CalendarList(
        UserID  => $Self->{UserID},
        ValidID => 1,
    );

    # collect calendar and appointment data
    # separate appointments to today, tomorrow
    # and the next five days (soon)
    my %Calendars;
    my %Appointments;
    my %AppointmentsCount;

    # check cache
    my $CacheKeyCalendars         = $Self->{CacheKey} . $Self->{UserID} . '::Calendars';
    my $CacheKeyAppointments      = $Self->{CacheKey} . $Self->{UserID} . '::Appointments::' . $Self->{Filter};
    my $CacheKeyAppointmentsCount = $Self->{CacheKey} . $Self->{UserID} . '::AppointmentsCount';

    my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');

    # get cached data
    my $DataCalendars = $CacheObject->Get(
        Type => 'Dashboard',
        Key  => $CacheKeyCalendars,
    );
    my $DataAppointments = $CacheObject->Get(
        Type => 'Dashboard',
        Key  => $CacheKeyAppointments,
    );
    my $DataAppointmentsCount = $CacheObject->Get(
        Type => 'Dashboard',
        Key  => $CacheKeyAppointmentsCount,
    );

    # if cache is up-to-date, use the given data
    if (
        ref $DataCalendars eq 'HASH'
        && ref $DataAppointments eq 'HASH'
        && ref $DataAppointmentsCount eq 'HASH'
        )
    {
        %Calendars         = %{$DataCalendars};
        %Appointments      = %{$DataAppointments};
        %AppointmentsCount = %{$DataAppointmentsCount};
    }

    # collect the data again if necessary
    else {

        CALENDAR:
        for my $Calendar (@CalendarList) {

            next CALENDAR if !$Calendar;
            next CALENDAR if !IsHashRefWithData($Calendar);
            next CALENDAR if $Calendars{ $Calendar->{CalendarID} };

            $Calendars{ $Calendar->{CalendarID} } = $Calendar;
        }

        # set cache for calendars
        $CacheObject->Set(
            Type  => 'Dashboard',
            Key   => $CacheKeyCalendars,
            Value => \%Calendars,
            TTL   => $Self->{Config}->{CacheTTLLocal} * 60,
        );

        # prepare calendar appointments
        my %AppointmentsUnsorted;

        # get a local appointment object
        my $AppointmentObject = $Kernel::OM->Get('Kernel::System::Calendar::Appointment');

        my $DateTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');

        CALENDARID:
        for my $CalendarID ( sort keys %Calendars ) {

            next CALENDARID if !$CalendarID;

            my @Appointments = $AppointmentObject->AppointmentList(
                CalendarID => $CalendarID,
                StartTime  => $DateTimeObject->ToString(),
                Result     => 'HASH',
            );

            next CALENDARID if !IsArrayRefWithData( \@Appointments );

            APPOINTMENT:
            for my $Appointment (@Appointments) {

                next APPOINTMENT if !$Appointment;
                next APPOINTMENT if !IsHashRefWithData($Appointment);

                # Save system time of StartTime.
                my $StartTimeObject = $Kernel::OM->Create(
                    'Kernel::System::DateTime',
                    ObjectParams => {
                        String => $Appointment->{StartTime},
                    },
                );
                $Appointment->{SystemTimeStart} = $StartTimeObject->ToEpoch();

                # save appointment in new hash for later sorting
                $AppointmentsUnsorted{ $Appointment->{AppointmentID} } = $Appointment;
            }
        }

        # get datetime strings for the dates with related offsets
        # (today, tomorrow and soon - which means the next 5 days
        # except today and tomorrow counted from current timestamp)
        my %DateOffset = (
            Today     => 0,
            Tomorrow  => 86400,
            PlusTwo   => 172800,
            PlusThree => 259200,
            PlusFour  => 345600,
        );

        my %Dates;

        my $CurrentSystemTime = $DateTimeObject->ToEpoch();

        for my $DateOffsetKey ( sort keys %DateOffset ) {

            # Get date components with current offset.
            my $OffsetTimeObject = $Kernel::OM->Create(
                'Kernel::System::DateTime',
                ObjectParams => {
                    Epoch => $CurrentSystemTime + $DateOffset{$DateOffsetKey},
                },
            );
            my $OffsetTimeSettings = $OffsetTimeObject->Get();

            $Dates{$DateOffsetKey} = sprintf(
                "%02d-%02d-%02d",
                $OffsetTimeSettings->{Year},
                $OffsetTimeSettings->{Month},
                $OffsetTimeSettings->{Day}
            );
        }

        $AppointmentsCount{Today}    = 0;
        $AppointmentsCount{Tomorrow} = 0;
        $AppointmentsCount{Soon}     = 0;

        APPOINTMENTID:
        for my $AppointmentID ( sort keys %AppointmentsUnsorted ) {

            next APPOINTMENTID if !$AppointmentID;
            next APPOINTMENTID if !IsHashRefWithData( $AppointmentsUnsorted{$AppointmentID} );
            next APPOINTMENTID if !$AppointmentsUnsorted{$AppointmentID}->{StartTime};

            # Extract current date (without time).
            my $StartTimeObject = $Kernel::OM->Create(
                'Kernel::System::DateTime',
                ObjectParams => {
                    String => $AppointmentsUnsorted{$AppointmentID}->{StartTime},
                },
            );
            my $StartTimeSettings = $StartTimeObject->Get();

            my $StartDate = sprintf(
                "%02d-%02d-%02d",
                $StartTimeSettings->{Year},
                $StartTimeSettings->{Month},
                $StartTimeSettings->{Day}
            );

            # today
            if ( $StartDate eq $Dates{Today} ) {

                $AppointmentsCount{Today}++;

                if ( $Self->{Filter} eq 'Today' ) {
                    $Appointments{$AppointmentID} = $AppointmentsUnsorted{$AppointmentID};
                }
            }

            # tomorrow
            elsif ( $StartDate eq $Dates{Tomorrow} ) {

                $AppointmentsCount{Tomorrow}++;

                if ( $Self->{Filter} eq 'Tomorrow' ) {
                    $Appointments{$AppointmentID} = $AppointmentsUnsorted{$AppointmentID};
                }
            }

            # soon
            elsif (
                $StartDate eq $Dates{PlusTwo}
                || $StartDate eq $Dates{PlusThree}
                || $StartDate eq $Dates{PlusFour}
                )
            {
                $AppointmentsCount{Soon}++;

                if ( $Self->{Filter} eq 'Soon' ) {
                    $Appointments{$AppointmentID} = $AppointmentsUnsorted{$AppointmentID};
                }
            }
        }

        # set cache for appointments
        $CacheObject->Set(
            Type  => 'Dashboard',
            Key   => $CacheKeyAppointments,
            Value => \%Appointments,
            TTL   => $Self->{Config}->{CacheTTLLocal} * 60,
        );

        # set cache for appointments count
        $CacheObject->Set(
            Type  => 'Dashboard',
            Key   => $CacheKeyAppointmentsCount,
            Value => \%AppointmentsCount,
            TTL   => $Self->{Config}->{CacheTTLLocal} * 60,
        );
    }

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

    my $AppointmentTableBlock = 'ContentSmallTable';

    # prepare appointments table
    $LayoutObject->Block(
        Name => 'ContentSmallTable',
    );

    my $Count = 0;
    my $Shown = 0;

    APPOINTMENTID:
    for my $AppointmentID (
        sort {
            $Appointments{$a}->{SystemTimeStart} <=> $Appointments{$b}->{SystemTimeStart}
                || $Appointments{$a}->{AppointmentID} <=> $Appointments{$b}->{AppointmentID}
        } keys %Appointments
        )
    {
        # pagination handling
        last APPOINTMENTID if $Shown >= $Self->{PageShown};

        if ( ( $Self->{StartHit} - 1 ) > $Count ) {
            $Count++;
            next APPOINTMENTID;
        }

        my $StartTimeObject = $Kernel::OM->Create(
            'Kernel::System::DateTime',
            ObjectParams => {
                String => $Appointments{$AppointmentID}->{StartTime},
            },
        );

        # Convert time to user time zone.
        if ( $Self->{UserTimeZone} ) {
            $StartTimeObject->ToTimeZone(
                TimeZone => $Self->{UserTimeZone},
            );
        }

        my $StartTimeSettings = $StartTimeObject->Get();

        # prepare dates and times
        my $StartTime     = sprintf( "%02d:%02d", $StartTimeSettings->{Hour}, $StartTimeSettings->{Minute} );
        my $StartTimeLong = $LayoutObject->{LanguageObject}
            ->FormatTimeString( $Appointments{$AppointmentID}->{StartTime}, 'DateFormatLong' );

        $LayoutObject->Block(
            Name => 'ContentSmallAppointmentRow',
            Data => {
                AppointmentID => $Appointments{$AppointmentID}->{AppointmentID},
                Title         => $Appointments{$AppointmentID}->{Title},
                StartTime     => $StartTime,
                StartTimeLong => $StartTimeLong,
                Color         => $Calendars{ $Appointments{$AppointmentID}->{CalendarID} }->{Color},
                CalendarName  => $Calendars{ $Appointments{$AppointmentID}->{CalendarID} }->{CalendarName},
            },
        );

        # increase shown item count
        $Shown++;
    }

    if ( !IsHashRefWithData( \%Appointments ) ) {

        # show up message for no appointments
        $LayoutObject->Block(
            Name => 'ContentSmallAppointmentNone',
        );
    }

    # set css class
    my %Summary;
    $Summary{ $Self->{Filter} . '::Selected' } = 'Selected';

    # filter bar
    $LayoutObject->Block(
        Name => 'ContentSmallAppointmentFilter',
        Data => {
            %{ $Self->{Config} },
            %Summary,
            Name          => $Self->{Name},
            TodayCount    => $AppointmentsCount{Today},
            TomorrowCount => $AppointmentsCount{Tomorrow},
            SoonCount     => $AppointmentsCount{Soon},
        },
    );

    # add page nav bar
    my $Total    = $AppointmentsCount{ $Self->{Filter} } || 0;
    my $LinkPage = 'Subaction=Element;Name=' . $Self->{Name} . ';Filter=' . $Self->{Filter} . ';';
    my %PageNav  = $LayoutObject->PageNavBar(
        StartHit    => $Self->{StartHit},
        PageShown   => $Self->{PageShown},
        AllHits     => $Total || 1,
        Action      => 'Action=' . $LayoutObject->{Action},
        Link        => $LinkPage,
        WindowSize  => 5,
        AJAXReplace => 'Dashboard' . $Self->{Name},
        IDPrefix    => 'Dashboard' . $Self->{Name},
        AJAX        => $Param{AJAX},
    );

    $LayoutObject->Block(
        Name => 'ContentSmallAppointmentFilterNavBar',
        Data => {
            %{ $Self->{Config} },
            Name => $Self->{Name},
            %PageNav,
        },
    );

    # check for refresh time
    my $Refresh = $LayoutObject->{ $Self->{PrefKeyRefresh} } // 1;

    my $NameHTML = $Self->{Name};
    $NameHTML =~ s{-}{_}xmsg;

    my $Content = $LayoutObject->Output(
        TemplateFile => 'AgentDashboardAppointmentCalendar',
        Data         => {
            %{ $Self->{Config} },
            Name        => $Self->{Name},
            NameHTML    => $NameHTML,
            RefreshTime => $Refresh * 60,
        },
        AJAX => $Param{AJAX},
    );

    # Send data to JS.
    $LayoutObject->AddJSData(
        Key   => 'AppointmentCalendar',
        Value => {
            Name        => $Self->{Name},
            NameHTML    => $NameHTML,
            RefreshTime => $Refresh * 60,
        },
    );

    return $Content;
}

1;