-
Notifications
You must be signed in to change notification settings - Fork 2
/
booking_timeslots.install
368 lines (347 loc) · 10.2 KB
/
booking_timeslots.install
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
<?php
/**
* @file
* Drupal installation file.
*/
/**
* Implements hook_schema().
*/
function booking_timeslots_schema() {
$schema['booking'] = array (
'description' => 'The main store for our entity',
'fields' => array(
'pid' => array(
'description' => 'Primary key for our table',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'cid' => array(
'description' => 'ID of the {booking_slot_config} table record',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'uid' => array(
'description' => 'ID of the user booking the slot',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'primary_id' => array(
'description' => 'ID of the 1st content type id related with the booking',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'secondary_id' => array(
'description' => 'ID of the 2nd content type id related with the booking',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'tertiary_id' => array(
'description' => 'ID of the 3rd content type id related with the booking',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'quaternary_id' => array(
'description' => 'ID of the 4th content type id related with the booking',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'created' => array(
'description' => 'The Unix timestamp when the booking was created.',
'type' => 'int',
'not null' => TRUE,
),
'slot_time' => array(
'description' => 'Slot date and time',
// @see: https://www.drupal.org/node/866340, https://www.drupal.org/node/2095623
'type' => 'datetime:normal',
'pgsql_type' => 'timestamp',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
'hour_start' => array(
'description' => 'Starting hour',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'min_start' => array(
'description' => 'Starting minute',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'duration' => array(
'description' => 'Duration',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'players' => array(
'description' => 'Number of players',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'total' => array(
'description' => 'Total',
'type' => 'float',
'not null' => TRUE,
'default' => 0
),
'deposit' => array(
'description' => 'Deposit',
'type' => 'float',
'not null' => TRUE,
'default' => 0
),
'tax' => array(
'description' => 'Tax',
'type' => 'float',
'not null' => TRUE,
'default' => 0
),
'notes' => array(
'description' => 'Customer notes',
'type' => 'varchar',
'length' => 255,
'default' => ''
),
),
'primary key' => array('pid'),
'indexes' => array(
'index_cid' => array('cid'),
'index_created' => array('created'),
)
);
$schema['booking_slot_config'] = array (
'description' => 'Configuration of booking slots per venue/facility',
'fields' => array( // id, nid, day_of_week, hour_start, min_start, hour_end, min_end, price, slot_length, capacity, closed
'cid' => array(
'description' => 'Primary key',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'vid' => array(
'description' => 'Venue ID',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'fid' => array(
'description' => 'Facility ID',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'day_of_week' => array(
'description' => 'Day of the week',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'hour_start' => array(
'description' => 'Starting hour',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'min_start' => array(
'description' => 'Starting minute',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'hour_end' => array(
'description' => 'Ending hour',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'min_end' => array(
'description' => 'Ending minute',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'price' => array(
'description' => 'Price in JSON structure',
'type' => 'varchar',
'length' => 1024,
'default' => '{}',
'not null' => TRUE,
),
'duration' => array(
'description' => 'Duration',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'capacity' => array(
'description' => 'Capacity',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
'closed' => array(
'description' => "If set, bookings won't be allowed",
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0
),
),
'primary key' => array('cid'),
'unique keys' => array(
'unique_vid_fid' => array('vid', 'fid')
),
'indexes' => array(
'index_closed' => array('closed'),
),
);
return $schema;
}
/**
* Implementation of hook_install().
*/
function booking_timeslots_install() {
$schema['opening_hours'] = drupal_get_schema('opening_hours');
booking_timeslots_schema_alter($schema);
db_add_field('opening_hours', 'capacity', $schema['opening_hours']['fields']['capacity']);
db_add_field('opening_hours', 'slot_length', $schema['opening_hours']['fields']['slot_length']);
db_change_field('opening_hours', 'notice', 'notice', $schema['opening_hours']['fields']['notice']);
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
break;
case 'pgsql':
db_query("
CREATE OR REPLACE FUNCTION DateDiff (units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
RETURNS INT AS $$
DECLARE
diff_interval INTERVAL;
diff INT = 0;
years_diff INT = 0;
BEGIN
IF units IN ('yy', 'yyyy', 'year', 'mm', 'm', 'month') THEN
years_diff = DATE_PART('year', end_t) - DATE_PART('year', start_t);
IF units IN ('yy', 'yyyy', 'year') THEN
-- SQL Server does not count full years passed (only difference between year parts)
RETURN years_diff;
ELSE
-- If end month is less than start month it will subtracted
RETURN years_diff * 12 + (DATE_PART('month', end_t) - DATE_PART('month', start_t));
END IF;
END IF;
-- Minus operator returns interval 'DDD days HH:MI:SS'
diff_interval = end_t - start_t;
diff = diff + DATE_PART('day', diff_interval);
IF units IN ('wk', 'ww', 'week') THEN
diff = diff/7;
RETURN diff;
END IF;
IF units IN ('dd', 'd', 'day') THEN
RETURN diff;
END IF;
diff = diff * 24 + DATE_PART('hour', diff_interval);
IF units IN ('hh', 'hour') THEN
RETURN diff;
END IF;
diff = diff * 60 + DATE_PART('minute', diff_interval);
IF units IN ('mi', 'n', 'minute') THEN
RETURN diff;
END IF;
diff = diff * 60 + DATE_PART('second', diff_interval);
RETURN diff;
END;
$$ LANGUAGE plpgsql;
");
break;
}
}
/**
* Implementation of hook_uninstall().
*/
function booking_timeslots_uninstall() {
$ret = array();
$schema['opening_hours'] = drupal_get_schema('opening_hours');
booking_timeslots_schema_alter($schema);
db_drop_field('opening_hours', 'capacity');
db_drop_field('opening_hours', 'slot_length');
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
break;
case 'pgsql':
db_query("DROP FUNCTION DateDiff(units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP);");
break;
}
}
/**
* Implementation of hook_schema_alter().
*/
function booking_timeslots_schema_alter(&$schema) {
$schema['opening_hours']['fields']['notice']['length'] = 4095;
$schema['opening_hours']['fields']['capacity'] = array(
'type' => 'int',
'size' => 'small',
'length' => 5,
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
);
$schema['opening_hours']['fields']['slot_length'] = array(
'type' => 'int',
'size' => 'small',
'length' => 5,
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
);
}
/**
* @implements hook_features_revert().
*/
function booking_timeslots_modules_enabled($modules) {
if (module_exists('booking_timeslots')) {
$calendar_view_name = 'bt_schedule';
$example_schedule_content_type_name = variable_get('booking_timeslots_view_' . $calendar_view_name);
booking_timeslots_choose_schedule_content_type($example_schedule_content_type_name);
}
}