This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
booking.php
550 lines (503 loc) · 16.5 KB
/
booking.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
require_once 'booking.civix.php';
/**
* Implementation of hook_civicrm_tabs()
*
* Display a booking tab listing booking belong to that contact.
*/
function booking_civicrm_tabs(&$tabs, $cid) {
$count = CRM_Booking_BAO_Booking::getBookingContactCount($cid); //TODO Count number of booking and show on the tab
$tab = array(
'id' => 'booking',
'count' => $count,
'title' => 'Bookings',
'weight' => 0, //we are at first tab
);
$tab['url'] = CRM_Utils_System::url('civicrm/contact/view/booking', "reset=1&cid={$cid}&snippet=1&force=1", false, null, false);
$tabs[] = $tab;
}
/**
* Implementation of hook_civicrm_config
*/
function booking_civicrm_config(&$config) {
// enable use of number_format php function in smarty templates when
// security on(on by default for emails)
$smarty = CRM_Core_Smarty::singleton();
$smarty->security_settings['MODIFIER_FUNCS'][] = "number_format";
_booking_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function booking_civicrm_xmlMenu(&$files) {
_booking_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function booking_civicrm_install() {
require_once 'CRM/Utils/Migrate/Import.php';
$import = new CRM_Utils_Migrate_Import( );
$extRoot = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
$op = $extRoot . 'xml' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'OptionGroups.xml';
$import->run( $op );
return _booking_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function booking_civicrm_uninstall() {
return _booking_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function booking_civicrm_enable() {
// rebuild the menu so our path is picked up
require_once 'CRM/Core/Invoke.php';
CRM_Core_Invoke::rebuildMenuAndCaches( );
return _booking_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function booking_civicrm_disable() {
return _booking_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function booking_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _booking_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function booking_civicrm_managed(&$entities) {
return _booking_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_queryObjects
*/
function booking_civicrm_queryObjects(&$queryObjects, $type) {
if ($type == 'Contact') {
$queryObjects[] = new CRM_Booking_BAO_Query();
}
elseif ($type == 'Report') {}
}
/**
* Implementation of hook_civicrm_postProcess
*/
function booking_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
if($objectName == 'Contribution'){
if($op == 'delete'){
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_booking_payment WHERE contribution_id = $objectId");
}
}
}
/**
* Implementation of hook_civicrm_entityTypes
*/
function booking_civicrm_entityTypes(&$entityTypes) {
$entityTypes[] = array(
'name' => 'AdhocCharges',
'class' => 'CRM_Booking_DAO_AdhocCharges',
'table' => 'civicrm_booking_adhoc_charges',
);
$entityTypes[] = array(
'name' => 'AdhocChargesItem',
'class' => 'CRM_Booking_DAO_AdhocChargesItem',
'table' => 'civicrm_booking_adhoc_charges_item',
);
$entityTypes[] = array(
'name' => 'Booking',
'class' => 'CRM_Booking_DAO_Booking',
'table' => 'civicrm_booking',
);
$entityTypes[] = array(
'name' => 'BookingPayment',
'class' => 'CRM_Booking_DAO_Payment',
'table' => 'civicrm_booking_payment',
);
$entityTypes[] = array(
'name' => 'Resource',
'class' => 'CRM_Booking_DAO_Resource',
'table' => 'civicrm_booking_resource',
);
$entityTypes[] = array(
'name' => 'ResourceConfigOption',
'class' => 'CRM_Booking_DAO_ResourceConfigOption',
'table' => 'civicrm_booking_resource_config_option',
);
$entityTypes[] = array(
'name' => 'ResourceConfigSet',
'class' => 'CRM_Booking_DAO_ResourceConfigSet',
'table' => 'civicrm_booking_resource_config_set',
);
$entityTypes[] = array(
'name' => 'Slot',
'class' => 'CRM_Booking_DAO_Slot',
'table' => 'civicrm_booking_slot',
);
$entityTypes[] = array(
'name' => 'SubSlot',
'class' => 'CRM_Booking_DAO_SubSlot',
'table' => 'civicrm_booking_sub_slot',
);
$entityTypes[] = array(
'name' => 'Cancellation',
'class' => 'CRM_Booking_DAO_Cancellation',
'table' => 'civicrm_booking_cancellation'
);
}
/**
* Implementation of hook_civicrm_merge
*/
function booking_civicrm_merge ( $type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL ){
if (!empty($mainId) && !empty($otherId) && $type == 'sqls'){
$query1 = "
UPDATE civicrm_booking
SET primary_contact_id=$mainId
WHERE primary_contact_id=$otherId;
";
$query2 = "
UPDATE civicrm_booking
SET secondary_contact_id=$mainId
WHERE secondary_contact_id=$otherId;
";
require_once('CRM/Core/DAO.php');
$dao = CRM_Core_DAO::executeQuery( $query1 );
$dao = CRM_Core_DAO::executeQuery( $query2 );
}
}
/**
* Add navigation for booking under "Administer" menu
*
* @param $params associated array of navigation menus
*/
function booking_civicrm_navigationMenu( &$params ) {
$result = civicrm_api3('OptionGroup', 'getsingle', array('name' => CRM_Booking_Utils_Constants::OPTION_BOOKING_STATUS));
if($result['id']){
$bookingStatusGid = $result['id'];
}
$result = civicrm_api3('OptionGroup', 'getsingle', array('name' => CRM_Booking_Utils_Constants::OPTION_RESOURCE_TYPE));
if($result['id']){
$resourceTypeGid = $result['id'];
}
$result = civicrm_api3('OptionGroup', 'getsingle', array('name' => CRM_Booking_Utils_Constants::OPTION_RESOURCE_LOCATION));
if($result['id']){
$resourceLocationGId = $result['id'];
}
$result = civicrm_api3('OptionGroup', 'getsingle', array('name' => CRM_Booking_Utils_Constants::OPTION_RESOURCE_CRITERIA));
if($result['id']){
$resourceCriteriaGId = $result['id'];
}
$result = civicrm_api3('OptionGroup', 'getsingle', array('name' => CRM_Booking_Utils_Constants::OPTION_SIZE_UNIT));
if($result['id']){
$sizeUnitGid = $result['id'];
}
$result = civicrm_api3('OptionGroup', 'getsingle', array('name' => CRM_Booking_Utils_Constants::OPTION_CANCELLATION_CHARGES));
if($result['id']){
$cancellationChargesGid = $result['id'];
}
// get the id of Administer Menu
$administerMenuId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Navigation', 'Administer', 'id', 'name');
// skip adding menu if there is no administer menu
if ($administerMenuId) {
// get the maximum key under administer menu
$maxAdminMenuKey = civibooking_getMenuKeyMax($params);
$nextAdminMenuKey = $maxAdminMenuKey+1;
$key = $nextAdminMenuKey;
$params[$administerMenuId]['child'][$nextAdminMenuKey] = array(
'attributes' => array(
'label' => ts('CiviBooking'),
'name' => 'admin_booking',
'url' => '',
'permission' => 'administer CiviBooking',
'operator' => null,
'separator' => 1,
'parentID' => $administerMenuId,
'navID' => $nextAdminMenuKey,
'active' => 1
),
'child' => array(
$key++ => array(
'attributes' => array(
'label' => ts('Resource Configuration Set'),
'name' => 'resource_config_set',
'url' => 'civicrm/admin/resource/config_set?reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 2,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Manage Resources'),
'name' => 'manage_resources',
'url' => 'civicrm/admin/resource?reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 2,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Additional Charges Item'),
'name' => 'adhoc_charges_item',
'url' => 'civicrm/admin/adhoc_charges_item?reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 2,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Booking Status'),
'name' => 'booking_status',
'url' => 'civicrm/admin/options?gid=' . $bookingStatusGid .'&reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 3,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Resource Type'),
'name' => 'resource_type',
'url' => 'civicrm/admin/options?gid=' . $resourceTypeGid .'&reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 4,
'active' => 1
),
'child' => null
),
/*$key++ => array(
'attributes' => array(
'label' => ts('Resource Criteria'),
'name' => 'resource_criteria',
'url' => 'civicrm/admin/options?gid=' . $resourceCriteriaGId .'&reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 5,
'active' => 1
),
'child' => null
),*/
$key++ => array(
'attributes' => array(
'label' => ts('Size Unit'),
'name' => 'size_unit',
'url' =>'civicrm/admin/options?gid=' . $sizeUnitGid .'&reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 5,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Cancellation Charges'),
'name' => 'cancellation_charges',
'url' =>'civicrm/admin/options?gid=' . $cancellationChargesGid .'&reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 6,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Booking Component Settings'),
'name' => 'booking_component_settings',
'url' =>'civicrm/admin/setting/preferences/booking?reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 7,
'active' => 1
),
'child' => null
),
$key++ => array(
'attributes' => array(
'label' => ts('Resource Location'),
'name' => 'resource_location',
'url' => 'civicrm/admin/options?gid=' . $resourceLocationGId .'&reset=1',
'permission' => null,
'operator' => null,
'separator' => 0,
'parentID' => $nextAdminMenuKey,
'navID' => 8,
'active' => 1
),
'child' => null
),
),
);
}
$maxKey = ( max( array_keys($params) ) );
$findBooking = array(
'attributes' => array(
'label' => ts('Find Bookings'),
'name' => 'find_booking',
'url' => 'civicrm/booking/search?reset=1',
'permission' => 'administer CiviBooking,create and update bookings,view all bookings',
'operator' => null,
'separator' => 0,
'parentID' => null,
'navID' => 3,
'active' => 1
),
'child' => null
);
$bookingMenu = array(
'attributes' => array(
'label' => ts('Booking'),
'name' => 'booking',
'url' => null,
'permission' => 'administer CiviBooking,create and update bookings,view all bookings',
'operator' => null,
'separator' => null,
'parentID' => null,
'navID' => null,
'active' => 1
),
'child' => array(
$key++ => array(
'attributes' => array(
'label' => ts('New Booking'),
'name' => 'new_booking',
'url' => 'civicrm/booking/add?reset=1',
'permission' => 'administer CiviBooking,create and update bookings',
'operator' => null,
'separator' => 0,
'parentID' => null,
'navID' => 2 ,
'active' => 1
),
'child' => null
),
$key++ => $findBooking,
$key++ => array(
'attributes' => array(
'label' => ts('Day View'),
'name' => 'day_view',
'url' => 'civicrm/booking/day-view?reset=1',
'permission' => 'administer CiviBooking,create and update bookings,view all bookings',
'operator' => null,
'separator' => 0,
'parentID' => null,
'navID' => 2 ,
'active' => 1
)
),
)
);
array_push($params, $bookingMenu);
}
function civibooking_getMenuKeyMax($menuArray) {
$max = array(max(array_keys($menuArray)));
foreach($menuArray as $v) {
if (!empty($v['child'])) {
$max[] = civibooking_getMenuKeyMax($v['child']);
}
}
return max($max);
}
function booking_civicrm_permission(&$permissions){
$prefix = ts('CiviBooking') . ': ';
$permissions['administer CiviBooking'] = $prefix . ts('administer CiviBooking');
$permissions['create and update bookings'] = $prefix . ts('create and update bookings');
$permissions['view all bookings'] = $prefix . ts('view all bookings');
}
/*
* Implements hook_civicrm_alterAPIPermissions
* @see function _civicrm_api3_permissions for mentioned uppercase issue
*/
function booking_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
$commonBookingAPIPermissions = array(
'create' => array(
'administer CiviBooking',
),
'delete' => array(
'administer CiviBooking',
),
'get' => array(
array(
'administer CiviBooking',
'create and update bookings',
'view all bookings',
)
),
'update' => array(
'administer CiviBooking',
),
);
$bookingEntities = array(
'BookingPayment',
'Booking',
'Cancellation',
'Slot',
'SubSlot'
);
$configEntities = array(
'AdhocChargesItem',
'AdhocCharges',
'ResourceConfigOption',
'ResourceConfigSet',
'Resource',
);
// set common permissions
foreach (array_merge($bookingEntities, $configEntities) as $entityName) {
// permissions implementation needs lowercase entities
$permissions[_civicrm_api_get_entity_name_from_camel($entityName)] = $commonBookingAPIPermissions;
}
//add custom permissions for create/update role
foreach ($bookingEntities as $entityName) {
$permissionArray = array(array('administer CiviBooking', 'create and update bookings'));
// permissions implementation needs lowercase entities
$entityName = _civicrm_api_get_entity_name_from_camel($entityName);
$permissions[$entityName]['create'] = $permissionArray;
$permissions[$entityName]['update'] = $permissionArray;
}
}