/
farm_plan.module
1078 lines (962 loc) · 31.2 KB
/
farm_plan.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
* Farm plan - A farm plan entity type.
*/
// Include Features code.
include_once 'farm_plan.features.inc';
/****************************************************************
* Drupal hooks
* **************************************************************/
/**
* Implements hook_permission().
*/
function farm_plan_permission() {
$perms = array(
'administer farm_plan module' => array(
'title' => t('Administer farm plan module'),
'description' => t('Gives full access to everything in the farm plan module.'),
'restrict access' => TRUE,
),
'administer farm_plan types' => array(
'title' => t('Administer farm plan types'),
'restrict access' => TRUE,
),
'view farm plans' => array(
'title' => t('View farm plans'),
'description' => t('Allows users to view the full list of farm plans.'),
),
);
// Add permissions for each farm_plan type.
foreach (farm_plan_types() as $farm_plan_type) {
$type = $farm_plan_type->type;
$ops = array('view', 'edit', 'delete');
$scopes = array('any', 'own');
$perms += array(
"create $type farm plans" => array(
'title' => t('Create new %type_name farm plans', array('%type_name' => $farm_plan_type->label)),
),
);
foreach ($ops as $op) {
foreach ($scopes as $scope) {
$perms += array(
"$op $scope $type farm plans" => array(
'title' => drupal_ucfirst($op) . ' ' . $scope . ' ' . t('%type_name farm plans', array('%type_name' => $farm_plan_type->label)),
),
);
}
}
}
return $perms;
}
/**
* Implements hook_menu().
*/
function farm_plan_menu() {
$items = array();
$items['farm/plan/add'] = array(
'title' => 'Add plan',
'page callback' => 'farm_plan_add_types_page',
'access callback' => 'farm_plan_add_access',
'file' => 'farm_plan.pages.inc',
);
foreach (farm_plan_types() as $type => $info) {
$items['farm/plan/add/' . $type] = array(
'title' => 'Add ' . $info->label,
'page callback' => 'farm_plan_add',
'page arguments' => array(3),
'access callback' => 'farm_plan_access',
'access arguments' => array('create', 3),
'file' => 'farm_plan.pages.inc',
);
}
$farm_plan_uri = 'farm/plan/%farm_plan';
$farm_plan_uri_argument_position = 2;
$items[$farm_plan_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('farm_plan', $farm_plan_uri_argument_position),
'page callback' => 'farm_plan_view',
'page arguments' => array($farm_plan_uri_argument_position),
'access callback' => 'farm_plan_access',
'access arguments' => array('view', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
);
$items[$farm_plan_uri . '/view'] = array(
'title' => 'Plan',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$farm_plan_uri . '/delete'] = array(
'title' => 'Delete plan',
'title callback' => 'farm_plan_label',
'title arguments' => array($farm_plan_uri_argument_position),
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_delete_form', $farm_plan_uri_argument_position),
'access callback' => 'farm_plan_access',
'access arguments' => array('update', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
);
$items[$farm_plan_uri . '/edit'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_form', $farm_plan_uri_argument_position),
'access callback' => 'farm_plan_access',
'access arguments' => array('update', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => 100,
);
// Form for removing a record from a plan.
// This is the same as the form for deleting records (below), but it will not
// delete records. It will only unlink them from the plan.
$items[$farm_plan_uri . '/%/%/remove'] = array(
'title' => 'Remove record',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_record_remove_form', $farm_plan_uri_argument_position, 3, 4),
'access callback' => 'farm_plan_access',
'access arguments' => array('update', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
'type' => MENU_CALLBACK,
);
// Form for deleting a record from a plan.
// This is the same as the form for removing records (above), but it also
// gives the option to delete the record.
$items[$farm_plan_uri . '/%/%/delete'] = array(
'title' => 'Remove record',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_record_delete_form', $farm_plan_uri_argument_position, 3, 4),
'access callback' => 'farm_plan_access',
'access arguments' => array('update', $farm_plan_uri_argument_position),
'file' => 'farm_plan.pages.inc',
'type' => MENU_CALLBACK,
);
// Plan type delete form.
$items['admin/config/farm/plan-types/%farm_plan_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_plan_type_form_delete_confirm', 4),
'access arguments' => array('administer farm_plan types'),
'weight' => 1,
'type' => MENU_NORMAL_ITEM,
'file' => 'farm_plan.admin.inc',
);
return $items;
}
/**
* Implements hook_entity_info().
*/
function farm_plan_entity_info() {
$return = array(
'farm_plan' => array(
'label' => t('Farm plan'),
'entity class' => 'FarmPlan',
'controller class' => 'FarmPlanController',
'base table' => 'farm_plan',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'bundle' => 'type',
'label' => 'name',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(),
'load hook' => 'farm_plan_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'farm_plan',
'access callback' => 'farm_plan_access',
),
);
$return['farm_plan_type'] = array(
'label' => t('Farm plan type'),
'entity class' => 'FarmPlanType',
'controller class' => 'FarmPlanTypeController',
'base table' => 'farm_plan_type',
'fieldable' => FALSE,
'bundle of' => 'farm_plan',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'module' => 'farm_plan',
// Enable the entity API admin UI.
'admin ui' => array(
'path' => 'admin/config/farm/plan-types',
'file' => 'farm_plan.admin.inc',
'controller class' => 'FarmPlanTypeUIController',
),
'access callback' => 'farm_plan_type_access',
);
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function farm_plan_entity_info_alter(&$entity_info) {
foreach (farm_plan_types() as $type => $info) {
$entity_info['farm_plan']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/config/farm/plan-types/manage/%farm_plan_type',
'real path' => 'admin/config/farm/plan-types/manage/' . $type,
'bundle argument' => 5,
),
);
}
}
/**
* Implements hook_entity_property_info_alter().
*/
function farm_plan_entity_property_info_alter(&$info) {
$properties = &$info['farm_plan']['properties'];
$properties['name'] = array(
'label' => t('Name'),
'description' => t('The name of the plan.'),
'setter callback' => 'entity_property_verbatim_set',
'schema field' => 'name',
);
$properties['type'] = array(
'label' => t('Farm plan type'),
'type' => 'token',
'description' => t('The farm plan type.'),
'setter callback' => 'entity_property_verbatim_set',
'access callback' => 'farm_plan_properties_access',
'options list' => 'farm_plan_type_get_names',
'required' => TRUE,
'schema field' => 'type',
);
$properties['uid'] = array(
'label' => t('Owner'),
'type' => 'user',
'description' => t('The owner of the plan.'),
'setter callback' => 'entity_property_verbatim_set',
'access callback' => 'farm_plan_properties_access',
'required' => TRUE,
'schema field' => 'uid',
);
$properties['created'] = array(
'label' => t('Created'),
'type' => 'date',
'description' => t('The timestamp when the plan was created.'),
'setter callback' => 'entity_property_verbatim_set',
'access callback' => 'farm_plan_properties_access',
'required' => TRUE,
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t('Changed'),
'type' => 'date',
'description' => t('The timestamp when the plan was last modified.'),
'setter callback' => 'entity_property_verbatim_set',
'access callback' => 'farm_plan_properties_access',
'required' => TRUE,
'schema field' => 'changed',
);
$properties['active'] = array(
'label' => t('Active'),
'description' => t('Whether the plan is active.'),
'setter callback' => 'entity_property_verbatim_set',
'access callback' => 'farm_plan_properties_access',
'schema field' => 'active',
'type' => 'boolean',
);
}
/**
* Implements hook_field_extra_fields().
*/
function farm_plan_field_extra_fields() {
$farm_plan_types = farm_plan_types();
$extra_fields = array(
'farm_plan' => array(),
);
foreach ($farm_plan_types as $type) {
$extra_fields['farm_plan'][$type->type] = array(
'form' => array(
// Add plan name field to field UI.
'name' => array(
'label' => t('Name'),
'description' => t('The name of the plan.'),
'weight' => -10,
),
),
);
}
return $extra_fields;
}
/**
* Implements hook_entity_view().
*/
function farm_plan_entity_view($entity, $type, $view_mode, $langcode) {
// If the entity is not a farm_plan, bail.
if ($type != 'farm_plan') {
return;
}
// Add the plan type.
$plan_types = farm_plan_type_get_names();
if (!empty($plan_types[$entity->type])) {
$entity->content['type'] = array(
'#markup' => '<div><strong>Plan type:</strong> ' . $plan_types[$entity->type] . '</div>',
'#weight' => -101,
);
}
// Add the plan's "active" status.
if ($entity->active) {
$status = 'Yes';
}
else {
$status = 'No';
drupal_set_message(t('This plan is no longer active. Inactive plans should be considered "archived" and should not be edited or deleted unless they contain information that is incorrect.'), 'warning');
}
$entity->content['active'] = array(
'#markup' => '<div><strong>Active plan:</strong> ' . $status . '</div>',
'#weight' => -100,
);
// Summarize areas and assets associated with the plan (logs will be added via
// Views in hook_farm_ui_entity_views() below.
$plan_areas = farm_plan_linked_records('area', $entity->id);
if (!empty($plan_areas)) {
$plan_area_links = array();
foreach ($plan_areas as $area_id) {
$area = taxonomy_term_load($area_id);
$entity_label = entity_label('taxonomy_term', $area);
$entity_uri = entity_uri('taxonomy_term', $area);
$plan_area_links[] = l($entity_label, $entity_uri['path']);
}
$entity->content['farm_plan_areas'] = array(
'#markup' => '<div><strong>Areas:</strong> ' . implode(', ', $plan_area_links) . '</div>',
);
}
$plan_assets = farm_plan_linked_records('asset', $entity->id);
if (!empty($plan_assets)) {
$plan_asset_links = array();
foreach ($plan_assets as $asset_id) {
$asset = farm_asset_load($asset_id);
$entity_label = entity_label('farm_asset', $asset);
$entity_uri = entity_uri('farm_asset', $asset);
$plan_asset_links[] = l($entity_label, $entity_uri['path']);
}
$entity->content['farm_plan_assets'] = array(
'#markup' => '<div><strong>Assets:</strong> ' . implode(', ', $plan_asset_links) . '</div>',
);
}
}
/**
* Implements hook_farm_ui_entity_views().
*/
function farm_plan_farm_ui_entity_views($entity_type, $bundle, $entity) {
// If the entity is not a plan, bail.
if (!($entity_type == 'farm_plan')) {
return array();
}
// Display a View of logs associated with the plan.
return array(
'farm_plan_logs',
);
}
/***************************************************************
* Access functions
* *************************************************************/
/**
* Access callback for plan entities.
*
* @param string $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'.
* @param FarmPlan|string $farm_plan
* Optionally a specific plan entity to check.
* @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 farm_plan_access($op, $farm_plan = NULL, $account = NULL) {
$rights = &drupal_static(__FUNCTION__, array());
// If $op is not one of the supported ones, deny access.
if (!in_array($op, array('create', 'view', 'update', 'delete'), TRUE)) {
return FALSE;
}
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
global $user;
$account = $user;
}
// If no plan is provided, check for access to all plans.
if (empty($farm_plan)) {
return user_access('view farm plans', $account);
}
// $farm_plan may be either an object or a plan type. Since plan types
// cannot be an integer, use either id or type as the static cache id.
$cid = is_object($farm_plan) ? $farm_plan->id : $farm_plan;
// If we've already checked access for this plan, user and op, return from
// cache.
if (isset($rights[$account->uid][$cid][$op])) {
return $rights[$account->uid][$cid][$op];
}
// If the user has 'administer farm_plan module' permission, grant them
// access.
if (user_access('administer farm_plan module', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
// Check access to the plan based on it's type.
$type = is_string($farm_plan) ? $farm_plan : $farm_plan->type;
$farm_plan_types = farm_plan_types();
$type_names = array();
foreach ($farm_plan_types as $name => $farm_plan_type) {
$type_names[] = $name;
}
if (in_array($type, $type_names)) {
if ($op == 'create' && user_access('create ' . $type . ' farm plans', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
if ($op == 'view') {
if (user_access('view any ' . $type . ' farm plans', $account) || (user_access('view own ' . $type . ' farm plans', $account) && ($account->uid == $farm_plan->uid))) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' farm plans', $account) || (user_access('edit own ' . $type . ' farm plans', $account) && ($account->uid == $farm_plan->uid))) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' farm plans', $account) || (user_access('delete own ' . $type . ' farm plans', $account) && ($account->uid == $farm_plan->uid))) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
}
}
// If all else fails, deny access.
return FALSE;
}
/**
* Access callback: Checks whether the user has permission to add a plan.
*
* @param object|null $account
* The user account.
*
* @return bool
* TRUE if the user has add permission, otherwise FALSE.
*/
function farm_plan_add_access($account = NULL) {
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
global $user;
$account = $user;
}
// Check each of the plan types to see if the user has access.
$types = farm_plan_types();
foreach ($types as $type) {
if (farm_plan_access('create', $type->type, $account)) {
return TRUE;
}
}
// If all else fails, deny access.
return FALSE;
}
/**
* Access callback for plan types.
*
* @param string $op
* The operation being performed.
* @param FarmPlanType $farm_plan_type
* The farm plan entity.
*
* @return bool
* Returns true if the user has access.
*/
function farm_plan_type_access($op, FarmPlanType $farm_plan_type = NULL) {
return user_access('administer farm_plan types');
}
/**
* Access callback for farm_plan properties.
*/
function farm_plan_properties_access($op, $property, $entity = NULL, $account = NULL) {
// Delegate to the general farm_plan access callback, based on the $op.
switch ($op) {
case 'view':
return farm_plan_access('view', $entity, $account);
case 'edit':
return farm_plan_access('update', $entity, $account);
}
// Otherwise, deny access.
return FALSE;
}
/***************************************************************
* Farm plan API functions
* *************************************************************/
/**
* Load a plan.
*
* @param int $id
* The plan id.
* @param bool $reset
* Whether or not to reset the entity cache.
*
* @return FarmPlan
* Returns a farm plan object.
*/
function farm_plan_load($id, $reset = FALSE) {
$farm_plans = farm_plan_load_multiple(array($id), array(), $reset);
return reset($farm_plans);
}
/**
* Load multiple plans based on certain conditions.
*
* @param array $ids
* An array of farm plan ids.
* @param array $conditions
* An array of entity load conditions.
* @param bool $reset
* Whether or not to reset the entity cache.
*
* @return array
* Returns an array of farm plans.
*/
function farm_plan_load_multiple($ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('farm_plan', $ids, $conditions, $reset);
}
/**
* Save plan.
*
* @param FarmPlan $farm_plan
* The farm plan entity.
*/
function farm_plan_save(FarmPlan $farm_plan) {
entity_save('farm_plan', $farm_plan);
}
/**
* Delete single plan.
*
* @param FarmPlan $farm_plan
* The farm plan entity.
*/
function farm_plan_delete(FarmPlan $farm_plan) {
entity_delete('farm_plan', entity_id('farm_plan', $farm_plan));
}
/**
* Delete multiple plans.
*
* @param array $farm_plan_ids
* An array of farm plan ids.
*/
function farm_plan_delete_multiple(array $farm_plan_ids) {
entity_delete_multiple('farm_plan', $farm_plan_ids);
}
/***************************************************************
* Farm plan type API functions
* *************************************************************/
/**
* Load plan type.
*
* @param string $farm_plan_type
* The farm plan type.
*
* @return FarmPlanType
* Returns a farm plan type entity.
*/
function farm_plan_type_load($farm_plan_type) {
return farm_plan_types($farm_plan_type);
}
/**
* List of plan types.
*
* @param string $type_name
* The farm plan type name.
*
* @return FarmPlanType|array
* Returns either a single type, or an array of types.
*/
function farm_plan_types($type_name = NULL) {
$types = entity_load_multiple_by_name('farm_plan_type', isset($type_name) ? array($type_name) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
/**
* Save plan type entity.
*
* @param FarmPlanType $farm_plan_type
* The farm plan type entity.
*/
function farm_plan_type_save(FarmPlanType $farm_plan_type) {
entity_save('farm_plan_type', $farm_plan_type);
}
/**
* Delete single plan type.
*
* @param FarmPlanType $farm_plan_type
* The farm plan type entity.
*/
function farm_plan_type_delete(FarmPlanType $farm_plan_type) {
entity_delete('farm_plan_type', entity_id('farm_plan_type', $farm_plan_type));
}
/**
* Delete multiple plan types.
*
* @param array $farm_plan_type_ids
* An array of farm plan type ids.
*/
function farm_plan_type_delete_multiple(array $farm_plan_type_ids) {
entity_delete_multiple('farm_plan_type', $farm_plan_type_ids);
}
/**
* Get the names of all plan types.
*
* @return array
* Returns an array of plan type names, keyed by machine name.
*/
function farm_plan_type_get_names() {
$names = array();
$types = farm_plan_types();
foreach ($types as $type) {
$names[$type->type] = $type->label;
}
return $names;
}
/***************************************************************
* Functions for linking/unlinking plans with other records.
* *************************************************************/
/**
* Load a list of records associated with a plan.
*
* @param string $record_type
* The record type (see farm_plan_record_relationships()).
* @param int $plan_id
* The plan ID.
*
* @return array
* Returns an array of record IDs associated with the plan ID.
*/
function farm_plan_linked_records($record_type, $plan_id) {
// Start an empty records array.
$records = array();
// If the plan ID is empty, bail.
if (empty($plan_id)) {
return;
}
// Get available relationships between plans and other record types.
$relationships = farm_plan_record_relationships();
// If a database table and field are not available, bail.
if (empty($relationships[$record_type]['table']) || empty($relationships[$record_type]['field'])) {
return;
}
// Get the table and field.
$table = $relationships[$record_type]['table'];
$field = $relationships[$record_type]['field'];
// Query for record IDs.
$query = db_select($table, 'r');
$query->addField('r', $field);
$query->condition('plan_id', $plan_id);
$result = $query->execute();
foreach ($result as $row) {
if (!empty($row->{$field})) {
$records[] = $row->{$field};
}
}
// Return the record IDs.
return $records;
}
/**
* Link a plan to a record.
*
* @param string $record_type
* The record type (see farm_plan_record_relationships()).
* @param int $plan_id
* The plan ID.
* @param int $record_id
* The record ID.
* @param array $primary_record
* If this is not the primary/required record type in the table, the primary
* record key/value to needed for updating the existing record.
* For example: array('asset_id' => 5)
*/
function farm_plan_link_record($record_type, $plan_id, $record_id, $primary_record = array()) {
// First, delete any existing link.
farm_plan_unlink_record($record_type, $plan_id, $record_id);
// Get available relationships between plans and other record types.
$relationships = farm_plan_record_relationships();
// If a database table and field are not available, bail.
if (empty($relationships[$record_type]['table']) || empty($relationships[$record_type]['field'])) {
return;
}
// Get the table and field.
$table = $relationships[$record_type]['table'];
$field = $relationships[$record_type]['field'];
// If this record type is not required, and a primary key/value is provided,
// then update the existing relationship row.
if ($relationships[$record_type]['required'] === FALSE && !empty($primary_record)) {
$primary_record_key = key($primary_record);
$primary_record_id = current($primary_record);
$record = array(
'plan_id' => $plan_id,
$primary_record_key => $primary_record_id,
$field => $record_id,
);
$primary_keys = array(
'plan_id',
$primary_record_key,
);
drupal_write_record($table, $record, $primary_keys);
}
// Otherwise create a new relationship row.
else {
$record = array(
'plan_id' => $plan_id,
$field => $record_id,
);
drupal_write_record($table, $record);
}
}
/**
* Unlink a plan from an area.
*
* @param string $record_type
* The record type (see farm_plan_record_relationships()).
* @param int $plan_id
* The plan ID.
* @param int $record_id
* The record ID.
*/
function farm_plan_unlink_record($record_type, $plan_id, $record_id) {
// Get available relationships between plans and other record types.
$relationships = farm_plan_record_relationships();
// If a database table and field are not available, bail.
if (empty($relationships[$record_type]['table']) || empty($relationships[$record_type]['field'])) {
return;
}
// Get the table and field.
$table = $relationships[$record_type]['table'];
$field = $relationships[$record_type]['field'];
// If this record type is not required, simply update the relationship row in
// the database to remove reference to the record.
if ($relationships[$record_type]['required'] === FALSE) {
$query = db_update($table);
$query->fields(array(
$field => NULL,
));
$query->condition('plan_id', $plan_id);
$query->condition($field, $record_id);
$query->execute();
}
// Otherwise, delete the whole relationship row.
else {
$query = db_delete($table);
$query->condition('plan_id', $plan_id);
$query->condition($field, $record_id);
$query->execute();
}
}
/**
* Defines available relationships between plans and other record types.
*
* @return array
* Returns an array of record types with the database table and field names
* used to store relationships. See farm_plan.api.php.
*/
function farm_plan_record_relationships() {
// Ask modules for relationships.
$relationships = module_invoke_all('farm_plan_record_relationships');
// Set default values.
foreach ($relationships as &$relationship) {
if (!isset($relationship['required'])) {
$relationship['required'] = TRUE;
}
}
// Return the relationships.
return $relationships;
}
/**
* Implements hook_farm_plan_record_relationships().
*/
function farm_plan_farm_plan_record_relationships() {
return array(
'area' => array(
'label' => t('Area'),
'entity_type' => 'taxonomy_term',
'entity_type_table' => 'taxonomy_term_data',
'entity_pk' => 'tid',
'table' => 'farm_plan_area',
'field' => 'area_id',
),
'asset' => array(
'label' => t('Asset'),
'entity_type' => 'farm_asset',
'entity_pk' => 'id',
'table' => 'farm_plan_asset',
'field' => 'asset_id',
),
'log' => array(
'label' => t('Log'),
'entity_type' => 'log',
'entity_pk' => 'id',
'table' => 'farm_plan_log',
'field' => 'log_id',
),
'user' => array(
'label' => t('User'),
'entity_type' => 'user',
'entity_pk' => 'uid',
'table' => 'farm_plan_user',
'field' => 'user_id',
),
);
}
/**
* Implements hook_entity_view_alter().
*/
function farm_plan_entity_view_alter(&$build, $type) {
// Get the entity ID. Bail if not found.
$entity_id = NULL;
if (!empty($build['#entity'])) {
$entity_id = entity_id($type, $build['#entity']);
}
elseif (!empty($build['#term'])) {
$entity_id = entity_id($type, $build['#term']);
}
if (empty($entity_id)) {
return;
}
// Get available relationships between plans and other record types.
$relationships = farm_plan_record_relationships();
// Iterate through the relationships to find a matching record type.
$record_type = '';
foreach ($relationships as $relationship => $info) {
if ($type == $info['entity_type']) {
$record_type = $relationship;
break;
}
}
// If a record type wasn't found, bail.
if (empty($record_type)) {
return;
}
// Find plan(s) that this entity is associated with.
$query = 'SELECT plan_id FROM {' . $relationships[$record_type]['table'] . '} WHERE ' . $relationships[$record_type]['field'] . ' = :entity_id';
$args = array(':entity_id' => $entity_id);
$result = db_query($query, $args);
$plan_ids = array();
foreach ($result as $row) {
if (!empty($row->plan_id)) {
$plan_ids[] = $row->plan_id;
}
}
// Iterate through the plans.
foreach ($plan_ids as $plan_id) {
// Load the plan.
$plan = farm_plan_load($plan_id);
// Get the plan URL and name.
$plan_uri = entity_uri('farm_plan', $plan);
$plan_path = $plan_uri['path'];
$plan_name = entity_label('farm_plan', $plan);
// Set a message pointing to the plan.
$args = array(
'@record_type' => $relationships[$record_type]['label'],
'!plan_path' => url($plan_path),
'%plan_name' => $plan_name,
);
$message = t('This @record_type is part of the plan: <a href="!plan_path">%plan_name</a>', $args);
drupal_set_message($message);
}
}
/**
* Implements hook_entity_update().
*/
function farm_plan_entity_update($entity, $type) {
// If a plan is made inactive, archive all assets linked to it, and vice
// versa.
if ($type != 'farm_plan') {
return;
}
if (!(isset($entity->original) && isset($entity->original->active))) {
return;
}
if ($entity->active == $entity->original->active) {
return;
}
if (empty($entity->active)) {
$archived = REQUEST_TIME;
}
else {
$archived = FALSE;
}
$asset_ids = farm_plan_linked_records('asset', $entity->id);
foreach ($asset_ids as $asset_id) {
$asset = farm_asset_load($asset_id);