forked from Combodo/combodo-sla-computation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.combodo-sla-computation.php
196 lines (179 loc) · 6.45 KB
/
module.combodo-sla-computation.php
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
<?php
// Copyright (C) 2010-2017 Combodo SARL
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
SetupWebPage::AddModule(
__FILE__, // Path to the current file, all other file names are relative to the directory containing this file
'combodo-sla-computation/2.4.3',
array(
// Identification
//
'label' => 'Enhanced SLA Computation',
'category' => 'sla',
// Setup
//
'dependencies' => array(
'itop-sla-computation/1.0.0',
'itop-service-mgmt/2.4.0||itop-service-mgmt-provider/2.4.0', // Needed to place new menu entries
),
'mandatory' => true,
'visible' => false,
'installer' => 'CoverageWindowInstaller',
// Components
//
'datamodel' => array(
'model.combodo-sla-computation.php',
'main.combodo-sla-computation.php'
),
'webservice' => array(
),
'data.struct' => array(
// add your 'structure' definition XML files here,
),
'data.sample' => array(
// add your sample data XML files here,
),
// Documentation
//
'doc.manual_setup' => '', // hyperlink to manual setup documentation, if any
'doc.more_information' => '', // hyperlink to more information, if any
// Default settings
//
'settings' => array(
'coverage_oql' => 'SELECT CoverageWindow', // How to retrive the Coverage object for a given ticket (:this)
'holidays_oql' => 'SELECT Holiday', // How to retrive the list of Holidays for a given ticket (:this)
),
)
);
if (!class_exists('CoverageWindowInstaller'))
{
// Module installation handler
//
class CoverageWindowInstaller extends ModuleInstallerAPI
{
public static function BeforeWritingConfig(Config $oConfiguration)
{
// If you want to override/force some configuration values, do it here
return $oConfiguration;
}
/**
* Handler called before creating or upgrading the database schema
* @param $oConfiguration Config The new configuration of the application
* @param $sPreviousVersion string PRevious version number of the module (empty string in case of first install)
* @param $sCurrentVersion string Current version number of the module
*/
public static function BeforeDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
{
// If you want to migrate data from one format to another, do it here
}
/**
* Handler called after the creation/update of the database schema
* @param $oConfiguration Config The new configuration of the application
* @param $sPreviousVersion string Previous version number of the module (empty string in case of first install)
* @param $sCurrentVersion string Current version number of the module
*/
public static function AfterDatabaseCreation(Config $oConfiguration, $sPreviousVersion, $sCurrentVersion)
{
if ($sPreviousVersion != '')
{
// Convert the previous format where all data were stored as fields: monday_start, monday_end, tuesday_start...
// directly inside the CoverageWindow class, to the new format where the open hours are stored as "CoverageWindowInterval" objects
// Check if the "old" column "start_monday" exists. If so, then the data needs to be migrated
$sTableName = MetaModel::DBGetTable('CoverageWindow');
$aFields = CMDBSource::QueryToArray("SHOW COLUMNS FROM `$sTableName`");
// Note: without backticks, you get an error with some table names (e.g. "group")
$bOldColumns = false;
foreach ($aFields as $aFieldData)
{
if ($aFieldData["Field"] == 'monday_start')
{
$bOldColumns = true;
break;
}
}
if ($bOldColumns)
{
$aCoverageWindows = CMDBSource::QueryToArray("SELECT * FROM `$sTableName`");
$iCount = 0;
foreach($aCoverageWindows as $aCW)
{
$iId = $aCW['id'];
foreach(array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday') as $sWeekday)
{
if ($sWeekday == 'wednesday')
{
$sStartTime = $aCW['wendnesday_start']; // Arghhh!!!
}
else
{
$sStartTime = $aCW[$sWeekday.'_start'];
}
$sEndTime = $aCW[$sWeekday.'_end'];
if ($sStartTime != $sEndTime)
{
// Non-empty interval
$oInterval = new CoverageWindowInterval();
$oInterval->Set('coverage_window_id', $iId);
$oInterval->Set('weekday', $sWeekday);
$oInterval->Set('start_time', self::FromDecimalIfNeeded($sStartTime));
$oInterval->Set('end_time', self::FromDecimalIfNeeded($sEndTime));
$oInterval->DBInsert();
}
}
$iCount++;
}
SetupLog::Info("Conversion of open hours intervals: $iCount CoverageWindow instance(s) successfully processed.");
// Be careful 'wendnesday_start !!!
$sCleanup = "ALTER TABLE `$sTableName` DROP `monday_start`, DROP `monday_end`, DROP `tuesday_start`, DROP `tuesday_end`, DROP `wendnesday_start`, DROP `wednesday_end`, ";
$sCleanup .= "DROP `thursday_start`, DROP `thursday_end`, DROP `friday_start`, DROP `friday_end`, DROP `saturday_start`, DROP `saturday_end`, DROP `sunday_start`, DROP `sunday_end`";
CMDBSource::Query($sCleanup);
SetupLog::Info("CoverageWindow: cleanup of old columns: done.");
}
}
}
/**
* Convert (if needed) from the decimal format: e.g. 8.75 => 08:45
* Properly formatted strings (hh:mm) are not modified.
* @param string $sValue
* @return string
*/
protected static function FromDecimalIfNeeded($sValue)
{
if (!preg_match('/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$|^24:00$/', $sValue))
{
// The format does not match the new convention
// => Convert the decimal value into "hh:mm"
$fTime = (float) $sValue;
if ($sValue != '')
{
$iHour = floor($fTime);
$iMin = floor(60 * ($fTime - $iHour));
if ($iHour > 23)
{
$sValue = '24:00';
}
else
{
$sValue = sprintf('%02d:%02d', $iHour, $iMin);
}
}
else
{
$sValue = '00:00';
}
}
return $sValue;
}
}
}