forked from salleman33/reservation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
206 lines (180 loc) · 6.87 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
<?php
/**
* Install hook
*
* @return boolean
*/
function plugin_reservation_install() {
global $DB, $CFG_GLPI;
if ($DB->tableExists("glpi_plugin_reservation_manageresa")) { //UPDATE plugin < 2.0.0
$query = "ALTER TABLE `glpi_plugin_reservation_manageresa`
CHANGE `resaid` `reservations_id` int(11) NOT NULL,
DROP COLUMN `matid`,
CHANGE `date_return` `effectivedate` datetime,
CHANGE `date_theorique` `baselinedate` datetime NOT NULL,
DROP COLUMN `itemtype`,
CHANGE `dernierMail` `mailingdate` datetime";
$DB->queryOrDie($query, $DB->error());
$query = "RENAME TABLE `glpi_plugin_reservation_manageresa` TO `glpi_plugin_reservation_reservations`";
$DB->queryOrDie($query, $DB->error());
}
if (!$DB->tableExists("glpi_plugin_reservation_reservations")) { //INSTALL >= 2.0.0
$query = "CREATE TABLE `glpi_plugin_reservation_reservations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reservations_id` int(11) NOT NULL,
`baselinedate` datetime NOT NULL,
`effectivedate` datetime,
`mailingdate` datetime,
PRIMARY KEY (`id`),
KEY `reservations_id` (`reservations_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$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(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`value` VARCHAR(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`name`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_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());
}
if ($DB->tableExists("glpi_plugin_reservation_config")) { //UPDATE plugin < 2.0.0
$query = "ALTER TABLE `glpi_plugin_reservation_config`
MODIFY `name` VARCHAR(20) NOT NULL,
MODIFY `value` VARCHAR(20) NOT NULL";
$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());
$query = "UPDATE `glpi_plugin_reservation_configs`
SET `value` = (
SELECT `value`
FROM `glpi_plugin_reservation_config`
WHERE `name` = \"methode\"
)
WHERE `name` = \"mode_auto\"";
$DB->queryOrDie($query, $DB->error());
$query = "DROP TABLE `glpi_plugin_reservation_config`";
$DB->queryOrDie($query, $DB->error());
$query = "DROP TABLE `glpi_plugin_reservation_configdayforauto`";
$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]);
}
return true;
}
/**
* Uninstall hook
*
* @return boolean
*/
function plugin_reservation_uninstall() {
global $DB;
$tables = ["glpi_plugin_reservation_reservations", "glpi_plugin_reservation_configs"];
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);
}
/**
* 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"]
]
);
}