-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_booking.module
682 lines (615 loc) · 19.8 KB
/
resource_booking.module
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<?php
/**
* Implements hook_views_api().
*/
function resource_booking_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'resource_booking') . '/views',
);
}
/**
* Implements hook_entity_info().
*/
function resource_booking_entity_info() {
$entities = array();
$entities['booking'] = array(
'label' => t('Booking'),
'entity class' => 'Booking',
'controller class' => 'BookingController',
'base table' => 'booking',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'bid',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'load hook' => 'booking_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'resource_booking',
'access callback' => 'booking_access',
);
$entities['booking_type'] = array(
'label' => t('Booking type'),
'entity class' => 'BookingType',
'controller class' => 'BookingTypeController',
'base table' => 'booking_type',
'fieldable' => FALSE,
'bundle of' => 'booking',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'btid',
'name' => 'type',
'label' => 'label',
),
'module' => 'resource_booking',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/booking-types',
'file' => 'includes/booking.admin.inc',
'controller class' => 'BookingTypeUIController',
),
'access callback' => 'booking_type_access',
);
$entities['resource'] = array(
'label' => t('Resource'),
'entity class' => 'Resource',
'controller class' => 'ResourceController',
'base table' => 'resource',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'rid',
'bundle' => 'type',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'load hook' => 'resource_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'resource_booking',
'access callback' => 'resource_access',
);
$entities['resource_type'] = array(
'label' => t('Resource type'),
'entity class' => 'ResourceType',
'controller class' => 'ResourceTypeController',
'base table' => 'resource_type',
'fieldable' => FALSE,
'bundle of' => 'resource',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'rtid',
'name' => 'type',
'label' => 'label',
),
'module' => 'resource_booking',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/structure/resource-types',
'file' => 'includes/resource.admin.inc',
'controller class' => 'ResourceTypeUIController',
),
'access callback' => 'resource_type_access',
);
return $entities;
}
/**
* Implements hook_entity_info_alter().
*/
function resource_booking_entity_info_alter(&$entity_info) {
foreach (booking_type_get_types() as $type => $info) {
$entity_info['booking']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/booking-types/manage/%booking_type',
'real path' => 'admin/structure/booking-types/manage/' . $type,
'bundle argument' => 4,
),
);
}
foreach (resource_type_get_types() as $type => $info) {
$entity_info['resource']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/resource-types/manage/%resource_type',
'real path' => 'admin/structure/resource-types/manage/' . $type,
'bundle argument' => 4,
),
);
}
}
/**
* Implements hook_menu().
*/
function resource_booking_menu() {
$items = array();
// Booking
$items['booking/add'] = array(
'title' => 'Add booking',
'page callback' => 'booking_admin_add_page',
'access arguments' => array('administer booking types'),
'file' => 'includes/booking.admin.inc',
);
foreach (booking_type_get_types() as $type => $info) {
$items['booking/add/' . $type] = array(
'title' => 'Add booking',
'page callback' => 'booking_add',
'page arguments' => array(2),
'access callback' => 'entity_access',
'access arguments' => array('create', 'booking', $type),
'file' => 'includes/booking.admin.inc',
);
}
$booking_uri = 'booking/%booking';
$booking_uri_argument_position = 1;
$items[$booking_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('booking', $booking_uri_argument_position),
'page callback' => 'booking_view',
'page arguments' => array($booking_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('view', 'booking', $booking_uri_argument_position),
'file' => 'includes/booking.pages.inc',
);
$items[$booking_uri . '/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$booking_uri . '/delete'] = array(
'title' => 'Delete booking',
'title callback' => 'booking_label',
'title arguments' => array($booking_uri_argument_position),
'page callback' => 'drupal_get_form',
'page arguments' => array('booking_delete_form', $booking_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'booking', $booking_uri_argument_position),
'file' => 'includes/booking.admin.inc',
);
$items[$booking_uri . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('booking_form', $booking_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'booking', $booking_uri_argument_position),
'file' => 'includes/booking.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/structure/booking-types/%booking_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('booking_type_form_delete_confirm', 4),
'access arguments' => array('administer booking types'),
'weight' => 1,
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/booking.admin.inc',
);
// Resource
$items['resource/add'] = array(
'title' => 'Add resource',
'page callback' => 'resource_admin_add_page',
'access arguments' => array('administer resource types'),
'file' => 'includes/resource.admin.inc',
);
foreach (resource_type_get_types() as $type => $info) {
$items['resource/add/' . $type] = array(
'title' => 'Add resource',
'page callback' => 'resource_add',
'page arguments' => array(2),
'access callback' => 'entity_access',
'access arguments' => array('create', 'resource', $type),
'file' => 'includes/resource.admin.inc',
);
}
$resource_uri = 'resource/%booking';
$resource_uri_argument_position = 1;
$items[$resource_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('resource', $resource_uri_argument_position),
'page callback' => 'resource_view',
'page arguments' => array($resource_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('view', 'resource', $resource_uri_argument_position),
'file' => 'includes/resource.pages.inc',
);
$items[$resource_uri . '/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$resource_uri . '/delete'] = array(
'title' => 'Delete resource',
'title callback' => 'resource_label',
'title arguments' => array($resource_uri_argument_position),
'page callback' => 'drupal_get_form',
'page arguments' => array('resource_delete_form', $resource_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'resource', $resource_uri_argument_position),
'file' => 'includes/resource.admin.inc',
);
$items[$resource_uri . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('resource_form', $resource_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'resource', $resource_uri_argument_position),
'file' => 'includes/resource.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/structure/resource-types/%resource_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('resource_type_form_delete_confirm', 4),
'access arguments' => array('administer booking types'),
'weight' => 1,
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/resource.admin.inc',
);
return $items;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function resource_booking_menu_local_tasks_alter(&$data, $router_item, $root_path) {
switch ($root_path) {
case 'booking':
// Add action link to 'booking/add' on 'booking' page.
$item = menu_get_item('booking/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
break;
case 'resource':
// Add action link to 'resource/add' on 'resource' page.
$item = menu_get_item('resource/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
break;
}
}
/**
* Implements hook_admin_paths().
*/
function resource_booking_admin_paths() {
if (variable_get('node_admin_theme')) {
$paths = array(
'booking/*/edit' => TRUE,
'booking/*/delete' => TRUE,
'booking/add' => TRUE,
'booking/add/*' => TRUE,
'resource/*/edit' => TRUE,
'resource/*/delete' => TRUE,
'resource/add' => TRUE,
'resource/add/*' => TRUE,
);
return $paths;
}
}
/**
* Implements hook_permission().
*/
function resource_booking_permission() {
$permissions = array(
'administer booking types' => array(
'title' => t('Administer booking types'),
'description' => t('Allows users to configure booking types and their fields.'),
'restrict access' => TRUE,
),
'create booking entities' => array(
'title' => t('Create bookings'),
'description' => t('Allows users to create bookings.'),
'restrict access' => TRUE,
),
'view booking entities' => array(
'title' => t('View bookings'),
'description' => t('Allows users to view bookings.'),
'restrict access' => TRUE,
),
'edit any booking entities' => array(
'title' => t('Edit any bookings'),
'description' => t('Allows users to edit any bookings.'),
'restrict access' => TRUE,
),
'edit own booking entities' => array(
'title' => t('Edit own bookings'),
'description' => t('Allows users to edit own bookings.'),
'restrict access' => TRUE,
),
'administer resource types' => array(
'title' => t('Administer resource types'),
'description' => t('Allows users to configure resource types and their fields.'),
'restrict access' => TRUE,
),
'create resource entities' => array(
'title' => t('Create resources'),
'description' => t('Allows users to create resources.'),
'restrict access' => TRUE,
),
'view resource entities' => array(
'title' => t('View resources'),
'description' => t('Allows users to view resources.'),
'restrict access' => TRUE,
),
'edit any resource entities' => array(
'title' => t('Edit any resources'),
'description' => t('Allows users to edit any resources.'),
'restrict access' => TRUE,
),
'edit own resource entities' => array(
'title' => t('Edit own resources'),
'description' => t('Allows users to edit own resources.'),
'restrict access' => TRUE,
),
);
return $permissions;
}
/**
* Implements hook_entity_property_info_alter().
*/
function resource_booking_entity_property_info_alter(&$info) {
$properties = &$info['booking']['properties'];
$properties['created'] = array(
'label' => t('Date created'),
'type' => 'date',
'description' => t('The date the node was posted.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer booking types',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t('Date changed'),
'type' => 'date',
'description' => t('The date the booking was most recently updated.'),
'schema field' => 'changed',
);
$properties['uid'] = array(
'label' => t('Author'),
'type' => 'user',
'description' => t('The author of the booking.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer booking types',
'required' => TRUE,
'schema field' => 'uid',
);
$properties2 = &$info['resource']['properties'];
$properties2['created'] = array(
'label' => t('Date created'),
'type' => 'date',
'description' => t('The date the node was posted.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer resource types',
'schema field' => 'created',
);
$properties2['changed'] = array(
'label' => t('Date changed'),
'type' => 'date',
'description' => t('The date the resource was most recently updated.'),
'schema field' => 'changed',
);
$properties2['uid'] = array(
'label' => t('Author'),
'type' => 'user',
'description' => t('The author of the resource.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer resource types',
'required' => TRUE,
'schema field' => 'uid',
);
}
/*******************************************************************************
********************************* Booking API's *******************************
******************************************************************************/
/**
* Access callback for booking.
*/
function booking_access($op, $booking, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer booking types', $account)
|| user_access('create booking entities', $account);
case 'view':
return user_access('administer booking types', $account)
|| user_access('view booking entities', $account);
case 'edit':
return user_access('administer booking types')
|| user_access('edit any booking entities')
|| (user_access('edit own booking entities') && ($booking->uid == $account->uid));
}
}
/**
* Load a booking.
*/
function booking_load($bid, $reset = FALSE) {
$bookings = booking_load_multiple(array($bid), array(), $reset);
return reset($bookings);
}
/**
* Load multiple bookings based on certain conditions.
*/
function booking_load_multiple($bids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('booking', $bids, $conditions, $reset);
}
/**
* Save booking.
*/
function booking_save($booking) {
entity_save('booking', $booking);
}
/**
* Delete single booking.
*/
function booking_delete($booking) {
entity_delete('booking', entity_id('booking', $booking));
}
/**
* Delete multiple bookings.
*/
function booking_delete_multiple($booking_ids) {
entity_delete_multiple('booking', $booking_ids);
}
/*******************************************************************************
****************************** Booking type API's *****************************
******************************************************************************/
/**
* Access callback for booking type.
*/
function booking_type_access($op, $entity = NULL) {
return user_access('administer booking types');
}
/**
* Load booking type.
*/
function booking_type_load($booking_type) {
return booking_type_get_types($booking_type);
}
/**
* List of booking types.
*/
function booking_type_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('booking_type', isset($type_name) ? array($type_name) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Save booking type entity.
*/
function booking_type_save($booking_type) {
entity_save('booking_type', $booking_type);
}
/**
* Delete single case type.
*/
function booking_type_delete($booking_type) {
entity_delete('booking_type', entity_id('booking_type', $booking_type));
}
/**
* Delete multiple case types.
*/
function booking_type_delete_multiple($booking_type_ids) {
entity_delete_multiple('booking_type', $booking_type_ids);
}
/*******************************************************************************
********************************* Resource API's ******************************
******************************************************************************/
/**
* Access callback for resource.
*/
function resource_access($op, $resource, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer resource types', $account)
|| user_access('create resource entities', $account);
case 'view':
return user_access('administer resource types', $account)
|| user_access('view resource entities', $account);
case 'edit':
return user_access('administer resource types')
|| user_access('edit any resource entities')
|| (user_access('edit own resource entities') && ($resource->uid == $account->uid));
}
}
/**
* Load a resource.
*/
function resource_load($rid, $reset = FALSE) {
$resources = resource_load_multiple(array($rid), array(), $reset);
return reset($resources);
}
/**
* Load multiple resources based on certain conditions.
*/
function resource_load_multiple($rids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('resource', $rids, $conditions, $reset);
}
/**
* Save resource.
*/
function resource_save($resource) {
entity_save('resource', $resource);
}
/**
* Delete single resource.
*/
function resource_delete($resource) {
entity_delete('resource', entity_id('resource', $resource));
}
/**
* Delete multiple resources.
*/
function resource_delete_multiple($resource_ids) {
entity_delete_multiple('resource', $resource_ids);
}
/*******************************************************************************
****************************** Resource type API's ****************************
******************************************************************************/
/**
* Access callback for resource type.
*/
function resource_type_access($op, $entity = NULL) {
return user_access('administer resource types');
}
/**
* Load resource type.
*/
function resource_type_load($resource_type) {
return resource_type_get_types($resource_type);
}
/**
* List of resource types.
*/
function resource_type_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('resource_type', isset($type_name) ? array($type_name) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Save resource type entity.
*/
function resource_type_save($resource_type) {
entity_save('resource_type', $resource_type);
}
/**
* Delete single case type.
*/
function resource_type_delete($resource_type) {
entity_delete('resource_type', entity_id('resource_type', $resource_type));
}
/**
* Delete multiple case types.
*/
function resource_type_delete_multiple($resource_type_ids) {
entity_delete_multiple('resource_type', $resource_type_ids);
}