-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhook.php
252 lines (222 loc) · 8.43 KB
/
hook.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
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
<?php
/**
* Install hook
*
* @return boolean
*/
function plugin_reservation_install()
{
global $DB, $CFG_GLPI;
$version = plugin_version_reservation();
$migration = new Migration($version['version']);
if (!$DB->tableExists("glpi_plugin_reservation_categories")) {
$query = "CREATE TABLE `glpi_plugin_reservation_categories` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$DB->queryOrDie($query, $DB->error());
}
if (!$DB->tableExists("glpi_plugin_reservation_categories_items")) {
$query = "CREATE TABLE `glpi_plugin_reservation_categories_items` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`categories_id` int UNSIGNED NOT NULL,
`reservationitems_id` int UNSIGNED NOT NULL,
`priority` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `reservationitems_id` (`reservationitems_id`),
KEY `categories_id` (`categories_id`),
UNIQUE (`categories_id`, `reservationitems_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$DB->queryOrDie($query, $DB->error());
}
if ($DB->tableExists('glpi_plugin_reservation_reservations')) { // UPDATE 2.2.0
$migration->addField(
'glpi_plugin_reservation_reservations',
'checkindate',
'datetime',
['null' => true]
);
}
if (!$DB->tableExists("glpi_plugin_reservation_reservations")) { //INSTALL >= 2.0.0
$query = "CREATE TABLE `glpi_plugin_reservation_reservations` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`reservations_id` int UNSIGNED NOT NULL,
`baselinedate` timestamp NOT NULL,
`effectivedate` timestamp NULL,
`mailingdate` timestamp NULL,
`checkindate` timestamp NULL,
PRIMARY KEY (`id`),
KEY `reservations_id` (`reservations_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$DB->queryOrDie($query, $DB->error());
}
// 2.2.0
// mettre une date de checkin pour toutes les reservations precedentes à la date actuelle !
// $query = "UPDATE glpi_plugin_reservation_reservations
// SET checkindate = NOW()
// WHERE `begin` <= NOW()
// AND effectivedate is null
// AND checkindate is null
// ";
// $DB->queryOrDie($query, $DB->error());
// add existing reservations if necessary
$query = "SELECT *
FROM glpi_reservations
WHERE `end` >= NOW()
AND glpi_reservations.id NOT IN
(
SELECT reservations_id
FROM glpi_plugin_reservation_reservations
)";
$reservation = new Reservation();
foreach ($DB->request($query) as $data) {
$reservation->getFromDb($data['id']);
plugin_item_add_reservation($reservation);
}
if (!$DB->tableExists("glpi_plugin_reservation_configs")) { //INSTALL >= 2.0.0
$query = "CREATE TABLE `glpi_plugin_reservation_configs` (
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`value` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$DB->queryOrDie($query, $DB->error());
$query = "INSERT INTO `glpi_plugin_reservation_configs` (`name` , `value`)
VALUES (\"mode_auto\",0),
(\"conflict_action\",\"delete\")";
$DB->queryOrDie($query, $DB->error());
}
$cron = new CronTask();
if ($cron->getFromDBbyName('PluginReservationTask', 'SurveilleResa')) { // plugin < 2.0.0
CronTask::unregister("Reservation");
}
if (!$cron->getFromDBbyName('PluginReservationTask', 'checkReservations')) {
CronTask::Register(
'PluginReservationTask',
'checkReservations',
$CFG_GLPI['time_step'] * MINUTE_TIMESTAMP,
['param' => 24, 'mode' => 2, 'logs_lifetime' => 10]
);
}
if (!$cron->getFromDBbyName('PluginReservationTask', 'sendMailLateReservations')) {
CronTask::Register(
'PluginReservationTask',
'sendMailLateReservations',
DAY_TIMESTAMP,
['hourmin' => 23, 'hourmax' => 24, 'mode' => 2, 'logs_lifetime' => 30, 'state' => 0]
);
}
// update event to new event type to preserve previous behaviour
$query = "UPDATE `glpi_notifications`
SET
`event` = \"plugin_reservation_conflict_new_user\"
WHERE
`event` = \"plugin_reservation_conflit\"
OR
`event` = \"plugin_reservation_conflict\"";
$DB->queryOrDie($query, $DB->error());
//execute the whole migration
$migration->executeMigration();
return true;
}
/**
* Uninstall hook
*
* @return boolean
*/
function plugin_reservation_uninstall()
{
global $DB;
$tables = ["glpi_plugin_reservation_reservations", "glpi_plugin_reservation_configs", "glpi_plugin_reservation_categories", "glpi_plugin_reservation_categories_items"];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`");
}
CronTask::unregister("Reservation");
return true;
}
/**
* hook : add Plugin reservation when a GLPI reservation is added
*
* @return void
*/
function plugin_item_add_reservation($reservation)
{
global $DB;
$DB->insertOrDie('glpi_plugin_reservation_reservations', [
'reservations_id' => $reservation->fields['id'],
'baselinedate' => $reservation->fields['end'],
]);
Toolbox::logInFile('reservations_plugin', "plugin_item_add_reservation : " . json_encode($reservation) . "\n", $force = false);
$config = new PluginReservationConfig();
if ($config->getConfigurationValue("checkin", 0) == 1 && $config->getConfigurationValue("auto_checkin", 0) == 1) {
$time = time();
$until_auto_checkin = $config->getConfigurationValue("auto_checkin_time", 1) * MINUTE_TIMESTAMP;
$until = date("Y-m-d H:i:s", $time + $until_auto_checkin);
if ($reservation->fields['begin'] <= $until) {
Toolbox::logInFile('reservations_plugin', "auto-checkin enable : reservation " . json_encode($reservation->fields['id']) . " is checkin automatically\n", $force = false);
PluginReservationReservation::checkinReservation($reservation->fields['id']);
}
}
}
/**
* hook : update plugin reservation when a GLPI reservation is updated
*
* @return void
*/
function plugin_item_update_reservation($reservation)
{
global $DB;
$end = $reservation->fields['end'];
$query = 'SELECT `effectivedate`
FROM glpi_plugin_reservation_reservations
WHERE `reservations_id` = ' . $reservation->fields['id'];
// maybe the reservation is over
$resume = false;
foreach ($DB->request($query) as $data) {
if ($end >= $data['effectivedate']) {
$resume = true;
}
}
if ($resume) {
$DB->updateOrDie(
'glpi_plugin_reservation_reservations',
[
'baselinedate' => $end,
'effectivedate' => 'NULL',
],
[
'reservations_id' => $reservation->fields["id"],
]
);
} else {
$DB->updateOrDie(
'glpi_plugin_reservation_reservations',
[
'baselinedate' => $end,
],
[
'reservations_id' => $reservation->fields["id"],
]
);
}
Toolbox::logInFile('reservations_plugin', "plugin_item_update_reservation : " . json_encode($reservation) . "\n", $force = false);
}
/**
* hook : delete Plugin reservation when a GLPI reservation is delete
*
* @return void
*/
function plugin_item_purge_reservation($reservation)
{
global $DB;
Toolbox::logInFile('reservations_plugin', "plugin_item_purge_reservation : " . json_encode($reservation) . "\n", $force = false);
$DB->delete(
'glpi_plugin_reservation_reservations',
[
'reservations_id' => $reservation->fields["id"],
]
);
}