-
Notifications
You must be signed in to change notification settings - Fork 9
/
rooms.module
1478 lines (1314 loc) · 52.3 KB
/
rooms.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Provides basic underlying functionality and configuration options used
* by all Rooms modules
*/
define('ROOMS_CHILDREN_FEATURES', 'children_features');
define('ROOMS_ADD', 'add');
define('ROOMS_ADD_DAILY', 'add-daily');
define('ROOMS_SUB', 'sub');
define('ROOMS_SUB_DAILY', 'sub-daily');
define('ROOMS_REPLACE', 'replace');
define('ROOMS_INCREASE', 'increase');
define('ROOMS_DECREASE', 'decrease');
define('ROOMS_PRICE_SINGLE_OCCUPANCY', 'single_occupancy');
define('ROOMS_DYNAMIC_MODIFIER', 'dynamic_modifier');
define('ROOMS_ACCESS_ALLOW', 'allow');
define('ROOMS_ACCESS_DENY', 'deny');
define('ROOMS_ACCESS_IGNORE', NULL);
define('ROOMS_THIS_PAGE', 'this');
define('ROOMS_ALL_PAGES', 'all');
define('ROOMS_NONE', 'none');
define('ROOMS_SIZE_FAILURE', 0);
define('ROOMS_AVAILABILITY_FAILURE', 1);
define('ROOMS_NO_ROOMS', 2);
define('ROOMS_PER_TYPE', 'rooms_per_type');
define('ROOMS_INDIVIDUAL', 'rooms_individual');
define('ROOMS_ENQ_CHECKOUT', 'rooms_enq_checkout');
define('ROOMS_COMMERCE_CHECKOUT', 'rooms_commerce_checkout');
define('ROOMS_DISPLAY_CHILDREN', 1);
define('ROOMS_DISPLAY_CHILDREN_NO', 0);
define('FULL_PAYMENT', 10);
define('PERCENT_PAYMENT', 11);
define('FIRST_NIGHT_PAYMENT', 12);
define('ROOMS_DISPLAY_TYPE_SELECTOR', 1);
define('ROOMS_DISPLAY_TYPE_SELECTOR_NO', 0);
define('ROOMS_OPTION_OPTIONAL', 'optional');
define('ROOMS_OPTION_MANDATORY', 'mandatory');
define('ROOMS_OPTION_ONREQUEST', 'on_request');
define('ROOMIFY_STORE_URI', 'https://store.roomify.us');
/**
* Implements hook_permission().
*/
function rooms_permission() {
$permissions = array(
'configure room settings' => array(
'title' => t('Configure Rooms'),
'description' => t('Allows users to manage site-wide Rooms configurations.'),
'restrict access' => TRUE,
),
);
return $permissions;
}
/**
* Implements hook_menu().
*/
function rooms_menu() {
$items['rooms_options/ajax'] = array(
'title' => 'Remove item callback',
'page callback' => 'rooms_options_remove_js',
'delivery callback' => 'ajax_deliver',
'access callback' => TRUE,
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
'file path' => 'includes',
'file' => 'form.inc',
);
return $items;
}
/**
* Implements hook_libraries_info().
*/
function rooms_libraries_info() {
$libraries['fullcalendar'] = array(
'name' => 'FullCalendar',
'vendor url' => 'http://fullcalendar.io',
'download url' => 'https://github.com/arshaw/fullcalendar/releases/download/v3.1.0/fullcalendar-3.1.0.zip',
'version arguments' => array(
'file' => 'fullcalendar.js',
// 3.1.0
'pattern' => '/v(\d+\.\d+\.\d)/',
'lines' => 3,
),
'files' => array(
'js' => array(
'lib/moment.min.js',
'fullcalendar.js',
'gcal.js',
),
'css' => array(
'fullcalendar.css',
),
),
'variants' => array(
'minified' => array(
'files' => array(
'js' => array(
'lib/moment.min.js',
'fullcalendar.min.js',
'gcal.js',
),
'css' => array(
'fullcalendar.min.css',
),
),
),
'source' => array(
'files' => array(
'js' => array(
'lib/moment.min.js',
'fullcalendar.js',
'gcal.js',
),
'css' => array(
'fullcalendar.css',
),
),
),
),
);
return $libraries;
}
/**
* Checks if the FullCalendar Library is loaded.
*
* @return bool
* A boolean indicating the FullCalendar status.
*/
function rooms_fullcalendar_loaded() {
if (rooms_library_loaded('fullcalendar', 'minified')) {
return TRUE;
}
else {
// Alert the authorized user/administrator to the abscence of the library.
drupal_set_message(t('The FullCalendar Library could not be found.
Please check the installation instructions and the <a href="@status">Status Report</a>.',
array('@status' => url('admin/reports/status'))), 'warning');
return FALSE;
}
}
/**
* Implements hook_theme().
*/
function rooms_theme() {
return array(
'rooms_three_month_calendar' => array(
'template' => 'rooms_three_month_calendar',
'variables' => array(
'url' => NULL,
'form' => NULL,
'year' => NULL,
'month' => NULL,
'link_options' => NULL,
),
),
);
}
/**
* Helper function to check if a library is loaded properly or not.
*
* @return bool
* Boolean indicating if the library is properly loaded or not.
*/
function rooms_library_loaded($name, $variant = NULL) {
return ($library = libraries_load($name, $variant)) && !empty($library['loaded']);
}
/**
* Generic access control for Rooms entities.
*
* @param string $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param object $entity
* Optionally an entity to check access for. If no entity is given, it will be
* determined whether access is allowed for all entities of the given type.
* @param object $account
* The user to check for. Leave it to NULL to check for the global user.
* @param string $entity_type
* The entity type of the entity to check for.
*
* @return bool
* Boolean indicating whether the user is granted or not.
*
* @see entity_access()
*/
function rooms_entity_access($op, $entity, $account, $entity_type) {
$rights = &drupal_static(__FUNCTION__, array());
global $user;
$account = isset($account) ? $account : $user;
$entity_info = entity_get_info($entity_type);
// $entity may be either an object or a entity type. Since entity types cannot
// be an integer, use either nid or type as the static cache id.
$cid = is_object($entity) ? $entity->{$entity_info['entity keys']['id']} : $entity;
// If we are creating a new entity make sure we set the type so permissions get
// applied
if ($op == 'create' && $cid == '') {
$cid = $entity->type;
}
// If we've already checked access for this node, user and op, return from
// cache.
if (isset($rights[$account->uid][$cid][$op])) {
return $rights[$account->uid][$cid][$op];
}
// Grant generic administrator level access.
if (user_access('bypass ' . $entity_type . ' entities access', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
if ($op == 'view') {
if (isset($entity)) {
// When trying to figure out access to an entity, query the base table
// using our access control tag.
if (!empty($entity_info['access arguments']['access tag']) && module_implements('query_' . $entity_info['access arguments']['access tag'] . '_alter')) {
$query = db_select($entity_info['base table']);
$query->addExpression('1');
$result = (bool) $query
->addTag($entity_info['access arguments']['access tag'])
->addMetaData('account', $account)
->condition($entity_info['entity keys']['id'], $entity->{$entity_info['entity keys']['id']})
->range(0, 1)
->execute()
->fetchField();
$rights[$account->uid][$cid][$op] = $result;
return $result;
}
else {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
else {
$access = user_access('view any ' . $entity_type . ' entity', $account);
$rights[$account->uid][$cid][$op] = $access;
return $access;
}
}
else {
// First grant access to the entity for the specified operation if no other
// module denies it and at least one other module says to grant access.
$access_results = module_invoke_all('rooms_entity_access', $op, $entity, $account, $entity_type);
if (in_array(FALSE, $access_results, TRUE)) {
$rights[$account->uid][$cid][$op] = FALSE;
return FALSE;
}
elseif (in_array(TRUE, $access_results, TRUE)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
// Grant access based on entity type and bundle specific permissions with
// special handling for the create operation since the entity passed in will
// be initialized without ownership.
if ($op == 'create') {
// Assuming an entity was passed in and we know its bundle key, perform
// the entity type and bundle-level access checks.
if (isset($entity) && !empty($entity_info['entity keys']['bundle'])) {
$access = user_access('create ' . $entity_type . ' entities', $account) || user_access('create ' . $entity_type . ' entities of bundle ' . $entity->{$entity_info['entity keys']['bundle']}, $account);
$rights[$account->uid][$cid][$op] = $access;
return $access;
}
else {
// Otherwise perform an entity type-level access check.
$access = user_access('create ' . $entity_type . ' entities', $account);
$rights[$account->uid][$cid][$op] = $access;
return $access;
}
}
else {
// Finally perform checks for the rest of operations. Begin by
// extracting the bundle name from the entity if available.
$bundle_name = '';
if (isset($entity) && !empty($entity_info['entity keys']['bundle'])) {
$bundle_name = $entity->{$entity_info['entity keys']['bundle']};
}
// For the delete and delete operations, first perform the entity type and
// bundle-level access check for any entity.
if (user_access($op . ' any ' . $entity_type . ' entity', $account) ||
user_access($op . ' any ' . $entity_type . ' entity of bundle ' . $bundle_name, $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
// Then check an authenticated user's access to delete his own entities.
if ($account->uid && !empty($entity_info['access arguments']['user key']) && isset($entity->{$entity_info['access arguments']['user key']}) && $entity->{$entity_info['access arguments']['user key']} == $account->uid) {
if (user_access($op . ' own ' . $entity_type . ' entities', $account) ||
user_access($op . ' own ' . $entity_type . ' entities of bundle ' . $bundle_name, $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
}
}
return FALSE;
}
/**
* Return permission names for a given entity type.
*/
function rooms_entity_access_permissions($entity_type) {
$entity_info = entity_get_info($entity_type);
$labels = $entity_info['permission labels'];
$permissions = array();
// General 'bypass' permission.
$permissions['bypass ' . $entity_type . ' entities access'] = array(
'title' => t('Bypass access to @entity_type', array('@entity_type' => $labels['plural'])),
'description' => t('Allows users to perform any action on @entity_type.', array('@entity_type' => $labels['plural'])),
'restrict access' => TRUE,
);
// Generic create and edit permissions.
$permissions['create ' . $entity_type . ' entities'] = array(
'title' => t('Create @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['view own ' . $entity_type . ' entities'] = array(
'title' => t('View own @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
}
$permissions['view any ' . $entity_type . ' entity'] = array(
'title' => t('View any @entity_type of any type', array('@entity_type' => $labels['singular'])),
'restrict access' => TRUE,
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['update own ' . $entity_type . ' entities'] = array(
'title' => t('Edit own @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
}
$permissions['update any ' . $entity_type . ' entity'] = array(
'title' => t('Edit any @entity_type of any type', array('@entity_type' => $labels['singular'])),
'restrict access' => TRUE,
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['delete own ' . $entity_type . ' entities'] = array(
'title' => t('Delete own @entity_type of any type', array('@entity_type' => $labels['plural'])),
);
}
$permissions['delete any ' . $entity_type . ' entity'] = array(
'title' => t('Delete any @entity_type of any type', array('@entity_type' => $labels['singular'])),
'restrict access' => TRUE,
);
// Per-bundle create and edit permissions.
if (!empty($entity_info['entity keys']['bundle'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$permissions['create ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('Create %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['view own ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('View own %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
}
$permissions['view any ' . $entity_type . ' entity of bundle ' . $bundle_name] = array(
'title' => t('View any %bundle @entity_type', array('@entity_type' => $labels['singular'], '%bundle' => $bundle_info['label'])),
'restrict access' => TRUE,
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['update own ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('Edit own %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
}
$permissions['update any ' . $entity_type . ' entity of bundle ' . $bundle_name] = array(
'title' => t('Edit any %bundle @entity_type', array('@entity_type' => $labels['singular'], '%bundle' => $bundle_info['label'])),
'restrict access' => TRUE,
);
if (!empty($entity_info['access arguments']['user key'])) {
$permissions['delete own ' . $entity_type . ' entities of bundle ' . $bundle_name] = array(
'title' => t('Delete own %bundle @entity_type', array('@entity_type' => $labels['plural'], '%bundle' => $bundle_info['label'])),
);
}
$permissions['delete any ' . $entity_type . ' entity of bundle ' . $bundle_name] = array(
'title' => t('Delete any %bundle @entity_type', array('@entity_type' => $labels['singular'], '%bundle' => $bundle_info['label'])),
'restrict access' => TRUE,
);
}
}
return $permissions;
}
/**
* Generic implementation of hook_query_alter() for Rooms entities.
*/
function rooms_entity_access_query_alter($query, $entity_type, $base_table = NULL, $account = NULL, $op = 'view') {
global $user;
// Read the account from the query if available or current user by default.
if (!isset($account) && !$account = $query->getMetaData('account')) {
$account = $user;
}
// Do not apply any conditions for users with administrative view permissions.
if (user_access('bypass ' . $entity_type . ' access', $account)
|| user_access($op . ' any ' . $entity_type . ' entity', $account)) {
return;
}
// Get the entity type info array for the current access check and prepare a
// conditions object.
$entity_info = entity_get_info($entity_type);
// If a base table wasn't specified, attempt to read it from the query if
// available, look for a table in the query's tables array that matches the
// base table of the given entity type, or just default to the first table.
if (!isset($base_table) && !$base_table = $query->getMetaData('base_table')) {
// Initialize the base table to the first table in the array. If a table can
// not be found that matches the entity type's base table, this will result
// in an invalid query if the first table is not the table we expect,
// forcing the caller to actually properly pass a base table in that case.
$tables = $query->getTables();
reset($tables);
$base_table = key($tables);
foreach ($tables as $table_info) {
if (!($table_info instanceof SelectQueryInterface)) {
// If this table matches the entity type's base table, use its table
// alias as the base table for the purposes of bundle and ownership
// access checks.
if ($table_info['table'] == $entity_info['base table']) {
$base_table = $table_info['alias'];
}
}
}
}
// Prepare an OR container for conditions. Conditions will be added that seek
// to grant access, meaning any particular type of permission check may grant
// access even if none of the others apply. At the end of this function, if no
// conditions have been added to the array, a condition will be added that
// always returns FALSE (1 = 0).
$conditions = db_or();
// Perform bundle specific permission checks for the specified entity type.
// In the event that the user has permission to view every bundle of the given
// entity type, $really_restricted will remain FALSE, indicating that it is
// safe to exit this function without applying any additional conditions. If
// the user only had such permission for a subset of the defined bundles,
// conditions representing those access checks would still be added.
$really_restricted = FALSE;
// Loop over every possible bundle for the given entity type.
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
// If the user has access to operation entities of the current bundle...
if (user_access($op . ' any ' . $entity_type . ' entity of bundle ' . $bundle_name, $account)) {
// Add a condition granting access if the entity specified by the view
// query is of the same bundle.
$conditions->condition($base_table . '.' . $entity_info['entity keys']['bundle'], $bundle_name);
}
elseif ($account->uid && !empty($entity_info['access arguments']['user key']) && user_access($op . ' own ' . $entity_type . ' entities of bundle ' . $bundle_name, $account)) {
// Add an AND condition group that grants access if the entity specified
// by the view query matches the same bundle and belongs to the user.
$conditions->condition(db_and()
->condition($base_table . '.' . $entity_info['entity keys']['bundle'], $bundle_name)
->condition($base_table . '.' . $entity_info['access arguments']['user key'], $account->uid)
);
}
}
// If the given entity type has a user ownership key...
if (!empty($entity_info['access arguments']['user key'])) {
// Perform 'operation own' access control for the entity in the query if the
// user is authenticated.
if ($account->uid && user_access($op . ' own ' . $entity_type . ' entities', $account)) {
$conditions->condition($base_table . '.' . $entity_info['access arguments']['user key'], $account->uid);
}
}
// Prepare an array of condition alter hooks to invoke and an array of context
// data for the current query.
$hooks = array(
'rooms_entity_access_' . $op . '_condition_' . $entity_type,
'rooms_entity_access_' . $op . '_condition',
);
$context = array(
'account' => $account,
'entity_type' => $entity_type,
'base_table' => $base_table,
);
// Allow other modules to add conditions to the array as necessary.
drupal_alter($hooks, $conditions, $context);
// If we have more than one condition based on the entity access permissions
// and any hook implementations...
if (count($conditions)) {
// Add the conditions to the query.
$query->condition($conditions);
}
else {
// Otherwise, since we don't have any possible conditions to match against,
// we falsify this query. View checks are access grants, not access denials.
$query->where('1 = 0');
}
}
/**
* Implements hook_field_info().
*/
function rooms_field_info() {
return array(
'rooms_options' => array(
'label' => t('Unit Options'),
'description' => t('Bookable unit options.'),
'settings' => array(),
'default_widget' => 'rooms_options_combined',
'default_formatter' => 'rooms_options_default',
'property_type' => 'rooms_options',
'property_callbacks' => array('rooms_options_property_info_callback'),
),
);
}
/**
* Property callback for the Entity Metadata framework.
*/
function rooms_options_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
// Apply the default.
entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type);
// Finally add in instance specific property info.
$name = $field['field_name'];
$property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
$property['type'] = ($field['cardinality'] != 1) ? 'list<rooms_options>' : 'rooms_options';
$property['property info'] = rooms_options_data_property_info('Rooms options');
$property['getter callback'] = 'entity_metadata_field_verbatim_get';
$property['setter callback'] = 'entity_metadata_field_verbatim_set';
}
/**
* Defines info for the properties of the rooms_options data structure.
*/
function rooms_options_data_property_info($name = NULL) {
// Build an array of basic property information for rooms_options.
$properties = array(
'name' => array(
'label' => 'Name',
'type' => 'text',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'quantity' => array(
'label' => 'Quantity',
'type' => 'integer',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'operation' => array(
'label' => 'Operation',
'type' => 'text',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'value' => array(
'label' => 'Value',
'type' => 'integer',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
'type' => array(
'label' => 'Type',
'type' => 'text',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
);
// Add the default values for each of the rooms_options properties.
foreach ($properties as &$value) {
$value += array(
'description' => !empty($name) ? t('!label of field %name', array('!label' => $value['label'], '%name' => $name)) : '',
);
}
return $properties;
}
/**
* Implements hook_field_is_empty().
*/
function rooms_field_is_empty($item, $field) {
return empty($item['name']) || empty($item['quantity']) || !(is_numeric($item['quantity']) && is_integer((int) $item['quantity']))
|| empty($item['value']) || !is_numeric($item['value'])
|| empty($item['operation']) || !in_array($item['operation'], array_keys(rooms_price_options_options()));
}
/**
* Implements hook_field_widget_info().
*/
function rooms_field_widget_info() {
return array(
'rooms_options_combined' => array(
'label' => t('Combined text field'),
'field types' => array('rooms_options'),
'settings' => array(),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function rooms_field_formatter_info() {
return array(
'rooms_options_default' => array(
'label' => t('Rooms Options Default'),
'field types' => array('rooms_options'),
),
'rooms_options_price' => array(
'label' => t('Rooms Options Price'),
'field types' => array('rooms_options'),
),
'rooms_options_admin' => array(
'label' => t('Rooms Options Administrator'),
'field types' => array('rooms_options'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function rooms_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'rooms_options_default':
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => "{$item['quantity']} x {$item['name']}");
}
break;
case 'rooms_options_price':
if (module_exists('commerce_multicurrency')) {
$currency_code = commerce_multicurrency_get_user_currency_code();
}
else {
$currency_code = commerce_default_currency();
}
foreach ($items as $delta => $item) {
$item_price = $item['value'] * 100;
if (module_exists('commerce_multicurrency')) {
$item_price = commerce_currency_convert($item_price, commerce_default_currency(), $currency_code);
}
$price = commerce_currency_format($item_price, $currency_code);
if ($item['value'] > 0) {
$element[$delta] = array('#markup' => "{$item['quantity']} x {$item['name']} - {$price}");
}
else {
$element[$delta] = array('#markup' => "{$item['quantity']} x {$item['name']}");
}
}
break;
case 'rooms_options_admin':
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => "{$item['quantity']} x {$item['name']} - {$item['operation']} {$item['value']}");
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_form().
*/
function rooms_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'rooms_options_combined') {
$field_parents = $element['#field_parents'];
$field_name = $element['#field_name'];
$language = $element['#language'];
$parents = array_merge($field_parents, array($field_name, $language, $delta));
$element['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($items[$delta]['name']) ? $items[$delta]['name'] : NULL,
'#attributes' => array(
'class' => array('rooms-option--name'),
),
);
$element['quantity'] = array(
'#type' => 'select',
'#title' => t('Quantity'),
'#options' => rooms_assoc_range(1, 10),
'#default_value' => isset($items[$delta]['quantity']) ? $items[$delta]['quantity'] : NULL,
'#description' => t('How many of this option should be available'),
'#attributes' => array(
'class' => array('rooms-option--quantity'),
),
);
$price_options = rooms_price_options_options();
$element['operation'] = array(
'#type' => 'select',
'#title' => t('Operation'),
'#options' => $price_options,
'#default_value' => isset($items[$delta]['operation']) ? $items[$delta]['operation'] : NULL,
'#attributes' => array(
'class' => array('rooms-option--operation'),
),
);
$element['value'] = array(
'#type' => 'textfield',
'#title' => t('Value'),
'#size' => 10,
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
'#element_validate' => array('element_validate_number'),
'#attributes' => array(
'class' => array('rooms-option--value'),
),
);
$type_options = array(
ROOMS_OPTION_OPTIONAL => t('Optional'),
ROOMS_OPTION_MANDATORY => t('Mandatory'),
ROOMS_OPTION_ONREQUEST => t('On Request'),
);
$element['type'] = array(
'#type' => 'select',
'#title' => t('Type'),
'#options' => $type_options,
'#default_value' => isset($items[$delta]['type']) ? $items[$delta]['type'] : 'optional',
'#attributes' => array(
'class' => array('rooms-option--type'),
),
);
$element['remove'] = array(
'#delta' => $delta,
'#name' => implode('_', $parents) . '_remove_button',
'#type' => 'submit',
'#value' => t('Remove'),
'#validate' => array(),
'#submit' => array('rooms_options_remove_submit'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'path' => 'rooms_options/ajax',
'effect' => 'fade',
),
'#attributes' => array(
'class' => array('rooms-option--remove-button'),
),
);
$element['#attached']['css'] = array(drupal_get_path('module', 'rooms') . '/css/rooms_options_widget.css');
return $element;
}
}
/**
* Returns the available price options for booking_unit options field.
*/
function rooms_price_options_options() {
return array(
ROOMS_ADD => t('Add to price'),
ROOMS_ADD_DAILY => t('Add to price per night'),
ROOMS_SUB => t('Subtract from price'),
ROOMS_SUB_DAILY => t('Subtract from price per night'),
ROOMS_REPLACE => t('Replace price'),
ROOMS_INCREASE => t('Increase price by % amount'),
ROOMS_DECREASE => t('Decrease price by % amount'),
);
}
/**
* Utility function that returns the last day of each month given a year.
*
* @param int $year
* The year to get the end of month dates for
*
* @return array
* An array keyed by months
*/
function rooms_end_of_month_dates($year) {
$end_of_month_dates = array();
for ($i = 1; $i <= 12; $i++) {
$end_of_month_dates[$i] = date("t", mktime(0, 0, 0, $i, 1, $year));
}
return $end_of_month_dates;
}
/**
* Utility function to create two related datepickers.
*
* We have a few forms that need a start and end date field
* and we need to apply the same javascript to these forms in order to have a
* specific consistent behaviour and groups the form elements and javascript
* injection in one place.
*
* @param int $year
* @param int $month
*
* @return array
* The array holding the field definitions
*/
function rooms_date_range_fields($year = NULL, $month = NULL) {
$date_range_fields = array();
$date_format = variable_get('rooms_date_format', 'd-m-Y');
// Create unique ids and selectors for each picker.
$start_date_id = drupal_html_id('datepicker-start-date');
$start_date_selector = '#' . $start_date_id . ' .form-text';
$end_date_id = drupal_html_id('datepicker-end-date');
$end_date_selector = '#' . $start_date_id . ' .form-text';
$days_in_advance = '+' . variable_get('rooms_booking_start_date', 1) . 'd';
// Allow users with the "make same-day bookings" permission to make
// bookings starting on any day.
if (user_access('make same-day bookings')) {
$days_in_advance = '+0d';
}
// Specify the default datepicker parameters (see date_popup_element_info())
$datepicker_options = array(
'dateFormat' => date_popup_format_to_popup($date_format),
// Limit bookings to X days in advance, depending on the
// chosen configuration in your Rooms installation, defaults
// to one day in advance.
'minDate' => $days_in_advance,
);
if ($year && $month) {
// Calculate min and max dates of the specified year/month.
$date = new DateTime();
$date->setDate($year, $month, 01);
$min_date = $date->format($date_format);
$date->modify('last day of this month');
$max_date = $date->format($date_format);
$datepicker_options += array(
'minDate' => $min_date,
'maxDate' => $max_date,
'defaultDate' => $min_date,
'numberOfMonths' => 1,
);
}
else {
$datepicker_options += array(
'minDate' => $days_in_advance,
);
}
$date_range_fields['rooms_start_date'] = array(
'#prefix' => '<div class="form-wrapper rooms-date-range"><div class="start-date" id="' . $start_date_id . '">',
'#suffix' => '</div>',
'#type' => 'date_popup',
'#title' => variable_get_value('rooms_arrival_date'),
'#date_type' => DATE_DATETIME,
'#date_format' => $date_format,
'#date_increment' => 1,
'#date_year_range' => '-1:+3',
// Default parameters defined above, with an additional parameter
// linking to the jQuery selector for the end datepicker.
'#datepicker_options' => array_merge($datepicker_options, array('endDateSelector' => $end_date_selector)),
'#required' => TRUE,
);
$start_day = variable_get('rooms_booking_start_date', 1);
// Allow users with the "make same-day bookings" permission to make
// bookings starting on any day.
if (user_access('make same-day bookings')) {
$start_day = '0';
}
$date_range_fields['rooms_end_date'] = array(
'#prefix' => '<div class="end-date" id="' . $end_date_id . '">',
'#suffix' => '</div></div>',
'#type' => 'date_popup',
'#title' => variable_get_value('rooms_departure_date'),
'#date_type' => DATE_DATETIME,
'#date_format' => $date_format,
'#date_increment' => 1,
'#date_year_range' => '-1:+3',
// Default parameters defined above, with an additional parameter
// parameter linking to the jQuery selector for the start datepicker.
'#datepicker_options' => array_merge($datepicker_options, array('startDateSelector' => $start_date_selector)),
'#required' => TRUE,
'#attached' => array(
'css' => array(
drupal_get_path('module', 'rooms') . '/css/rooms_date_range_fields.css',
),
'js' => array(
drupal_get_path('module', 'rooms') . '/js/rooms_date_popup.js',
array(
'data' => array(
'rooms' => array(
'roomsStartYear' => $year,
'roomsStartMonth' => $month,
'roomsBookingStartDay' => $start_day,
'roomsDateFormat' => date_popup_format_to_popup($date_format),
// Here we create a listing of all datepickers registered on the
// current page. This is available for use in your own custom
// jQuery scripts as Drupal.settings.rooms.datepickers.
'datepickers' => array(
$start_date_selector => array(
'endDateSelector' => $end_date_selector,
),
),
),
),
'type' => 'setting',
),
),
),
);
return $date_range_fields;
}
/**
* Given a form_state locate the start/end dates in the input array and
* instantiate and return DateTime objects.
*/
function rooms_form_input_get_start_end_dates($form_state) {
// If form_state['values']['rooms_X_date'] is not set it is an empty array
// hence the need to check and set values so that _constructor below will not
// fail.
if (is_array($form_state['values']['rooms_start_date']) || is_array($form_state['values']['rooms_end_date'])) {
$form_state['values']['rooms_start_date'] = '';
$form_state['values']['rooms_end_date'] = '';
}
$start = new DateTime($form_state['values']['rooms_start_date']);
$end = new DateTime($form_state['values']['rooms_end_date']);
return array($start, $end);
}
/**
* Given a form_state locate the start/end dates in the values array and
* instantiate and return DateTime objects.
*/
function rooms_form_values_get_start_end_dates($form_state) {
// As values dates has a format of year-month-day that is one of the default
// expected formats there is no need to explicit define format.
// http://www.php.net/manual/en/datetime.formats.date.php
$start_date = $form_state['values']['rooms_start_date'];
$end_date = $form_state['values']['rooms_end_date'];
// If the input format is numeric we assume that is a unixtime seconds format.
if (is_numeric($start_date) && is_numeric($end_date)) {
// The @ indicate DateTime that the format is unixtime.
$start_date = '@' . $start_date;
$end_date = '@' . $end_date;
}
$start = new DateTime($start_date);
$end = new DateTime($end_date);
return array($start, $end);
}
/**
* Validation callback that could be reused in all the forms that need to
* validate dates. End date must be greater than start date.
*/
function rooms_form_start_end_dates_validate($form, &$form_state) {
list($start_date, $end_date) = rooms_form_input_get_start_end_dates($form_state);
$today_greater = FALSE;