-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroomify_property.module
More file actions
1997 lines (1746 loc) · 60.9 KB
/
Copy pathroomify_property.module
File metadata and controls
1997 lines (1746 loc) · 60.9 KB
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
*/
/**
* Implements hook_menu().
*/
function roomify_property_menu() {
$items = array();
$items['admin/bat/config/property/wizard/%ctools_js'] = array(
'page callback' => 'roomify_property_wizard',
'page arguments' => array(5),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/bat/config/type/wizard/%roomify_property/%ctools_js'] = array(
'page callback' => 'roomify_type_wizard',
'page arguments' => array(5, 6),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_entity_info().
*/
function roomify_property_entity_info() {
$return['roomify_property'] = array(
'label' => t('Roomify Property'),
// The entity class and controller class extend the classes provided by the
// Entity API.
'entity class' => 'RoomifyProperty',
'controller class' => 'RoomifyPropertyController',
'base table' => 'roomify_properties',
'revision table' => 'roomify_properties_revision',
'revision' => 'vid',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'property_id',
'bundle' => 'type',
'revision' => 'revision_id',
'label' => 'name',
'language' => 'language',
),
// Bundles are defined by the property bundles below.
'bundles' => array(),
// Bundle keys tell the FieldAPI how to extract information from the bundle
// objects.
'bundle keys' => array(
'bundle' => 'type',
),
'view modes' => array(
'display' => array(
'label' => t('Display'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'creation callback' => 'roomify_property_create',
'access callback' => 'roomify_property_access',
'access arguments' => array(
'user key' => 'uid',
'access tag' => 'roomify_property_access',
),
'permission labels' => array(
'singular' => t('property'),
'plural' => t('properties'),
),
'module' => 'roomify_property',
// The information below is used by the RoomifyPropertyUIController (which extends
// the EntityDefaultUIController).
'admin ui' => array(
'path' => 'admin/bat/config/property',
'file' => 'roomify_property.admin.inc',
'controller class' => 'RoomifyPropertyUIController',
'menu wildcard' => '%roomify_property',
),
'metadata controller class' => 'RoomifyPropertyMetadataController',
'translation' => array(
'entity_translation' => array(
'base path' => 'admin/bat/config/property/manage/%roomify_property',
'path wildcard' => '%roomify_property',
'path schemes' => array(
'default' => array(),
),
'default settings' => array(
'default_language' => LANGUAGE_NONE,
'hide_language_selector' => FALSE,
),
),
),
);
$return['roomify_property_type'] = array(
'label' => t('Roomify Property Type'),
'entity class' => 'RoomifyPropertyType',
'controller class' => 'RoomifyPropertyTypeController',
'base table' => 'roomify_property_types',
'fieldable' => TRUE,
'bundle of' => 'roomify_property',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'access callback' => 'roomify_property_type_access',
'module' => 'roomify_property',
// Enable the entity API's admin UI.
'admin ui' => array(
'path' => 'admin/bat/config/property-types',
'file' => 'roomify_property_type.admin.inc',
'controller class' => 'RoomifyPropertyTypeUIController',
),
);
return $return;
}
/**
* Implements hook_entity_info_alter().
*
* We are adding the info about the property types via a hook to avoid a recursion
* issue as loading the room types requires the entity info as well.
*/
function roomify_property_entity_info_alter(&$entity_info) {
foreach (roomify_property_get_types() as $type => $info) {
$entity_info['roomify_property']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/bat/config/property-types/manage/%roomify_property_type',
'real path' => 'admin/bat/config/property-types/manage/' . $type,
'bundle argument' => 5,
'access arguments' => array('bypass roomify_property entities access'),
),
);
}
}
/**
* Implements hook_permission().
*/
function roomify_property_permission() {
$permissions = array();
// Permission for Roomify Property Types.
$permissions += array(
'administer roomify_property_type entities' => array(
'title' => t('Administer property types'),
'description' => t('Allows users to add property types and configure their fields.'),
'restrict access' => TRUE,
),
);
$permissions += array(
'view any unpublished properties' => array(
'title' => t('View any unpublish property'),
'description' => t('Allows users to view any property currently suspended.'),
),
'view own unpublished properties' => array(
'title' => t('View own unpublish property'),
'description' => t('Allows users to view own property currently suspended.'),
),
'unpublish any property' => array(
'title' => t('Unpublish any property'),
'description' => t('Allows users to set any property as suspended.'),
),
'unpublish own property' => array(
'title' => t('Unpublish own property'),
'description' => t('Allows users to set own property as suspended.'),
),
'view roomify_property revisions' => array(
'title' => t('View property revisions'),
'description' => t('Allows users to view property revisions.'),
),
'revert roomify_property revisions' => array(
'title' => t('Revert property revisions'),
'description' => t('Allows users to revert property revisions.'),
),
'delete roomify_property revisions' => array(
'title' => t('Delete property revisions'),
'description' => t('Allows users to delete property revisions.'),
),
);
$permissions += bat_entity_access_permissions('roomify_property');
return $permissions;
}
/**
* Implements hook_views_api().
*/
function roomify_property_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'roomify_property') . '/views',
);
}
/**
* Implements hook_views_plugins().
*/
function roomify_property_views_plugins() {
$module_path = drupal_get_path('module', 'roomify_property');
return array(
'row' => array(
'availability_calendar_plugin_rows' => array(
'title' => t('Availability calendar'),
'help' => t('Availability calendar.'),
'path' => $module_path . '/views/plugins/calendar',
'handler' => 'AvailabilityCalendarPluginRows',
'uses fields' => FALSE,
'uses options' => TRUE,
'type' => 'normal',
),
),
'style' => array(
'availability_calendar_plugin_one_row' => array(
'title' => t('Availability calendar'),
'help' => t('Availability calendar.'),
'path' => $module_path . '/views/plugins/calendar',
'handler' => 'AvailabilityCalendarPluginOneRow',
'uses row plugin' => FALSE,
'uses options' => TRUE,
'type' => 'normal',
),
),
);
}
/**
* Determines whether the given user has access to a property revision.
*
* @param string $op
* The operation being performed. One of 'view', 'revert', 'delete'.
* @param RoomifyProperty $property
* @param $revision_id
* @param object $account
* The user to check for. Leave it to NULL to check for the global user.
*
* @return bool
* Whether access is allowed or not.
*/
function roomify_property_access_revision($op, $property, $revision_id, $account = NULL) {
global $user;
$account = isset($account) ? $account : $user;
if ($revision_property = roomify_property_load_revision($revision_id)) {
if ($property->property_id != $revision_property->property_id) {
return FALSE;
}
else {
if (user_access('bypass roomify_property entities access')) {
return TRUE;
}
if ($op == 'view') {
return user_access('view roomify_property revisions');
}
elseif ($op == 'revert') {
return user_access('revert roomify_property revisions');
}
elseif ($op == 'delete') {
return user_access('delete roomify_property revisions');
}
}
}
return FALSE;
}
/**
* Determines whether the given user has access to a property.
*
* @param string $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'
* or just 'edit' (being the same as 'create' or 'update').
* @param RoomifyProperty $property
* Optionally a property to check access for. If nothing is
* given, access for all properties is determined.
* @param object $account
* The user to check for. Leave it to NULL to check for the global user.
*
* @return bool
* Whether access is allowed or not.
*/
function roomify_property_access($op, $property = NULL, $account = NULL) {
global $user;
$account = isset($account) ? $account : $user;
if ($op == 'view' && $property !== NULL) {
if ($property->status == 0) {
if (!((user_access('view any unpublished properties', $account) ||
(user_access('view own unpublished properties', $account) && $account->uid == $property->uid)))) {
return FALSE;
}
}
}
return bat_entity_access($op, $property, $account, 'roomify_property');
}
/**
* Access callback: Checks whether the user has permission to add a property.
*
* @return bool
* TRUE if the user has add permission, otherwise FALSE.
*/
function roomify_property_add_access() {
if (user_access('administer roomify_property_type entities')) {
return TRUE;
}
$bundles = roomify_property_get_types();
foreach ($bundles as $bundle) {
if (roomify_property_access('create', roomify_property_create(array('type' => $bundle->type, 'uid' => 0)))) {
return TRUE;
}
}
return FALSE;
}
/**
* Access callback: Checks whether the user has permission to manage rooms.
*
* @return bool
* TRUE if the user has permission, otherwise FALSE.
*/
function roomify_property_manage_types_access($property) {
if (user_access('administer roomify_property_type entities')) {
return TRUE;
}
if (roomify_property_access('update', $property)) {
return TRUE;
}
return FALSE;
}
/**
* The class used for property entities.
*/
class RoomifyProperty extends Entity {
/**
*
*/
public function __construct($values = array()) {
parent::__construct($values, 'roomify_property');
}
/**
* {@inheritdoc}
*/
protected function defaultLabel() {
return $this->name;
}
/**
* {@inheritdoc}
*/
protected function defaultUri() {
switch ($this->type) {
case 'casa_property':
case 'locanda_property':
return array('path' => 'listing/' . $this->property_id);
case 'single_day_activity_property':
case 'multi_day_activity_property':
case 'single_day_tour_property':
case 'multi_day_tour_property':
return array('path' => 'activity/' . $this->property_id);
case 'space_property':
return array('path' => 'location/' . $this->property_id);
default:
return array('path' => 'property/' . $this->property_id);
}
}
}
/**
* The class used for property type entities.
*/
class RoomifyPropertyType extends Entity {
/**
* The property type.
*
* @var string
*/
public $type;
/**
* The property type label.
*
* @var string
*/
public $label;
/**
*
*/
public function __construct($values = array()) {
parent::__construct($values, 'roomify_property_type');
}
}
/**
* The MetadataController for RoomifyProperty entities.
*/
class RoomifyPropertyMetadataController extends EntityDefaultMetadataController {
/**
*
*/
public function entityPropertyInfo() {
$info = parent::entityPropertyInfo();
$properties = array('property_id', 'type', 'language', 'name', 'created', 'changed', 'uid');
foreach ($properties as $property) {
if (isset($info['roomify_property']['properties'][$property])) {
$info['roomify_property']['properties'][$property]['getter callback'] = 'entity_property_verbatim_get';
$info['roomify_property']['properties'][$property]['setter callback'] = 'entity_property_verbatim_set';
}
}
return $info;
}
}
/**
* The Controller for RoomifyProperty entities.
*/
class RoomifyPropertyController extends EntityAPIController {
/**
*
*/
public function __construct($entityType) {
parent::__construct($entityType);
}
/**
*
*/
public function create(array $values = array()) {
$values += array(
'property_id' => '',
'is_new' => TRUE,
'data' => '',
'name' => '',
'created' => '',
'language' => LANGUAGE_NONE,
);
$property = parent::create($values);
return $property;
}
/**
* {@inheritdoc}
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
global $user;
$entity->revision_uid = $user->uid;
$entity->revision_timestamp = REQUEST_TIME;
if (!isset($entity->log)) {
$entity->log = '';
}
parent::save($entity);
$entity->revision = FALSE;
// Add Path module support.
if (module_exists('path')) {
$this->savePath($entity);
}
// Add Pathauto module support.
if (module_exists('pathauto') && module_exists('entity_token')) {
module_load_include('inc', 'roomify_property', 'roomify_property.pathauto');
roomify_property_pathauto_update_alias($entity, empty($entity->is_new) ? 'update' : 'insert');
}
}
/**
* {@inheritdoc}
*/
public function delete($ids, DatabaseTransaction $transaction = NULL) {
foreach (roomify_property_load_multiple($ids) as $property) {
$uri = $property->uri();
// Add Path module support.
if (module_exists('path')) {
$this->deletePath($property);
}
// Add Pathauto module support.
if (module_exists('pathauto') && module_exists('entity_token')) {
pathauto_entity_path_delete_all('roomify_property', $property, $uri['path']);
}
}
parent::delete($ids);
}
/**
* Overriding the buildContent function to add entity specific fields.
*/
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
return $content;
}
/**
* Updates a property path alias upon saving.
*
* @param RoomifyProperty $property
* The property being saved.
*/
public function savePath(RoomifyProperty $property) {
$uri = $property->uri();
if (isset($property->path)) {
$path = $property->path;
$path['alias'] = trim($path['alias']);
// Delete old alias if user erased it.
if (empty($property->is_new)) {
if (!empty($path['pid']) && empty($path['alias'])) {
path_delete($path['pid']);
}
}
// Only save a non-empty alias.
if (!empty($path['alias'])) {
// Ensure fields for programmatic executions.
$langcode = entity_language('roomify_property', $property);
$path['source'] = $uri['path'];
$path['language'] = isset($langcode) ? $langcode : LANGUAGE_NONE;
path_save($path);
}
}
}
/**
* Removes property path alias upon deletion.
*
* @param RoomifyProperty $property
* The property being deleted.
*/
public function deletePath(RoomifyProperty $property) {
$uri = $property->uri();
path_delete(array('source' => $uri['path']));
}
}
/**
* The Controller for RoomifyPropertyType entities.
*/
class RoomifyPropertyTypeController extends EntityAPIControllerExportable {
/**
*
*/
public function __construct($entityType) {
parent::__construct($entityType);
}
/**
*
*/
public function create(array $values = array()) {
$values += array(
'id' => '',
'is_new' => TRUE,
'data' => '',
);
$property_type = parent::create($values);
return $property_type;
}
}
/**
*
*/
function roomify_property_type_access() {
return TRUE;
}
/**
* Create a property object.
*/
function roomify_property_create($values = array()) {
return entity_get_controller('roomify_property')->create($values);
}
/**
* Create a property type object.
*/
function roomify_property_type_create($values = array()) {
return entity_get_controller('roomify_property_type')->create($values);
}
/**
* Menu argument loader; Load a property type by string.
*
* @param $type
* The machine-readable name of a property type to load.
* @param bool $reset
* A boolean indicating whether the internal cache should be reset.
*
* @return array|false
* A property type array or FALSE if $type does not exist.
*/
function roomify_property_type_load($type, $reset = FALSE) {
return roomify_property_get_types($type, $reset);
}
/**
* Gets an array of all property types, keyed by the type name.
*
* @param string $type_name
* If set, the type with the given name is returned.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return RoomifyPropertyType[]
* Depending whether $type isset, an array of property types or a single one.
*/
function roomify_property_get_types($type_name = NULL, $reset = FALSE) {
// entity_load() will get the Entity controller for our property type
// entity and call the load function of that object.
$types = entity_load_multiple_by_name('roomify_property_type', isset($type_name) ? array($type_name) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Retrieve all property type labels.
*
* Ideally used for populating option lists.
*
* @return array
* An array of RoomifyProperty labels, keyed by their machine names.
*/
function roomify_property_type_labels() {
$property_types = roomify_property_get_types();
foreach ($property_types as &$property_type) {
$property_type = $property_type->label();
}
return $property_types;
}
/**
* Saves a property type to the db.
*
* @param RoomifyPropertyType $property_type
* The property type to save.
*/
function roomify_property_type_save(RoomifyPropertyType $property_type) {
$property_type->save();
}
/**
* Deletes a property type from the db.
*/
function roomify_property_type_delete(RoomifyPropertyType $bundle) {
$bundle->delete();
}
/**
* URI callback for Roomify Properties.
*/
function roomify_property_uri(RoomifyProperty $property) {
return array(
'path' => 'property/' . $property->property_id,
);
}
/**
* Fetches a property object.
*
* @param int $property_id
* Integer specifying the property id.
* @param bool $reset
* A boolean indicating whether the internal cache should be reset.
*
* @return RoomifyProperty|false
* A fully-loaded $property object or FALSE if it cannot be loaded.
*
* @see roomify_property_load_multiple()
*/
function roomify_property_load($property_id, $reset = FALSE) {
$properties = roomify_property_load_multiple(array($property_id), array(), $reset);
return reset($properties);
}
/**
* @param $revision_id
*/
function roomify_property_load_revision($revision_id) {
return entity_revision_load('roomify_property', $revision_id);
}
/**
* @param RoomifyProperty $property
*/
function roomify_property_revision_list($property) {
$revisions = array();
$result = db_query('SELECT r.revision_id, r.name, r.log, r.revision_uid, n.revision_id AS current_vid, r.revision_timestamp, u.name FROM {roomify_properties_revision} r LEFT JOIN {roomify_properties} n ON n.revision_id = r.revision_id INNER JOIN {users} u ON u.uid = r.revision_uid WHERE r.property_id = :property_id ORDER BY r.revision_id DESC', array(':property_id' => $property->property_id));
foreach ($result as $revision) {
$revisions[$revision->revision_id] = $revision;
}
return $revisions;
}
/**
* Retrieve a list of revisions with a revision_id greater than the current.
*
* @param int $property_id
* The property id to retrieve.
*
* @return array
* An array of revisions (latest first), each containing revision_id, name and
* bundle.
*/
function roomify_property_get_pending_revisions($property_id) {
$sql = "SELECT r.revision_id, r.name, n.type FROM {roomify_properties} n INNER JOIN {roomify_properties_revision} r ON n.property_id = r.property_id WHERE (r.revision_id > n.revision_id AND n.property_id = :property_id) ORDER BY r.revision_id DESC";
$result = db_query($sql, array(
':property_id' => $property_id,
));
$revisions = array();
foreach ($result as $revision) {
$revisions[$revision->revision_id] = $revision;
}
return $revisions;
}
/**
* Get the id of the current revision that the supplied property is pointing to.
*
* Used in cases where the property object wasn't fully loaded or was loaded
* with a different revision.
*
* @param int $property_id
* The id of the property whose current revision id is to be returned.
*
* @return int
* A single number being the current revision id (revision_id).
*/
function roomify_property_get_current_property_revision_id($property_id) {
$result = db_query("SELECT revision_id FROM {roomify_properties} WHERE property_id = :property_id", array(':property_id' => $property_id));
return $result->fetchField();
}
/**
* Loads multiple properties based on certain conditions.
*
* @param array $property_ids
* An array of property IDs.
* @param array $conditions
* An array of conditions to match against the {roomify_properties} table.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return array
* An array of property objects, indexed by property_id.
*
* @see entity_load()
* @see roomify_property_load()
*/
function roomify_property_load_multiple($property_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('roomify_property', $property_ids, $conditions, $reset);
}
/**
* Deletes a property.
*
* @param RoomifyProperty $property
* The RoomifyProperty object that represents the property to delete.
*/
function roomify_property_delete(RoomifyProperty $property) {
$property->delete();
}
/**
* Deletes multiple properties.
*
* @param array $property_ids
* An array of property IDs.
*/
function roomify_property_delete_multiple(array $property_ids) {
entity_get_controller('roomify_property')->delete($property_ids);
}
/**
* Implements hook_theme().
*/
function roomify_property_theme() {
return array(
'roomify_property_add_list' => array(
'variables' => array('content' => array()),
'file' => 'roomify_property.admin.inc',
),
);
}
/**
* Add "Type" reference field.
*/
function roomify_property_add_bat_type_reference_field($type_bundle) {
field_info_cache_clear();
// "property_bat_type_reference" field.
if (field_read_field('property_bat_type_reference') === FALSE) {
$field = array(
'field_name' => 'property_bat_type_reference',
'type' => 'entityreference',
'cardinality' => -1,
'locked' => 1,
'settings' => array(
'target_type' => 'bat_type',
),
);
field_create_field($field);
}
field_cache_clear();
// "property_bat_type_reference" field instance.
if (field_read_instance('roomify_property', 'property_bat_type_reference', $type_bundle) === FALSE) {
$instance = array(
'field_name' => 'property_bat_type_reference',
'entity_type' => 'roomify_property',
'label' => 'Type',
'bundle' => $type_bundle,
'required' => FALSE,
'widget' => array(
'type' => 'entityreference_autocomplete',
),
);
// Check for IEF integration and use IEF widget if present.
$entity_info = entity_get_info('bat_type');
if (isset($entity_info['inline entity form'])) {
$instance['widget']['type'] = 'inline_entity_form';
}
// Create field instance.
field_create_instance($instance);
}
}
/**
* Implements hook_entity_insert().
*/
function roomify_property_entity_insert($entity, $type) {
global $user;
if ($type == 'roomify_property_type') {
roomify_property_add_bat_type_reference_field($entity->type);
}
if ($type == 'roomify_property') {
$types_reference = field_get_items($type, $entity, 'property_bat_type_reference');
foreach ($types_reference as $reference) {
$type_referenced = bat_type_load($reference['target_id']);
$bat_units = bat_unit_load_multiple(FALSE, array('type_id' => $type_referenced->type_id));
if (empty($bat_units)) {
$unit = bat_unit_create(array('type' => 'default'));
$unit->name = $type_referenced->name . ' 1';
$unit->created = !empty($unit->date) ? strtotime($unit->date) : REQUEST_TIME;
$unit->type_id = $type_referenced->type_id;
$unit->default_state = 1;
$unit->uid = $user->uid;
$unit->save();
}
}
}
}
/**
* Gets a list of Roomify Properties keyed by id and name in value.
*/
function roomify_property_ids($bundle = '') {
$properties = array();
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'roomify_property');
if ($bundle != '') {
$query->entityCondition('bundle', $bundle);
}
$result = $query->execute();
if (count($result) > 0) {
$entities = entity_load('roomify_property', array_keys($result['roomify_property']));
foreach ($entities as $type) {
$wrapper = entity_metadata_wrapper('roomify_property', $type);
$properties[$wrapper->property_id->value()] = $wrapper->name->value();
}
}
return $properties;
}
/**
* Implements hook_views_pre_render().
*/
function roomify_property_views_pre_render(&$view) {
// Hide "Add Types" button if this is a Casa.
if ($view->name == 'property_types') {
if (count($view->result) == 1 && $view->result[0]->bat_types_type == 'home') {
unset($view->header['area']);
}
elseif (isset($view->header['area'])) {
$link_text = 'add types';
if (variable_get('install_profile', '') == 'roomify_for_spaces') {
$link_text = 'add spaces';
}
$view->header['area']->options['content'] = l(t('@text', array('@text' => $link_text)), 'admin/bat/config/type/wizard/' . $view->args[0] . '/nojs', array('attributes' => array('class' => 'button add-type-button ctools-use-modal ctools-modal-roomify-dashboard-modal-style')));
}
}
}
/**
* Callback for Add property wizard.
*/
function roomify_property_wizard($ajax) {
if ($ajax) {
ctools_include('ajax');
ctools_include('modal');
$form_state = array(
'ajax' => TRUE,
'title' => t('Add New Property'),
);
// Use ctools to generate ajax instructions for the browser to create
// a form in a modal popup.
$output = ctools_modal_form_wrapper('roomify_property_wizard_form', $form_state);
// If the form has been submitted, there may be additional instructions
// such as dismissing the modal popup.
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
// Return the ajax instructions to the browser via ajax_render().
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('roomify_property_wizard_form');
}
}
/**
* Roomify Add Property Wizard form main callback.
*/
function roomify_property_wizard_form($form, &$form_state) {
// Initialize a description of the steps for the wizard.
if (empty($form_state['step'])) {
$form_state['step'] = 1;
// This array contains the function to be called at each step to get the
// relevant form elements. It will also store state information for each
// step.
$form_state['step_information'] = _roomify_property_wizard_steps();
}