Maintenance.pm
4.24 KB
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
# --
# Kernel/System/Maintenanca.pm - maintenance instance
#
# Copyright (C) 2014-2018 - SeTIC - UFSC - http://setic.ufsc.br/
# Version 2015-08-01 - Adjustments for OTRS 4
# Version 2018-01-05 - Adjustments for OTRS 6
#
# 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::System::Maintenance;
use strict;
use warnings;
use Data::Dumper;
our @ObjectDependencies = (
"Kernel::System::DB",
"Kernel::Output::HTML::Layout", # OLD Layout object
"Kernel::System::Maintenance",
"Kernel::System::DateUtils",
"Kernel::System::Log",
"Kernel::Config"
);
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {%Param};
bless( $Self, $Type );
return $Self;
}
=item CreateMaintenance()
Creates a maintenance
$ocurrenceObject->CreateMaintenance(
Description => string
StartDate => string (format yyyy-mm-dd hh:nn:ss)
ScheduledEndDate => string (format yyyy-mm-dd hh:nn:ss)
UserID => integer
);
=cut
sub CreateMaintenance {
my ( $Self, %Param ) = @_;
for (qw(Description StartDate ScheduledEndDate UserID)) {
if ( !$Param{$_} ) {
$Kernel::OM->Get("Kernel::Output::HTML::Layout")->FatalError( Message => Dumper( \%Param ) . "got no $_!" );
}
}
my $status = $Self->SCHEDULED_STATUS();
$Kernel::OM->Get("Kernel::System::DB")->Do(
SQL => "insert into maintenance (description, start_date, scheduled_end_date, user_id, status) values (?, ?, ?, ?, ?)",
Bind => [ \$Param{Description}, \$Param{StartDate}, \$Param{ScheduledEndDate}, \$Param{UserID}, \$status ]
);
}
sub ListMaintenances {
my ( $Self, %Param ) = @_;
my @retorno = ();
my $dateUtils = $Kernel::OM->Get("Kernel::System::DateUtils");
my $type = $Param{Type} || "current_scheduled";
my $sql = "SELECT id,description,start_date,scheduled_end_date,COALESCE(status,0),end_date,CURRENT_TIMESTAMP FROM maintenance WHERE ORDER BY start_date DESC, id asc";
my $where = "";
if ($Param{Type} eq "current") {
$where = "WHERE COALESCE(status,0) = " . $Self->SCHEDULED_STATUS() . " AND start_date <= CURRENT_TIMESTAMP";
} elsif ($Param{Type} eq "all") {
$where = "";
} elsif ($Param{Type} eq "scheduled") {
$where = "WHERE COALESCE(status,0) = " . $Self->SCHEDULED_STATUS() . " AND start_date > CURRENT_TIMESTAMP";
} elsif ($Param{Type} eq "current_scheduled") {
$where = "WHERE COALESCE(status,0) = " . $Self->SCHEDULED_STATUS();
} elsif ($Param{Type} eq "canceled") {
$where = "WHERE COALESCE(status,0) = " . $Self->CANCELED_STATUS();
}
$sql =~ s/WHERE/$where/;
$Kernel::OM->Get("Kernel::System::DB")->Prepare(
SQL => $sql,
Bind => []
);
while ( my @row = $Kernel::OM->Get("Kernel::System::DB")->FetchrowArray() ) {
my %ret = ();
$ret{'ID'} = $row[0];
$ret{'Description'} = $row[1];
$ret{'StartDate'} = $row[2];
$ret{'ScheduledEndDate'} = $row[3];
$ret{'Status'} = $row[4];
$ret{'EndDate'} = $row[5];
my $startDate = $dateUtils->FromSQL(StringDate => $row[2]);
my $currentDate = $dateUtils->FromSQL(StringDate => $row[6]);
$ret{'Active'} = ($row[4] == $Self->SCHEDULED_STATUS()) && ($startDate < $currentDate);
push @retorno, \%ret;
}
return @retorno;
}
sub EndMaintenance {
my ( $Self, %Param ) = @_;
$Kernel::OM->Get("Kernel::System::DB")->Prepare(
SQL => "UPDATE maintenance SET end_date=CURRENT_TIMESTAMP,status=1 WHERE id=?",
Bind => [ \$Param{ID} ]
);
}
sub CancelMaintenance {
my ( $Self, %Param ) = @_;
$Kernel::OM->Get("Kernel::System::DB")->Prepare(
SQL => "UPDATE maintenance SET status=" . $Self->CANCELED_STATUS() . " WHERE id=?",
Bind => [ \$Param{ID} ]
);
}
sub GetStatusID {
my ( $Self, %Param ) = @_;
my %retorno = ();
$Kernel::OM->Get("Kernel::System::DB")->Prepare(
SQL => "SELECT count(id), max(id) FROM maintenance where start_date < CURRENT_TIMESTAMP and COALESCE(end_date,0) = 0 and status <> 2",
Bind => []
);
my @row = $Kernel::OM->Get("Kernel::System::DB")->FetchrowArray();
if ($row[0] != 0) {
return ( $row[0], $row[1] );
}
else {
return ( 0, 0 );
}
}
sub SCHEDULED_STATUS {
return 0;
}
sub FINISHED_STATUS {
return 1;
}
sub CANCELED_STATUS {
return 2;
}
1;