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
/
farm_grazing.herds.detail.inc
366 lines (302 loc) · 11.1 KB
/
farm_grazing.herds.detail.inc
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
<?php
/**
* @file
* Grazing herd detail form.
*/
/**
* Herd detail page callback.
*/
function farm_grazing_plan_herds_detail_form($form, &$form_state, $plan_obj, $herd_id) {
// Load utility functions.
module_load_include('inc', 'farm_grazing', 'farm_grazing.utils');
// Add herds CSS.
drupal_add_css(drupal_get_path('module', 'farm_grazing') . '/css/herds.css');
// get the plan id from the plan object
$plan = $plan_obj->id;
// Load the herd and generate its label.
$herd_group = farm_asset_load($herd_id);
$herd_name = entity_label('farm_asset', $herd_group);
// Set the page title.
drupal_set_title(t('Herd details: @herd_name', array('@herd_name' => $herd_name)));
$form['#tree'] = TRUE;
$form['plan'] = array(
'#type' => 'value',
'#value' => $plan,
);
// Load and assemble information about this herd.
$herd_data = array();
$herd_summary = farm_grazing_get_herd_data_and_summary($herd_id, $plan, $herd_data);
$this_herd = array(
'herd_id' => $herd_id,
'herd_name' => $herd_name,
'herd_data' => $herd_data,
'summary' => $herd_summary,
);
// --------- Add table of herd details -----------------
$type_options = farm_grazing_animal_type_options();
foreach ($this_herd['herd_data'] as $rec) {
// Create an array for the animal fields, and a reference to it.
$form['herd_details'][$this_herd['herd_id']]['animals'][$rec['id']] = array();
$animal_fields = &$form['herd_details'][$this_herd['herd_id']]['animals'][$rec['id']];
// Animal record name.
$animal_fields['name'] = array(
'#markup' => l($rec['name'], 'farm/asset/' . $rec['id'], array('query' => array('destination' => current_path()))),
);
// Animal type.
$animal_fields['type'] = array(
'#title' => t('Animal type'),
'#title_display' => 'invisible',
'#type' => 'select',
'#options' => $type_options,
'#default_value' => !empty($rec['type']) ? $rec['type'] : '',
);
// Head count.
$animal_fields['head_count'] = array(
'#title' => t('Head count'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#default_value' => $rec['head_count'],
'#size' => 30,
'#maxlength' => 30,
'#element_validate' => array('element_validate_number'),
);
// Weight.
$animal_fields['weight'] = array(
'#title' => t('Average weight'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#default_value' => $rec['weight'],
'#size' => 30,
'#maxlength' => 30,
'#element_validate' => array('element_validate_number'),
);
$animal_fields['total_weight'] = array(
'#markup' => $rec['weight'] * $rec['head_count'],
);
// SAU.
$animal_fields['sau'] = array(
'#markup' => round($rec['weight'] * $rec['head_count'] / 1000.0, 2),
);
// DMI.
$animal_fields['dmi'] = array(
'#markup' => round($rec['head_count'] * $rec['weight'] * $rec['dmi_factor'], 2),
);
// Planned arrival.
$animal_fields['arrival'] = array(
'#title' => t('Planned arrival'),
'#title_display' => 'invisible',
'#type' => 'date_popup',
'#date_format' => 'M j Y',
'#date_type' => DATE_FORMAT_UNIX,
'#date_year_range' => '-10:+3',
'#date_label_position' => 'none',
'#default_value' => $rec['arrival'],
);
// Planned departure.
$animal_fields['departure'] = array(
'#title' => t('Planned departure'),
'#title_display' => 'invisible',
'#type' => 'date_popup',
'#date_format' => 'M j Y',
'#date_type' => DATE_FORMAT_UNIX,
'#date_year_range' => '-10:+3',
'#date_label_position' => 'none',
'#default_value' => $rec['departure'],
);
// If the animal type or weight is empty...
if (empty($rec['type']) || empty($rec['weight'])) {
// If animal type is empty, add an error class to the dropdown.
if (empty($rec['type'])) {
$animal_fields['type']['#attributes']['class'] = array('error');
}
// If weight is empty, add an error class to the weight field, the SAU,
// and the DMI.
if (empty($rec['weight'])) {
$animal_fields['weight']['#attributes']['class'] = array('error');
$animal_fields['sau']['#prefix'] = '<span class="error">';
$animal_fields['sau']['#suffix'] = '</span>';
$animal_fields['dmi']['#prefix'] = '<span class="error">';
$animal_fields['dmi']['#suffix'] = '</span>';
}
}
}
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Changes'),
);
// Return the form.
return $form;
}
/**
* Validation handler for saving the herd information.
*/
function farm_grazing_plan_herds_detail_form_validate($form, &$form_state) {
// If there are no herd details, bail.
if (empty($form_state['values']['herd_details'])) {
return;
}
// Iterate through the herds.
foreach ($form_state['values']['herd_details'] as $herd_id => &$herd_details) {
// If there are no animals in the herd, skip it.
if (empty($herd_details['animals'])) {
continue;
}
// Iterate through the animals.
foreach ($herd_details['animals'] as $animal_id => &$animal_details) {
// Get the original $form array for this animal.
$animal_form = $form['herd_details'][$herd_id]['animals'][$animal_id];
// If certain fields are modified, make sure they are not left blank.
$required_fields = array(
'type' => t('Animal type'),
'head_count' => t('Head count'),
'weight' => t('Weight'),
'arrival' => t('Planned arrival'),
'departure' => t('Planned departure'),
);
foreach ($required_fields as $field => $label) {
// If the field is 'type', and it is 0, set it to empty for comparison
// purposes.
if ($field == 'type' && $animal_details[$field] === "0") {
$animal_details[$field] = '';
}
// If the field is 'arrival' or 'departure', and it is NULL, set it to
// empty for comparison purposes. The Date Popup widget sets empty
// field values to NULL, not empty.
if (in_array($field, array('arrival', 'departure')) && is_null($animal_details[$field])) {
$animal_details[$field] = '';
}
// Compare the default form value to the submitted form value.
if ($animal_form[$field]['#default_value'] != $animal_details[$field]) {
if (empty($animal_details[$field])) {
form_set_error('herd_details][' . $herd_id . '][animals][' . $animal_id . '][' . $field, $label . ' ' . t('is required.'));
}
}
}
}
}
}
/**
* Submit handler for saving the herd information.
*/
function farm_grazing_plan_herds_detail_form_submit($form, &$form_state) {
// If there are no herd details, bail.
if (empty($form_state['values']['herd_details'])) {
return;
}
// Remember if values have been updated.
$updated = FALSE;
// Iterate through the herds.
foreach ($form_state['values']['herd_details'] as $herd_id => $herd_details) {
// If there are no animals in the herd, skip it.
if (empty($herd_details['animals'])) {
continue;
}
// Iterate through the animals.
foreach ($herd_details['animals'] as $animal_id => $animal_details) {
// Get the original $form array for this animal.
$animal_form = $form['herd_details'][$herd_id]['animals'][$animal_id];
// If the animal's type has changed...
if ($animal_form['type']['#default_value'] != $animal_details['type']) {
// Set the asset property value.
farm_asset_property_set($animal_id, 'farm_grazing_animal_type', $animal_details['type']);
$updated = TRUE;
}
// If the animal's head count has changed...
if ($animal_form['head_count']['#default_value'] != $animal_details['head_count']) {
// Create an inventory log.
$animal = farm_asset_load($animal_id);
if (!empty($animal)) {
farm_inventory_set($animal, $animal_details['head_count']);
$updated = TRUE;
}
}
// If the animal's weight has changed...
if ($animal_form['weight']['#default_value'] != $animal_details['weight']) {
// Create a weight log.
$animal = farm_asset_load($animal_id);
if (!empty($animal)) {
farm_livestock_weight_set($animal, $animal_details['weight'], 'lbs');
$updated = TRUE;
}
}
// If the animal's planned arrival has changed...
if ($animal_form['arrival']['#default_value'] != $animal_details['arrival']) {
// Set the asset property value.
$planned_arrival = strtotime($animal_details['arrival']);
farm_asset_property_set($animal_id, 'farm_grazing_planned_arrival', $planned_arrival);
$updated = TRUE;
}
// If the animal's planned departure has changed...
if ($animal_form['departure']['#default_value'] != $animal_details['departure']) {
// Set the asset property value.
$planned_departure = strtotime($animal_details['departure']);
farm_asset_property_set($animal_id, 'farm_grazing_planned_departure', $planned_departure);
$updated = TRUE;
}
}
}
// If any values were updated, show a message.
if ($updated) {
drupal_set_message(t('Animals were updated.'));
}
}
/**
* theme_farm_grazing_plan_herds_detail_form
*/
function theme_farm_grazing_plan_herds_detail_form(&$vars) {
$form = $vars['form'];
$output = '';
// render table for herd details
if (!empty($form['herd_details'])) {
foreach (element_children($form['herd_details']) as $herd_id) {
// render table titles
$output .= drupal_render($form['herd_details'][$herd_id]['title']);
$table_id = 'herd_details_' . $herd_id;
// assemble the table rows
$rows = array();
if (!empty($form['herd_details'][$herd_id]['animals'])) {
foreach (element_children($form['herd_details'][$herd_id]['animals']) as $aid) {
$item =& $form['herd_details'][$herd_id]['animals'][$aid];
// Assemble the row.
$rows[] = array(
'data' => array(
drupal_render($item['name']),
drupal_render($item['type']),
drupal_render($item['head_count']),
drupal_render($item['weight']),
drupal_render($item['total_weight']),
drupal_render($item['sau']),
drupal_render($item['dmi']),
drupal_render($item['arrival']),
drupal_render($item['departure']),
),
);
}
}
// define the table
$header = array(
t('Animal Record'),
t('Type'),
t('Head Count'),
t('Weight (Average)'),
t('Total Weight'),
t('SAU'),
t('Daily Intake'),
t('Planned Arrival'),
t('Planned Departure'),
);
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
'empty' => t('No animals found in herd'),
'attributes' => array('id' => $table_id),
));
}
}
// render any remaining elements
$output .= drupal_render_children($form);
return $output;
}