This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfarm_livestock.module
293 lines (254 loc) · 6.42 KB
/
farm_livestock.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
<?php
/**
* @file
* Code for the Farm Livestock feature.
*/
include_once 'farm_livestock.features.inc';
/**
* Implements hook_farm_access_perms().
*/
function farm_livestock_farm_access_perms($role) {
// Assemble a list of entity types provided by this module.
$types = array(
'farm_asset' => array(
'animal',
),
'log' => array(
'farm_medical',
),
'taxonomy' => array(
'farm_animal_groups',
'farm_animal_types',
),
);
// Grant different CRUD permissions based on the role.
$perms = array();
switch ($role) {
// Farm Manager and Worker
case 'Farm Manager':
case 'Farm Worker':
$perms = farm_access_entity_perms($types);
break;
// Farm Viewer
case 'Farm Viewer':
$perms = farm_access_entity_perms($types, array('view'));
break;
}
return $perms;
}
/**
* Implements hook_farm_area_type_info().
*/
function farm_livestock_farm_area_type_info() {
return array(
'paddock' => array(
'label' => t('Paddock'),
'style' => 'farm_map_style_dark_green',
'weight' => 5,
),
);
}
/**
* Implements hook_farm_admin_actions().
*/
function farm_livestock_farm_admin_actions() {
// Define farm area actions.
$actions = array(
'animal' => array(
'title' => t('Add an animal'),
'href' => 'farm/asset/add/animal',
'views' => array(
'farm_animals',
),
),
'animal_group' => array(
'title' => t('Add a group'),
'href' => 'admin/structure/taxonomy/farm_animal_groups/add',
'views' => array(
'farm_animal_groups',
),
),
'animal_type' => array(
'title' => t('Add a type'),
'href' => 'admin/structure/taxonomy/farm_animal_types/add',
'views' => array(
'farm_animal_types',
),
),
'medical' => array(
'title' => t('Add a medical record'),
'href' => 'log/add/farm_medical',
'assets' => array(
'animal',
),
'views' => array(
'farm_log_medical',
),
),
);
return $actions;
}
/**
* Implements hook_farm_asset_breadcrumb().
*/
function farm_livestock_farm_asset_breadcrumb($farm_asset) {
// If the asset is an animal, add a link to the animals list.
$breadcrumb = array();
if ($farm_asset->type == 'animal') {
$breadcrumb[] = l(t('Assets'), 'farm/assets');
$breadcrumb[] = l(t('Animals'), 'farm/assets/animals');
}
return $breadcrumb;
}
/**
* Implements hook_farm_asset_view_views().
*/
function farm_livestock_farm_asset_view_views($farm_asset) {
// If the entity is not an animal, bail.
if ($farm_asset->type != 'animal') {
return array();
}
// Return a list of Views to include on Animals.
return array(
array(
'name' => 'farm_log_activity',
'weight' => 0,
),
array(
'name' => 'farm_log_observation',
'weight' => 10,
),
array(
'name' => 'farm_log_medical',
'weight' => 20,
),
array(
'name' => 'farm_log_input',
'weight' => 50,
),
array(
'name' => 'farm_log_harvest',
'weight' => 60,
),
array(
'name' => 'farm_log_movement',
'weight' => 100,
),
array(
'name' => 'farm_asset_children',
'display' => 'page',
'title' => t('Children'),
'weight' => 110,
),
);
}
/**
* Implements hook_farm_taxonomy_breadcrumb().
*/
function farm_livestock_farm_taxonomy_breadcrumb($term) {
$breadcrumb = array();
// Switch through available vocabularies.
switch ($term->vocabulary_machine_name) {
// If the term is in farm_animal_groups...
case 'farm_animal_groups':
$breadcrumb[] = l(t('Assets'), 'farm/assets');
$breadcrumb[] = l(t('Animals'), 'farm/assets/animals');
$breadcrumb[] = l(t('Groups'), 'farm/assets/animals/groups');
break;
// If the term is in farm_animal_types...
case 'farm_animal_types':
$breadcrumb[] = l(t('Assets'), 'farm/assets');
$breadcrumb[] = l(t('Animals'), 'farm/assets/animals');
$breadcrumb[] = l(t('Species/Breeds'), 'farm/assets/animals/types');
break;
}
return $breadcrumb;
}
/**
* Implements hook_farm_taxonomy_term_view_views().
*/
function farm_livestock_farm_taxonomy_term_view_views($term) {
// Start a list of View names.
$views = array();
// Switch logic depending on the vocabulary.
switch ($term->vocabulary_machine_name) {
// Farm areas:
case 'farm_areas':
$views[] = array(
'name' => 'farm_animals',
'weight' => -10,
);
break;
// Farm animal groups:
case 'farm_animal_groups':
$views[] = array(
'name' => 'farm_animals',
'arg' => 2,
'always' => TRUE,
);
break;
// Farm animal types:
case 'farm_animal_types':
$views[] = array(
'name' => 'farm_animals',
'arg' => 3,
'always' => TRUE,
);
break;
}
return $views;
}
/**
* Implements hook_farm_area_links().
*/
function farm_livestock_farm_area_links($id) {
$links = array();
// Add link to livestock.
$view = views_get_view('farm_animals');
$view->preview('default', array($id));
if ($view->total_rows > 0) {
$links[] = array(
'title' => t('Animals') . ': ' . $view->total_rows,
'href' => 'farm/assets/animals/' . $id,
);
}
return $links;
}
/**
* Implements hook_views_post_render().
*/
function farm_livestock_views_post_render(&$view, &$output, &$cache) {
// If this is the Farm Animals page...
if ($view->name == 'farm_animals' && $view->current_display == 'page') {
// If dashboard maps are disabled in the farm_map module settings, bail.
if (!variable_get('farm_map_show', TRUE)) {
return;
}
// If there are any arguments, bail.
/**
* @todo
* Display a map that is filtered by the same arguments.
*/
if (!empty($view->args)) {
return;
}
// If the View result is not empty...
if (!empty($view->result)) {
// Add the Animals asset map to the top of the View.
$map = \Drupal\openlayers\Openlayers::load('Map', 'farm_assets_animal');
if (!empty($map)) {
$map_build = $map->build();
$output = drupal_render($map_build) . $output;
}
}
}
}
/**
* Implements hook_farm_log_allowed_asset_reference_type().
*/
function farm_livestock_farm_log_allowed_asset_reference_type($log_type) {
// On medical logs, only animals can be referenced.
if ($log_type == 'farm_medical') {
return 'animal';
}
}